comparison LightClone/Source/Mediator.cpp @ 75:57c0ce406a68 tip

Add main menu
author koryspansel <koryspansel@bendbroadband.com>
date Tue, 18 Oct 2011 17:08:17 -0700
parents 40c0b5305de8
children
comparison
equal deleted inserted replaced
74:40c0b5305de8 75:57c0ce406a68
3 */ 3 */
4 4
5 #include "Mediator.h" 5 #include "Mediator.h"
6 #include "VertexTypes.h" 6 #include "VertexTypes.h"
7 7
8 #pragma warning(disable:4355)
9
10 /*
11 * fUpdatePeriod
12 */
13 static const float fUpdatePeriod = 1.0f / 60.0f;
14
15 /* 8 /*
16 * Mediator 9 * Mediator
17 */ 10 */
18 Mediator::Mediator() : kWindow(this) 11 Mediator::Mediator() : Application()
19 { 12 {
20 pGraphicsDevice = NULL; 13 pGraphicsDevice = NULL;
21 } 14 }
22 15
23 /* 16 /*
24 * Run
25 */
26 ErrorCode Mediator::Run()
27 {
28 ErrorCode eCode = Initialize();
29 if(eCode == Error_Success)
30 {
31 kClock.Reset();
32
33 fAccumulator = fUpdatePeriod;
34 while(nApplicationState != ApplicationState_Exit)
35 {
36 ProcessMessages();
37 ProcessUpdate();
38 ProcessRender();
39 }
40
41 Terminate();
42 }
43
44 return eCode;
45 }
46
47 /*
48 * OnMessage
49 */
50 int32 Mediator::OnMessage(Window* pInstance, uint32 nMessage, WPARAM wParam, LPARAM lParam)
51 {
52 if(nMessage == WM_CLOSE)
53 {
54 pInstance->Terminate();
55 return 0;
56 }
57 else
58
59 if(nMessage == WM_DESTROY)
60 {
61 PostQuitMessage(0);
62 return 0;
63 }
64
65 return DefWindowProc(pInstance->GetHandle(), nMessage, wParam, lParam);
66 }
67
68 /*
69 * Initialize 17 * Initialize
70 */ 18 */
71 ErrorCode Mediator::Initialize() 19 ErrorCode Mediator::Initialize()
72 { 20 {
73 InitializeTrace(TraceFlag_Debug | TraceFlag_File); 21 InitializeTrace(TraceFlag_Debug | TraceFlag_File);
74 22
75 ErrorCode eCode = kWindow.Initialize(); 23 ErrorCode eCode = Application::Initialize();
76 if(eCode != Error_Success) 24 if(eCode != Error_Success)
77 { 25 {
78 TRACE("Error: Failed to initialize window\n"); 26 TRACE("Error: Failed to initialize application\n");
79 27
80 Terminate(); 28 Terminate();
81 return eCode; 29 return eCode;
82 } 30 }
83 31
160 108
161 Terminate(); 109 Terminate();
162 return eCode; 110 return eCode;
163 } 111 }
164 112
165 eCode = InitializeInterface(&kServiceProvider); 113 eCode = kInterface.Initialize(&kServiceProvider);
166 if(eCode != Error_Success) 114 if(eCode != Error_Success)
167 { 115 {
168 TRACE("Error: Failed to initialize interface\n"); 116 TRACE("Error: Failed to initialize interface\n");
169 117
170 Terminate(); 118 Terminate();
171 return eCode; 119 return eCode;
172 } 120 }
173 121
174 nGameState = GameState_Load; 122 eCode = InitializeMainMenu();
175 nSimulationState = SimulationState_Idle; 123 if(eCode != Error_Success)
176 nCurrentLevel = 0; 124 {
125 TRACE("Error: Failed to initialize main menu\n");
126
127 Terminate();
128 return eCode;
129 }
130
131 eCode = InitializeHelpMenu();
132 if(eCode != Error_Success)
133 {
134 TRACE("Error: Failed to initialize help menu\n");
135
136 Terminate();
137 return eCode;
138 }
139
140 eCode = InitializeHud();
141 if(eCode != Error_Success)
142 {
143 TRACE("Error: Failed to initialize HUD\n");
144
145 Terminate();
146 return eCode;
147 }
148
149 SetApplicationState(ApplicationState_Main);
177 150
178 return eCode; 151 return eCode;
179 } 152 }
180 153
181 /* 154 /*
201 */ 174 */
202 void Mediator::Update(float fElapsed) 175 void Mediator::Update(float fElapsed)
203 { 176 {
204 kInputManager.Update(fElapsed); 177 kInputManager.Update(fElapsed);
205 178
179 UpdateInput(fElapsed);
180
206 if(nApplicationState == ApplicationState_Main) 181 if(nApplicationState == ApplicationState_Main)
207 { 182 {
208 } 183 }
209 else 184 else
210 185
211 if(nApplicationState == ApplicationState_Game) 186 if(nApplicationState == ApplicationState_Game)
212 { 187 {
213 UpdateInput(fElapsed);
214 UpdateLogic(fElapsed); 188 UpdateLogic(fElapsed);
215 189
216 { 190 {
217 const D3DXVECTOR3& kCameraPosition = kCameraController.GetLocation(); 191 const D3DXVECTOR3& kCameraPosition = kCameraController.GetLocation();
218 192
219 char kBuffer[256]; 193 char kBuffer[256];
220 sprintf_s(kBuffer, "Camera: <%.2f, %.2f, %.2f> (%.2f, %.2f, %.2f)", kCameraPosition.x, kCameraPosition.y, kCameraPosition.z, kCameraController.fCameraDistance, kCameraController.fCameraYaw, kCameraController.fCameraPitch); 194 sprintf_s(kBuffer, "Camera: <%.2f, %.2f, %.2f> (%.2f, %.2f, %.2f)", kCameraPosition.x, kCameraPosition.y, kCameraPosition.z, kCameraController.fCameraDistance, kCameraController.fCameraYaw, kCameraController.fCameraPitch);
221 pDebugText->SetText(kBuffer); 195 pDebugText->SetText(kBuffer);
222 } 196 }
223
224 kInterface.Update(fElapsed);
225 } 197 }
226 else 198 else
227 199
228 if(nApplicationState == ApplicationState_Pause) 200 if(nApplicationState == ApplicationState_Pause)
229 { 201 {
231 else 203 else
232 204
233 if(nApplicationState == ApplicationState_Help) 205 if(nApplicationState == ApplicationState_Help)
234 { 206 {
235 } 207 }
236 else 208
237 209 kInterface.Update(fElapsed);
238 if(nApplicationState == ApplicationState_Confirm)
239 {
240 }
241
242 //kScreenManager.Update(fElapsed);
243 } 210 }
244 211
245 /* 212 /*
246 * Render 213 * Render
247 */ 214 */
248 void Mediator::Render() 215 void Mediator::Render()
249 { 216 {
250 const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32); 217 const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32);
251 218
252 kContext.Begin(nColor); 219 kContext.Begin(nColor);
253 //kScreenManager.Render(kContext);
254 220
255 if(nApplicationState == ApplicationState_Main) 221 if(nApplicationState == ApplicationState_Main)
256 { 222 {
257 } 223 }
258 else 224 else
259 225
260 if(nApplicationState == ApplicationState_Game) 226 if(nApplicationState == ApplicationState_Game)
261 { 227 {
262 if(nGameState != GameState_Load) 228 if(nGameState >= GameState_Active)
263 { 229 {
264 D3DVIEWPORT9 kOriginal; 230 RenderGame();
265 kContext.GetViewport(&kOriginal); 231 }
266
267 D3DVIEWPORT9 kViewport;
268 kViewport.X = 0;
269 kViewport.Y = 0;
270 kViewport.Width = ScreenSizeX - 280; // minus size of interface
271 kViewport.Height = ScreenSizeY;
272 kViewport.MinZ = kOriginal.MinZ;
273 kViewport.MaxZ = kOriginal.MaxZ;
274
275 kContext.SetViewport(kViewport);
276
277 kCameraController.SetMode(CameraMode_3D);
278
279 kEnvironment.Render(kContext, kCameraController);
280 kBot.Render(kContext, kCameraController);
281
282 kContext.SetViewport(kOriginal);
283 }
284
285 kCameraController.SetMode(CameraMode_2D);
286 kInterface.Render(kContext, kCameraController);
287 } 232 }
288 else 233 else
289 234
290 if(nApplicationState == ApplicationState_Pause) 235 if(nApplicationState == ApplicationState_Pause)
291 { 236 {
293 else 238 else
294 239
295 if(nApplicationState == ApplicationState_Help) 240 if(nApplicationState == ApplicationState_Help)
296 { 241 {
297 } 242 }
298 else 243
299 244 RenderInterface();
300 if(nApplicationState == ApplicationState_Confirm)
301 {
302 }
303 245
304 kContext.End(); 246 kContext.End();
305 } 247 }
306 248
307 /* 249 /*
308 * ProcessMessages 250 * InitializeMainMenu
309 */ 251 */
310 void Mediator::ProcessMessages() 252 ErrorCode Mediator::InitializeMainMenu()
311 { 253 {
312 MSG kMessage; 254 pMainBackground = new GuiImage();
313 255 pMainBackground->Initialize(&kServiceProvider);
314 while(PeekMessage(&kMessage, NULL, 0, 0, PM_REMOVE)) 256 pMainBackground->SetTexture("Data\\Textures\\MainMenu\\Background.png", true);
315 { 257 pMainBackground->SetPosition(0, 0);
316 if(kMessage.message == WM_QUIT) 258 pMainBackground->ClearFlag(GuiElementFlag_Visible);
317 { 259 pMainBackground->SetDepth(512.0f);
318 nApplicationState = ApplicationState_Exit; 260
261 kInterface.Add(pMainBackground);
262
263 return Error_Success;
264 }
265
266 /*
267 * InitializeHelpMenu
268 */
269 ErrorCode Mediator::InitializeHelpMenu()
270 {
271 return Error_Success;
272 }
273
274 /*
275 * InitializeHud
276 */
277 ErrorCode Mediator::InitializeHud()
278 {
279 pHudBackground = new GuiImage();
280 pHudBackground->Initialize(&kServiceProvider);
281 pHudBackground->SetTexture("Data\\Textures\\Background04.tga", true);
282 pHudBackground->SetPosition(ScreenSizeX - pHudBackground->GetWidth(), 0.0f);
283 pHudBackground->SetDepth(512.0f);
284 pHudBackground->ClearFlag(GuiElementFlag_Visible);
285
286 pToolbar = new ActionPanel(4, 2);
287 pToolbar->Initialize(&kServiceProvider);
288 pToolbar->SetTexture("Data\\Textures\\PanelA.png");
289 pToolbar->SetPosition(16, 16.0f);
290 pToolbar->SetAction(0, Action_Forward);
291 pToolbar->SetAction(1, Action_RotateCW);
292 pToolbar->SetAction(2, Action_RotateCCW);
293 pToolbar->SetAction(3, Action_Jump);
294 pToolbar->SetAction(4, Action_Light);
295 pToolbar->SetAction(5, Action_FunctionA);
296 pToolbar->SetAction(6, Action_FunctionB);
297 pToolbar->SetPermanent(true);
298 pToolbar->SetDepth(256.0f);
299
300 GuiLabel* pMainLabel = new GuiLabel();
301 pMainLabel->Initialize(&kServiceProvider);
302 pMainLabel->SetFont("Courier New", 16);
303 pMainLabel->SetText("Main:");
304 pMainLabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
305 pMainLabel->SetPosition(26.0f, 149.0f);
306 pMainLabel->SetDepth(256.0f);
307
308 pCode[0] = new ActionPanel(4, 3);
309 pCode[0]->Initialize(&kServiceProvider);
310 pCode[0]->SetTexture("Data\\Textures\\PanelB.png");
311 pCode[0]->SetPosition(16.0f, 160.0f);
312 pCode[0]->Subscribe(ActionPanel::EventAction, &Mediator::OnAction, this);
313 pCode[0]->SetDepth(256.0f);
314
315 GuiLabel* pFunctionALabel = new GuiLabel();
316 pFunctionALabel->Initialize(&kServiceProvider);
317 pFunctionALabel->SetFont("Courier New", 16);
318 pFunctionALabel->SetText("Function 1:");
319 pFunctionALabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
320 pFunctionALabel->SetPosition(26.0f, 349.0f);
321
322 pCode[1] = new ActionPanel(4, 2);
323 pCode[1]->Initialize(&kServiceProvider);
324 pCode[1]->SetTexture("Data\\Textures\\PanelA.png");
325 pCode[1]->SetPosition(16.0f, 360.0f);
326 pCode[1]->Subscribe(ActionPanel::EventAction, &Mediator::OnAction, this);
327
328 GuiLabel* pFunctionBLabel = new GuiLabel();
329 pFunctionBLabel->Initialize(&kServiceProvider);
330 pFunctionBLabel->SetFont("Courier New", 16);
331 pFunctionBLabel->SetText("Function 2:");
332 pFunctionBLabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
333 pFunctionBLabel->SetPosition(26.0f, 493.0f);
334
335 pCode[2] = new ActionPanel(4, 2);
336 pCode[2]->Initialize(&kServiceProvider);
337 pCode[2]->SetTexture("Data\\Textures\\PanelA.png");
338 pCode[2]->SetPosition(16.0f, 504.0f);
339 pCode[2]->Subscribe(ActionPanel::EventAction, &Mediator::OnAction, this);
340
341 const float fButtonPadding = 32.0f;
342 const float fButtonSpacing = 8.0f;
343 const float fButtonSize = 48.0f;
344
345 pButtonPlay = new GuiButton();
346 pButtonPlay->Initialize(&kServiceProvider);
347 pButtonPlay->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
348 pButtonPlay->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
349 pButtonPlay->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
350 pButtonPlay->SetFont("Courier New", 16, FW_BOLD);
351 pButtonPlay->SetText("Play");
352 pButtonPlay->SetColor(D3DCOLOR_XRGB(0, 0, 0));
353 pButtonPlay->SetPosition(fButtonPadding + 0.0f * (fButtonSize + fButtonSpacing), 652.0f);
354 pButtonPlay->Subscribe(GuiButton::EventClick, &Mediator::OnPlay, this);
355
356 pButtonStop = new GuiButton();
357 pButtonStop->Initialize(&kServiceProvider);
358 pButtonStop->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
359 pButtonStop->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
360 pButtonStop->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
361 pButtonStop->SetFont("Courier New", 16, FW_BOLD);
362 pButtonStop->SetText("Stop");
363 pButtonStop->SetColor(D3DCOLOR_XRGB(0, 0, 0));
364 pButtonStop->SetPosition(fButtonPadding + 1.0f * (fButtonSize + fButtonSpacing), 652.0f);
365 pButtonStop->Subscribe(GuiButton::EventClick, &Mediator::OnStop, this);
366
367 pButtonReset = new GuiButton();
368 pButtonReset->Initialize(&kServiceProvider);
369 pButtonReset->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
370 pButtonReset->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
371 pButtonReset->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
372 pButtonReset->SetFont("Courier New", 16, FW_BOLD);
373 pButtonReset->SetText("Reset");
374 pButtonReset->SetColor(D3DCOLOR_XRGB(0, 0, 0));
375 pButtonReset->SetPosition(fButtonPadding + 2.0f * (fButtonSize + fButtonSpacing), 652.0f);
376 pButtonReset->Subscribe(GuiButton::EventClick, &Mediator::OnReset, this);
377
378 pButtonExit = new GuiButton();
379 pButtonExit->Initialize(&kServiceProvider);
380 pButtonExit->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
381 pButtonExit->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
382 pButtonExit->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
383 pButtonExit->SetFont("Courier New", 16, FW_BOLD);
384 pButtonExit->SetText("Exit");
385 pButtonExit->SetColor(D3DCOLOR_XRGB(0, 0, 0));
386 pButtonExit->SetPosition(fButtonPadding + 3.0f * (fButtonSize + fButtonSpacing), 652.0f);
387 pButtonExit->Subscribe(GuiButton::EventClick, &Mediator::OnExit, this);
388
389 pHudBackground->Add(pToolbar);
390 pHudBackground->Add(pMainLabel);
391 pHudBackground->Add(pCode[0]);
392 pHudBackground->Add(pFunctionALabel);
393 pHudBackground->Add(pCode[1]);
394 pHudBackground->Add(pFunctionBLabel);
395 pHudBackground->Add(pCode[2]);
396 pHudBackground->Add(pButtonPlay);
397 pHudBackground->Add(pButtonStop);
398 pHudBackground->Add(pButtonReset);
399 pHudBackground->Add(pButtonExit);
400
401 pDebugText = new GuiLabel();
402 pDebugText->Initialize(&kServiceProvider);
403 pDebugText->SetFont("Courier New", 16);
404 pDebugText->SetPosition(10.0f, 10.0f);
405 pDebugText->SetText("Debug");
406 pDebugText->SetColor(D3DCOLOR_XRGB(255, 255, 255));
407
408 kInterface.Add(pHudBackground);
409 kInterface.Add(pDebugText);
410
411 return Error_Success;
412 }
413
414 /*
415 * UpdateInput
416 */
417 void Mediator::UpdateInput(float fElapsed)
418 {
419 if(nApplicationState == ApplicationState_Main)
420 {
421 if(kInputManager.IsKeyDown(DIK_SPACE))
422 {
423 nCurrentLevel = 0;
424 nGameState = GameState_Load;
425 nSimulationState = SimulationState_Idle;
426
427 SetApplicationState(ApplicationState_Game);
428 }
429 }
430 else
431
432 if(nApplicationState == ApplicationState_Help)
433 {
434 }
435 else
436
437 if(nApplicationState == ApplicationState_Game)
438 {
439 #if defined(_DEBUG)
440 if(kInputManager.IsKeyDown(DIK_LEFT))
441 {
442 kCameraController.Yaw(0.01f);
319 } 443 }
320 else 444 else
321 { 445
322 TranslateMessage(&kMessage); 446 if(kInputManager.IsKeyDown(DIK_RIGHT))
323 DispatchMessage(&kMessage); 447 {
324 } 448 kCameraController.Yaw(-0.01f);
325 } 449 }
326 } 450
327 451 if(kInputManager.IsKeyDown(DIK_UP))
328 /* 452 {
329 * ProcessUpdate 453 kCameraController.Pitch(0.01f);
330 */ 454 }
331 void Mediator::ProcessUpdate() 455 else
332 { 456
333 fAccumulator += Min(kClock.GetElapsed(), fUpdatePeriod); 457 if(kInputManager.IsKeyDown(DIK_DOWN))
334 while(fAccumulator >= fUpdatePeriod) 458 {
335 { 459 kCameraController.Pitch(-0.01f);
336 Update(fUpdatePeriod); 460 }
337 fAccumulator -= fUpdatePeriod; 461
338 } 462 if(kInputManager.IsKeyDown(DIK_NEXT))
339 } 463 {
340 464 kCameraController.Move(0.1f);
341 /* 465 }
342 * ProcessRender 466 else
343 */ 467
344 void Mediator::ProcessRender() 468 if(kInputManager.IsKeyDown(DIK_PRIOR))
345 { 469 {
346 Render(); 470 kCameraController.Move(-0.1f);
347 } 471 }
348 472
349 /* 473 static bool bControl = false;
350 * Load 474 static uint32 nBuffer[4] = {0};
351 */ 475 static uint32 nCount = 0;
352 ErrorCode Mediator::Load(const char* pName) 476
353 { 477 if(bControl)
354 ErrorCode eCode = kLoader.Load(pName); 478 {
479 if(kInputManager.IsKeyDown(DIK_0) && !kInputManager.WasKeyDown(DIK_0))
480 {
481 nBuffer[nCount++] = 0;
482 }
483 else
484
485 if(kInputManager.IsKeyDown(DIK_1) && !kInputManager.WasKeyDown(DIK_1))
486 {
487 nBuffer[nCount++] = 1;
488 }
489 else
490
491 if(kInputManager.IsKeyDown(DIK_2) && !kInputManager.WasKeyDown(DIK_2))
492 {
493 nBuffer[nCount++] = 2;
494 }
495 else
496
497 if(kInputManager.IsKeyDown(DIK_3) && !kInputManager.WasKeyDown(DIK_3))
498 {
499 nBuffer[nCount++] = 3;
500 }
501 else
502
503 if(kInputManager.IsKeyDown(DIK_4) && !kInputManager.WasKeyDown(DIK_4))
504 {
505 nBuffer[nCount++] = 4;
506 }
507 else
508
509 if(kInputManager.IsKeyDown(DIK_5) && !kInputManager.WasKeyDown(DIK_5))
510 {
511 nBuffer[nCount++] = 5;
512 }
513 else
514
515 if(kInputManager.IsKeyDown(DIK_6) && !kInputManager.WasKeyDown(DIK_6))
516 {
517 nBuffer[nCount++] = 6;
518 }
519 else
520
521 if(kInputManager.IsKeyDown(DIK_7) && !kInputManager.WasKeyDown(DIK_7))
522 {
523 nBuffer[nCount++] = 7;
524 }
525 else
526
527 if(kInputManager.IsKeyDown(DIK_8) && !kInputManager.WasKeyDown(DIK_8))
528 {
529 nBuffer[nCount++] = 8;
530 }
531 else
532
533 if(kInputManager.IsKeyDown(DIK_9) && !kInputManager.WasKeyDown(DIK_9))
534 {
535 nBuffer[nCount++] = 9;
536 }
537
538 if(!kInputManager.IsKeyDown(DIK_LCONTROL))
539 {
540 if(nCount > 0)
541 {
542 nCurrentLevel = 0;
543 nGameState = GameState_Load;
544
545 for(uint32 i = 0; i < nCount; ++i)
546 {
547 nCurrentLevel += (uint32)(nBuffer[i] * powf(10.0f, (float)i));
548 }
549 }
550
551 bControl = false;
552 }
553 }
554 else
555 {
556 if(kInputManager.IsKeyDown(DIK_LCONTROL))
557 {
558 bControl = true;
559 nCount = 0;
560 }
561 }
562 #endif
563 }
564 }
565
566 /*
567 * UpdateLogic
568 */
569 void Mediator::UpdateLogic(float fElapsed)
570 {
571 if(nGameState == GameState_Load)
572 {
573 ErrorCode eCode = LoadLevel(nCurrentLevel++);
574 if(eCode == Error_Success)
575 {
576 kProgram.Clear();
577
578 for(uint32 i = 0; i < MaximumFunctionCount; ++i)
579 {
580 pCode[i]->Clear();
581 }
582
583 nGameState = GameState_Active;
584 }
585 else
586 {
587 pMessageDialog->SetButton(0, "Ok", DialogResult_Ok);
588 pMessageDialog->SetMessage("Congratulations!\nYou've won the game!");
589 pMessageDialog->Show();
590
591 nGameState = GameState_Over;
592 }
593
594 nSimulationState = SimulationState_Idle;
595 }
596 else
597
598 if(nGameState == GameState_Active)
599 {
600 if(nSimulationState == SimulationState_Active)
601 {
602 if(kBot.Update(fElapsed))
603 {
604 if(kEnvironment.RequirementsMet())
605 {
606 pMessageDialog->SetButton(0, "Ok", DialogResult_Ok);
607 pMessageDialog->SetMessage("Congratulations!\nYou've completed level %d", nCurrentLevel);
608 pMessageDialog->Show();
609
610 nGameState = GameState_Level;
611 }
612 }
613 }
614 }
615 }
616
617 /*
618 * RenderGame
619 */
620 void Mediator::RenderGame()
621 {
622 D3DVIEWPORT9 kOriginal;
623 kContext.GetViewport(&kOriginal);
624
625 D3DVIEWPORT9 kViewport;
626 kViewport.X = 0;
627 kViewport.Y = 0;
628 kViewport.Width = ScreenSizeX - 280; // minus size of interface
629 kViewport.Height = ScreenSizeY;
630 kViewport.MinZ = kOriginal.MinZ;
631 kViewport.MaxZ = kOriginal.MaxZ;
632
633 kContext.SetViewport(kViewport);
634
635 kCameraController.SetMode(CameraMode_3D);
636
637 kEnvironment.Render(kContext, kCameraController);
638 kBot.Render(kContext, kCameraController);
639
640 kContext.SetViewport(kOriginal);
641 }
642
643 /*
644 * RenderInterface
645 */
646 void Mediator::RenderInterface()
647 {
648 kCameraController.SetMode(CameraMode_2D);
649 kInterface.Render(kContext, kCameraController);
650 }
651
652 /*
653 * SetApplicationState
654 */
655 void Mediator::SetApplicationState(uint32 nState)
656 {
657 if(nApplicationState == ApplicationState_Main)
658 {
659 pMainBackground->ClearFlag(GuiElementFlag_Visible);
660 }
661 else
662
663 if(nApplicationState == ApplicationState_Help)
664 {
665 }
666 else
667
668 if(nApplicationState == ApplicationState_Game)
669 {
670 pHudBackground->ClearFlag(GuiElementFlag_Visible);
671 }
672
673 nApplicationState = nState;
674
675 if(nApplicationState == ApplicationState_Main)
676 {
677 pMainBackground->SetFlag(GuiElementFlag_Visible);
678 }
679 else
680
681 if(nApplicationState == ApplicationState_Help)
682 {
683 }
684 else
685
686 if(nApplicationState == ApplicationState_Game)
687 {
688 pHudBackground->SetFlag(GuiElementFlag_Visible);
689 }
690 }
691
692 /*
693 * LoadLevel
694 */
695 ErrorCode Mediator::LoadLevel(uint32 nLevel)
696 {
697 char kName[MAX_PATH];
698 sprintf(kName, "Data\\Maps\\Map%02d.map", nLevel);
699
700 ErrorCode eCode = kLoader.Load(kName);
355 if(eCode == Error_Success) 701 if(eCode == Error_Success)
356 { 702 {
357 const Size& kSize = kLoader.GetSize(); 703 const Size& kSize = kLoader.GetSize();
358 704
359 eCode = kEnvironment.Setup(kSize.X, kSize.Y); 705 eCode = kEnvironment.Setup(kSize.X, kSize.Y);
369 } 715 }
370 716
371 kBot.Setup(&kEnvironment); 717 kBot.Setup(&kEnvironment);
372 kBot.SetPosition(kLoader.GetInitialPosition()); 718 kBot.SetPosition(kLoader.GetInitialPosition());
373 kBot.SetDirection(kLoader.GetInitialDirection()); 719 kBot.SetDirection(kLoader.GetInitialDirection());
374
375 //kCameraController.SetDistance(
376 } 720 }
377 } 721 }
378 722
379 return eCode; 723 return eCode;
380 } 724 }
381 725
382 /* 726 /*
383 * InitializeInterface 727 * OnStart
384 */ 728 */
385 ErrorCode Mediator::InitializeInterface(ServiceProvider* pServiceProvider) 729 void Mediator::OnStart(GuiEventArguments& kArguments)
386 { 730 {
387 ErrorCode eCode = kInterface.Initialize(pServiceProvider); 731 }
388 if(eCode == Error_Success) 732
389 { 733 /*
390 pBackground = new GuiImage(); 734 * OnHelp
391 pBackground->Initialize(pServiceProvider); 735 */
392 pBackground->SetTexture("Data\\Textures\\Background04.tga", true); 736 void Mediator::OnHelp(GuiEventArguments& kArguments)
393 pBackground->SetPosition(ScreenSizeX - pBackground->GetWidth(), 0.0f); 737 {
394 pBackground->SetDepth(512.0f);
395
396 pToolbar = new ActionPanel(4, 2);
397 pToolbar->Initialize(pServiceProvider);
398 pToolbar->SetTexture("Data\\Textures\\PanelA.png");
399 pToolbar->SetPosition(16, 16.0f);
400 pToolbar->SetAction(0, Action_Forward);
401 pToolbar->SetAction(1, Action_RotateCW);
402 pToolbar->SetAction(2, Action_RotateCCW);
403 pToolbar->SetAction(3, Action_Jump);
404 pToolbar->SetAction(4, Action_Light);
405 pToolbar->SetAction(5, Action_FunctionA);
406 pToolbar->SetAction(6, Action_FunctionB);
407 pToolbar->SetPermanent(true);
408 pToolbar->SetDepth(256.0f);
409
410 GuiLabel* pMainLabel = new GuiLabel();
411 pMainLabel->Initialize(pServiceProvider);
412 pMainLabel->SetFont("Courier New", 16);
413 pMainLabel->SetText("Main:");
414 pMainLabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
415 pMainLabel->SetPosition(26.0f, 149.0f);
416 pMainLabel->SetDepth(256.0f);
417
418 pCode[0] = new ActionPanel(4, 3);
419 pCode[0]->Initialize(pServiceProvider);
420 pCode[0]->SetTexture("Data\\Textures\\PanelB.png");
421 pCode[0]->SetPosition(16.0f, 160.0f);
422 pCode[0]->Subscribe(ActionPanel::EventAction, &Mediator::OnAction, this);
423 pCode[0]->SetDepth(256.0f);
424
425 GuiLabel* pFunctionALabel = new GuiLabel();
426 pFunctionALabel->Initialize(pServiceProvider);
427 pFunctionALabel->SetFont("Courier New", 16);
428 pFunctionALabel->SetText("Function 1:");
429 pFunctionALabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
430 pFunctionALabel->SetPosition(26.0f, 349.0f);
431
432 pCode[1] = new ActionPanel(4, 2);
433 pCode[1]->Initialize(pServiceProvider);
434 pCode[1]->SetTexture("Data\\Textures\\PanelA.png");
435 pCode[1]->SetPosition(16.0f, 360.0f);
436 pCode[1]->Subscribe(ActionPanel::EventAction, &Mediator::OnAction, this);
437
438 GuiLabel* pFunctionBLabel = new GuiLabel();
439 pFunctionBLabel->Initialize(pServiceProvider);
440 pFunctionBLabel->SetFont("Courier New", 16);
441 pFunctionBLabel->SetText("Function 2:");
442 pFunctionBLabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
443 pFunctionBLabel->SetPosition(26.0f, 493.0f);
444
445 pCode[2] = new ActionPanel(4, 2);
446 pCode[2]->Initialize(pServiceProvider);
447 pCode[2]->SetTexture("Data\\Textures\\PanelA.png");
448 pCode[2]->SetPosition(16.0f, 504.0f);
449 pCode[2]->Subscribe(ActionPanel::EventAction, &Mediator::OnAction, this);
450
451 const float fButtonPadding = 32.0f;
452 const float fButtonSpacing = 8.0f;
453 const float fButtonSize = 48.0f;
454
455 pButtonPlay = new GuiButton();
456 pButtonPlay->Initialize(pServiceProvider);
457 pButtonPlay->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
458 pButtonPlay->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
459 pButtonPlay->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
460 pButtonPlay->SetFont("Courier New", 16, FW_BOLD);
461 pButtonPlay->SetText("Play");
462 pButtonPlay->SetColor(D3DCOLOR_XRGB(0, 0, 0));
463 pButtonPlay->SetPosition(fButtonPadding + 0.0f * (fButtonSize + fButtonSpacing), 652.0f);
464 pButtonPlay->Subscribe(GuiButton::EventClick, &Mediator::OnPlay, this);
465
466 pButtonStop = new GuiButton();
467 pButtonStop->Initialize(pServiceProvider);
468 pButtonStop->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
469 pButtonStop->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
470 pButtonStop->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
471 pButtonStop->SetFont("Courier New", 16, FW_BOLD);
472 pButtonStop->SetText("Stop");
473 pButtonStop->SetColor(D3DCOLOR_XRGB(0, 0, 0));
474 pButtonStop->SetPosition(fButtonPadding + 1.0f * (fButtonSize + fButtonSpacing), 652.0f);
475 pButtonStop->Subscribe(GuiButton::EventClick, &Mediator::OnStop, this);
476
477 pButtonReset = new GuiButton();
478 pButtonReset->Initialize(pServiceProvider);
479 pButtonReset->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
480 pButtonReset->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
481 pButtonReset->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
482 pButtonReset->SetFont("Courier New", 16, FW_BOLD);
483 pButtonReset->SetText("Reset");
484 pButtonReset->SetColor(D3DCOLOR_XRGB(0, 0, 0));
485 pButtonReset->SetPosition(fButtonPadding + 2.0f * (fButtonSize + fButtonSpacing), 652.0f);
486 pButtonReset->Subscribe(GuiButton::EventClick, &Mediator::OnReset, this);
487
488 pButtonExit = new GuiButton();
489 pButtonExit->Initialize(pServiceProvider);
490 pButtonExit->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button2N.png", true);
491 pButtonExit->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button2H.png", true);
492 pButtonExit->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button2D.png", true);
493 pButtonExit->SetFont("Courier New", 16, FW_BOLD);
494 pButtonExit->SetText("Exit");
495 pButtonExit->SetColor(D3DCOLOR_XRGB(0, 0, 0));
496 pButtonExit->SetPosition(fButtonPadding + 3.0f * (fButtonSize + fButtonSpacing), 652.0f);
497 pButtonExit->Subscribe(GuiButton::EventClick, &Mediator::OnExit, this);
498
499 pBackground->Add(pToolbar);
500 pBackground->Add(pMainLabel);
501 pBackground->Add(pCode[0]);
502 pBackground->Add(pFunctionALabel);
503 pBackground->Add(pCode[1]);
504 pBackground->Add(pFunctionBLabel);
505 pBackground->Add(pCode[2]);
506 pBackground->Add(pButtonPlay);
507 pBackground->Add(pButtonStop);
508 pBackground->Add(pButtonReset);
509 pBackground->Add(pButtonExit);
510
511 pDebugText = new GuiLabel();
512 pDebugText->Initialize(pServiceProvider);
513 pDebugText->SetFont("Courier New", 16);
514 pDebugText->SetPosition(10.0f, 10.0f);
515 pDebugText->SetText("Debug");
516 pDebugText->SetColor(D3DCOLOR_XRGB(255, 255, 255));
517
518 kInterface.Add(pBackground);
519 kInterface.Add(pDebugText);
520 }
521
522 return eCode;
523 }
524
525 /*
526 * UpdateInput
527 */
528 void Mediator::UpdateInput(float fElapsed)
529 {
530 #if defined(_DEBUG)
531 if(pInputManager->IsKeyDown(DIK_LEFT))
532 {
533 kCameraController.Yaw(0.01f);
534 }
535 else
536
537 if(pInputManager->IsKeyDown(DIK_RIGHT))
538 {
539 kCameraController.Yaw(-0.01f);
540 }
541
542 if(pInputManager->IsKeyDown(DIK_UP))
543 {
544 kCameraController.Pitch(0.01f);
545 }
546 else
547
548 if(pInputManager->IsKeyDown(DIK_DOWN))
549 {
550 kCameraController.Pitch(-0.01f);
551 }
552
553 if(pInputManager->IsKeyDown(DIK_NEXT))
554 {
555 kCameraController.Move(0.1f);
556 }
557 else
558
559 if(pInputManager->IsKeyDown(DIK_PRIOR))
560 {
561 kCameraController.Move(-0.1f);
562 }
563
564 static bool bControl = false;
565 static uint32 nBuffer[4] = {0};
566 static uint32 nCount = 0;
567
568 if(bControl)
569 {
570 if(pInputManager->IsKeyDown(DIK_0) && !pInputManager->WasKeyDown(DIK_0))
571 {
572 nBuffer[nCount++] = 0;
573 }
574 else
575
576 if(pInputManager->IsKeyDown(DIK_1) && !pInputManager->WasKeyDown(DIK_1))
577 {
578 nBuffer[nCount++] = 1;
579 }
580 else
581
582 if(pInputManager->IsKeyDown(DIK_2) && !pInputManager->WasKeyDown(DIK_2))
583 {
584 nBuffer[nCount++] = 2;
585 }
586 else
587
588 if(pInputManager->IsKeyDown(DIK_3) && !pInputManager->WasKeyDown(DIK_3))
589 {
590 nBuffer[nCount++] = 3;
591 }
592 else
593
594 if(pInputManager->IsKeyDown(DIK_4) && !pInputManager->WasKeyDown(DIK_4))
595 {
596 nBuffer[nCount++] = 4;
597 }
598 else
599
600 if(pInputManager->IsKeyDown(DIK_5) && !pInputManager->WasKeyDown(DIK_5))
601 {
602 nBuffer[nCount++] = 5;
603 }
604 else
605
606 if(pInputManager->IsKeyDown(DIK_6) && !pInputManager->WasKeyDown(DIK_6))
607 {
608 nBuffer[nCount++] = 6;
609 }
610 else
611
612 if(pInputManager->IsKeyDown(DIK_7) && !pInputManager->WasKeyDown(DIK_7))
613 {
614 nBuffer[nCount++] = 7;
615 }
616 else
617
618 if(pInputManager->IsKeyDown(DIK_8) && !pInputManager->WasKeyDown(DIK_8))
619 {
620 nBuffer[nCount++] = 8;
621 }
622 else
623
624 if(pInputManager->IsKeyDown(DIK_9) && !pInputManager->WasKeyDown(DIK_9))
625 {
626 nBuffer[nCount++] = 9;
627 }
628
629 if(!pInputManager->IsKeyDown(DIK_LCONTROL))
630 {
631 if(nCount > 0)
632 {
633 nCurrentLevel = 0;
634 nGameState = GameState_Load;
635
636 for(uint32 i = 0; i < nCount; ++i)
637 {
638 nCurrentLevel += (uint32)(nBuffer[i] * powf(10.0f, (float)i));
639 }
640 }
641
642 bControl = false;
643 }
644 }
645 else
646 {
647 if(pInputManager->IsKeyDown(DIK_LCONTROL))
648 {
649 bControl = true;
650 nCount = 0;
651 }
652 }
653 #endif
654 }
655
656 /*
657 * UpdateLogic
658 */
659 void Mediator::UpdateLogic(float fElapsed)
660 {
661 if(nGameState == GameState_Load)
662 {
663 char kBuffer[256];
664 sprintf(kBuffer, "Data\\Maps\\Map%02d.map", nCurrentLevel++);
665
666 ErrorCode eCode = Load(kBuffer);
667 if(eCode == Error_Success)
668 {
669 kProgram.Clear();
670
671 for(uint32 i = 0; i < MaximumFunctionCount; ++i)
672 {
673 pCode[i]->Clear();
674 }
675
676 nGameState = GameState_Active;
677 }
678 else
679 {
680 //pScreenManager->Push("GameOver");
681 }
682
683 nSimulationState = SimulationState_Idle;
684 }
685 else
686
687 if(nGameState == GameState_Active)
688 {
689 if(nSimulationState == SimulationState_Active)
690 {
691 if(kBot.Update(fElapsed))
692 {
693 if(kEnvironment.RequirementsMet())
694 {
695 nGameState = GameState_Load;
696 //pScreenManager->Push("LevelOver");
697 }
698 }
699 }
700 }
701 } 738 }
702 739
703 /* 740 /*
704 * OnAction 741 * OnAction
705 */ 742 */
768 * OnExit 805 * OnExit
769 */ 806 */
770 void Mediator::OnExit(GuiEventArguments& kArguments) 807 void Mediator::OnExit(GuiEventArguments& kArguments)
771 { 808 {
772 //pScreenManager->Push("Confirm"); 809 //pScreenManager->Push("Confirm");
773 } 810 bRunning = false;
811 }