Advertisement
teaowl

Godot: "breakable" property for game objects

Jan 29th, 2023 (edited)
1,688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Area2D
  2.  
  3. # Service class, used for attaching new behaviour to body
  4. # It's used instead of multiple inheritance or programmatical composition
  5. # For example: instead of inheriting Wall or Jar from class Breakable, we'd
  6. # add Breakable node that is an instance of Area2D with predefined script for
  7. # taking damage and vanishing
  8. # This can be achieved programmaticaly, via creating Breakable class, and than
  9. # incapsulating and using it in target's script, but why bother?
  10.  
  11. # --------- STATS ------------------------- #
  12. export(int) var hp = 1 #minimal hp - destroys after any hit
  13.  
  14. # --------- SOUNDS ------------------------ #
  15. # Arrays allow to use multiple sounds with some chance that one of them will play on trigger
  16. export(Array, Resource) var recieving_damage_sound: Array
  17. export(Array, Resource) var destroy_sound: Array
  18.  
  19. # --------- ANIMATION --------------------- #
  20. export(Resource) var destroy_animation
  21. export(Resource) var recieve_damage_animation
  22.  
  23. # --------- METADATA ---------------------- #
  24. var rng = RandomNumberGenerator.new()
  25.  
  26. func _ready():
  27.     if(destroy_animation != null):
  28.         $AnimationPlayer.add_animation("DESTROY", destroy_animation)
  29.     if(recieve_damage_animation != null):
  30.         $AnimationPlayer.add_animation("RECIEVE_DAMAGE", recieve_damage_animation)
  31.     rng.randomize()
  32.  
  33. func recieve_damage(damage):
  34.     if(damage < hp):
  35.         hp -= damage
  36.         _play_random_sound(recieving_damage_sound)
  37.         if($AnimationPlayer.has_animation("RECIEVE_DAMAGE")):
  38.             $AnimationPlayer.play("RECIEVE_DAMAGE")
  39.     else:
  40.         _play_random_sound(destroy_sound)
  41.         if($AnimationPlayer.has_animation("DESTROY")):
  42.             $AnimationPlayer.play("DESTROY")
  43.             $DestroyTimer.wait_time = $AnimationPlayer.current_animation_length
  44.         # if there is no animation to measure destroy delay
  45.         if(!$AnimationPlayer.is_playing() and $AudioStreamPlayer.playing):
  46.             $DestroyTimer.wait_time = $AudioStreamPlayer.stream.get_length()
  47.         #$DestroyTimer.start() # if assuming that destroy animation triggers destroy() method itself
  48.         if(get_parent().has_node("AnimationPlayer")):
  49.             $"../AnimationPlayer".play("SHATTER")
  50.  
  51. func _play_random_sound(collection:Array):
  52.     if(!collection.empty()):
  53.         if not $AudioStreamPlayer.playing:
  54.             var sound_number = rng.randi_range(0, collection.size()-1)
  55.             $AudioStreamPlayer.stream = collection[sound_number]
  56.             $AudioStreamPlayer.play()
  57.  
  58. func _destroy():
  59.     var parent_of_entity = get_parent().get_parent() # assuming that Breakable element is a direct child of Entity that should posess such quality
  60.     parent_of_entity.remove_child(self.get_parent()) # remove parent and self from stage
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement