Skip to content

Instantly share code, notes, and snippets.

@kellymears
Last active January 27, 2018 22:56
Show Gist options
  • Save kellymears/2c06a67c47728188ea42 to your computer and use it in GitHub Desktop.
Save kellymears/2c06a67c47728188ea42 to your computer and use it in GitHub Desktop.
GameMaker example for Aaron
/* State determination */
// --------------------------------
if(instance_exists(obj_player)) {
// enemy sees player
if(!collision_line(x,y,obj_player.x,obj_player.y,obj_wall,1,0)) {
state_attacking = 1;
state_pursuing = 1;
state_doubt = 0;
image_angle = point_direction(x,y,obj_player.x,obj_player.y);
}
// enemy doesn't see player
else {
state_attacking = 0;
// enemy doesn't see player but is pursuing
if(state_pursuing) {
state_doubt += 1;
if(state_doubt > 100) { // ..enemy gives up after a bit
state_pursuing = 0;
state_doubt = 0;
}
}
}
// stop pursuing when enemy nears player
if(distance_to_object(obj_player) < 150 && state_pursuing == 1) {
state_pursuing = 0;
}
/* Pathfinding/Behaviors */
// -------------------------------------------
// Pursue
if(state_pursuing==1) {
if(mp_grid_path(global.enemy_grid, enemy_path, x, y, obj_player.x, obj_player.y, 1)) {
image_speed = 1;
path_set_precision(enemy_path,8);
path_start(enemy_path,pursuit_speed,0,0);
}
// Stop Pursuing
} else {
image_speed = 0;
path_end();
}
// Attack
if(state_attacking==1) {
enemy_attack();
}
}
/* Destroy */
// --------------------------------
if(health_level<=0) {
instance_destroy();
obj_player.stats_score_total += 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment