#define health skill100
#define _lifetime skill99 // lifetime of the bullet, the flying projectile
#define _removeable skill98 // 1 = c_explode running 0 = ok, all done, remove now
void c_explode (ENTITY* _ent, _explorange){ // usage: c_explode (me, 300); <- explosion @ me's Position with range of 300
if (!_ent) return;
var _stored_you = 0; if (you) _stored_you = handle (you); // saves handle...
for (you = ent_next (NULL); you; you = ent_next (you)){
if (!_ent) break;
var _dist2ent = 0; _dist2ent = _explorange + 1; // always start "false"
_dist2ent = vec_dist (_ent.x, you.x);
if (you.health && !you._lifetime){
trace_mode = (IGNORE_ME | IGNORE_PASSABLE | USE_POLYGON);
if (_dist2ent < _explorange) if (c_trace (_ent.x, your.x, trace_mode)){
if (HIT_TARGET) draw_point3d (target.x, vector (50, 50, 255), 100, 3);
you.health -= dist2ent / 2; // less dist? more damage!
}
}
}
if (_stored_you) you = ptr_for_handle (_stored_you); // ...restores handle
if (_ent) _ent._removeable = 1;
}
void _event_flying_bullet(){
if (event_type == EVENT_IMPACT || event_type == EVENT_ENTITY) my.health = 0; // BOOOM!
}
void _flying_bullet(){
my.group = 1; // we'll ignore, bullets wont block each other
my.health = 1;
my._lifetime = 100;
my.emask |= (ENABLE_IMPACT | ENABLE_ENTITY);
my.event = _event_flying_bullet;
if (you) vec_set (my.pan, you.pan); // you = entity that spawned this bullet
set (my, POLYGON); // or use custom hull, often the better choice
while (my.health && my._lifetime){
move_mode = (IGNORE_ME | IGNORE_PASSABLE | IGNORE_YOU);
c_ignore (1, 0); // bullets ignore bullets
c_move (me, vector (20 * time_step, 0, 0), nullvector, move_mode);
my._lifetime -= time_step;
wait (1);
}
my.event = NULL;
set (my, INVISIBLE | PASSABLE);
c_explode (me, 300); // init explosion
while (!my.removeable) wait (1); // wait until c_explode is done
wait (1);
ptr_remove (me); // finally, remove projectile
}
action Hero_WED(){ // example how to create a bullet from player
my.health = 100;
my.group = 1; // the bullets ignore the player
var _snd_shoot = 0; // simply holds handle of sound file later
while (my.health){
.
..
...
if (key_ctrl || mouse_left) if (!snd_playing (_snd_shoot)){
_snd_shoot = snd_play (sound_playerfires_rocket, 80, 0);
ent_create ("bullet.mdl", vector (my.x, my.x, my.z), _flying_bullet);
}
...
..
.
wait (1);
}
}