Skip to content

Commit ee4dc88

Browse files
committed
Refactored Reward system
Distributed functions to more cohesive classes Removed Reward class Added PlayerStats into StageProducer, so its global and can be persistent. Refined battle status checking
1 parent 8403182 commit ee4dc88

10 files changed

Lines changed: 146 additions & 146 deletions

File tree

Globals/Scribe.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using FunkEngine;
34
using Godot;
45

@@ -31,6 +32,20 @@ public partial class Scribe : Node
3132

3233
public static readonly RelicTemplate[] RelicDictionary = new[]
3334
{
35+
new RelicTemplate(
36+
"Breakfast", //Reference ha ha, Item to give when relic pool is empty.
37+
new RelicEffect[]
38+
{
39+
new RelicEffect(
40+
BattleEffectTrigger.NotePlaced,
41+
1,
42+
(director, val) =>
43+
{
44+
director.Player.Heal(val);
45+
}
46+
),
47+
}
48+
),
3449
new RelicTemplate(
3550
"Good Vibes",
3651
new RelicEffect[]
@@ -60,4 +75,25 @@ public partial class Scribe : Node
6075
}
6176
),
6277
};
78+
79+
//TODO: Item pool(s)
80+
81+
public static RelicTemplate[] GetRandomRelics(RelicTemplate[] ownedRelics, int count)
82+
{
83+
var availableRelics = Scribe
84+
.RelicDictionary.Where(r => !ownedRelics.Any(o => o.Name == r.Name))
85+
.ToArray();
86+
87+
availableRelics = availableRelics
88+
.OrderBy(_ => StageProducer.GlobalRng.Randi())
89+
.Take(count)
90+
.Select(r => r.Clone())
91+
.ToArray();
92+
93+
for (int i = availableRelics.Length; i < count; i++)
94+
{
95+
availableRelics = availableRelics.Append(RelicDictionary[0].Clone()).ToArray();
96+
}
97+
return availableRelics;
98+
}
6399
}

Globals/StageProducer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public partial class StageProducer : Node
1616

1717
private MapGrid _map = new MapGrid();
1818

19+
//Hold here to persist between changes
20+
//TODO: Allow for permanent changes and battle temporary stat changes.
21+
public static PlayerStats PlayerStats;
22+
1923
public class MapGrid
2024
{
2125
private int[,] _map;
@@ -108,6 +112,7 @@ public void StartGame()
108112
_map.InitMapGrid(2, 2, 1);
109113
_seed = GlobalRng.Seed;
110114
_lastRngState = GlobalRng.State;
115+
PlayerStats = new PlayerStats();
111116
}
112117

113118
public void TransitionStage(Stages nextStage)
@@ -119,6 +124,7 @@ public void TransitionStage(Stages nextStage)
119124
GetTree().ChangeSceneToFile("res://scenes/SceneTransitions/TitleScreen.tscn");
120125
break;
121126
case Stages.Battle:
127+
StartGame();
122128
GetTree().ChangeSceneToFile("res://scenes/BattleDirector/test_battle_scene.tscn");
123129
break;
124130
case Stages.Quit:

Reward.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

RewardSelectionUI.tscn

Lines changed: 0 additions & 18 deletions
This file was deleted.

scenes/BattleDirector/scripts/BattleDirector.cs

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public partial class BattleDirector : Node2D
3232

3333
private SongData _curSong;
3434

35-
private bool battleLost = false;
36-
private bool battleWon = false;
37-
3835
#endregion
3936

4037
#region Note Handling
@@ -75,6 +72,7 @@ public override void _Ready()
7572

7673
Player = new PlayerPuppet();
7774
AddChild(Player);
75+
Player.Defeated += CheckBattleStatus;
7876
EventizeRelics();
7977
//TODO: Refine
8078
foreach (var note in Player.Stats.CurNotes)
@@ -88,6 +86,7 @@ public override void _Ready()
8886
Enemy = new PuppetTemplate();
8987
Enemy.SetPosition(new Vector2(400, 0));
9088
AddChild(Enemy);
89+
Enemy.Defeated += CheckBattleStatus;
9190
Enemy.Init(GD.Load<Texture2D>("res://scenes/BattleDirector/assets/Enemy1.png"), "Enemy");
9291
Enemy.Sprite.Scale *= 2;
9392

@@ -119,12 +118,6 @@ private void Begin()
119118

120119
public override void _Process(double delta)
121120
{
122-
//check if either one is dead until one is true, feel free to remove if we have a more efficent way of checking
123-
//alternatively, stop the other process since the battle is over
124-
if (!battleLost || !battleWon)
125-
{
126-
CheckBattleStatus();
127-
}
128121
TimeKeeper.CurrentTime = Audio.GetPlaybackPosition();
129122
CD.CheckMiss();
130123
}
@@ -203,6 +196,31 @@ private Timing CheckTiming(double beatDif)
203196
return Timing.Miss;
204197
}
205198

