Demo: Refactor agents, add health, hitbox and hurtbox

This commit is contained in:
Serhii Snitsaruk 2024-01-30 14:59:50 +01:00
parent a839be1ada
commit 1d35d84ec5
14 changed files with 929 additions and 1211 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_n83hi")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_y7pic")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_y7pic")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_y7pic")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_y7pic")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_y7pic")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_y7pic")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_115kp")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_cb5f2")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_cb5f2")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_cb5f2")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_cb5f2")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_cb5f2")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_cb5f2")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_fkh4o")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_r3phr")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_r3phr")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_r3phr")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_r3phr")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_r3phr")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_r3phr")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_y8fj3")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_iv43q")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_iv43q")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_iv43q")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_iv43q")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_iv43q")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_iv43q")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_afx5l")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_e51r0")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_e51r0")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_e51r0")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_e51r0")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_e51r0")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_e51r0")

View File

@ -3,22 +3,22 @@
[ext_resource type="PackedScene" uid="uid://ooigbfhfy4wa" path="res://demo/agents/agent_base.tscn" id="1_l180o"]
[ext_resource type="Texture2D" uid="uid://cw8s50856x8ct" path="res://demo/assets/agent_melee_simple.png" id="2_bvbes"]
[node name="Bobby" instance=ExtResource("1_l180o")]
[node name="AgentMeleeSimple" instance=ExtResource("1_l180o")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_bvbes")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_bvbes")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_bvbes")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_bvbes")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_bvbes")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_bvbes")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_oa4xm")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_37lvn")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_37lvn")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_37lvn")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_37lvn")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_37lvn")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_37lvn")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_2ir76")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_w8tqw")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_w8tqw")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_w8tqw")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_w8tqw")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_w8tqw")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_w8tqw")

View File

@ -5,20 +5,20 @@
[node name="Bobby" instance=ExtResource("1_neifu")]
[node name="LegL" parent="Rig" index="0"]
[node name="LegL" parent="Root/Rig" index="0"]
texture = ExtResource("2_fcti4")
[node name="LegR" parent="Rig" index="1"]
[node name="LegR" parent="Root/Rig" index="1"]
texture = ExtResource("2_fcti4")
[node name="Body" parent="Rig" index="2"]
[node name="Body" parent="Root/Rig" index="2"]
texture = ExtResource("2_fcti4")
[node name="Hat" parent="Rig/Body" index="0"]
[node name="Hat" parent="Root/Rig/Body" index="0"]
texture = ExtResource("2_fcti4")
[node name="HandL" parent="Rig/Body" index="1"]
[node name="HandL" parent="Root/Rig/Body" index="1"]
texture = ExtResource("2_fcti4")
[node name="HandR" parent="Rig/Body" index="2"]
[node name="HandR" parent="Root/Rig/Body" index="2"]
texture = ExtResource("2_fcti4")

View File

@ -0,0 +1,21 @@
extends CharacterBody2D
## Base agent script.
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var bt_player: BTPlayer = $BTPlayer
@onready var health: Health = $Health
func _ready() -> void:
health.damaged.connect(_damaged)
health.death.connect(_die)
func _damaged(_amount: float) -> void:
animation_player.play(&"hit")
func _die() -> void:
animation_player.play(&"death")
bt_player.active = false

View File

@ -0,0 +1,29 @@
class_name Health
extends Node
## Tracks health and emits signal when damaged or dead.
signal death
signal damaged(amount: float)
@export var max_health: float = 10.0
var _current: float
func _ready() -> void:
_current = max_health
func take_damage(amount: float) -> void:
_current -= amount
_current = max(_current, 0.0)
if _current <= 0.0:
death.emit()
else:
damaged.emit(amount)
func get_current() -> float:
return _current

View File

@ -0,0 +1,14 @@
class_name Hitbox
extends Area2D
## Area that deals damage.
@export var damage: float = 1.0
func _ready() -> void:
area_entered.connect(_on_area_entered)
func _on_area_entered(area: Area2D) -> void:
var hurtbox := area as Hurtbox
hurtbox.take_damage(damage, self)

View File

@ -0,0 +1,12 @@
class_name Hurtbox
extends Area2D
## Area that registers damage.
@export var health: Health
func take_damage(amount: float, source: Area2D) -> void:
if source.owner == owner:
# Don't damage yourself.
return
health.take_damage(amount)