Skip to content

Commit 2f03e1d

Browse files
authored
Merge pull request #28 from Project-Funk-Engine/healthBar
Health bar
2 parents 51ae9ef + 4b23be9 commit 2f03e1d

7 files changed

Lines changed: 175 additions & 5 deletions

File tree

scenes/ChartViewport/BattleDirector.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public partial class BattleDirector : Node2D
1818

1919
private double _timingInterval = .1; //secs
2020

21+
[Signal]
22+
public delegate void PlayerDamageEventHandler(int damage);
23+
24+
[Signal]
25+
public delegate void EnemyDamageEventHandler(int damage);
26+
2127
public struct SongData
2228
{
2329
public int Bpm;
@@ -128,18 +134,22 @@ private void CheckNoteTiming(NoteArrow.ArrowType type)
128134
if (beatDif < _timingInterval * 2)
129135
{
130136
GD.Print("Perfect");
137+
EmitSignal(nameof(EnemyDamage), 10);
131138
}
132139
else if (beatDif < _timingInterval * 4)
133140
{
134141
GD.Print("Good");
142+
EmitSignal(nameof(EnemyDamage), 5);
135143
}
136144
else if (beatDif < _timingInterval * 6)
137145
{
138146
GD.Print("Okay");
147+
EmitSignal(nameof(EnemyDamage), 1);
139148
}
140149
else
141150
{
142151
GD.Print("Miss");
152+
EmitSignal(nameof(PlayerDamage), 10);
143153
}
144154
}
145155
}

scenes/ChartViewport/Enemy.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Godot;
3+
4+
public partial class Enemy : Node2D
5+
{
6+
[Export]
7+
public HealthBar EnemyHealthBar;
8+
9+
[Export]
10+
public BattleDirector BattleDirectorInstance;
11+
12+
private int _enemyMaxHealth = 100;
13+
14+
public override void _Ready()
15+
{
16+
if (EnemyHealthBar != null)
17+
{
18+
EnemyHealthBar.SetHealth(_enemyMaxHealth, _enemyMaxHealth);
19+
}
20+
21+
if (BattleDirectorInstance != null)
22+
{
23+
BattleDirectorInstance.Connect(
24+
(nameof(BattleDirectorInstance.EnemyDamage)),
25+
new Callable(this, nameof(EnemyTakeDamage))
26+
);
27+
}
28+
}
29+
30+
private void EnemyTakeDamage(int damage)
31+
{
32+
EnemyHealthBar.TakeDamage(damage);
33+
GD.Print("Enemy takes damage:" + damage);
34+
}
35+
}

scenes/ChartViewport/Player.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Godot;
3+
4+
public partial class Player : Node2D
5+
{
6+
[Export]
7+
public HealthBar PlayerHealthBar;
8+
9+
[Export]
10+
public BattleDirector BattleDirectorInstance;
11+
private int _playerHealthMax = 100;
12+
private int _playerHealth = 100;
13+
14+
public override void _Ready()
15+
{
16+
if (PlayerHealthBar != null)
17+
{
18+
PlayerHealthBar.SetHealth(_playerHealthMax, _playerHealth);
19+
}
20+
21+
if (BattleDirectorInstance != null)
22+
{
23+
BattleDirectorInstance.Connect(
24+
(nameof(BattleDirectorInstance.PlayerDamage)),
25+
new Callable(this, nameof(PlayerTakeDamage))
26+
);
27+
}
28+
}
29+
30+
private void PlayerTakeDamage(int damage)
31+
{
32+
GD.Print("Player taking damage: " + damage);
33+
PlayerHealthBar.TakeDamage(damage);
34+
}
35+
}

scenes/ChartViewport/test_scene.tscn

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://b0mrgr7h0ty1y"]
1+
[gd_scene load_steps=6 format=3 uid="uid://b0mrgr7h0ty1y"]
22

33
[ext_resource type="PackedScene" uid="uid://dfevfib11kou1" path="res://scenes/ChartViewport/ChartViewport.tscn" id="1_1vh1u"]
44
[ext_resource type="Script" path="res://scenes/ChartViewport/BattleDirector.cs" id="1_jbxt1"]
5+
[ext_resource type="PackedScene" uid="uid://bgomxovxs7sr8" path="res://scenes/HealthBar.tscn" id="2_1t4a4"]
6+
[ext_resource type="Script" path="res://scenes/ChartViewport/Player.cs" id="3_gtvac"]
7+
[ext_resource type="Script" path="res://scenes/ChartViewport/Enemy.cs" id="5_nkoeo"]
58

