-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alchemy.cs
325 lines (281 loc) · 12.6 KB
/
Alchemy.cs
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
using Godot;
using System;
public class Alchemy : Node2D
{
private enum AlchemyStage
{
MortarPestle,
Calcinator,
Retort,
Alembic
}
private enum MortarPestleStage
{
PickReagents,
Crush
}
private enum ReagentAnim
{
Fade,
Hover,
Fall,
End
}
// State machines
private AlchemyStage _alchemyStage = AlchemyStage.MortarPestle;
private MortarPestleStage _mortarPestleStage = MortarPestleStage.PickReagents;
private ReagentAnim _reagentAnimState = ReagentAnim.Fade;
// Export
[Export] // If using high quality particles
private bool _highParticles = true;
// Nodes
private Mortar _mortar;
private PotionCircle _potionCircle;
private VBoxContainer _potionReagentsBox;
private AcceptDialog _helpDialog;
private Button _proceedToCrush;
private Tween _tween;
private Inventory _inventory;
private ItemTooltip _itemTooltip;
private DebugOverlay _debugOverlay;
// Lists
private System.Collections.Generic.List<Item.ItemStack> _itemList = new System.Collections.Generic.List<Item.ItemStack>();
private System.Collections.Generic.List<Item> _potionReagents = new System.Collections.Generic.List<Item>();
private Sprite[] _reagentAnimSprites = new Sprite[4];
// Assets
private Texture _itemBtnBg;
private Texture _singleItemSlot;
private DynamicFont _smallFont;
// Properties
public bool HighParticles { get { return _highParticles; } set { _highParticles = value; } }
public override void _Ready()
{
GD.Randomize();
// Get references to all the nodes we need to manipulate
_mortar = GetNode<Mortar>("MortarPestle/Mortar");
_potionCircle = GetNode<PotionCircle>("MortarPestle/Crush/PotionCircle");
_potionReagentsBox = GetNode<VBoxContainer>("MortarPestle/PickReagents/PotionReagentsBox");
_helpDialog = GetNode<AcceptDialog>("HelpDialog");
_proceedToCrush = GetNode<Button>("MortarPestle/PickReagents/ProceedToCrush");
_tween = GetNode<Tween>("MortarPestle/PickReagents/Tween");
_inventory = GetNode<Inventory>("MortarPestle/PickReagents/Inventory");
_inventory.SetSize(4, 4);
_inventory.DrawPosition = new Vector2(324f, 16f);
_inventory.CanDeselect = false;
_itemTooltip = GetNode<ItemTooltip>("MortarPestle/PickReagents/ItemTooltip");
_debugOverlay = GetNode<DebugOverlay>("DebugOverlay");
// Load assets needed
_itemBtnBg = GD.Load<Texture>("res://textures/item_slot.png");
_singleItemSlot = GD.Load<Texture>("res://textures/single_item_slot.png");
_smallFont = GD.Load<DynamicFont>("res://font/small_font.tres");
// Signal connections
GetNode<TouchScreenButton>("HelpButton").Connect("released", this, nameof(DisplayHelpPopup));
_proceedToCrush.Connect("pressed", this, nameof(ProceedToCrushPressed));
_tween.Connect("tween_all_completed", this, nameof(TweenAllCompleted));
_inventory.Connect("ItemSlotSelected", this, nameof(InventorySlotSelected));
GetNode<Button>("MortarPestle/PickReagents/AddToPotion").Connect("pressed", this, nameof(ItemButtonReleased));
// Adding items to simulate an inventory
_itemList.AddRange(new Item.ItemStack[] { new Item.ItemStack(Items.BRIMSTONE, 1), new Item.ItemStack(Items.FLY_AGARIC, 3), new Item.ItemStack(Items.ELDERBERRIES, 5), new Item.ItemStack(Items.ORPIMENT, 1), new Item.ItemStack(Items.HOLLY_BERRIES, 3) });
_inventory.UpdateSlots(_itemList);
// Tracking stuff for debug
_debugOverlay.TrackProperty(nameof(_mortarPestleStage), this, "MortarPestleStage");
_debugOverlay.TrackProperty(nameof(_mortar.CurrentParticleColour), _mortar, "Splash Colour");
}
public void PestleHitMortar(float power)
{
if (_mortarPestleStage == MortarPestleStage.Crush)
_potionCircle.InitiateCircleFill(power);
}
public void ItemButtonReleased()
{
if (_inventory.SelectedItemId < 0)
return;
Item.ItemStack selectedItemStack = _itemList[_inventory.SelectedItemId];
if (_alchemyStage == AlchemyStage.MortarPestle && _mortarPestleStage == MortarPestleStage.PickReagents && _potionReagents.Count < 4)
{
if (selectedItemStack.stackCount > 1)
{
_itemList[_inventory.SelectedItemId] = Item.DecreaseItemStackCount(selectedItemStack, 1);
}
else
{
_itemList.RemoveAt(_inventory.SelectedItemId);
}
_inventory.Update();
HBoxContainer itemInfo = new HBoxContainer();
itemInfo.Set("custom_constants/separation", 10f);
Control itemIcon = new Control();
itemIcon.RectMinSize = new Vector2(16f, 16f);
Sprite itemBG = new Sprite();
itemBG.Texture = _singleItemSlot;
itemBG.Centered = false;
Sprite itemSprite = new Sprite();
itemSprite.Texture = selectedItemStack.item.IconTex;
itemSprite.Centered = false;
itemSprite.Position = new Vector2(2f, 2f);
itemBG.AddChild(itemSprite);
itemIcon.AddChild(itemBG);
itemInfo.AddChild(itemIcon);
Label itemName = new Label();
itemName.Text = selectedItemStack.item.Name;
itemName.AddFontOverride("font", _smallFont);
itemName.MarginLeft = 4f;
itemInfo.AddChild(itemName);
_potionReagentsBox.AddChild(itemInfo);
_potionReagents.Add(selectedItemStack.item);
_proceedToCrush.Disabled = false;
}
GD.Print("Selected: " + selectedItemStack.item.Name);
}
public void DisplayHelpPopup()
{
_helpDialog.PopupCentered();
switch (_alchemyStage)
{
case AlchemyStage.MortarPestle:
{
switch (_mortarPestleStage)
{
case MortarPestleStage.PickReagents:
{
_helpDialog.DialogText = "Select your reagents from the list to the right.";
break;
}
case MortarPestleStage.Crush:
{
_helpDialog.DialogText = "Crush up the reagents with the mortar and pestle! Just pick up the pestle and start smashing.";
break;
}
}
break;
}
case AlchemyStage.Calcinator:
{
break;
}
case AlchemyStage.Alembic:
{
break;
}
case AlchemyStage.Retort:
{
break;
}
}
}
public void NextStage()
{
switch (_alchemyStage)
{
case AlchemyStage.MortarPestle:
{
if (_mortarPestleStage == MortarPestleStage.PickReagents)
{
_mortarPestleStage = MortarPestleStage.Crush;
GetNode<Node2D>("MortarPestle/PickReagents").Visible = false;
GetNode<Node2D>("MortarPestle/Crush").Visible = true;
_tween.InterpolateProperty(_potionCircle, "scale", new Vector2(0, 1), new Vector2(1, 1), .4f, Tween.TransitionType.Cubic, Tween.EaseType.InOut);
_tween.InterpolateProperty(GetNode("MortarPestle/Crush/PotionProgress"), "rect_scale", new Vector2(0, 1), new Vector2(1, 1), .4f, Tween.TransitionType.Cubic, Tween.EaseType.InOut);
_tween.Start();
for (int i = 1; i < _potionReagents.Count; i++)
{
_mortar.AverageColour = _potionReagents[i].PotionColour.LinearInterpolate(_potionReagents[i - 1].PotionColour, .5f);
}
// Create mortar crush gradient
for (int i = 0; i < _potionReagents.Count; i++)
{
_mortar.CrushGradient.SetOffset(i, ((float)i + 1f) / (float)_potionReagents.Count);
_mortar.CrushGradient.SetColor(i, _potionReagents[i].PotionColour);
}
}
break;
}
}
}
public void ProceedToCrushPressed()
{
for (int i = 0; i < _potionReagentsBox.GetChildCount(); i++)
{
//ItemSprite
Node2D N = ((Node)_potionReagentsBox.GetChildren()[i]).GetChild(0).GetChild(0) as Node2D;
_tween.InterpolateProperty(GetNode("MortarPestle/PickReagents"), "modulate", Colors.White, new Color(1f, 1f, 1f, 0f), .5f);
_tween.InterpolateProperty(_proceedToCrush, "modulate", Colors.White, new Color(1f, 1f, 1f, 0f), .5f);
Sprite reagentSprite = new Sprite()
{
Texture = _potionReagents[i].IconTex,
GlobalPosition = N.GlobalPosition + new Vector2(2f, 2f),
Centered = false,
ZIndex = -2
};
AddChild(reagentSprite);
_reagentAnimSprites[i] = reagentSprite;
}
_tween.Start();
_reagentAnimState = ReagentAnim.Hover;
}
public void TweenAllCompleted()
{
switch (_alchemyStage)
{
case AlchemyStage.MortarPestle:
{
switch (_mortarPestleStage)
{
case MortarPestleStage.PickReagents:
{
if (_reagentAnimState == ReagentAnim.Hover)
{
_proceedToCrush.Disabled = true;
foreach (Node N in _potionReagentsBox.GetChildren())
{
N.QueueFree();
}
foreach (Sprite S in _reagentAnimSprites)
{
if (S != null)
{
_tween.InterpolateProperty(S, "global_position", S.GlobalPosition, new Vector2(160f + (float)GD.RandRange(-10, 10), 40f + (float)GD.RandRange(-10, 10)), 1f, Tween.TransitionType.Cubic);
}
}
_tween.Start();
_reagentAnimState = ReagentAnim.Fall;
}
else if (_reagentAnimState == ReagentAnim.Fall)
{
foreach (Sprite S in _reagentAnimSprites)
{
if (S != null)
{
_tween.InterpolateProperty(S, "global_position", S.GlobalPosition, _mortar.GlobalPosition + new Vector2((float)GD.RandRange(-10, 10), (float)GD.RandRange(-10, 10)), 1.5f, Tween.TransitionType.Cubic);
}
}
_tween.Start();
_reagentAnimState = ReagentAnim.End;
}
else if (_reagentAnimState == ReagentAnim.End)
{
NextStage();
for (int i = 0; i < _reagentAnimSprites.Length; i++)
{
if (_reagentAnimSprites[i] != null)
{
_reagentAnimSprites[i].QueueFree();
_reagentAnimSprites[i] = null;
}
}
}
break;
}
}
break;
}
}
}
private void InventorySlotSelected(int slotId)
{
if (slotId >= 0)
_itemTooltip.Open(_itemList[slotId].item, _itemTooltip.RectPosition);
else
_itemTooltip.Close();
}
}