Posted By: Superku
[Code/ Idea] bullets without entities - 01/15/12 23:49
When you're creating a shooter (side-scroller, FPS, space shooter), you'd probably most of the time use models for flying bullets. As the (ent_)creation of all those bullets, their removal, rendering and esp. c_move (that includes a USE_BOX trace) gets quite slow, I suggest you don't use models at all and simply run a function that emulates a bullet as follows:
You can still use c_ignore and most of the events. Additionally, if you really need a lot of bullets, you could handle all of them with just one wait (save the bullets in some array/ linked list).
Code:
/////////////////////////////// #include <acknex.h> #include <default.c> /////////////////////////////// void p_bullet(PARTICLE* p) { vec_set(p.blue,vector(random(128),128+random(128),255)); set(p, STREAK | BRIGHT); p.alpha = 100; p.size = 10; p.lifespan = 0.01; p.event = NULL; } void bullet_create(VECTOR* vpos,VECTOR* vang,var speed) { var alive = 16; VECTOR pos,temp,vel; vec_set(pos,vpos); vec_set(vel,vector(speed,0,0)); vec_rotate(vel,vang); while(alive > 0) { // calculate new position vec_set(temp,vel); vec_scale(temp,time_step); vec_add(temp,pos); c_trace(pos,temp,IGNORE_PASSABLE | USE_POLYGON); // optional: SCAN_TEXTURE if(trace_hit) { /*if(you) ... // reduce health, blood effect else ... // spark effect*/ alive = 0; } effect(p_bullet,1,pos,vel); // you may want to shorten vel on impact vec_set(pos,temp); // move to new position alive -= time_step; wait(1); } } void main() { fps_max = 60; video_mode = 9; level_load(NULL); ent_create(CUBE_MDL,vector(500,-10,-5),NULL); def_move(); while(1) { if(mouse_left) { VECTOR temp; vec_set(temp,vector(5,-20,-10)); vec_rotate(temp,camera.pan); vec_add(temp,camera.x); bullet_create(temp,camera.pan,500); wait(-0.1); } wait(1); } }
You can still use c_ignore and most of the events. Additionally, if you really need a lot of bullets, you could handle all of them with just one wait (save the bullets in some array/ linked list).