/// CREATE EVENT ============================
enemyState = "idle"; // You could also use an enumerator system
sightRange = 128; // The radius of the sight area
enemySpeed = 4; // The speed of the enemy
attackRange = 8; // The distance in pixels where the enemy starts to attack when in the chase state
/// STEP EVENT ===========================
switch (enemyState) {
case "idle":
// Make sure the player's instance exists
if (instance_exists(oPlayer)) {
// Is the player in sight (?)
var playerInSight = point_in_circle(oPlayer.x, oPlayer.y, x, y, sightRange);
// The player is in the enemy sight range
if (playerInSight) {
// Chase the player
enemyState = "chase";
}
}
break;
case "chase":
// Make sure the player's instance exists
if (instance_exists(oPlayer)) {
// The player is in the attack range
if (point_distance(x, y, oPlayer.x, oPlayer.y) < attackRange) {
// Stop moving
speed = 0;
// Attack the player
enemyState = "attack";
} else {
// Is the player in sight (?)
var playerInSight = point_in_circle(oPlayer.x, oPlayer.y, x, y, sightRange);
// The player is in the enemy sight range
if (playerInSight) {
// Move towards the player
move_towards_point(oPlayer.x, oPlayer.y, enemySpeed);
} else {
// The player is not in the sight range, go back to the idle state
enemyState = "idle";
speed = 0;
}
}
}
break;
case "attack":
// Attack the player
break;
}