-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameState.angelscript
190 lines (168 loc) · 5.02 KB
/
GameState.angelscript
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
class GameState : BaseState
{
private string m_sceneName;
private GameLayer@ m_gameLayer;
private GameMenuLayer@ m_gameMenuLayer;
private uint m_levelIndex;
private GameStateProperties@ m_props;
GameState(const uint levelIndex, const string &in sceneName, GameStateProperties@ props)
{
m_levelIndex = levelIndex;
m_sceneName = sceneName;
@m_props = @props;
}
void start()
{
BaseState::start();
loadScene(m_sceneName);
}
void preLoop()
{
BaseState::preLoop();
@m_gameLayer = GameLayer(@m_props, m_levelIndex);
@m_gameMenuLayer = GameMenuLayer(@m_props, m_levelIndex);
m_layerManager.addLayer(@m_gameLayer);
m_layerManager.addLayer(@m_gameMenuLayer);
m_layerManager.setCurrentLayer("GameLayer");
g_timeManager.resume();
}
void loop()
{
BaseState::loop();
if (m_gameLayer.isButtonPressed("menu_button"))
{
m_gameLayer.setButtonPressed("menu_button", false);
logEvent("GAME_PAUSE_BUTTON_PRESSED", EventDataPair("level", "" + m_levelIndex));
showMenuPopup();
}
if (m_gameMenuLayer.isButtonPressed("resume_button"))
{
m_gameMenuLayer.setButtonPressed("resume_button", false);
logEvent("GAME_RESUME_BUTTON_PRESSED", EventDataPair("level", "" + m_levelIndex));
hideMenuPopup();
}
handleBackButton();
}
void handleBackButton()
{
if (GetInputHandle().GetKeyState(K_BACK) == KS_HIT || GetInputHandle().GetKeyState(K_PAUSE) == KS_HIT)
{
UILayer@ currentLayer = m_layerManager.getCurrentLayer();
if (currentLayer !is null)
{
if (currentLayer.getName() == "GameMenuLayer")
{
hideMenuPopup();
logEvent("GAME_RESUMED_WITH_BACK_BUTTON", EventDataPair("level", "" + m_levelIndex));
}
else if (currentLayer.getName() == "GameLayer")
{
showMenuPopup();
logEvent("GAME_PAUSED_WITH_BACK_BUTTON", EventDataPair("level", "" + m_levelIndex));
}
}
}
}
void showMenuPopup()
{
m_layerManager.setCurrentLayer("GameMenuLayer");
g_timeManager.pause();
}
void hideMenuPopup()
{
m_gameMenuLayer.hide(true);
m_layerManager.setCurrentLayer("GameLayer");
g_timeManager.resume();
}
string getName()
{
return "GameState";
}
}
class GameLayer : UILayer
{
private GameStateProperties@ m_props;
private uint m_currentLevel;
GameLayer(GameStateProperties@ props, const uint currentLevel)
{
m_currentLevel = currentLevel;
@m_props = @props;
UIButton@ menuButton = null;
if (m_props.menuButton != "")
{
// addButton parameters: button name id, sprite file name, normalized pos, normalized origin
addButton("menu_button", m_props.menuButton, m_props.menuButtonNormPos, m_props.menuButtonNormPos);
@menuButton = getButton("menu_button");
menuButton.setCustomColor(m_props.menuButtonsCustomColor);
}
if (m_props.restartLevelButton != "")
{
vector2 menuButtonSize(0, 0);
if (menuButton !is null)
menuButtonSize = menuButton.getSize();
const float screenWidth = GetScreenSize().x;
const vector2 restartLevelButtonPos((screenWidth - menuButtonSize.x) / screenWidth, 0.0f);
addButton("restart_level", m_props.restartLevelButton, restartLevelButtonPos, vector2(1,0));
UIButton@ restartButton = getButton("restart_level");
restartButton.setCustomColor(m_props.menuButtonsCustomColor);
restartButton.setSound(m_props.restartLevelButtonSound);
}
}
void update()
{
UILayer::update();
if (isButtonPressed("restart_level"))
{
setButtonPressed("restart_level", false);
g_stateManager.setState(g_gameStateFactory.createGameState(m_currentLevel));
logEvent("GAME_RESTART_LEVEL_BUTTON", EventDataPair("level", "" + m_currentLevel));
}
}
string getName() const
{
return "GameLayer";
}
}
class GameMenuLayer : SoundPanelLayer
{
private GameStateProperties@ m_props;
private uint m_levelIndex;
GameMenuLayer(GameStateProperties@ props, const uint levelIndex)
{
super(@props);
m_levelIndex = levelIndex;
@m_props = @props;
addSprite(m_props.whiteSquareSprite, props.backgroundColor, V2_ZERO, GetScreenSize(), V2_ZERO);
addButton("back_button", m_props.backToMainMenuButton, m_props.gameMenuExitButtonPos);
addButton("resume_button", m_props.resumeGameButton, m_props.gameMenuResumeButtonPos);
}
void update()
{
SoundPanelLayer::update();
if (isButtonPressed("back_button"))
{
setButtonPressed("back_button", false);
if (m_props.returnToLevelSelect)
g_stateManager.setState(g_gameStateFactory.createLevelSelectState());
else
g_stateManager.setState(g_gameStateFactory.createMenuState());
hide(true);
logEvent("GAME_PAUSE_SCREEN_EXIT_BUTTON", EventDataPair("level", "" + m_levelIndex));
}
}
void draw()
{
SoundPanelLayer::draw();
FloatColor currentLayerColor(getButton("resume_button").getColor()); currentLayerColor.setColor(V3_ONE);
if (m_props.levelNumberFont != "")
{
drawCenteredText(GetScreenSize() * m_props.levelNumberStringNormPos,
m_props.levelNumberString + (m_levelIndex + 1),
m_props.levelNumberFont, g_scale.scale(m_props.levelNumberFontScale), currentLayerColor.getUInt());
}
}
string getName() const
{
return "GameMenuLayer";
}
}