199+
private void CheckBattleStatus(PuppetTemplate puppet)
200+
{
201+
if (puppet == Player)
202+
{
203+
GD.Print("Player is Dead");
204+
return;
205+
}
206+
207+
//will have to adjust this to account for when we have multiple enemies at once
208+
if (puppet == Enemy)
209+
{
210+
GD.Print("Enemy is dead");
211+
ShowRewardSelection(3);
212+
}
213+
}
214+
215+
private void ShowRewardSelection(int amount)
216+
{
217+
var rewardUI = GD.Load<PackedScene>("res://scenes/UI/RewardSelectionUI.tscn")
218+
.Instantiate<RewardSelect>();
219+
AddChild(rewardUI);
220+
rewardUI.Initialize(Player.Stats, amount);
221+
GetTree().Paused = true;
222+
}
223+
206224
#endregion
207225

208226
#region BattleEffect Handling
@@ -228,47 +246,8 @@ private void EventizeRelics()
228246
}
229247
#endregion
230248

231-
232-
private void CheckBattleStatus()
233-
{
234-
if (battleLost || battleWon)
235-
return;
236-
237-
if (Player.GetCurrentHealth() <= 0)
238-
{
239-
GD.Print("Player is Dead");
240-
battleLost = true;
241-
return;
242-
}
243-
244-
//will have to adjust this to account for when we have multiple enemies at once
245-
if (Enemy.GetCurrentHealth() <= 0)
246-
{
247-
GD.Print("Enemy is dead");
248-
battleWon = true;
249-
250-
//below, old method that just adds a random relic to the inventory
251-
//Reward.GiveRandomRelic(Player.Stats);
252-
//EventizeRelics(); //literally just here to force the ui to update and see if it was added, remove with the proper ui update
253-
//probably won't even need it since we'll be loading to seperate scene anyways
254-
255-
//new method that allows player to choose a relic
256-
ShowRewardSelection();
257-
258-
return;
259-
}
260-
}
261-
262249
private void DebugKillEnemy()
263250
{
264251
Enemy.TakeDamage(1000);
265252
}
266-
267-
private void ShowRewardSelection()
268-
{
269-
var rewardUI = GD.Load<PackedScene>("res://RewardSelectionUI.tscn")
270-
.Instantiate<RewardSelect>();
271-
AddChild(rewardUI);
272-
rewardUI.Initialize(Player.Stats);
273-
}
274253
}

scenes/Puppets/scripts/PlayerPuppet.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33

44
public partial class PlayerPuppet : PuppetTemplate
55
{
6-
public PlayerStats Stats = new PlayerStats();
6+
public PlayerStats Stats;
77

88
public override void _Ready()
99
{
10-
base._Ready();
10+
Stats = StageProducer.PlayerStats;
11+
12+
_currentHealth = Stats.CurrentHealth;
13+
_maxHealth = Stats.MaxHealth;
14+
1115
Init(GD.Load<Texture2D>("res://scenes/BattleDirector/assets/Character1.png"), "Player");
1216
SetPosition(new Vector2(80, 0));
1317
Sprite.Position += Vector2.Down * 30; //TEMP
18+
base._Ready();
19+
}
20+
21+
public override void TakeDamage(int amount)
22+
{
23+
base.TakeDamage(amount);
24+
Stats.CurrentHealth = _currentHealth;
25+
}
26+
27+
public override void Heal(int amount)
28+
{
29+
base.Heal(amount);
30+
Stats.CurrentHealth = _currentHealth;
1431
}
1532
}

scenes/Puppets/scripts/PlayerStats.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44

55
public partial class PlayerStats : Resource
66
{
7-
public int MaxHealth = 300;
8-
public Note[] CurNotes = new Note[] { Scribe.NoteDictionary[1].Clone() };
7+
public int MaxHealth = 100;
8+
public int CurrentHealth = 100;
9+
public Note[] CurNotes = new Note[] { Scribe.NoteDictionary[1].Clone() }; //TODO: Get a better method than .Clone
910

10-
public RelicTemplate[] CurRelics = new[] { Scribe.RelicDictionary[0].Clone() };
11+
public RelicTemplate[] CurRelics = Array.Empty<RelicTemplate>();
1112
public int Attack = 1;
13+
14+
public void AddRelic(RelicTemplate relic)
15+
{
16+
if (CurRelics.Any(r => r.Name == relic.Name))
17+
{
18+
GD.PrintErr("Relic already in inventory: " + relic.Name);
19+
return;
20+
}
21+
CurRelics = CurRelics.Append(relic).ToArray();
22+
GD.Print("Adding relic: " + relic.Name);
23+
}
1224
}

scenes/Puppets/scripts/PuppetTemplate.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77
public partial class PuppetTemplate : Node2D
88
{
9+
public delegate void DefeatedHandler(PuppetTemplate self);
10+
public event DefeatedHandler Defeated;
11+
912
protected HealthBar _healthBar;
1013
public Sprite2D Sprite = new Sprite2D();
1114

@@ -34,14 +37,18 @@ public void Init(Texture2D texture, string name)
3437
UniqName = name;
3538
}
3639

37-
public void TakeDamage(int amount)
40+
public virtual void TakeDamage(int amount)
3841
{
3942
_currentHealth = _healthBar.ChangeHP(-amount);
43+
if (_currentHealth <= 0)
44+
{
45+
Defeated?.Invoke(this);
46+
}
4047
}
4148

42-
public void Heal(int amount)
49+
public virtual void Heal(int amount)
4350
{
44-
_healthBar.ChangeHP(amount);
51+
_currentHealth = _healthBar.ChangeHP(amount);
4552
}
4653

4754
public int GetCurrentHealth()

0 commit comments

Comments
 (0)