enemy.gd 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. extends KinematicBody2D
  2. const GRAVITY_VEC = Vector2(0, 900)
  3. const FLOOR_NORMAL = Vector2(0, -1)
  4. const WALK_SPEED = 70
  5. const STATE_WALKING = 0
  6. const STATE_KILLED = 1
  7. var linear_velocity = Vector2()
  8. var direction = -1
  9. var anim=""
  10. var state = STATE_WALKING
  11. onready var detect_floor_left = $detect_floor_left
  12. onready var detect_wall_left = $detect_wall_left
  13. onready var detect_floor_right = $detect_floor_right
  14. onready var detect_wall_right = $detect_wall_right
  15. onready var sprite = $sprite
  16. func _physics_process(delta):
  17. var new_anim = "idle"
  18. if state==STATE_WALKING:
  19. linear_velocity += GRAVITY_VEC * delta
  20. linear_velocity.x = direction * WALK_SPEED
  21. linear_velocity = move_and_slide(linear_velocity, FLOOR_NORMAL)
  22. if not detect_floor_left.is_colliding() or detect_wall_left.is_colliding():
  23. direction = 1.0
  24. if not detect_floor_right.is_colliding() or detect_wall_right.is_colliding():
  25. direction = -1.0
  26. sprite.scale = Vector2(direction, 1.0)
  27. new_anim = "walk"
  28. else:
  29. new_anim = "explode"
  30. if anim != new_anim:
  31. anim = new_anim
  32. $anim.play(anim)
  33. func hit_by_bullet():
  34. state = STATE_KILLED