netmasked/scripts/hitbox.gd

35 lines
997 B
GDScript

class_name Hitbox extends Area2D
var from_player = true
signal onhit;
var hitplayer = false
#func _process(delta: float) -> void:
#EventBus.cut_grass_at.emit(global_position, 14)
func _ready() -> void:
body_entered.connect(_on_body_entered)
func _on_body_entered(body: Node) -> void:
print(body)
if body is Enemy and from_player:
var bname := str(body.name)
if body.has_method("hit"):
body.hit()
onhit.emit()
body.knockback()
EventBus.debug_print.emit("Hit: " + bname)
else:
EventBus.debug_print.emit("Hitbox touched " + bname + " but it lacks 'reduce_health' method.")
if body is Player and not from_player and not hitplayer:
var bname := str(body.name)
if body.has_method("hit"):
hitplayer = true
body.hit()
onhit.emit()
EventBus.debug_print.emit("Hit: " + bname)
else:
EventBus.debug_print.emit("Hitbox touched " + bname + " but it lacks 'reduce_health' method.")
EventBus.debug_print.emit(str(body))
if body is TileMapLayer:
onhit.emit()