Skip to content

Commit a60c792

Browse files
committed
Refine event scene
EventActions take EventScene as a node Conditions and actions can be null instead of holding a func point to () => true Handle continue button in scene instead of ContinuePressed
1 parent 387f3a9 commit a60c792

5 files changed

Lines changed: 70 additions & 58 deletions

File tree

Classes/Events/Assets/Event2.png

-12.8 KB
Loading

Classes/Events/EventDatabase.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/// <summary>
55
/// Holds all game events and their associated logic.
66
/// </summary>
7-
public partial class EventDatabase
7+
public class EventDatabase
88
{
99
public static readonly EventTemplate[] EventDictionary = new[]
1010
{
@@ -18,15 +18,15 @@ public partial class EventDatabase
1818
{
1919
int randIndex = StageProducer.GlobalRng.RandiRange(
2020
0,
21-
StageProducer.PlayerStats.CurNotes.Length
21+
StageProducer.PlayerStats.CurNotes.Length - 1
2222
);
2323
StageProducer.PlayerStats.RemoveNote(randIndex);
2424
},
2525
(self, node) =>
2626
{
2727
int randIndex = StageProducer.GlobalRng.RandiRange(
2828
0,
29-
StageProducer.PlayerStats.CurRelics.Length
29+
StageProducer.PlayerStats.CurRelics.Length - 1
3030
);
3131
StageProducer.PlayerStats.RemoveRelic(randIndex);
3232
},
@@ -50,16 +50,16 @@ public partial class EventDatabase
5050
[
5151
(self, node) =>
5252
{
53-
var spinner = node.GetNodeOrNull<Sprite2D>("EventSprite");
54-
int spinOutcome = (int)StageProducer.GlobalRng.RandiRange(0, 5);
53+
var spinner = node.EventSprite;
54+
int spinOutcome = StageProducer.GlobalRng.RandiRange(0, 5);
5555

5656
int outcomeCount = 6;
5757
float sectorAngle = 360f / outcomeCount;
5858
float targetAngle = spinOutcome * sectorAngle;
5959
float fullSpins = 6 * 360f;
6060
float finalRotation = spinner.RotationDegrees % 360f + fullSpins + targetAngle;
6161

62-
var tween = node.GetTree().CreateTween();
62+
var tween = node.CreateTween();
6363
tween
6464
.TweenProperty(spinner, "rotation_degrees", finalRotation, 2.5f)
6565
.SetTrans(Tween.TransitionType.Cubic)
@@ -112,15 +112,14 @@ public partial class EventDatabase
112112
);
113113
break;
114114
}
115+
node.AnyButtonPressed(0);
115116
})
116117
);
117118
},
118-
(self, node) => {
119-
// does nothing
120-
},
119+
null,
121120
],
122121
GD.Load<Texture2D>("res://Classes/Events/Assets/Event2.png"),
123-
[() => true, () => true]
122+
[null, null]
124123
),
125124
new EventTemplate(
126125
2,
@@ -147,7 +146,7 @@ public partial class EventDatabase
147146
},
148147
],
149148
GD.Load<Texture2D>("res://Classes/Events/Assets/TEMP.png"),
150-
[() => true, () => true, () => StageProducer.PlayerStats.Money >= 30]
149+
[null, null, () => StageProducer.PlayerStats.Money >= 30]
151150
),
152151
};
153152
}

Classes/Events/EventTemplate.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using Godot;
33

4-
public delegate void EventAction(EventTemplate self, Node contextNode);
4+
public delegate void EventAction(EventTemplate self, EventScene contextNode);
55
public delegate bool EventCondition();
66

