if (place_meeting(x, y, obj_wall)) instance_destroy()

spr_idle = spr_enemy
spr_aggro = spr_enemy_aggro
spr_hurt = spr_enemy_hurt

spd = 1.5
wander_dist = 96
hsp = 0
vsp = 0
target_x = x
target_y = y
hp = 1

dir = 0
time_pass = 0 //if we try to go in wall for too long give up
wait_dur = 60 + random_range(-20, 20)
wait = 0
aggro_dist = 200
mp_potential_settings(90, 30, 10, true)

///@func move_and_collide()
///@returns N/A
move_and_collide = function(){
	// horizontal collision
	if (place_meeting(x + hsp, y, obj_wall)){
		while (!place_meeting(x + sign(hsp), y, obj_wall)){
			x = x + sign(hsp)
		}
		hsp = 0
	}
	x = x + hsp

	// vertical collision
	if (place_meeting(x, y + vsp, obj_wall)){
		while (!place_meeting(x, y + sign(vsp), obj_wall)){
			y = y + sign(vsp)
		}
		vsp = 0
	}
	y = y + vsp
}

state = new StateMachine("wander")
state.add("wander", {
    enter: function(){
        sprite_index = spr_idle
    },
    step: function(){
        if ((x == target_x && y == target_y) || time_pass > wander_dist / spd){
            hsp = 0
            vsp = 0
            
            //new dest baybee
            if (++wait >= wait_dur){
                wait = 0
                time_pass = 0
                dir = point_direction(x, y, xstart, ystart) + random_range(-45, 45)
                target_x += lengthdir_x(wander_dist, dir)
                target_y += lengthdir_y(wander_dist, dir)
            }
        }
        else{
            time_pass++
            var _dist = point_distance(x, y, target_x, target_y)
            var _spd_this_frame = spd
            if (_dist < spd) _spd_this_frame = _dist
            dir = point_direction(x, y, target_x, target_y)
            hsp = lengthdir_x(_spd_this_frame, dir)
            vsp = lengthdir_y(_spd_this_frame, dir)
            
            
            move_and_collide()
        }
		
		if (hsp != 0) image_xscale = sign(hsp)
		
		if (instance_exists(obj_player) && point_distance(x, y, obj_player.x, obj_player.y) < aggro_dist) state_switch("chase")
    }
})
state.add("chase", {
    enter: function(){
        sprite_index = spr_aggro
    },
    step: function(){
        //image_angle = point_direction(x, y, obj_player.x, obj_player.y)
		if (hsp != 0) image_xscale = sign(hsp)
        mp_potential_step_object(obj_player.x, obj_player.y, spd, par_enemy_avoid)
		
		if (point_distance(x, y, obj_player.x, obj_player.y) > aggro_dist) state_switch(get_previous_state(id))
    }
})
state.add("hurt", {
	enter: function(){
		hp = approach(hp, 0, 1)
		sprite_index = spr_hurt
	},
	step: function(){
		if (hp == 0) instance_destroy()
		
		if (animation_end()) state_switch(get_previous_state(id))
		
		if (hsp != 0) image_xscale = sign(hsp)
	}
})
state.add("hurt", {
	enter: function(){
		hp = approach(hp, 0, 1)
		sprite_index = spr_hurt
	},
	step: function(){
		if (hp == 0){
			repeat (5){
				instance_create_layer(x, y, "Instances", obj_dust)
			}
			instance_destroy()
		}
		
		if (animation_end()) state_switch(get_previous_state(id))
		
		if (hsp != 0) image_xscale = sign(hsp)
	}
})
