Settled on hitbox/hurtbox routines.

This commit is contained in:
Nitsud Yarg 2024-06-07 11:09:52 -07:00
parent b091bdc143
commit 4fcd85c936
8 changed files with 51 additions and 16 deletions

View File

@ -1,8 +1,18 @@
extends Area2D extends Area2D
export(int) var damage_amount = 0
func set_hitbox(enabled: bool): func set_hitbox(enabled: bool):
for child in get_children(): for child in get_children():
if child is CollisionShape2D: if child is CollisionShape2D:
child.set_deferred("disabled", !enabled) child.set_deferred("disabled", !enabled)
## TODO: This breaks now I guess because I moved the process functions out to parent class ## TODO: This breaks now I guess because I moved the process functions out to parent class
#player_hurtbox.set_deferred("disabled", !enabled) #WTF Apparently this is how to do it #player_hurtbox.set_deferred("disabled", !enabled) #WTF Apparently this is how to do it
func _on_Hitbox_Component_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
var colliding_node = area.get_parent()
if colliding_node.has_node("Hurtbox_Component"):
colliding_node.get_node("Hurtbox_Component").on_hit(self)
else:
print(owner.name, ": Attempted to Hit an unready node, no Hurtbox_Component reciever node in ", colliding_node.name)

View File

@ -6,3 +6,5 @@
collision_layer = 128 collision_layer = 128
collision_mask = 0 collision_mask = 0
script = ExtResource( 1 ) script = ExtResource( 1 )
[connection signal="area_shape_entered" from="." to="." method="_on_Hitbox_Component_area_shape_entered"]

View File

@ -1,23 +1,43 @@
extends Area2D extends Area2D
export(String) var hurtbox_entered_function = ""
var call_function: FuncRef
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
if hurtbox_entered_function:
call_function = funcref(owner, hurtbox_entered_function)
func set_hittbox(enabled: bool): func set_hittbox(enabled: bool):
for child in get_children(): for child in get_children():
if child is CollisionShape2D: if child is CollisionShape2D:
child.set_deferred("disabled", !enabled) child.set_deferred("disabled", !enabled)
# Switching to a new model that starts with Hitbox
func on_hit(sender):
print("Received hit from: " + sender.name)
if hurtbox_entered_function:
call_function.call_func(sender.damage_amount)
# This one doesn't seem to work # This one doesn't seem to work
func _on_Hurtbox_Component_body_entered(body): #func _on_Hurtbox_Component_body_entered(body):
print(body.name, "hit", owner.name) # print(body.name, "hit", owner.name)
if owner.has_method("gotcha"): # if owner.has_method("gotcha"):
print(body.name, " you can do it.", owner.name) # print(body.name, " you can do it.", owner.name)
# well this one works. # well this one works.
func _on_Hurtbox_Component_area_shape_entered(area_rid, area, area_shape_index, local_shape_index): #func _on_Hurtbox_Component_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
#print(area.get_parent().name, "hit", owner.name) # #print(area.get_parent().name, "hit", owner.name)
# if area.get_parent().has_method("gotcha"): ## if area.get_parent().has_method("gotcha"):
print(area.get_parent().name, " you can do it.", owner.name) # print(area.get_parent().name, " you can do it.", owner.name)
#
if owner.has_method("_on_Hurtbox_area_entered"): # if owner.has_method("_on_Hurtbox_area_entered"):
owner.call("_on_Hurtbox_area_entered") # owner.call("_on_Hurtbox_area_entered")
print(area.get_parent().name, " just hit me: ", owner.name) # print(area.get_parent().name, " just hit me: ", owner.name)

View File

@ -4,5 +4,3 @@
[node name="Hurtbox_Component" type="Area2D"] [node name="Hurtbox_Component" type="Area2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )
[connection signal="area_shape_entered" from="." to="." method="_on_Hurtbox_Component_area_shape_entered"]

View File

@ -185,6 +185,7 @@ cast_to = Vector2( 21, 0 )
collision_mask = 2 collision_mask = 2
[node name="Hitbox_Component" parent="." index="8" instance=ExtResource( 4 )] [node name="Hitbox_Component" parent="." index="8" instance=ExtResource( 4 )]
damage_amount = 10
[node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox_Component" index="0"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox_Component" index="0"]
modulate = Color( 1, 0.596078, 0.121569, 1 ) modulate = Color( 1, 0.596078, 0.121569, 1 )

View File

@ -34,8 +34,8 @@ func shoot_projectile():
#func play_sound(): #func play_sound():
# sound_effects.stream = "res://assets/Sounds/crappy pew.wav" # sound_effects.stream = "res://assets/Sounds/crappy pew.wav"
func gotcha(): func gotcha(damage):
print("oh! You got me.") print("oh! You got me for ", damage)
func heylookatme(sender): func heylookatme(sender):
print("does it work!") print("does it work!")

View File

@ -513,6 +513,7 @@ animation_suffix = true
collision_layer = 32768 collision_layer = 32768
collision_mask = 128 collision_mask = 128
monitoring = false monitoring = false
hurtbox_entered_function = "gotcha"
[node name="CollisionShape2D" type="CollisionShape2D" parent="Hurtbox_Component"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Hurtbox_Component"]
modulate = Color( 1, 0, 0, 1 ) modulate = Color( 1, 0, 0, 1 )

View File

@ -94,9 +94,12 @@ func process_frame(_delta: float) -> State:
animations.play(animation_sequence[animation_index] + animation_suffix) animations.play(animation_sequence[animation_index] + animation_suffix)
print("An animation sequence: ", animations.animation , animation_index) print("An animation sequence: ", animations.animation , animation_index)
current_animation_sequence = animation_index current_animation_sequence = animation_index
# Singal based frame call.
if emitter_frame_subscriptions.has(animations.frame): if emitter_frame_subscriptions.has(animations.frame):
if emitter_frame_subscriptions[animations.frame] == animations.animation: if emitter_frame_subscriptions[animations.frame] == animations.animation:
emit_signal("frame_reached", self.name, animations.animation, animations.frame) emit_signal("frame_reached", self.name, animations.animation, animations.frame)
return null return null
func process_physics(_delta: float) -> State: func process_physics(_delta: float) -> State: