-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastercontrol.cpp
314 lines (276 loc) · 11.4 KB
/
mastercontrol.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
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
/* heXon
// Copyright (C) 2015 LucKey Productions (luckeyproductions.nl)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//#include <Urho3D/Urho3D.h>
//#include <Urho3D/Engine/Engine.h>
//#include <Urho3D/Engine/Console.h>
//#include <Urho3D/Graphics/Graphics.h>
//#include <Urho3D/Graphics/DebugRenderer.h>
//#include <Urho3D/Engine/DebugHud.h>
//#include <Urho3D/DebugNew.h>
//#include <Urho3D/UI/Text.h>
//#include <Urho3D/UI/Font.h>
//#include <Urho3D/Scene/Scene.h>
//#include <Urho3D/Physics/PhysicsWorld.h>
//#include <Urho3D/Physics/CollisionShape.h>
//#include <Urho3D/Graphics/Model.h>
//#include <Urho3D/Graphics/StaticModel.h>
//#include <Urho3D/Graphics/Light.h>
//#include <Urho3D/Graphics/Camera.h>
//#include <Urho3D/Graphics/Material.h>
//#include <Urho3D/Graphics/RenderPath.h>
//#include <Urho3D/IO/FileSystem.h>
//#include <Urho3D/Resource/ResourceCache.h>
//#include <Urho3D/Resource/XMLFile.h>
//#include <Urho3D/Resource/Resource.h>
//#include <Urho3D/Audio/Sound.h>
//#include <Urho3D/Audio/SoundSource.h>
//#include <Urho3D/IO/Log.h>
//#include <Urho3D/Scene/SceneEvents.h>
//#include <Urho3D/Core/CoreEvents.h>
//#include <Urho3D/Graphics/Octree.h>
//#include <Urho3D/Graphics/OctreeQuery.h>
//#include <Urho3D/Graphics/Zone.h>
#include "mastercontrol.h"
#include "hexocam.h"
#include "inputmaster.h"
#include "spawnmaster.h"
#include "player.h"
#include "apple.h"
#include "heart.h"
#include "arenaedge.h"
#include "bullet.h"
#include "seeker.h"
#include "flash.h"
#include "hitfx.h"
#include "explosion.h"
#include "muzzle.h"
DEFINE_APPLICATION_MAIN(MasterControl);
MasterControl::MasterControl(Context *context):
Application(context),
paused_(false),
editMode_(false)
{
}
void MasterControl::Setup()
{
// Modify engine startup parameters.
//Set custom window title and icon.
engineParameters_["WindowTitle"] = "heXon";
engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs")+"heXon.log";
engineParameters_["FullScreen"] = false;
engineParameters_["Headless"] = false;
//engineParameters_["WindowWidth"] = 1920;
//engineParameters_["WindowHeight"] = 1080;
engineParameters_["WindowWidth"]=1280;
engineParameters_["WindowHeight"]=720;
}
void MasterControl::Start()
{
new InputMaster(context_, this);
cache_ = GetSubsystem<ResourceCache>();
graphics_ = GetSubsystem<Graphics>();
renderer_ = GetSubsystem<Renderer>();
CreateSineLookupTable();
// Get default style
defaultStyle_ = cache_->GetResource<XMLFile>("UI/DefaultStyle.xml");
//Create console and debug HUD.
CreateConsoleAndDebugHud();
//Create the scene content
CreateScene();
//Create the UI content
CreateUI();
//Hook up to the frame update and render post-update events
SubscribeToEvents();
Sound* music = cache_->GetResource<Sound>("Resources/Music/Alien Chaos - Disorder.ogg"); //Battle
music->SetLooped(true);
Node* musicNode = world.scene->CreateChild("Music");
SoundSource* musicSource = musicNode->CreateComponent<SoundSource>();
musicSource->SetGain(0.32f);
musicSource->SetSoundType(SOUND_MUSIC);
musicSource->Play(music);
}
void MasterControl::Stop()
{
engine_->DumpResources(true);
}
void MasterControl::SubscribeToEvents()
{
//Subscribe scene update event.
SubscribeToEvent(E_SCENEUPDATE, HANDLER(MasterControl, HandleSceneUpdate));
//Subscribe HandleUpdate() function for processing update events
SubscribeToEvent(E_UPDATE, HANDLER(MasterControl, HandleUpdate));
//Subscribe scene update event.
SubscribeToEvent(E_SCENEUPDATE, HANDLER(MasterControl, HandleSceneUpdate));
}
void MasterControl::CreateConsoleAndDebugHud()
{
// Create console
Console* console = engine_->CreateConsole();
console->SetDefaultStyle(defaultStyle_);
console->GetBackground()->SetOpacity(0.8f);
// Create debug HUD.
DebugHud* debugHud = engine_->CreateDebugHud();
debugHud->SetDefaultStyle(defaultStyle_);
}
void MasterControl::CreateUI()
{
ui_ = GetSubsystem<UI>();
//Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will control the camera
world.cursor.uiCursor = new Cursor(context_);
world.cursor.uiCursor->SetVisible(false);
ui_->SetCursor(world.cursor.uiCursor);
//Set starting position of the cursor at the rendering window center
world.cursor.uiCursor->SetPosition(graphics_->GetWidth()/2, graphics_->GetHeight()/2);
//Construct new Text object, set string to display and font to use
/*Text* instructionText = ui_->GetRoot()->CreateChild<Text>();
instructionText->SetText("heXon");
instructionText->SetFont(cache_->GetResource<Font>("Resources/Fonts/skirmishergrad.ttf"), 32);
instructionText->SetColor(Color(0.0f, 0.75f, 1.0f, 0.5f));
//The text has multiple rows. Center them in relation to each other
instructionText->SetHorizontalAlignment(HA_CENTER);
instructionText->SetVerticalAlignment(VA_CENTER);
instructionText->SetPosition(0, ui_->GetRoot()->GetHeight()/2.1);*/
}
void MasterControl::CreateScene()
{
world.scene = new Scene(context_);
world.octree = world.scene->CreateComponent<Octree>();
physicsWorld_ = world.scene->CreateComponent<PhysicsWorld>();
physicsWorld_->SetGravity(Vector3::ZERO);
world.scene->CreateComponent<DebugRenderer>();
//Create a Zone component for ambient ing & fog control
Node* zoneNode = world.scene->CreateChild("Zone");
Zone* zone = zoneNode->CreateComponent<Zone>();
zone->SetBoundingBox(BoundingBox(Vector3(-100.0f, -50.0f, -100.0f),Vector3(100.0f, 0.0f, 100.0f)));
zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
zone->SetFogColor(Color(0.0f, 0.0f, 0.0f));
zone->SetFogStart(56.8f);
zone->SetFogEnd(61.8f);
//Add a directional light to the world. Enable cascaded shadows on it
Node* lightNode = world.scene->CreateChild("PointLight");
lightNode->SetPosition(Vector3::UP*5.0);
lightNode->SetRotation(Quaternion(90.0f, 0.0f, 0.0f));
Light* light = lightNode->CreateComponent<Light>();
light->SetLightType(LIGHT_DIRECTIONAL);
light->SetBrightness(1.0f);
light->SetRange(7.0f);
light->SetColor(Color(1.0f, 0.9f, 0.95f));
light->SetCastShadows(false);
light->SetShadowBias(BiasParameters(0.00025f, 0.5f));
//Set cascade splits at 10, 50, 200 world unitys, fade shadows at 80% of maximum shadow distance
light->SetShadowCascade(CascadeParameters(7.0f, 23.0f, 42.0f, 500.0f, 0.8f));
//Create cursor
world.cursor.sceneCursor = world.scene->CreateChild("Cursor");
//world.cursor.sceneCursor->SetPosition(Vector3(0.0f,0.0f,0.0f));
StaticModel* cursorObject = world.cursor.sceneCursor->CreateComponent<StaticModel>();
cursorObject->SetModel(cache_->GetResource<Model>("Resources/Models/Hexagon.mdl"));
cursorObject->SetMaterial(cache_->GetResource<Material>("Resources/Materials/Glow.xml"));
world.cursor.sceneCursor->SetEnabled(false);
//Create an invisible plane for mouse raycasting
world.voidNode = world.scene->CreateChild("Void");
//Location is set in update since the plane moves with the camera.
world.voidNode->SetScale(Vector3(1000.0f, 1.0f, 1000.0f));
StaticModel* planeObject = world.voidNode->CreateComponent<StaticModel>();
planeObject->SetModel(cache_->GetResource<Model>("Models/Plane.mdl"));
planeObject->SetMaterial(cache_->GetResource<Material>("Resources/Materials/Invisible.xml"));
//Create camera
world.camera = new heXoCam(context_, this);
//Create arena
tileMaster_ = new TileMaster(context_, this);
for (int i = 0; i < 6; i++){
new ArenaEdge(context_, this, (60.0f * i)+30.0f);
}
spawnMaster_ = new SpawnMaster(context_, this);
player_ = new Player(context_, this);
apple_ = new Apple(context_, this);
heart_ = new Heart(context_, this);
}
void MasterControl::HandleUpdate(StringHash eventType, VariantMap &eventData)
{
}
void MasterControl::HandleSceneUpdate(StringHash eventType, VariantMap &eventData)
{
using namespace Update;
double timeStep = eventData[P_TIMESTEP].GetFloat();
UpdateCursor(timeStep);
}
void MasterControl::UpdateCursor(double timeStep)
{
world.cursor.sceneCursor->Rotate(Quaternion(0.0f,100.0f*timeStep,0.0f));
//world.cursor.sceneCursor->SetScale((world.cursor.sceneCursor->GetWorldPosition() - world.camera->GetWorldPosition()).Length()*0.05f);
if (CursorRayCast(250.0f, world.cursor.hitResults))
{
for (int i = 0; i < world.cursor.hitResults.Size(); i++)
{
if (world.cursor.hitResults[i].node_->GetNameHash() == N_VOID)
{
Vector3 camHitDifference = world.camera->rootNode_->GetWorldPosition() - world.cursor.hitResults[i].position_;
camHitDifference /= world.camera->rootNode_->GetWorldPosition().y_ - world.voidNode->GetPosition().y_;
camHitDifference *= world.camera->rootNode_->GetWorldPosition().y_;
world.cursor.sceneCursor->SetWorldPosition(world.camera->rootNode_->GetWorldPosition()-camHitDifference);
}
}
}
}
bool MasterControl::CursorRayCast(double maxDistance, PODVector<RayQueryResult> &hitResults)
{
IntVector2 mousePos = GetSubsystem<Input>()->GetMousePosition();
Ray cameraRay = world.camera->camera_->GetScreenRay(0.5f,0.5f);
RayOctreeQuery query(hitResults, cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY);
world.scene->GetComponent<Octree>()->Raycast(query);
if (hitResults.Size()) return true;
else return false;
}
bool MasterControl::PhysicsRayCast(PODVector<PhysicsRaycastResult> &hitResults, Ray ray, float distance, unsigned collisionMask = M_MAX_UNSIGNED)
{
physicsWorld_->Raycast(hitResults, ray, distance, collisionMask);
if (hitResults.Size()) return true;
else return false;
}
bool MasterControl::PhysicsSphereCast(PODVector<RigidBody*> &hitResults, Vector3 center, float radius, unsigned collisionMask = M_MAX_UNSIGNED)
{
physicsWorld_->GetRigidBodies(hitResults, Sphere(center, radius), collisionMask);
if (hitResults.Size()) return true;
else return false;
}
void MasterControl::Exit()
{
engine_->Exit();
}
void MasterControl::Restart()
{
}
void MasterControl::CreateSineLookupTable()
{
//Generate sine lookup array
for (int i = 0; i < 1024; i++){
sine_.Push(sin((i/512.0)*2.0*M_PI));
}
}
double MasterControl::Sine(double freq, double min, double max, double shift)
{
double phase = freq * (world.scene->GetElapsedTime() + shift);
double add = 0.5*(min+max);
return Sine(phase) * 0.5 * (max - min) + add;
}
float MasterControl::Sine(float freq, float min, float max, float shift)
{
float phase = freq * world.scene->GetElapsedTime() + shift;
float add = 0.5f*(min+max);
return Sine(phase) * 0.5f * (max - min) + add;
}