JNR
stageJRGame.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: stageJRGame.h
32 //
33 // Jumping Ronnin - Game Stage
34 //
35 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
36 //
37 #include "stdafx.h"
38 #include "laStagesJR.h"
39 
40 stageJRGame::stageJRGame() {
41  _shutterCinematic.parameters(laColor(0,0,0,100), 0.8, M_TRUE);
42  _shutterMenu.parameters(laColor(255,255,255,100), 0.5, M_FALSE);
43 }
44 
45 stageJRGame::~stageJRGame(void) {
46 }
47 
48 void stageJRGame::onInit()
49 {
50  ERRORLEVEL_BEGIN;
51  stageJR::onInit();
52 
53  // Init timers
54  //
55  _ttMenu.parameters(0.5, 0.5, M_FALSE);
56  _ttMenu.enable(M_FALSE);
57 
58  _ttJump.parameters(0.1, 0.1, M_FALSE);
59  _ttJump.enable(M_FALSE);
60  _mkSomersault.set(KEY_SPACE, 0.2, 2);
61  _nJump = 0;
62 
63  _mkAttack.set(KEY_X, 20, 1, M_FALSE);
64 
65  _mkQuickAttack.set(KEY_X, 0.2, 2);
66  _nQuickAttack = 0;
67 
68  ERRORLEVEL_END;
69 }
70 
71 // Initialize GUI
72 //
73 void stageJRGame::_init_gui()
74 {
75  _uiDesktop.create("ui\\desktop-ingame.dsk");
76  _pMenu = new uiMenu_JR(M_JRMENU_INGAME);
77  _uiDesktop.insert(_pMenu);
78 }
79 
80 void stageJRGame::onActivate()
81 {
82  ERRORLEVEL_BEGIN;
83  ASSERT(_pGame, "Nil game object");
84  stageJR::onActivate();
85 
86  _shutterMenu.activate(M_FALSE);
87  _ttMenu.reset();
88  _pMenu->hide();
89 
90  // Authomatically switch to the intro section if present
91  //
92  if( _pGame->level.isIntroPresent() && (!_pGame->level.isIntroOver()) )
93  {
94  ((stageIntro*)(laSystemIntegrator::getGame()->getStage(M_STAGE_INTRO)))
95  ->setTS( _pGame->level.getTS() );
96  laSystemIntegrator::getGame()->setStage(M_STAGE_INTRO);
97  }
98 
99  ::laSystemIntegrator::getEnvironment()->getTimer()->statistics_enable();
100  ERRORLEVEL_END;
101 }
102 
103 void stageJRGame::onDeactivate()
104 {
105  ERRORLEVEL_BEGIN;
106  ::laSystemIntegrator::getEnvironment()->getTimer()->statistics_enable(M_FALSE);
107  ASSERT(_pGame, "Nil game object");
108  stageJR::onDeactivate();
109 
110  // Delete game objects
111  // NOTE: IF THERE IS AN INTRO or OUTRO, objects must not be deleted; The intro/outro will return to this stage
112  //
113  if( _pGame->level.isIntroPresent() ? _pGame->level.isIntroOver() : M_FALSE )
114  {
115  delete _pGame;
116  _pGame = NULL;
117  }
118  // The game stage had beeen activated, so the intro must have passed
119  else if(_pGame->level.isIntroPresent()) _pGame->level.introOver();
120 
121  ERRORLEVEL_END;
122 }
123 
124 // Draw GUI
125 //
126 void stageJRGame::_draw_UI(laRenderer *pr, laTimer *pt)
127 {
128  PROFILE(stageJR__draw_UI);
129  ERRORLEVEL_BEGIN;
130  laFont* f = pr->font("plain");
131 
132  laSettings* ps = laSystemIntegrator::getSettings();
133  double w = laSystemIntegrator::getSettings()->graphics_resolution_w;
134  double h = laSystemIntegrator::getSettings()->graphics_resolution_h;
135 
136  _pGame->level.drawInterface(pr, laPoint3());
137 
138  _shutterMenu.draw(pr);
139  _uiDesktop.draw();
140  _uiDesktop.reply();
141 
142  //_cinematic_onframe(pr, pt);
143  _shutterCinematic.animate(*pt);
144  _shutterCinematic.draw(pr);
145 
146  // Print FPS
147  //
148  //if(ps->debug_show_fps)
149  {
150  f->ctlAlignH(M_AR);
151  f->ctlSize(12);
152  pr->styleSet(laColor(255,255,255,150));
153  f->draw(laPoint3(w-20, 20), "fps: %.1f", pt->fps_sampled());
154 
155  laPoint3 pt = (_pGame->level.getPlayer())->getPosition();
156  int seg = _pGame->level.segmentIndex(pt.x());
157  int nx = (pt.x() - seg*M_UNIT*M_SEGW)/M_UNIT ;
158  int ny = pt.y()/M_UNIT;
159 
160  f->draw(laPoint3(w-40, 50), "_nCrSeg: %d, x:%d, y:%d", _nCrSeg /*seg*/, nx, ny);
161 
162 
163  f->draw(laPoint3(w-40, 80), "_nColDomains: %d", _nColDomains);
164  }
165 
166  // Menu fade-in effect
167  //
168  _shutterMenu.animate(*pt);
169  _ttMenu.animate(*pt);
170 
171  ERRORLEVEL_END;
172 }
173 
174 // Animate game
175 //
176 void stageJRGame::_animate(laInputManager *pi, laTimer *pt)
177 {
178  PROFILE(stageJR__animate);
179  ERRORLEVEL_BEGIN;
180 
181  laPlayer *pPlayer = _pGame->level.getPlayer();
182  laRenderer *pr = laSystemIntegrator::getRenderer();
183 
184  if( !_pMenu->isVisible() )
185  {
186  if( (pPlayer->isAlive()) && (!_pGame->level.isCompleted()) )
187  _animate_PlayerControls(pi, pt);
188 
189  //_setup_3d_camera(pr, pt);
190  _pGame->level.cameraSetup(pr); // NOTE: Must be in 3D mode like the level, to display debugging info
191  _animate_Dynamics(pi, pt);
192  }
193 
194  _animate_GUI(pi, pt);
195 
196  ERRORLEVEL_END;
197 }
198 
199 // Player controls
200 //
201 void stageJRGame::_animate_PlayerControls(laInputManager *pi, laTimer *pt)
202 {
203  ERRORLEVEL_BEGIN;
204  laPlayer *pPlayer = (laPlayer*)(_pGame->level.getPlayer());
205 
206  if(pPlayer->isDialogActive()) return;
207 
208  // Acrobatics
209  //
210  _ttJump.animate(*pt);
211  _mkSomersault.animate(pi, *pt);
212 
213  if( _ttJump.isElapsed() && (_nJump<3)) //Enable next jump if min. interval has elapsed
214  {
215  _ttJump.enable(M_FALSE);
216  }
217 
218  if( !pi->key(KEY_SPACE) ) //Immediately enable jump if JK is released
219  {
220  _nJump = 0;
221  _ttJump.enable(M_FALSE);
222  }
223  else if( !_ttJump.isEnabled() ) //Jump if stop-timer is disabled
224  {
225  if(pPlayer->jump()) _nJump++; //Jump & count
226 
227  _ttJump.reset(); //Stop-timer
228  _ttJump.enable();
229  }
230 
231  if( _mkSomersault.isActivated() )
232  {
233  pPlayer->somersault();
234  _mkSomersault.reset();
235  }
236 
237  // Attacks
238  //
239  _mkAttack.animate(pi, *pt);
240  _mkQuickAttack.animate(pi, *pt);
241 
242  if( _mkQuickAttack.isActivated() && (!pPlayer->attackIsQuick()) /*&& (_nQuickAttack<1)*/ )
243  {
244  pPlayer->attackStop();
245  pPlayer->attackSelect(1);
246  pPlayer->attackInitiate(M_TRUE);
247  //_nQuickAttack++;
248 
249  _mkQuickAttack.reset();
250  _mkAttack.reset();
251  //_ttAttack.reset();
252  //_ttAttack.enable();
253 
254  if(_mkQuickAttack.getNPress() >= 10)
255  {
256  _mkQuickAttack.set(KEY_X, _mkQuickAttack.getInterval(), 10);
257  }
258  else _mkQuickAttack.set(KEY_X, _mkQuickAttack.getInterval(), _mkQuickAttack.getNPress()+2);
259  }
260  else if( _mkAttack.isActivated() )
261  {
262  pPlayer->attackSelect(0);
263  pPlayer->attackInitiate();
264  _mkAttack.reset();
265 
266  //_nQuickAttack = 0;
267  if(!_mkQuickAttack.isActivated() )
268  {
269  if(_mkQuickAttack.getNPress() <= 2) _mkQuickAttack.set(KEY_X, _mkQuickAttack.getInterval(), 2);
270  else _mkQuickAttack.set(KEY_X, _mkQuickAttack.getInterval(), _mkQuickAttack.getNPress()-2);
271  }
272  }
273 
274 
275  if(pi->key(KEY_LEFT)) pPlayer->move(-1);
276  else if(pi->key(KEY_RIGHT)) pPlayer->move(+1);
277  else pPlayer->move(0);
278 
279  ERRORLEVEL_END;
280 }
281 
282 // Animate GUI and menu
283 //
284 void stageJRGame::_animate_GUI(laInputManager *pi, laTimer *pt)
285 {
286  PROFILE(stageJR__animate_GUI);
287  ERRORLEVEL_BEGIN;
288  laPlayer *pPlayer = _pGame->level.getPlayer();
289 
290  // Hide/show menu
291  //
292  if( (in::key(KEY_ESCAPE)) && (_ttMenu.isElapsed() || !_ttMenu.isEnabled()))
293  {
294  if(_pMenu->isVisible())
295  {
296  _pMenu->hide();
297  _shutterMenu.activate(M_FALSE);
298  _ttMenu.reset();
299  _ttMenu.enable();
300  }
301  else
302  {
303  _pMenu->show();
304  _shutterMenu.activate();
305  _ttMenu.reset();
306  _ttMenu.enable();
307  }
308  }
309 
310  // Go to next level
311  //
312  if( _pGame->level.isCompleted() && pi->key(KEY_RETURN))
313  {
314  //::laSystemIntegrator::getEnvironment()->terminate();
315  //TODO: go to next level
316 
317  char* strNextLevel = _pGame->level.nextLevel();
318 
319  if(strNextLevel)
320  {
321  strcpy(laSystemIntegrator::getSettings()->load_level, strNextLevel);
322  laSystemIntegrator::getGame()->setStage(M_STAGE_LOAD);
323  }
324  else
325  {
326  laSystemIntegrator::getGame()->setStage(M_STAGE_MENU);
327  }
328  }
329 
330  // Reset level
331  //
332  if(pi->key(KEY_R)) _pGame->level.reset();
333 
334  if(pi->key(KEY_M))
335  {
336  //_uiLoading.message(20,"Loading Level '%s'...", laSystemIntegrator::getSettings()->load_level);
337  _pGame->level.load(laSystemIntegrator::getSettings()->load_level, M_TRUE);
338  laPlayer *pPlayer = (laPlayer*)(_pGame->level.getPlayer());
339  //pCE->message(M_TRUE, "stageJRGame: Level loaded...");
340  }
341 
342  if(pi->key(KEY_P))
343  ::laSystemIntegrator::getRenderer()->modeProfiler( 1 - laSystemIntegrator::getRenderer()->isProfilerOn() );
344 
345  ERRORLEVEL_END;
346 }
laFont * font(char *strName)
Get a font renderer.
Definition: laRenderer.h:192
JR Main Menu, and in-game Menu.
Definition: uiMenu_JR.h:43
Font and Text Drawing.
Definition: laFont.h:41
Playable Character.
Definition: laPlayer.h:46
#define M_UNIT
Unit of 1 meter.
#define M_AR
Text align right.
Game Settings.
Definition: laSettings.h:38
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void somersault()