Add health stats and functionality

This commit is contained in:
Moe Poi ~ 2023-10-07 22:13:27 +07:00
parent 2933207108
commit 85c09ac63f
7 changed files with 72 additions and 17 deletions

21
scenes/base/base.tscn Normal file
View file

@ -0,0 +1,21 @@
[gd_scene load_steps=4 format=3 uid="uid://cikgeqqhctm7k"]
[ext_resource type="Texture2D" uid="uid://c0tosekulqglv" path="res://assets/icons/logo.png" id="1_8p1wv"]
[ext_resource type="Script" path="res://scripts/base/base.gd" id="1_wqsa0"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_o06lk"]
radius = 14.0
height = 40.0
[node name="base" type="Area2D"]
script = ExtResource("1_wqsa0")
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.05, 0.05)
texture = ExtResource("1_8p1wv")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, 2)
shape = SubResource("CapsuleShape2D_o06lk")
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=12 format=3 uid="uid://btdewn34d67m"]
[gd_scene load_steps=13 format=3 uid="uid://btdewn34d67m"]
[ext_resource type="Script" path="res://scripts/stages/1/game_manager.gd" id="1_aosti"]
[ext_resource type="Texture2D" uid="uid://cnxj0mf3luxo" path="res://assets/tileset/ashlands/tf_A5_ashlands_3.png" id="2_1njjc"]
@ -7,6 +7,7 @@
[ext_resource type="PackedScene" uid="uid://cd5r1v6f8hahx" path="res://scenes/ui/tower_menu.tscn" id="5_3w5lp"]
[ext_resource type="PackedScene" uid="uid://cy88k4uxt1v37" path="res://scenes/ui/game_stats.tscn" id="6_hbxxi"]
[ext_resource type="PackedScene" uid="uid://bvoenhbaqiqj2" path="res://scenes/towers/placement/placement.tscn" id="6_mao4r"]
[ext_resource type="PackedScene" uid="uid://cikgeqqhctm7k" path="res://scenes/base/base.tscn" id="8_rsf1n"]
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_7yb1o"]
texture = ExtResource("2_1njjc")
@ -564,4 +565,7 @@ position = Vector2(676, 192)
[node name="placement3" parent="Placements" instance=ExtResource("6_mao4r")]
position = Vector2(844, 393)
[node name="base" parent="." instance=ExtResource("8_rsf1n")]
position = Vector2(1179, 502)
[connection signal="timeout" from="Path2D/Timer" to="Path2D" method="_on_timer_timeout"]

View file

@ -80,26 +80,26 @@ offset_bottom = 1200.0
scale = Vector2(0.03, 0.03)
columns = 6
[node name="Health" type="TextureRect" parent="Health/GridContainer"]
[node name="1" type="TextureRect" parent="Health/GridContainer"]
layout_mode = 2
texture = ExtResource("3_r1i66")
[node name="Health2" type="TextureRect" parent="Health/GridContainer"]
[node name="2" type="TextureRect" parent="Health/GridContainer"]
layout_mode = 2
texture = ExtResource("3_r1i66")
[node name="Health3" type="TextureRect" parent="Health/GridContainer"]
[node name="3" type="TextureRect" parent="Health/GridContainer"]
layout_mode = 2
texture = ExtResource("3_r1i66")
[node name="Health4" type="TextureRect" parent="Health/GridContainer"]
[node name="4" type="TextureRect" parent="Health/GridContainer"]
layout_mode = 2
texture = ExtResource("3_r1i66")
[node name="Health5" type="TextureRect" parent="Health/GridContainer"]
[node name="5" type="TextureRect" parent="Health/GridContainer"]
layout_mode = 2
texture = ExtResource("3_r1i66")
[node name="Health6" type="TextureRect" parent="Health/GridContainer"]
[node name="6" type="TextureRect" parent="Health/GridContainer"]
layout_mode = 2
texture = ExtResource("3_r1i66")

19
scripts/base/base.gd Normal file
View file

@ -0,0 +1,19 @@
extends Area2D
signal enemy_entered_base(enemy)
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_body_entered(body):
if body.is_in_group("Enemy"):
var enemy = body.get_parent()
enemy_entered_base.emit(enemy)
body.get_parent().destroy()

View file

@ -3,6 +3,7 @@ extends PathFollow2D
var config = null
var enemy_id: int = 0
var health: int = 0
var damage: int = 0
var speed: int = 0
var direction_progress = []
var direction = []
@ -13,6 +14,7 @@ func set_config(config_path):
func set_enemy(id: int):
enemy_id = id
health = config.enemy[enemy_id]['health']
damage = config.enemy[enemy_id]['damage']
speed = config.enemy[enemy_id]['speed']
func set_direction():
@ -45,3 +47,6 @@ func update_healthbar():
func hit(damage : int):
health = health - damage
func destroy():
queue_free()

View file

@ -12,6 +12,7 @@ var data = {
func _ready():
$CanvasLayer/GameStats.set_resource(data['resource'])
$base.connect("enemy_entered_base", on_enemy_entered_base)
var placements = $Placements.get_children()
for placement in placements:
placement.connect("on_placement", on_game_placement)
@ -42,7 +43,6 @@ func on_game_placement(pos, tower_placement_id):
if data['resource'] < config.menu[1]['price']:
disabled_2 = true
$CanvasLayer/TowerMenu.show_menu(tower_id, pos, tower_placement_id, disabled, disabled_2)
print(data)
func on_build_tower(pos, tower_id, tower_placement_id):
if data['tower'].has(str(tower_placement_id)):
@ -59,3 +59,10 @@ func on_build_tower(pos, tower_id, tower_placement_id):
data['tower'][str(tower_placement_id)]['name'] = tower.name
data['resource'] -= config.menu[tower_id]['price']
$CanvasLayer/GameStats.set_resource(data['resource'])
func on_enemy_entered_base(enemy):
data['health'] -= enemy.damage
$CanvasLayer/GameStats.set_damage(enemy.damage)
if data['health'] < 1:
print("Game Over")

View file

@ -1,14 +1,13 @@
extends Control
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func set_resource(value):
$Resources/Label.text = str(value)
func set_damage(value):
var health = $Health/GridContainer.get_children()
for x in range(value):
if !health.is_empty():
health[len(health) - 1].queue_free()
health.pop_back()