forked from ScruffyFurn/SumoDX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumoDX.cpp
287 lines (217 loc) · 8.49 KB
/
SumoDX.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
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "../Rendering/GameRenderer.h"
#include <time.h>
#include "../Utilities/DirectXSample.h"
#include "../GameObjects/Cylinder.h"
using namespace concurrency;
using namespace DirectX;
using namespace Microsoft::WRL;
using namespace Windows::Storage;
using namespace Windows::UI::Core;
//----------------------------------------------------------------------
SumoDX::SumoDX():
m_gameActive(false)
{
m_topScore.bestRoundTime = 0;
}
//----------------------------------------------------------------------
void SumoDX::Initialize(
_In_ MoveLookController^ controller,
_In_ GameRenderer^ renderer
)
{
// This method is expected to be called as an asynchronous task.
// Care should be taken to not call rendering methods on the
// m_renderer as this would result in the D3D Context being
// used in multiple threads, which is not allowed.
m_controller = controller;
m_renderer = renderer;
m_renderObjects = std::vector<GameObject^>();
m_savedState = ref new PersistentState();
m_savedState->Initialize(ApplicationData::Current->LocalSettings->Values, "SumoGame");
m_timer = ref new GameTimer();
srand(time(NULL));
// Create a box primitive to represent the player.
// The box will be used to handle collisions and constrain the player in the world.
m_player = ref new SumoBlock();
m_player->Position(XMFLOAT3(-3.0f, 0.5f, 0.0f));
// It is added to the list of render objects so that it appears on screen.
m_renderObjects.push_back(m_player);
//Create the enemy
// The box will be used to handle collisions and constrain the player in the world.
m_enemy = ref new AISumoBlock(XMFLOAT3(3.0f, 0.5f, 0.0f), m_player, static_cast<GameConstants::Behavior>(rand() % 3));
// It is added to the list of render objects so that it appears on screen.
m_renderObjects.push_back(m_enemy);
//tell the player about their new opponent
m_player->Target(m_enemy);
//floor model
Cylinder^ cylinder;
cylinder = ref new Cylinder(XMFLOAT3(0.0f, -1.0f, 0.0f), 10.0f, XMFLOAT3(0.0f, 1.0f, 0.0f));
m_renderObjects.push_back(cylinder);
m_camera = ref new Camera;
m_camera->SetProjParams(XM_PI / 2, 1.0f, 0.01f, 100.0f);
m_camera->SetViewParams(
XMFLOAT3(0,7,10),// Eye point in world coordinates.
XMFLOAT3(0.0f, -5.0f, 0.0f),// Look at point in world coordinates.
XMFLOAT3 (0.0f, 1.0f, 0.0f) // The Up vector for the camera.
);
m_controller->Pitch(m_camera->Pitch());
m_controller->Yaw(m_camera->Yaw());
// Load the top score from disk if it exists.
LoadHighScore();
// Load the currentScore for saved state if it exists.
LoadState();
m_controller->Active(false);
}
//----------------------------------------------------------------------
void SumoDX::LoadGame()
{
//reset player and enemy
m_player->Position(XMFLOAT3(-3.0f, 0.5f, 0.0f));
m_enemy->Position(XMFLOAT3(3.0f, 0.5f, 0.0f));
m_enemy->Behavior(static_cast<GameConstants::Behavior>(rand() % 3));
//reset camera
m_camera->SetViewParams(
XMFLOAT3(0, 7, 10),// Eye point in world coordinates.
XMFLOAT3(0.0f, -5.0f, 0.0f),// Look at point in world coordinates.
XMFLOAT3(0.0f, 1.0f, 0.0f) // The Up vector for the camera.
);
m_controller->Pitch(m_camera->Pitch());
m_controller->Yaw(m_camera->Yaw());
//reset other things
m_gameActive = false;
m_timer->Reset();
//reset the save game
SaveState();
}
//----------------------------------------------------------------------
void SumoDX::StartLevel()
{
m_timer->Reset();
m_timer->Start();
m_gameActive = true;
m_controller->Active(true);
}
//----------------------------------------------------------------------
void SumoDX::PauseGame()
{
m_timer->Stop();
SaveState();
}
//----------------------------------------------------------------------
void SumoDX::ContinueGame()
{
m_timer->Start();
m_controller->Active(true);
}
//----------------------------------------------------------------------
GameState SumoDX::RunGame()
{
// This method is called to execute a single time interval for active game play.
// It returns the resulting state of game play after the interval has been executed.
m_timer->Update();
//update the camera to look where the user has specified.
m_camera->LookDirection(m_controller->LookDirection());
m_controller->Pitch(m_camera->Pitch());
m_controller->Yaw(m_camera->Yaw());
// run one frame of game play.
m_player->Velocity(m_controller->Velocity());
UpdateDynamics();
//did either leave the mat?
if (abs(XMVectorGetY(XMVector3Length(m_player->VectorPosition()))) > 10.0f)
{
//player lost, place the ringout time into the score variable but don't save.
m_topScore.bestRoundTime = m_timer->PlayingTime();
return GameState::PlayerLost;
}
if (abs(XMVectorGetY(XMVector3Length(m_enemy->VectorPosition()))) > 10.0f)
{
//player won, save his time if it is a new high score.
m_topScore.bestRoundTime = m_timer->PlayingTime();
SaveHighScore();
return GameState::GameComplete;
}
return GameState::Active;
}
//----------------------------------------------------------------------
void SumoDX::OnSuspending()
{
}
//----------------------------------------------------------------------
void SumoDX::OnResuming()
{
}
//----------------------------------------------------------------------
void SumoDX::UpdateDynamics()
{
float timeTotal = m_timer->PlayingTime();
float timeFrame = m_timer->DeltaTime();
// If the elapsed time is too long, we slice up the time and handle physics over several
// smaller time steps to avoid missing collisions.
float timeLeft = timeFrame;
float deltaTime;
while (timeLeft > 0.0f)
{
deltaTime = min(timeLeft, GameConstants::Physics::FrameLength);
timeLeft -= deltaTime;
// Update the player position.
m_player->Position(m_player->VectorPosition() + m_player->VectorVelocity() * deltaTime);
//AI Update
m_enemy->DetermineAIAction(deltaTime);
//Check for player/enemy colision
float xDelta = m_enemy->Position().x - m_player->Position().x;
float zDelta = m_enemy->Position().z - m_player->Position().z;
//since each of our sumo's is 1 unit wide if we subtract one from their position deltas
float overlap = sqrt(xDelta * xDelta + zDelta * zDelta) - 1;
XMVECTOR playerToEnemy = m_enemy->VectorPosition() - m_player->VectorPosition();
// then if we get their overlap value as a negative number a contact has occured.
if (overlap < 0)
{
m_player->Position(m_player->VectorPosition() + playerToEnemy * overlap * 0.5f);
m_enemy->Position(m_enemy->VectorPosition() - playerToEnemy * overlap * 0.5f);
}
}
}
//----------------------------------------------------------------------
void SumoDX::SaveState()
{
// Save basic state of the game.
m_savedState->SaveBool(":GameActive", m_gameActive);
m_savedState->SaveSingle(":LevelPlayingTime", m_timer->PlayingTime());
m_savedState->SaveXMFLOAT3(":PlayerPosition", m_player->Position());
m_savedState->SaveXMFLOAT3(":EnemyPosition", m_enemy->Position());
}
//----------------------------------------------------------------------
void SumoDX::LoadState()
{
m_gameActive = m_savedState->LoadBool(":GameActive", m_gameActive);
if (m_gameActive)
{
// Loading from the last known state means the game wasn't finished when it was last played,
// Reload the current player and enemy position.
m_player->Position(m_savedState->LoadXMFLOAT3(":PlayerPosition", XMFLOAT3(0.0f, 0.0f, 0.0f)));
m_enemy->Position(m_savedState->LoadXMFLOAT3(":EnemyPosition", XMFLOAT3(0.0f, 0.0f, 0.0f)));
m_timer->PlayingTime(m_savedState->LoadSingle(":LevelPlayingTime", 0.0f));
}
}
//----------------------------------------------------------------------
void SumoDX::SaveHighScore()
{
int currentBest = m_savedState->LoadSingle(":HighScore:LevelCompleted", 0);
if (currentBest==NULL || currentBest > m_topScore.bestRoundTime)
{
m_savedState->LoadSingle(":HighScore:LevelCompleted", m_topScore.bestRoundTime);
}
}
//----------------------------------------------------------------------
void SumoDX::LoadHighScore()
{
m_topScore.bestRoundTime = m_savedState->LoadSingle(":HighScore:LevelCompleted", 0);
}
//----------------------------------------------------------------------