Boo.gd 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. extends Node2D
  2. var target_player: Player = null
  3. var velocity := Vector2.ZERO
  4. const MOVE_SPEED := 30
  5. const SMOKE_PARTICLE = preload("uid://d08nv4qtfouv1")
  6. var direction := -1
  7. signal killed
  8. func _physics_process(delta: float) -> void:
  9. target_player = get_tree().get_first_node_in_group("Players")
  10. if $TrackJoint.is_attached == false:
  11. handle_movement(delta)
  12. $Sprite.scale.x = direction
  13. func handle_movement(delta: float) -> void:
  14. var target_direction = sign(target_player.global_position.x - global_position.x)
  15. if target_direction != 0:
  16. direction = target_direction
  17. if target_player.direction == direction:
  18. if $Sprite.animation != "Move":
  19. $Sprite.play("Move")
  20. velocity = lerp(velocity, 30 * global_position.direction_to(target_player.global_position), delta * 5)
  21. else:
  22. if $Sprite.animation != "Idle":
  23. $Sprite.play("Idle")
  24. velocity = lerp(velocity, Vector2.ZERO, delta * 5)
  25. global_position += velocity * delta
  26. func on_area_entered(area: Area2D) -> void:
  27. if area.owner is Player:
  28. if area.owner.is_invincible:
  29. die()
  30. else:
  31. area.owner.damage()
  32. func die() -> void:
  33. summon_smoke_particle()
  34. queue_free()
  35. killed.emit()
  36. func flag_die() -> void:
  37. die()
  38. func summon_smoke_particle() -> void:
  39. var particle = SMOKE_PARTICLE.instantiate()
  40. particle.global_position = global_position
  41. add_sibling(particle)