/
Tower.cpp
219 lines (174 loc) · 6.71 KB
/
Tower.cpp
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
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Engine/Console.h>
#include <Urho3D/Engine/DebugHud.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/Texture2D.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/UI/Sprite.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/Urho2D/StaticSprite2D.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/IO/FileSystem.h>
#include <Urho3D/Urho2D/PhysicsWorld2D.h>
#include "Tower.h"
TowerApp::TowerApp(Context *context) :
Application(context),
yaw(0.0f),
pitch(0.0f),
screenJoystickIndex(M_MAX_UNSIGNED),
screenJoystickSettingsIndex(M_MAX_UNSIGNED),
paused(false),
drawDebugGeometry(false) {
// nothing
}
void TowerApp::Setup() {
engineParameters_["WindowTitle"] = GetTypeName();
engineParameters_["LogName"] =
GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log";
engineParameters_["FullScreen"] = false;
engineParameters_["TripleBuffer"] = true;
engineParameters_["Headless"] = false;
engineParameters_["Sound"] = false;
}
void TowerApp::Start() {
// Set custom window Title & Icon
SetupWindow();
// Create console and debug HUD
CreateConsoleAndDebugHud();
// Subscribe key down event
SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(TowerApp, HandleKeyDown));
// Create the scene content
CreateScene();
// Create the UI content
CreateInstructions();
// Setup the viewport for displaying the scene
SetupViewport();
// Hook up to the frame update events
SubscribeToEvents();
}
void TowerApp::SetupWindow() {
ResourceCache *cache = GetSubsystem<ResourceCache>();
Graphics *graphics = GetSubsystem<Graphics>();
Image *icon = cache->GetResource<Image>("Textures/UrhoIcon.png");
graphics->SetWindowIcon(icon);
graphics->SetWindowTitle("The Tower");
// Enable OS cursor
GetSubsystem<Input>()->SetMouseVisible(true);
}
void TowerApp::CreateConsoleAndDebugHud() {
// Get default style
ResourceCache *cache = GetSubsystem<ResourceCache>();
XMLFile *xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
// Create console
Console *console = engine_->CreateConsole();
console->SetDefaultStyle(xmlFile);
console->GetBackground()->SetOpacity(0.8f);
// Create debug HUD.
DebugHud *debugHud = engine_->CreateDebugHud();
debugHud->SetDefaultStyle(xmlFile);
}
void TowerApp::CreateScene() {
scene = new GameScene();
scene->Init(context_);
}
void TowerApp::CreateInstructions() {
ResourceCache *cache = GetSubsystem<ResourceCache>();
UI *ui = GetSubsystem<UI>();
// Construct new Text object, set string to display and font to use
Text *instructionText = ui->GetRoot()->CreateChild<Text>();
instructionText->SetText("Use WASD keys to move, use PageUp PageDown keys to zoom.");
instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
// Position the text relative to the screen center
instructionText->SetHorizontalAlignment(HA_CENTER);
instructionText->SetVerticalAlignment(VA_CENTER);
instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
}
void TowerApp::SetupViewport() {
Renderer *renderer = GetSubsystem<Renderer>();
// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
SharedPtr<Viewport> viewport(new Viewport(context_, scene->GetScene(), scene->GetCamera()->GetComponent<Camera>()));
renderer->SetViewport(0, viewport);
}
void TowerApp::MoveCamera(float timeStep) {
// Do not move if the UI has a focused element (the console)
if (GetSubsystem<UI>()->GetFocusElement())
return;
Input *input = GetSubsystem<Input>();
// Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
const SharedPtr<Node> &cameraNode = scene->GetCamera();
cameraNode->GetComponent<Camera>();
if (input->GetKeyDown(KEY_PAGEUP)) {
Camera *camera = cameraNode->GetComponent<Camera>();
camera->SetZoom(camera->GetZoom() * 1.01f);
}
if (input->GetKeyDown(KEY_PAGEDOWN)) {
Camera *camera = cameraNode->GetComponent<Camera>();
camera->SetZoom(camera->GetZoom() * 0.99f);
}
}
void TowerApp::SubscribeToEvents() {
// Subscribe HandleUpdate() function for processing update events
SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(TowerApp, HandleUpdate));
// Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
UnsubscribeFromEvent(E_SCENEUPDATE);
// Post render stuff subscription
SubscribeToEvent(E_POSTRENDERUPDATE, URHO3D_HANDLER(TowerApp, HandlePostRenderUpdate));
}
void TowerApp::HandleUpdate(StringHash, VariantMap &eventData) {
using namespace Update;
// Take the frame time step, which is stored as a float
float timeStep = eventData[P_TIMESTEP].GetFloat();
// Move the camera, scale movement with time step
MoveCamera(timeStep);
}
void TowerApp::HandleKeyDown(StringHash, VariantMap &eventData) {
using namespace KeyDown;
int key = eventData[P_KEY].GetInt();
// Close console (if open) or exit when ESC is pressed
if (key == KEY_ESC) {
Console *console = GetSubsystem<Console>();
if (console->IsVisible())
console->SetVisible(false);
else
engine_->Exit();
return;
}
if (key == '`') {
GetSubsystem<Console>()->Toggle();
return;
}
// Toggle debug HUD with F2
if (key == KEY_F2) {
DebugHud *debugHud = GetSubsystem<DebugHud>();
if (debugHud->GetMode() == 0 || debugHud->GetMode() == DEBUGHUD_SHOW_ALL_MEMORY)
debugHud->SetMode(DEBUGHUD_SHOW_ALL);
else
debugHud->SetMode(DEBUGHUD_SHOW_NONE);
return;
}
if (key == KEY_F3) {
DebugHud *debugHud = GetSubsystem<DebugHud>();
if (debugHud->GetMode() == 0 || debugHud->GetMode() == DEBUGHUD_SHOW_ALL)
debugHud->SetMode(DEBUGHUD_SHOW_ALL_MEMORY);
else
debugHud->SetMode(DEBUGHUD_SHOW_NONE);
return;
}
if (key == KEY_F4) {
drawDebugGeometry = !drawDebugGeometry;
return;
}
}
void TowerApp::HandlePostRenderUpdate(StringHash eventType, VariantMap &eventData) {
PhysicsWorld2D *physicsWorld = scene->GetScene()->GetComponent<PhysicsWorld2D>();
if (physicsWorld && drawDebugGeometry)
physicsWorld->DrawDebugGeometry();
}