HammerBro.gd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. extends Enemy
  2. var jumping := false
  3. var jump_direction := 0
  4. @export var auto_charge := false
  5. var charging := false
  6. var wall_jump := false
  7. var target_player: Player = null
  8. const HAMMER = preload("res://Scenes/Prefabs/Entities/Items/Hammer.tscn")
  9. func _ready() -> void:
  10. $MovementAnimations.play("Movement")
  11. $Timer.start()
  12. $JumpTimer.start()
  13. $HammerTimer.start()
  14. func _process(delta: float) -> void:
  15. target_player = get_tree().get_first_node_in_group("Players")
  16. direction = sign(target_player.global_position.x - global_position.x)
  17. $Sprite.scale.x = direction
  18. if $TrackJoint.is_attached: $MovementAnimations.play("RESET")
  19. func _physics_process(delta: float) -> void:
  20. apply_enemy_gravity(delta)
  21. if charging and target_player != null:
  22. if is_on_wall() and is_on_floor():
  23. jump(true)
  24. velocity.x = 50 * direction
  25. else:
  26. velocity.x = 0
  27. move_and_slide()
  28. handle_collision()
  29. func handle_collision() -> void:
  30. var can_pass_block := false
  31. if jump_direction == -1:
  32. can_pass_block = velocity.y < -50
  33. elif jump_direction == 1:
  34. can_pass_block = velocity.y <= 250
  35. $Collision.set_deferred("disabled", can_pass_block and jumping and not wall_jump)
  36. if is_on_floor() and jumping:
  37. jumping = false
  38. func jump(wall := false) -> void:
  39. if is_on_floor() == false:
  40. return
  41. wall_jump = wall
  42. jumping = true
  43. jump_direction = [-1, 1].pick_random()
  44. if jump_direction == -1 and $UpBlock.is_colliding() == false:
  45. jump_direction = 1
  46. if jump_direction == 1 and ($BlockDetect.is_colliding() or global_position.y >= -1):
  47. jump_direction = -1
  48. if jump_direction == -1:
  49. velocity.y = -300
  50. else:
  51. velocity.y = -140
  52. $JumpTimer.start(randf_range(1, 5))
  53. func do_hammer_throw() -> void:
  54. for i in randi_range(1, 6):
  55. await throw_hammer()
  56. await get_tree().create_timer(0.25, false).timeout
  57. $HammerTimer.start(randf_range(2, 5))
  58. func throw_hammer() -> void:
  59. $Sprite/Hammer.show()
  60. $Sprite.play("Hammer")
  61. await get_tree().create_timer(0.5, false).timeout
  62. spawn_hammer()
  63. $Sprite.play("Idle")
  64. $Sprite/Hammer.hide()
  65. func spawn_hammer() -> void:
  66. var node = HAMMER.instantiate()
  67. node.global_position = $Sprite/Hammer.global_position
  68. node.direction = direction
  69. if $TrackJoint.is_attached:
  70. get_parent().owner.add_sibling(node)
  71. else:
  72. add_sibling(node)
  73. func charge() -> void:
  74. charging = true
  75. $MovementAnimations.play("RESET")
  76. func on_screen_entered() -> void:
  77. if auto_charge:
  78. charge()