extends KinematicBody2D
var player = null
var seePlayer = false
var gravity = 400
var speed = 50
var runSpeed = 70
var velocity = Vector2(0,0)
var isInvincible = false
export var direction = -1
export var detectsCliffs = true
export var health = 30
export var damage = 40
func _ready():
if direction == 1:
$AnimatedSprite.flip_h = true
$PlayerChecker.scale.x *= -1
$FloorCheck.position.x = $CollisionShape2D.shape.get_extents().x * direction
$FloorCheck.enabled = detectsCliffs
func _physics_process(delta):
velocity.y += gravity * delta
velocity.x = speed * direction
velocity = move_and_slide(velocity, Vector2.UP)
if is_on_wall() || not $FloorCheck.is_colliding() && detectsCliffs && is_on_floor() && seePlayer == false:
direction = direction * -1
$PlayerChecker.scale.x *= -1
$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
$FloorCheck.position.x = $CollisionShape2D.shape.get_extents().x * direction
if seePlayer == false:
$AnimatedSprite.play("Walk")
if seePlayer == true:
$AnimatedSprite.play("Run")
var player = get_node("../../Player2")
var is_player_on_right = player.position.x > position.x
var velocity = global_position.direction_to(player.global_position)
$AnimatedSprite.flip_h = is_player_on_right
if is_player_on_right:
direction = 1
move_and_slide(runSpeed * velocity)
func _on_player_hit(damage):
if !isInvincible:
$Timer.start()
set_physics_process(false)
speed = 0
$AnimatedSprite.play("Hit")
health -= damage
print("Enemy health = ", health)
print("Enemy was hit!")
isInvincible = true
if health > 0:
$SoundHit.play()
else:
$SoundDeath.play()
func _on_Timer_timeout():
speed = 50
set_physics_process(true)
isInvincible = false
if health <= 0:
queue_free()
func _on_PlayerChecker_body_entered(body):
if body.name == "Player2":
seePlayer = true
func _on_PlayerChecker_body_exited(body):
if body.name == "Player2":
seePlayer = false