Alot of people who have played multi-player RPG's will have noticed that when you select an enemy, and cast a spell, as long as you don't move while casting, and arn't interupted, the spell will be launched, and hit the enemy regardless of wether or not the enemy is moving. Here is a little snippet that will do just that. This scenario is for "projectile" spells, such as fireballs, magic missles, or in this case a chargebolt.
there's alot of variables that I hope I explain well, so that you can transfer this to your code:
Code:
if ((vec_dist (enemy01.x, player.x) < 800) && (enemy_switching == 1))
{
if ((mouse_left == 1) && (charge_bolt == 1) && (charge_bolt_countdown == 0) && (energy_barslidex <= 155))
{
attack_percentage = 0;
chargebolt_timer();
button_ctr04();
chargebolt_cost();
while (attack_percentage < 100)
{
ent_animate(my, "standtomagbtl", attack_percentage, NULL);
attack_percentage += 13 * time_step;
wait (1);
}
attack_percentage = 0;
while (attack_percentage < 21)
{
ent_animate(my, "rangedspell", attack_percentage, NULL);
attack_percentage += 5 * time_step;
wait (1);
}
chargebolt_soundinit();
chargebolt_create();
chargebolt_animations();
while (casting_spell == 1)
{
if ((key_w == 0) && (key_s == 0) && (key_a == 0) && (key_d == 0))
{
my.frame = 185;
}
if ((key_w == 1) || (key_s == 1) || (key_a == 1) || (key_d == 1))
{
if (spell_chargebolt != NULL)
{
ent_remove (spell_chargebolt);
}
casting_spell = 0;
}
wait (1);
}
}
}
First, I check to see if the distance between the enemy, and player is within the casting range of chargebolt, and that I have the enemy selected (enemy_switching == 1). Then, if the mouse_left was pressed, the skill button was pressed (charge_bolt == 1), the countdown for the spell is ready, and can be used (charge_bolt_countdown == 0), and I have enough energy (energy_barslidex <= 155), I reset the attack animation, run the timer, timer window, deduct the spell cost, and run the animations. In this case the spell is not instant, so I wait for the animations to finish, the play the sound, create the chargebolt, run it's animations, and begin the fun part. As long as I dont move, I'll keep casting this spell untill it's done. And while I'm casting I'll hang on the frame that matches my spell. BUt ifI move, I will remove the spell and it's effects, and reset my spellcasting to zero.
Here is the part that makes the spell work:
function chargebolt_create()
{
casting_spell = 1;
vec_for_vertex (chargebolt_vector, player, 250);
spell_chargebolt = ent_create ("test_spell.mdl", chargebolt_vector, chargebolt_move);
}
Because I've created the spell, I toggle spellcasting on "1". I'll make it and it's animations, or effects match the location of my players hand at creation, and then create it.
Code:
function chargebolt_move()
{
set (my, PASSABLE);
vec_zero (temp_spell01);
vec_set (temp_spell01, enemy01.x);
vec_sub (temp_spell01, my.x);
vec_to_angle (my.pan, temp_spell01);
wait (-3);
casting_spell = 0;
while ((enemy01 != NULL) && (vec_dist (my.x, enemy01.x) > 50))
{
vec_set (temp_spell01, enemy01.x);
vec_sub (temp_spell01, my.x);
vec_to_angle (my.pan, temp_spell01);
my.tilt = 0;
c_move (me, vector (30 * time_step,0,0), nullvector, NULL);
wait (1);
}
enemy01.ENEMYHEALTH -= 500;
ent_remove (my);
}
I'll first zero out any vectors, and set the spell to passable, to avoid problems. Then after 3 seconds the casting time is finished and the spell is launched, so I can toggle spellcasting off "0". While the enemy exists, and the distance
between the spell and the enemy is greater than 50, I will make sure that the spell turns toward the enemy, and move it
at a speed of 30. Once it has reached 50 or less in distance, I will deal damage to the enemy, and then remove the spell.
Well I hope this helped someone, I know theres alot of missing holes, but at least you can make spells follow your enemies now
