Chicken.gd 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. class_name Chicken
  2. extends KinematicBody2D
  3. export var walk_linear_speed = 100
  4. export var run_boost = 4
  5. var velocity = Vector2.ZERO
  6. func attack(from: Vector2, angle: float):
  7. position = from
  8. velocity = Vector2(walk_linear_speed, 0).rotated(angle)
  9. $AnimatedSprite.play("walk")
  10. _update_sprite_direction()
  11. func _physics_process(delta):
  12. var collision = move_and_collide(velocity * delta)
  13. _collide_and_react(collision)
  14. func _on_VisibilityNotifier2D_screen_exited():
  15. _die()
  16. func _on_Spawner_reset():
  17. _die()
  18. func _die():
  19. queue_free()
  20. func _collide_and_react(collision: KinematicCollision2D) -> void:
  21. if collision == null:
  22. return
  23. var collider = collision.get_collider()
  24. if collider is KinematicBody2D and (collider as Node).is_in_group("chicken") :
  25. _turn_away()
  26. func _turn_away() -> void:
  27. velocity.x = -velocity.x
  28. _update_sprite_direction()
  29. func _update_sprite_direction() -> void:
  30. $AnimatedSprite.flip_h = velocity.x < 0.0