69
[node name="ProtoBattleDirector" type="Node2D" node_paths=PackedStringArray("CM", "NM")]
710
script = ExtResource("1_jbxt1")
@@ -21,3 +24,21 @@ offset_top = -34.0
2124
offset_right = 673.0
2225
offset_bottom = 393.0
2326
color = Color(0.147672, 0.147672, 0.147672, 1)
27+
28+
[node name="PlayerObject" type="Node2D" parent="." node_paths=PackedStringArray("PlayerHealthBar", "BattleDirectorInstance")]
29+
script = ExtResource("3_gtvac")
30+
PlayerHealthBar = NodePath("HP")
31+
BattleDirectorInstance = NodePath("..")
32+
33+
[node name="HP" parent="PlayerObject" instance=ExtResource("2_1t4a4")]
34+
35+
[node name="EnemyObject" type="Node2D" parent="." node_paths=PackedStringArray("EnemyHealthBar", "BattleDirectorInstance")]
36+
script = ExtResource("5_nkoeo")
37+
EnemyHealthBar = NodePath("EHP")
38+
BattleDirectorInstance = NodePath("..")
39+
40+
[node name="EHP" parent="EnemyObject" instance=ExtResource("2_1t4a4")]
41+
offset_left = 403.0
42+
offset_top = -4.0
43+
offset_right = 443.0
44+
offset_bottom = 36.0

scenes/HealthBar.tscn

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://bgomxovxs7sr8"]
2+
3+
[ext_resource type="Script" path="res://scripts/HealthBar.cs" id="1_b1t4i"]
4+
5+
[node name="Control" type="Control" node_paths=PackedStringArray("PlayerHealthBar")]
6+
layout_mode = 3
7+
anchors_preset = 0
8+
offset_right = 40.0
9+
offset_bottom = 40.0
10+
script = ExtResource("1_b1t4i")
11+
PlayerHealthBar = NodePath("ProgressBar")
12+
13+
[node name="ProgressBar" type="ProgressBar" parent="."]
14+
layout_mode = 0
15+
offset_left = 29.0
16+
offset_top = 18.0
17+
offset_right = 229.0
18+
offset_bottom = 45.0

scenes/main.tscn

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[gd_scene load_steps=2 format=3 uid="uid://diq2m6va1x52k"]
2-
3-
[ext_resource type="Script" path="res://scripts/Main.cs" id="1_it331"]
1+
[gd_scene format=3 uid="uid://diq2m6va1x52k"]
42

53
[node name="Main" type="Node2D"]
6-
script = ExtResource("1_it331")

scripts/HealthBar.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Godot;
3+
4+
public partial class HealthBar : Control
5+
{
6+
const int MaxHealth = 100;
7+
int _health = MaxHealth;
8+
9+
[Export]
10+
public ProgressBar PlayerHealthBar;
11+
12+
//we can change this to a Texture Progress bar once we have art assets for it
13+
14+
15+
16+
public override void _Ready()
17+
{
18+
if (PlayerHealthBar != null)
19+
{
20+
GD.Print("Player Health Bar");
21+
PlayerHealthBar.MaxValue = MaxHealth;
22+
}
23+
//Connect(nameof(BattleDirector.PlayerDamage), new Callable(this ,nameof(PlayerDamage)));
24+
}
25+
26+
public void SetHealth(int max, int current)
27+
{
28+
PlayerHealthBar.MaxValue = max;
29+
PlayerHealthBar.Value = current;
30+
}
31+
32+
private void _updateHealthBar()
33+
{
34+
PlayerHealthBar.Value = _health;
35+
}
36+
37+
public void TakeDamage(int damage)
38+
{
39+
_health -= damage;
40+
GD.Print("Health: " + _health);
41+
if (_health <= 0)
42+
{
43+
GD.Print("You are dead");
44+
}
45+
_updateHealthBar();
46+
}
47+
48+
/*public override void _Input(InputEvent @event)
49+
{
50+
if(@event.IsActionPressed("ui_accept"))
51+
_takeDamage(10);
52+
53+
}*/
54+
}

0 commit comments

Comments
 (0)