CheeseMyBaby
Member
I'm working on the AI of a new enemy. It's a long way from done but the things I've made so far is working except for one highly annoying thing:
Short background:
When the enemy is in the state "moving" it's.. just moving and if the player gets into its line of sight it'll change state to "init_attack" where it should run towards the player and then change state to the "attack" state.
EDIT: All tiles and most sprites are placeholders. I do however apologize for the "enemy" which is a placeholder for the placeholder.
The problem:
When the enemy "sees" the player it instantly changes to the opposite direction, runs to a wall and then it runs towards the player and does what it's supposed to do.
If you look at the GIF below you'll see that as soon as the player jumps over the wall (that's when the enemy "sees" him) he changes direction and hits the far wall before running towards the player.
The code:
This is the MOVING-state in the machine
And this is the INIT_ATTACK-state
There's two different collision checks called from the different states. Here's both of them:
Collision check from the MOVING state:
And this is the collision check called from the INIT_ATTACK-state
All thoughts, tips, tricks, help would be immensely appreciated!!
<3
Short background:
When the enemy is in the state "moving" it's.. just moving and if the player gets into its line of sight it'll change state to "init_attack" where it should run towards the player and then change state to the "attack" state.
EDIT: All tiles and most sprites are placeholders. I do however apologize for the "enemy" which is a placeholder for the placeholder.
The problem:
When the enemy "sees" the player it instantly changes to the opposite direction, runs to a wall and then it runs towards the player and does what it's supposed to do.
If you look at the GIF below you'll see that as soon as the player jumps over the wall (that's when the enemy "sees" him) he changes direction and hits the far wall before running towards the player.
The code:
This is the MOVING-state in the machine
Code:
show_debug_message("MOVING")
alertTimer = 0;
if (inLos)
{
counter++;
if (counter >= 10)
{
counter = 0;
state = enemyAI.ALERTED; // Change state if SEEN by enemy
}
}
else counter = 0;
scrAICollider(); // Call collision script
Code:
show_debug_message("init_Attack");
hsp = runsp;
if (!inLos) // When not seeing player, init state change
{
counter++;
if (counter >= runSuccess)
{
counter = 0;
recentlySeen = true;
state = enemyAI.SEARCHING;
}
}
else counter = 0;
if (collision_line(x, y, target.x, target.y, objWall, true, true) = noone) // If nothing between player and NMY...
{
if (collision_circle(x, y, 32, target, false, false))
{
state = enemyAI.ATTACKING;
}
}
eAI_IA_Collision();
Collision check from the MOVING state:
Code:
// Behaviour
vsp = vsp + grv;
// Don't walk off ledges (Shaun Spalding)
if (grounded) && (vertigo) && (!place_meeting(x + hsp, y+1, objWall))
{
hsp = -hsp
}
// Colliding horisontally?
if (place_meeting(x+hsp, y, objWall))
{
while (!place_meeting(x+sign(hsp),y,objWall))
{
x = x + sign(hsp);
}
hsp = -hsp;
}
x = x + hsp;
// Colliding vertically?
if (place_meeting(x, y+vsp, objWall))
{
while (!place_meeting(x,y+sign(vsp),objWall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;
And this is the collision check called from the INIT_ATTACK-state
Code:
// Running into walls?
if (place_meeting(x+runsp, y, objWall))
{
while (!place_meeting(x+sign(runsp),y,objWall))
{
x = x + sign(runsp);
}
runsp = -runsp;
}
x = x + runsp;
All thoughts, tips, tricks, help would be immensely appreciated!!
<3
Last edited: