-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShopScene.cs
More file actions
362 lines (306 loc) · 9.87 KB
/
ShopScene.cs
File metadata and controls
362 lines (306 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Collections.Generic;
using System.Linq;
using FunkEngine;
using Godot;
public partial class ShopScene : Control
{
public static readonly string LoadPath = "res://Scenes/ShopScene/ShopScene.tscn";
[Export]
private Label _moneyLabel;
[Export]
private Button _exitButton;
[Export]
private Button _removalButton;
[Export]
private Button _healButton;
[Export]
private GridContainer _noteGrid;
[Export]
private GridContainer _relicGrid;
[Export]
private CenterContainer _confirmationPopup;
[Export]
private Button _confirmationButton;
[Export]
private Button _denyButton;
[Export]
private Label _descriptionLabel;
[Export]
private Control _removalPanel;
[Export]
private GridContainer _possessionGrid;
[Export]
private Button _removalAcceptButton;
[Export]
private Button _cancelRemoveButton;
[Export]
private Label _removalCostLabel;
[Export]
private PlayerPuppet _player;
private ButtonGroup _bGroup;
private readonly int[] _priceByRarity = [100, 90, 80, 70, 60, 50, 9];
const int NoteCost = 45;
private List<ShopItem> _shopItems = new List<ShopItem>();
public override void _EnterTree()
{
BgAudioPlayer.LiveInstance.ResumeLevelMusic();
_bGroup = new ButtonGroup();
Initialize();
_confirmationButton.Pressed += TryPurchase;
_denyButton.Pressed += CloseConfirmationPopup;
_removalButton.Pressed += OpenRemovalPane;
_removalAcceptButton.Pressed += RemoveNote;
_cancelRemoveButton.Pressed += CloseRemovalPane;
_healButton.Pressed += TryHeal;
}
private void Initialize()
{
UpdateMoneyLabel();
GenerateShopItems();
PopulatePossessedNotes();
UpdateHealButton();
}
public override void _Input(InputEvent @event)
{
if (GetViewport().GuiGetFocusOwner() == null)
{
_exitButton.GrabFocus();
}
if (@event.IsActionPressed("ui_cancel"))
{
if (_confirmationPopup.Visible)
{
CloseConfirmationPopup();
GetViewport().SetInputAsHandled();
}
else if (_removalPanel.Visible)
{
CloseRemovalPane();
GetViewport().SetInputAsHandled();
}
}
}
private void UpdateMoneyLabel()
{
_moneyLabel.Text = StageProducer.PlayerStats.Money.ToString();
}
private const int RelicOptions = 3;
private const int NoteOptions = 5;
private void GenerateShopItems()
{
List<RelicTemplate> _shopRelics = Scribe
.GetRandomRelics(
RelicOptions,
StageProducer.CurRoom + 10,
StageProducer.PlayerStats.RarityOdds
)
.ToList();
List<Note> _shopNotes = Scribe
.GetRandomRewardNotes(NoteOptions, StageProducer.CurRoom + 10)
.ToList();
foreach (var relic in _shopRelics)
{
int price = _priceByRarity[(int)relic.Rarity];
AddShopItem(_relicGrid, relic, price);
}
foreach (var note in _shopNotes)
{
int price = NoteCost;
AddShopItem(_noteGrid, note, price);
}
}
private void RefreshShopPrices()
{
foreach (ShopItem sItem in _shopItems)
{
sItem.UpdateCost(GetPrice(sItem.BaseCost));
}
}
private void AddShopItem(GridContainer container, IDisplayable item, int basePrice)
{
if (container == null || item == null)
{
GD.PushError("AddShopItem called with null!");
return;
}
int price = GetPrice(basePrice);
ShopItem newItem = GD.Load<PackedScene>(ShopItem.LoadPath).Instantiate<ShopItem>();
newItem.Display(basePrice, price, item.Texture, item.Name);
newItem.DisplayButton.Pressed += () => SetPurchasable(item, newItem);
newItem.DisplayButton.SetButtonGroup(_bGroup);
newItem.DisplayButton.ToggleMode = true;
newItem.DisplayButton.FocusEntered += () => ChangeDescription(item);
_shopItems.Add(newItem);
container.AddChild(newItem);
}
private int GetPrice(int basePrice)
{
return Math.Max(basePrice * (1 - StageProducer.PlayerStats.DiscountPercent / 100), 0); //Price can't go negative
}
private IDisplayable _currentItem;
private ShopItem _currentUItem;
private void SetPurchasable(IDisplayable item, ShopItem uItem)
{
if (item == null || uItem == null)
return;
_currentItem = item;
_currentUItem = uItem;
_confirmationButton.Disabled = StageProducer.PlayerStats.Money < _currentUItem.Price;
OpenConfirmationPopup();
}
private void TryPurchase()
{
if (StageProducer.PlayerStats.Money < _currentUItem.Price)
return;
StageProducer.PlayerStats.Money -= _currentUItem.Price;
switch (_currentItem)
{
case Note note:
StageProducer.PlayerStats.AddNote(note);
AddNoteToPossessions(note);
break;
case RelicTemplate relic:
StageProducer.PlayerStats.AddRelic(relic);
break;
}
_shopItems.Remove(_currentUItem);
CloseConfirmationPopup();
GetViewport().GuiGetFocusOwner().FindNextValidFocus().GrabFocus(); //slightly hacky
_currentUItem.Visible = false;
_currentUItem.QueueFree();
UpdateMoneyLabel();
_currentItem = null;
_currentUItem = null;
RefreshShopPrices();
UpdateHealButton();
}
private Control _lastFocused;
private void OpenConfirmationPopup()
{
_confirmationPopup.Visible = true;
_lastFocused = GetViewport().GuiGetFocusOwner();
_denyButton.GrabFocus();
}
private void CloseConfirmationPopup()
{
_confirmationPopup.Visible = false;
_lastFocused.GrabFocus();
_lastFocused = null;
_bGroup.GetPressedButton().SetPressed(false);
}
private void ChangeDescription(IDisplayable displayable)
{
if (displayable == null)
{
_descriptionLabel.Text = "";
return;
}
string name = displayable.Name.ToUpper();
name = name.Replace(" ", "");
string type = displayable switch
{
Note => "NOTE_",
RelicTemplate => "RELIC_",
_ => "UNKNOWN_",
};
_descriptionLabel.Text = Tr(type + name + "_NAME") + ": " + Tr(type + name + "_TOOLTIP");
}
private const int RemovalCost = 50;
private bool _hasRemoved;
private void OpenRemovalPane()
{
if (_hasRemoved)
return;
_removalCostLabel.Text = RemovalCost.ToString();
_removalButton.Disabled = true;
_exitButton.Disabled = true;
_removalPanel.Visible = true;
_removalAcceptButton.Visible = false;
_removalAcceptButton.Disabled = StageProducer.PlayerStats.Money < RemovalCost;
_bGroup.GetPressedButton()?.SetPressed(false);
_noteGrid.Visible = false;
_relicGrid.Visible = false;
_cancelRemoveButton.GrabFocus();
}
private void CloseRemovalPane()
{
_removalButton.Disabled = _hasRemoved;
_exitButton.Disabled = false;
_removalPanel.Visible = false;
_removalButton.GrabFocus();
_toRemove = null;
_selectedRemoveButton = null;
_noteGrid.Visible = true;
_relicGrid.Visible = true;
ChangeDescription(null);
}
private void PopulatePossessedNotes()
{
foreach (var note in StageProducer.PlayerStats.CurNotes)
{
AddNoteToPossessions(note);
}
}
private void AddNoteToPossessions(Note note)
{
if (note == null)
return;
DisplayButton disButton = GD.Load<PackedScene>(DisplayButton.LoadPath)
.Instantiate<DisplayButton>();
disButton.Display(note.Texture, note.Name);
disButton.ToggleMode = true;
disButton.FocusEntered += () => ChangeDescription(note);
disButton.Pressed += () => RemovalSelection(note, disButton);
disButton.ButtonGroup = _bGroup;
_possessionGrid.AddChild(disButton);
}
private Note _toRemove;
private Button _selectedRemoveButton;
private void RemovalSelection(Note note, Button button)
{
_toRemove = note;
_selectedRemoveButton = button;
_removalAcceptButton.Visible = true;
button.SetPressed(true);
}
private void RemoveNote()
{
if (_toRemove == null || _selectedRemoveButton == null)
return;
StageProducer.PlayerStats.Money -= RemovalCost;
_removalButton.Disabled = true;
_hasRemoved = true;
StageProducer.PlayerStats.RemoveNote(_toRemove);
_selectedRemoveButton.QueueFree();
CloseRemovalPane();
UpdateMoneyLabel();
UpdateHealButton();
}
private bool _hasHealed;
private const int HealCost = 30;
private int _healAmount = (StageProducer.PlayerStats.MaxHealth / 4);
private void UpdateHealButton()
{
_healButton.Disabled =
StageProducer.PlayerStats.Money <= HealCost
|| StageProducer.PlayerStats.CurrentHealth == StageProducer.PlayerStats.MaxHealth
|| _hasHealed;
}
private void TryHeal()
{
if (
StageProducer.PlayerStats.Money <= HealCost
|| StageProducer.PlayerStats.CurrentHealth == StageProducer.PlayerStats.MaxHealth
|| _hasHealed
)
{
return;
}
StageProducer.PlayerStats.Money -= HealCost;
UpdateMoneyLabel();
UpdateHealButton();
_hasHealed = true;
_player.Heal(_healAmount);
}
}