7-
public partial class EventTemplate
7+
//TODO: Consider making event option struct for better parallelizing of option parameters
8+
9+
public class EventTemplate
810
{
911
public int Id;
1012
public string EventDescription;

Scenes/EventScene/EventScene.cs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,19 @@ public partial class EventScene : Node
1919
[Export]
2020
private VBoxContainer _buttonContainer;
2121

22-
private static Theme _buttonTheme = GD.Load<Theme>("res://Scenes/UI/Assets/GeneralTheme.tres"); // Store the theme
22+
[Export]
23+
private Button _continueButton;
2324

24-
private EventTemplate _eventReference;
25-
private int updateOutputIndex;
25+
[Export]
26+
private MarginContainer _continueContainer;
2627

27-
public static EventScene NewEventScene()
28-
{
29-
EventScene result = GD.Load<PackedScene>(LoadPath).Instantiate<EventScene>();
30-
result._player = GD.Load<PackedScene>(PlayerPuppet.LoadPath).Instantiate<PlayerPuppet>();
31-
result.PlayerMarker.AddChild(result._player);
32-
result.DisplayEvent();
33-
return result;
34-
}
28+
private static readonly Theme ButtonTheme = GD.Load<Theme>(
29+
"res://Scenes/UI/Assets/GeneralTheme.tres"
30+
); // Store the theme
31+
32+
private EventTemplate _eventReference;
33+
private int _updateOutputIndex;
3534

36-
//Eventually remove this, once integrated into game
3735
public override void _Ready()
3836
{
3937
GD.Print("loaded event");
@@ -57,14 +55,13 @@ public override void _Process(double delta)
5755

5856
if (_eventDescription.Text == "")
5957
{
60-
_eventDescription.Text = _eventReference.OutcomeDescriptions[updateOutputIndex];
58+
_eventDescription.Text = _eventReference.OutcomeDescriptions[_updateOutputIndex];
6159
}
6260
}
6361

6462
/// <summary>
65-
/// Displays the event with the given index.
63+
/// Displays the set event.
6664
/// </summary>
67-
/// <param name="eventIndex">Index of the event in EventDatabase.</param>
6865
public void DisplayEvent()
6966
{
7067
_eventDescription.Text = _eventReference.EventDescription;
@@ -75,15 +72,25 @@ public void DisplayEvent()
7572
var button = new Button
7673
{
7774
Text = _eventReference.ButtonDescriptions[i],
78-
Theme = _buttonTheme,
75+
Theme = ButtonTheme,
7976
SizeFlagsVertical = Control.SizeFlags.Expand | Control.SizeFlags.Fill,
8077
};
8178

82-
// Check if the button should be enabled
83-
bool isEnabled =
79+
bool isEnabled;
80+
if (
8481
_eventReference.OptionEnabledConditions == null
8582
|| _eventReference.OptionEnabledConditions.Length <= i
86-
|| _eventReference.OptionEnabledConditions[i]?.Invoke() != false;
83+
)
84+
{
85+
GD.PushWarning("Event Conditions are invalid for event: " + _eventReference.Id);
86+
isEnabled = true;
87+
}
88+
else
89+
{ // Check if the button should be enabled
90+
isEnabled =
91+
_eventReference.OptionEnabledConditions[i] == null
92+
|| _eventReference.OptionEnabledConditions[i].Invoke();
93+
}
8794

8895
button.Disabled = !isEnabled;
8996

@@ -105,29 +112,12 @@ public void DisplayEvent()
105112
}
106113
}
107114

108-
private void AnyButtonPressed(int capturedIndex)
115+
public void AnyButtonPressed(int capturedIndex)
109116
{
110-
updateOutputIndex = capturedIndex;
111-
112-
foreach (var choices in _buttonContainer.GetChildren())
113-
{
114-
if (choices is not Button)
115-
continue;
116-
choices.QueueFree();
117-
}
118-
117+
_updateOutputIndex = capturedIndex;
118+
_buttonContainer.GetParent<Control>().Visible = false;
119+
_continueContainer.Visible = _eventReference.OutcomeDescriptions[capturedIndex] != "";
119120
_eventDescription.Text = _eventReference.OutcomeDescriptions[capturedIndex];
120-
var button = new Button { Text = "EVENT_CONTINUE_BUTTON", Theme = _buttonTheme };
121-
122-
button.Pressed += ContinuePressed;
123-
124-
button.SizeFlagsVertical = Control.SizeFlags.Expand | Control.SizeFlags.Fill;
125-
button.CallDeferred("grab_focus");
126-
_buttonContainer.AddChild(button);
127-
}
128-
129-
private void ContinuePressed()
130-
{
131-
StageProducer.LiveInstance.TransitionStage(Stages.Map);
121+
_continueButton.GrabFocus();
132122
}
133123
}

Scenes/EventScene/EventScene.tscn

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=8 format=3 uid="uid://u7oppwtmvmci"]
1+
[gd_scene load_steps=10 format=3 uid="uid://u7oppwtmvmci"]
22

