Lessons: Player health, damage, stats, resources

This commit is contained in:
Felipe M 2021-01-30 22:58:28 +01:00
parent 147199a012
commit 7842e15c6c
Signed by: fmartingr
GPG key ID: 716BC147715E716F
13 changed files with 153 additions and 18 deletions

View file

@ -2,8 +2,12 @@ extends KinematicBody2D
export (int) var MAX_SPEED = 15
onready var stats = $EnemyStats
var motion = Vector2.ZERO
func _on_Hurtbox_hit(damage):
stats.health -= damage
func _on_Hurtbox_hit(_damage):
func _on_EnemyStats_enemy_died():
queue_free()

View file

@ -1,7 +1,9 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://Scenes/Enemies/Enemy.gd" type="Script" id=1]
[ext_resource path="res://Scenes/Objects/Hurtbox.tscn" type="PackedScene" id=2]
[ext_resource path="res://Scenes/Enemies/EnemyStats.tscn" type="PackedScene" id=3]
[ext_resource path="res://Scenes/Objects/Hitbox.tscn" type="PackedScene" id=4]
[node name="Enemy" type="KinematicBody2D"]
collision_layer = 0
@ -16,4 +18,10 @@ script = ExtResource( 1 )
[node name="Hurtbox" parent="." instance=ExtResource( 2 )]
collision_mask = 8
[node name="Hitbox" parent="." instance=ExtResource( 4 )]
collision_mask = 4
[node name="EnemyStats" parent="." instance=ExtResource( 3 )]
[connection signal="hit" from="Hurtbox" to="." method="_on_Hurtbox_hit"]
[connection signal="enemy_died" from="EnemyStats" to="." method="_on_EnemyStats_enemy_died"]

View file

@ -0,0 +1,13 @@
extends Node
signal enemy_died
export (int) var max_health = 1
# Needs to be onready to use the max_health variable
onready var health = max_health setget set_health
func set_health(value):
health = clamp(value, 0, max_health)
if health == 0:
emit_signal("enemy_died")

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Scenes/Enemies/EnemyStats.gd" type="Script" id=1]
[node name="EnemyStats" type="Node"]
script = ExtResource( 1 )

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=8 format=2]
[ext_resource path="res://Scenes/Enemies/WalkingEnemy.gd" type="Script" id=1]
[ext_resource path="res://Assets/Enemies/WalkingEnemy.png" type="Texture" id=2]
@ -26,9 +26,11 @@ tracks/0/keys = {
[sub_resource type="RectangleShape2D" id=3]
extents = Vector2( 7, 6 )
[sub_resource type="RectangleShape2D" id=4]
extents = Vector2( 8, 6 )
[node name="WalkingEnemy" instance=ExtResource( 3 )]
script = ExtResource( 1 )
MAX_SPEED = 15
[node name="Sprite" parent="." index="0"]
position = Vector2( 0, -9 )
@ -76,4 +78,13 @@ collision_mask = 0
position = Vector2( 0, -7 )
shape = SubResource( 3 )
[node name="Collider" parent="Hitbox" index="0"]
position = Vector2( 0, -8 )
shape = SubResource( 4 )
[node name="EnemyStats" parent="." index="9"]
max_health = 3
[editable path="Hurtbox"]
[editable path="Hitbox"]