SpikeBall.gd 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class_name SpikeBall
  2. extends CharacterBody2D
  3. var can_gravity := false
  4. const SPIKE_BALL_DESTRUCTION_PARTICLES = preload("uid://bk0arhpyyila6")
  5. func _physics_process(delta: float) -> void:
  6. handle_movement(delta)
  7. $Sprite.rotation_degrees += velocity.x * delta * 8
  8. handle_block_collision()
  9. func handle_movement(delta: float) -> void:
  10. if can_gravity:
  11. velocity.y += (Global.entity_gravity / delta) * delta
  12. if is_on_floor():
  13. can_gravity = true
  14. velocity.x += get_floor_normal().x * 4
  15. if is_on_wall():
  16. destroy()
  17. if sign(get_floor_normal().x) != sign(velocity.x) and abs(get_floor_normal().x) > 0.2 and is_on_floor():
  18. velocity.y = (velocity.length() * get_floor_normal().y) + Global.entity_gravity
  19. move_and_slide()
  20. func destroy() -> void:
  21. summon_particles()
  22. AudioManager.play_sfx("block_break", global_position)
  23. queue_free()
  24. func handle_block_collision() -> void:
  25. for i in $Hitbox.get_overlapping_bodies():
  26. if i is Block:
  27. if global_position.y - 8 < i.global_position.y:
  28. i.shell_block_hit.emit(null)
  29. func summon_particles() -> void:
  30. var particles = SPIKE_BALL_DESTRUCTION_PARTICLES.instantiate()
  31. particles.global_position = global_position
  32. add_sibling(particles)
  33. func on_area_entered(area: Area2D) -> void:
  34. if area.owner is SpikeBall and area.owner != self:
  35. destroy()
  36. if area.owner is Enemy:
  37. if area.owner.has_node("ShellDetection"):
  38. area.owner.die_from_object(self)
  39. elif area.owner is Player:
  40. if area.owner.is_invincible:
  41. destroy()
  42. else:
  43. area.owner.damage()