33
[ext_resource type="Script" uid="uid://md1vhln8ji5h" path="res://Scenes/EventScene/EventScene.cs" id="1_x82kl"]
44
[ext_resource type="AudioStream" uid="uid://be5ial13ynf3o" path="res://Audio/Song1.ogg" id="2_vywvm"]
@@ -7,14 +7,18 @@
77
[ext_resource type="Texture2D" uid="uid://8u3xvcma81d" path="res://Scenes/UI/Assets/UI_CrystalFrame.png" id="5_erk58"]
88
[ext_resource type="Script" uid="uid://cp6t6haqyef7o" path="res://Scenes/AreaBasedBackground.cs" id="5_v3lan"]
99
[ext_resource type="Texture2D" uid="uid://burj10os057fx" path="res://Scenes/UI/Assets/UI_CrystalFrameInset.png" id="6_4prsq"]
10+
[ext_resource type="Theme" uid="uid://d37e3tpsbxwak" path="res://Scenes/UI/Assets/GeneralTheme.tres" id="8_4prsq"]
11+
[ext_resource type="Script" uid="uid://cahjluc6v7ked" path="res://Scenes/UI/TitleScreen/Scripts/SceneChange.cs" id="9_lw2xf"]
1012

11-
[node name="EventScene" type="Node2D" node_paths=PackedStringArray("PlayerMarker", "EventSprite", "_eventDescription", "_buttonContainer")]
13+
[node name="EventScene" type="Node2D" node_paths=PackedStringArray("PlayerMarker", "EventSprite", "_eventDescription", "_buttonContainer", "_continueButton", "_continueContainer")]
1214
process_mode = 1
1315
script = ExtResource("1_x82kl")
1416
PlayerMarker = NodePath("PlayerMarker")
1517
EventSprite = NodePath("EventSprite")
1618
_eventDescription = NodePath("Control/MarginContainer/PanelContainer/HBoxContainer/DescBox/DescMargin/Description")
1719
_buttonContainer = NodePath("Control/MarginContainer/PanelContainer/HBoxContainer/ButtonContainer/VBoxContainer")
20+
_continueButton = NodePath("Control/MarginContainer/PanelContainer/HBoxContainer/ContinueContainer/ContinueButton")
21+
_continueContainer = NodePath("Control/MarginContainer/PanelContainer/HBoxContainer/ContinueContainer")
1822

1923
[node name="UILayer" type="CanvasLayer" parent="." node_paths=PackedStringArray("CurSceneNode")]
2024
script = ExtResource("2_x82kl")
@@ -87,7 +91,7 @@ size_flags_horizontal = 3
8791
size_flags_vertical = 3
8892
theme_override_constants/margin_left = 5
8993
theme_override_constants/margin_top = 5
90-
theme_override_constants/margin_right = -10
94+
theme_override_constants/margin_right = 5
9195
theme_override_constants/margin_bottom = 5
9296

9397
[node name="DescBackground" type="NinePatchRect" parent="Control/MarginContainer/PanelContainer/HBoxContainer/DescBox"]
@@ -100,6 +104,7 @@ patch_margin_bottom = 7
100104

101105
[node name="DescMargin" type="MarginContainer" parent="Control/MarginContainer/PanelContainer/HBoxContainer/DescBox"]
102106
layout_mode = 2
107+
size_flags_stretch_ratio = 0.0
103108
theme_override_constants/margin_left = 0
104109
theme_override_constants/margin_top = 0
105110
theme_override_constants/margin_right = 0
@@ -117,10 +122,26 @@ text_overrun_behavior = 1
117122

118123
[node name="ButtonContainer" type="MarginContainer" parent="Control/MarginContainer/PanelContainer/HBoxContainer"]
119124
layout_mode = 2
120-
theme_override_constants/margin_left = 10
125+
theme_override_constants/margin_left = -5
121126
theme_override_constants/margin_top = 7
122127
theme_override_constants/margin_right = 10
123128
theme_override_constants/margin_bottom = 7
124129

125130
[node name="VBoxContainer" type="VBoxContainer" parent="Control/MarginContainer/PanelContainer/HBoxContainer/ButtonContainer"]
126131
layout_mode = 2
132+
133+
[node name="ContinueContainer" type="MarginContainer" parent="Control/MarginContainer/PanelContainer/HBoxContainer"]
134+
visible = false
135+
layout_mode = 2
136+
theme_override_constants/margin_left = -5
137+
theme_override_constants/margin_top = 7
138+
theme_override_constants/margin_right = 10
139+
theme_override_constants/margin_bottom = 7
140+
141+
[node name="ContinueButton" type="Button" parent="Control/MarginContainer/PanelContainer/HBoxContainer/ContinueContainer"]
142+
layout_mode = 2
143+
size_flags_vertical = 8
144+
theme = ExtResource("8_4prsq")
145+
text = "EVENT_CONTINUE_BUTTON"
146+
script = ExtResource("9_lw2xf")
147+
ScenePath = 7

0 commit comments

Comments
 (0)