GameBearShootWindow.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "DeviceContext.h"
  23. #include "Window.h"
  24. #include "UserInterfaceLocal.h"
  25. #include "GameBearShootWindow.h"
  26. #define BEAR_GRAVITY 240
  27. #define BEAR_SIZE 24.f
  28. #define BEAR_SHRINK_TIME 2000.f
  29. #define MAX_WINDFORCE 100.f
  30. idCVar bearTurretAngle( "bearTurretAngle", "0", CVAR_FLOAT, "" );
  31. idCVar bearTurretForce( "bearTurretForce", "200", CVAR_FLOAT, "" );
  32. /*
  33. *****************************************************************************
  34. * BSEntity
  35. ****************************************************************************
  36. */
  37. BSEntity::BSEntity(idGameBearShootWindow* _game) {
  38. game = _game;
  39. visible = true;
  40. entColor = colorWhite;
  41. materialName = "";
  42. material = NULL;
  43. width = height = 8;
  44. rotation = 0.f;
  45. rotationSpeed = 0.f;
  46. fadeIn = false;
  47. fadeOut = false;
  48. position.Zero();
  49. velocity.Zero();
  50. }
  51. BSEntity::~BSEntity() {
  52. }
  53. /*
  54. ======================
  55. BSEntity::WriteToSaveGame
  56. ======================
  57. */
  58. void BSEntity::WriteToSaveGame( idFile *savefile ) {
  59. game->WriteSaveGameString( materialName, savefile );
  60. savefile->Write( &width, sizeof(width) );
  61. savefile->Write( &height, sizeof(height) );
  62. savefile->Write( &visible, sizeof(visible) );
  63. savefile->Write( &entColor, sizeof(entColor) );
  64. savefile->Write( &position, sizeof(position) );
  65. savefile->Write( &rotation, sizeof(rotation) );
  66. savefile->Write( &rotationSpeed, sizeof(rotationSpeed) );
  67. savefile->Write( &velocity, sizeof(velocity) );
  68. savefile->Write( &fadeIn, sizeof(fadeIn) );
  69. savefile->Write( &fadeOut, sizeof(fadeOut) );
  70. }
  71. /*
  72. ======================
  73. BSEntity::ReadFromSaveGame
  74. ======================
  75. */
  76. void BSEntity::ReadFromSaveGame( idFile *savefile, idGameBearShootWindow* _game ) {
  77. game = _game;
  78. game->ReadSaveGameString( materialName, savefile );
  79. SetMaterial( materialName );
  80. savefile->Read( &width, sizeof(width) );
  81. savefile->Read( &height, sizeof(height) );
  82. savefile->Read( &visible, sizeof(visible) );
  83. savefile->Read( &entColor, sizeof(entColor) );
  84. savefile->Read( &position, sizeof(position) );
  85. savefile->Read( &rotation, sizeof(rotation) );
  86. savefile->Read( &rotationSpeed, sizeof(rotationSpeed) );
  87. savefile->Read( &velocity, sizeof(velocity) );
  88. savefile->Read( &fadeIn, sizeof(fadeIn) );
  89. savefile->Read( &fadeOut, sizeof(fadeOut) );
  90. }
  91. /*
  92. ======================
  93. BSEntity::SetMaterial
  94. ======================
  95. */
  96. void BSEntity::SetMaterial(const char* name) {
  97. materialName = name;
  98. material = declManager->FindMaterial( name );
  99. material->SetSort( SS_GUI );
  100. }
  101. /*
  102. ======================
  103. BSEntity::SetSize
  104. ======================
  105. */
  106. void BSEntity::SetSize( float _width, float _height ) {
  107. width = _width;
  108. height = _height;
  109. }
  110. /*
  111. ======================
  112. BSEntity::SetVisible
  113. ======================
  114. */
  115. void BSEntity::SetVisible( bool isVisible ) {
  116. visible = isVisible;
  117. }
  118. /*
  119. ======================
  120. BSEntity::Update
  121. ======================
  122. */
  123. void BSEntity::Update( float timeslice ) {
  124. if ( !visible ) {
  125. return;
  126. }
  127. // Fades
  128. if ( fadeIn && entColor.w < 1.f ) {
  129. entColor.w += 1 * timeslice;
  130. if ( entColor.w >= 1.f ) {
  131. entColor.w = 1.f;
  132. fadeIn = false;
  133. }
  134. }
  135. if ( fadeOut && entColor.w > 0.f ) {
  136. entColor.w -= 1 * timeslice;
  137. if ( entColor.w <= 0.f ) {
  138. entColor.w = 0.f;
  139. fadeOut = false;
  140. }
  141. }
  142. // Move the entity
  143. position += velocity * timeslice;
  144. // Rotate Entity
  145. rotation += rotationSpeed * timeslice;
  146. }
  147. /*
  148. ======================
  149. BSEntity::Draw
  150. ======================
  151. */
  152. void BSEntity::Draw() {
  153. if ( visible ) {
  154. dc->DrawMaterialRotated( position.x, position.y, width, height, material, entColor, 1.0f, 1.0f, DEG2RAD(rotation) );
  155. }
  156. }
  157. /*
  158. *****************************************************************************
  159. * idGameBearShootWindow
  160. ****************************************************************************
  161. */
  162. idGameBearShootWindow::idGameBearShootWindow(idUserInterfaceLocal *g) : idWindow(g) {
  163. gui = g;
  164. CommonInit();
  165. }
  166. idGameBearShootWindow::~idGameBearShootWindow() {
  167. entities.DeleteContents(true);
  168. }
  169. /*
  170. =============================
  171. idGameBearShootWindow::WriteToSaveGame
  172. =============================
  173. */
  174. void idGameBearShootWindow::WriteToSaveGame( idFile *savefile ) {
  175. idWindow::WriteToSaveGame( savefile );
  176. gamerunning.WriteToSaveGame( savefile );
  177. onFire.WriteToSaveGame( savefile );
  178. onContinue.WriteToSaveGame( savefile );
  179. onNewGame.WriteToSaveGame( savefile );
  180. savefile->Write( &timeSlice, sizeof(timeSlice) );
  181. savefile->Write( &timeRemaining, sizeof(timeRemaining) );
  182. savefile->Write( &gameOver, sizeof(gameOver) );
  183. savefile->Write( &currentLevel, sizeof(currentLevel) );
  184. savefile->Write( &goalsHit, sizeof(goalsHit) );
  185. savefile->Write( &updateScore, sizeof(updateScore) );
  186. savefile->Write( &bearHitTarget, sizeof(bearHitTarget) );
  187. savefile->Write( &bearScale, sizeof(bearScale) );
  188. savefile->Write( &bearIsShrinking, sizeof(bearIsShrinking) );
  189. savefile->Write( &bearShrinkStartTime, sizeof(bearShrinkStartTime) );
  190. savefile->Write( &turretAngle, sizeof(turretAngle) );
  191. savefile->Write( &turretForce, sizeof(turretForce) );
  192. savefile->Write( &windForce, sizeof(windForce) );
  193. savefile->Write( &windUpdateTime, sizeof(windUpdateTime) );
  194. int numberOfEnts = entities.Num();
  195. savefile->Write( &numberOfEnts, sizeof(numberOfEnts) );
  196. for ( int i=0; i<numberOfEnts; i++ ) {
  197. entities[i]->WriteToSaveGame( savefile );
  198. }
  199. int index;
  200. index = entities.FindIndex( turret );
  201. savefile->Write( &index, sizeof(index) );
  202. index = entities.FindIndex( bear );
  203. savefile->Write( &index, sizeof(index) );
  204. index = entities.FindIndex( helicopter );
  205. savefile->Write( &index, sizeof(index) );
  206. index = entities.FindIndex( goal );
  207. savefile->Write( &index, sizeof(index) );
  208. index = entities.FindIndex( wind );
  209. savefile->Write( &index, sizeof(index) );
  210. index = entities.FindIndex( gunblast );
  211. savefile->Write( &index, sizeof(index) );
  212. }
  213. /*
  214. =============================
  215. idGameBearShootWindow::ReadFromSaveGame
  216. =============================
  217. */
  218. void idGameBearShootWindow::ReadFromSaveGame( idFile *savefile ) {
  219. idWindow::ReadFromSaveGame( savefile );
  220. // Remove all existing entities
  221. entities.DeleteContents(true);
  222. gamerunning.ReadFromSaveGame( savefile );
  223. onFire.ReadFromSaveGame( savefile );
  224. onContinue.ReadFromSaveGame( savefile );
  225. onNewGame.ReadFromSaveGame( savefile );
  226. savefile->Read( &timeSlice, sizeof(timeSlice) );
  227. savefile->Read( &timeRemaining, sizeof(timeRemaining) );
  228. savefile->Read( &gameOver, sizeof(gameOver) );
  229. savefile->Read( &currentLevel, sizeof(currentLevel) );
  230. savefile->Read( &goalsHit, sizeof(goalsHit) );
  231. savefile->Read( &updateScore, sizeof(updateScore) );
  232. savefile->Read( &bearHitTarget, sizeof(bearHitTarget) );
  233. savefile->Read( &bearScale, sizeof(bearScale) );
  234. savefile->Read( &bearIsShrinking, sizeof(bearIsShrinking) );
  235. savefile->Read( &bearShrinkStartTime, sizeof(bearShrinkStartTime) );
  236. savefile->Read( &turretAngle, sizeof(turretAngle) );
  237. savefile->Read( &turretForce, sizeof(turretForce) );
  238. savefile->Read( &windForce, sizeof(windForce) );
  239. savefile->Read( &windUpdateTime, sizeof(windUpdateTime) );
  240. int numberOfEnts;
  241. savefile->Read( &numberOfEnts, sizeof(numberOfEnts) );
  242. for ( int i=0; i<numberOfEnts; i++ ) {
  243. BSEntity *ent;
  244. ent = new (TAG_OLD_UI) BSEntity( this );
  245. ent->ReadFromSaveGame( savefile, this );
  246. entities.Append( ent );
  247. }
  248. int index;
  249. savefile->Read( &index, sizeof(index) );
  250. turret = entities[index];
  251. savefile->Read( &index, sizeof(index) );
  252. bear = entities[index];
  253. savefile->Read( &index, sizeof(index) );
  254. helicopter = entities[index];
  255. savefile->Read( &index, sizeof(index) );
  256. goal = entities[index];
  257. savefile->Read( &index, sizeof(index) );
  258. wind = entities[index];
  259. savefile->Read( &index, sizeof(index) );
  260. gunblast = entities[index];
  261. }
  262. /*
  263. =============================
  264. idGameBearShootWindow::ResetGameState
  265. =============================
  266. */
  267. void idGameBearShootWindow::ResetGameState() {
  268. gamerunning = false;
  269. gameOver = false;
  270. onFire = false;
  271. onContinue = false;
  272. onNewGame = false;
  273. // Game moves forward 16 milliseconds every frame
  274. timeSlice = 0.016f;
  275. timeRemaining = 60.f;
  276. goalsHit = 0;
  277. updateScore = false;
  278. bearHitTarget = false;
  279. currentLevel = 1;
  280. turretAngle = 0.f;
  281. turretForce = 200.f;
  282. windForce = 0.f;
  283. windUpdateTime = 0;
  284. bearIsShrinking = false;
  285. bearShrinkStartTime = 0;
  286. bearScale = 1.f;
  287. }
  288. /*
  289. =============================
  290. idGameBearShootWindow::CommonInit
  291. =============================
  292. */
  293. void idGameBearShootWindow::CommonInit() {
  294. BSEntity * ent;
  295. // Precache sounds
  296. declManager->FindSound( "arcade_beargroan" );
  297. declManager->FindSound( "arcade_sargeshoot" );
  298. declManager->FindSound( "arcade_balloonpop" );
  299. declManager->FindSound( "arcade_levelcomplete1" );
  300. // Precache dynamically used materials
  301. declManager->FindMaterial( "game/bearshoot/helicopter_broken" );
  302. declManager->FindMaterial( "game/bearshoot/goal_dead" );
  303. declManager->FindMaterial( "game/bearshoot/gun_blast" );
  304. ResetGameState();
  305. ent = new (TAG_OLD_UI) BSEntity( this );
  306. turret = ent;
  307. ent->SetMaterial( "game/bearshoot/turret" );
  308. ent->SetSize( 272, 144 );
  309. ent->position.x = -44;
  310. ent->position.y = 260;
  311. entities.Append( ent );
  312. ent = new (TAG_OLD_UI) BSEntity( this );
  313. ent->SetMaterial( "game/bearshoot/turret_base" );
  314. ent->SetSize( 144, 160 );
  315. ent->position.x = 16;
  316. ent->position.y = 280;
  317. entities.Append( ent );
  318. ent = new (TAG_OLD_UI) BSEntity( this );
  319. bear = ent;
  320. ent->SetMaterial( "game/bearshoot/bear" );
  321. ent->SetSize( BEAR_SIZE, BEAR_SIZE );
  322. ent->SetVisible( false );
  323. ent->position.x = 0;
  324. ent->position.y = 0;
  325. entities.Append( ent );
  326. ent = new (TAG_OLD_UI) BSEntity( this );
  327. helicopter = ent;
  328. ent->SetMaterial( "game/bearshoot/helicopter" );
  329. ent->SetSize( 64, 64 );
  330. ent->position.x = 550;
  331. ent->position.y = 100;
  332. entities.Append( ent );
  333. ent = new (TAG_OLD_UI) BSEntity( this );
  334. goal = ent;
  335. ent->SetMaterial( "game/bearshoot/goal" );
  336. ent->SetSize( 64, 64 );
  337. ent->position.x = 550;
  338. ent->position.y = 164;
  339. entities.Append( ent );
  340. ent = new (TAG_OLD_UI) BSEntity( this );
  341. wind = ent;
  342. ent->SetMaterial( "game/bearshoot/wind" );
  343. ent->SetSize( 100, 40 );
  344. ent->position.x = 500;
  345. ent->position.y = 430;
  346. entities.Append( ent );
  347. ent = new (TAG_OLD_UI) BSEntity( this );
  348. gunblast = ent;
  349. ent->SetMaterial( "game/bearshoot/gun_blast" );
  350. ent->SetSize( 64, 64 );
  351. ent->SetVisible( false );
  352. entities.Append( ent );
  353. }
  354. /*
  355. =============================
  356. idGameBearShootWindow::HandleEvent
  357. =============================
  358. */
  359. const char *idGameBearShootWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
  360. int key = event->evValue;
  361. // need to call this to allow proper focus and capturing on embedded children
  362. const char *ret = idWindow::HandleEvent(event, updateVisuals);
  363. if ( event->evType == SE_KEY ) {
  364. if ( !event->evValue2 ) {
  365. return ret;
  366. }
  367. if ( key == K_MOUSE1) {
  368. // Mouse was clicked
  369. } else {
  370. return ret;
  371. }
  372. }
  373. return ret;
  374. }
  375. /*
  376. =============================
  377. idGameBearShootWindow::ParseInternalVar
  378. =============================
  379. */
  380. bool idGameBearShootWindow::ParseInternalVar(const char *_name, idTokenParser *src) {
  381. if ( idStr::Icmp(_name, "gamerunning") == 0 ) {
  382. gamerunning = src->ParseBool();
  383. return true;
  384. }
  385. if ( idStr::Icmp(_name, "onFire") == 0 ) {
  386. onFire = src->ParseBool();
  387. return true;
  388. }
  389. if ( idStr::Icmp(_name, "onContinue") == 0 ) {
  390. onContinue = src->ParseBool();
  391. return true;
  392. }
  393. if ( idStr::Icmp(_name, "onNewGame") == 0 ) {
  394. onNewGame = src->ParseBool();
  395. return true;
  396. }
  397. return idWindow::ParseInternalVar(_name, src);
  398. }
  399. /*
  400. =============================
  401. idGameBearShootWindow::GetWinVarByName
  402. =============================
  403. */
  404. idWinVar *idGameBearShootWindow::GetWinVarByName(const char *_name, bool winLookup, drawWin_t** owner) {
  405. idWinVar *retVar = NULL;
  406. if ( idStr::Icmp(_name, "gamerunning") == 0 ) {
  407. retVar = &gamerunning;
  408. } else if ( idStr::Icmp(_name, "onFire") == 0 ) {
  409. retVar = &onFire;
  410. } else if ( idStr::Icmp(_name, "onContinue") == 0 ) {
  411. retVar = &onContinue;
  412. } else if ( idStr::Icmp(_name, "onNewGame") == 0 ) {
  413. retVar = &onNewGame;
  414. }
  415. if(retVar) {
  416. return retVar;
  417. }
  418. return idWindow::GetWinVarByName(_name, winLookup, owner);
  419. }
  420. /*
  421. =============================
  422. idGameBearShootWindow::PostParse
  423. =============================
  424. */
  425. void idGameBearShootWindow::PostParse() {
  426. idWindow::PostParse();
  427. }
  428. /*
  429. =============================
  430. idGameBearShootWindow::Draw
  431. =============================
  432. */
  433. void idGameBearShootWindow::Draw(int time, float x, float y) {
  434. int i;
  435. //Update the game every frame before drawing
  436. UpdateGame();
  437. for( i = entities.Num()-1; i >= 0; i-- ) {
  438. entities[i]->Draw();
  439. }
  440. }
  441. /*
  442. =============================
  443. idGameBearShootWindow::Activate
  444. =============================
  445. */
  446. const char *idGameBearShootWindow::Activate(bool activate) {
  447. return "";
  448. }
  449. /*
  450. =============================
  451. idGameBearShootWindow::UpdateTurret
  452. =============================
  453. */
  454. void idGameBearShootWindow::UpdateTurret() {
  455. idVec2 pt;
  456. idVec2 turretOrig;
  457. idVec2 right;
  458. float dot, angle;
  459. pt.x = gui->CursorX();
  460. pt.y = gui->CursorY();
  461. turretOrig.Set( 80.f, 348.f );
  462. pt = pt - turretOrig;
  463. pt.NormalizeFast();
  464. right.x = 1.f;
  465. right.y = 0.f;
  466. dot = pt * right;
  467. angle = RAD2DEG( acosf( dot ) );
  468. turretAngle = idMath::ClampFloat( 0.f, 90.f, angle );
  469. }
  470. /*
  471. =============================
  472. idGameBearShootWindow::UpdateBear
  473. =============================
  474. */
  475. void idGameBearShootWindow::UpdateBear() {
  476. int time = gui->GetTime();
  477. bool startShrink = false;
  478. // Apply gravity
  479. bear->velocity.y += BEAR_GRAVITY * timeSlice;
  480. // Apply wind
  481. bear->velocity.x += windForce * timeSlice;
  482. // Check for collisions
  483. if ( !bearHitTarget && !gameOver ) {
  484. idVec2 bearCenter;
  485. bool collision = false;
  486. bearCenter.x = bear->position.x + bear->width/2;
  487. bearCenter.y = bear->position.y + bear->height/2;
  488. if ( bearCenter.x > (helicopter->position.x + 16) && bearCenter.x < (helicopter->position.x + helicopter->width - 29) ) {
  489. if ( bearCenter.y > (helicopter->position.y + 12) && bearCenter.y < (helicopter->position.y + helicopter->height - 7) ) {
  490. collision = true;
  491. }
  492. }
  493. if ( collision ) {
  494. // balloons pop and bear tumbles to ground
  495. helicopter->SetMaterial( "game/bearshoot/helicopter_broken" );
  496. helicopter->velocity.y = 230.f;
  497. goal->velocity.y = 230.f;
  498. common->SW()->PlayShaderDirectly( "arcade_balloonpop" );
  499. bear->SetVisible( false );
  500. if ( bear->velocity.x > 0 ) {
  501. bear->velocity.x *= -1.f;
  502. }
  503. bear->velocity *= 0.666f;
  504. bearHitTarget = true;
  505. updateScore = true;
  506. startShrink = true;
  507. }
  508. }
  509. // Check for ground collision
  510. if ( bear->position.y > 380 ) {
  511. bear->position.y = 380;
  512. if ( bear->velocity.Length() < 25 ) {
  513. bear->velocity.Zero();
  514. } else {
  515. startShrink = true;
  516. bear->velocity.y *= -1.f;
  517. bear->velocity *= 0.5f;
  518. if ( bearScale ) {
  519. common->SW()->PlayShaderDirectly( "arcade_balloonpop" );
  520. }
  521. }
  522. }
  523. // Bear rotation is based on velocity
  524. float angle;
  525. idVec2 dir;
  526. dir = bear->velocity;
  527. dir.NormalizeFast();
  528. angle = RAD2DEG( atan2( dir.x, dir.y ) );
  529. bear->rotation = angle - 90;
  530. // Update Bear scale
  531. if ( bear->position.x > 650 ) {
  532. startShrink = true;
  533. }
  534. if ( !bearIsShrinking && bearScale && startShrink ) {
  535. bearShrinkStartTime = time;
  536. bearIsShrinking = true;
  537. }
  538. if ( bearIsShrinking ) {
  539. if ( bearHitTarget ) {
  540. bearScale = 1 - ( (float)(time - bearShrinkStartTime) / BEAR_SHRINK_TIME );
  541. } else {
  542. bearScale = 1 - ( (float)(time - bearShrinkStartTime) / 750 );
  543. }
  544. bearScale *= BEAR_SIZE;
  545. bear->SetSize( bearScale, bearScale );
  546. if ( bearScale < 0 ) {
  547. gui->HandleNamedEvent( "EnableFireButton" );
  548. bearIsShrinking = false;
  549. bearScale = 0.f;
  550. if ( bearHitTarget ) {
  551. goal->SetMaterial( "game/bearshoot/goal" );
  552. goal->position.x = 550;
  553. goal->position.y = 164;
  554. goal->velocity.Zero();
  555. goal->velocity.y = (currentLevel-1) * 30;
  556. goal->entColor.w = 0.f;
  557. goal->fadeIn = true;
  558. goal->fadeOut = false;
  559. helicopter->SetVisible( true );
  560. helicopter->SetMaterial( "game/bearshoot/helicopter" );
  561. helicopter->position.x = 550;
  562. helicopter->position.y = 100;
  563. helicopter->velocity.Zero();
  564. helicopter->velocity.y = goal->velocity.y;
  565. helicopter->entColor.w = 0.f;
  566. helicopter->fadeIn = true;
  567. helicopter->fadeOut = false;
  568. }
  569. }
  570. }
  571. }
  572. /*
  573. =============================
  574. idGameBearShootWindow::UpdateHelicopter
  575. =============================
  576. */
  577. void idGameBearShootWindow::UpdateHelicopter() {
  578. if ( bearHitTarget && bearIsShrinking ) {
  579. if ( helicopter->velocity.y != 0 && helicopter->position.y > 264 ) {
  580. helicopter->velocity.y = 0;
  581. goal->velocity.y = 0;
  582. helicopter->SetVisible( false );
  583. goal->SetMaterial( "game/bearshoot/goal_dead" );
  584. common->SW()->PlayShaderDirectly( "arcade_beargroan", 1 );
  585. helicopter->fadeOut = true;
  586. goal->fadeOut = true;
  587. }
  588. } else if ( currentLevel > 1 ) {
  589. int height = helicopter->position.y;
  590. float speed = (currentLevel-1) * 30;
  591. if ( height > 240 ) {
  592. helicopter->velocity.y = -speed;
  593. goal->velocity.y = -speed;
  594. } else if ( height < 30 ) {
  595. helicopter->velocity.y = speed;
  596. goal->velocity.y = speed;
  597. }
  598. }
  599. }
  600. /*
  601. =============================
  602. idGameBearShootWindow::UpdateButtons
  603. =============================
  604. */
  605. void idGameBearShootWindow::UpdateButtons() {
  606. if ( onFire ) {
  607. idVec2 vec;
  608. gui->HandleNamedEvent( "DisableFireButton" );
  609. common->SW()->PlayShaderDirectly( "arcade_sargeshoot" );
  610. bear->SetVisible( true );
  611. bearScale = 1.f;
  612. bear->SetSize( BEAR_SIZE, BEAR_SIZE );
  613. vec.x = idMath::Cos( DEG2RAD(turretAngle) );
  614. vec.x += ( 1 - vec.x ) * 0.18f;
  615. vec.y = -idMath::Sin( DEG2RAD(turretAngle) );
  616. turretForce = bearTurretForce.GetFloat();
  617. bear->position.x = 80 + ( 96 * vec.x );
  618. bear->position.y = 334 + ( 96 * vec.y );
  619. bear->velocity.x = vec.x * turretForce;
  620. bear->velocity.y = vec.y * turretForce;
  621. gunblast->position.x = 55 + ( 96 * vec.x );
  622. gunblast->position.y = 310 + ( 100 * vec.y );
  623. gunblast->SetVisible( true );
  624. gunblast->entColor.w = 1.f;
  625. gunblast->rotation = turretAngle;
  626. gunblast->fadeOut = true;
  627. bearHitTarget = false;
  628. onFire = false;
  629. }
  630. }
  631. /*
  632. =============================
  633. idGameBearShootWindow::UpdateScore
  634. =============================
  635. */
  636. void idGameBearShootWindow::UpdateScore() {
  637. if ( gameOver ) {
  638. gui->HandleNamedEvent( "GameOver" );
  639. return;
  640. }
  641. goalsHit++;
  642. gui->SetStateString( "player_score", va("%i", goalsHit ) );
  643. // Check for level progression
  644. if ( !(goalsHit % 5) ) {
  645. currentLevel++;
  646. gui->SetStateString( "current_level", va("%i", currentLevel ) );
  647. common->SW()->PlayShaderDirectly( "arcade_levelcomplete1", 3 );
  648. timeRemaining += 30;
  649. }
  650. }
  651. /*
  652. =============================
  653. idGameBearShootWindow::UpdateGame
  654. =============================
  655. */
  656. void idGameBearShootWindow::UpdateGame() {
  657. int i;
  658. if ( onNewGame ) {
  659. ResetGameState();
  660. if ( goal ) {
  661. goal->position.x = 550;
  662. goal->position.y = 164;
  663. goal->velocity.Zero();
  664. }
  665. if ( helicopter ) {
  666. helicopter->position.x = 550;
  667. helicopter->position.y = 100;
  668. helicopter->velocity.Zero();
  669. }
  670. if ( bear ) {
  671. bear->SetVisible( false );
  672. }
  673. bearTurretAngle.SetFloat( 0.f );
  674. bearTurretForce.SetFloat( 200.f );
  675. gamerunning = true;
  676. }
  677. if ( onContinue ) {
  678. gameOver = false;
  679. timeRemaining = 60.f;
  680. onContinue = false;
  681. }
  682. if(gamerunning == true) {
  683. int current_time = gui->GetTime();
  684. idRandom rnd( current_time );
  685. // Check for button presses
  686. UpdateButtons();
  687. if ( bear ) {
  688. UpdateBear();
  689. }
  690. if ( helicopter && goal ) {
  691. UpdateHelicopter();
  692. }
  693. // Update Wind
  694. if ( windUpdateTime < current_time ) {
  695. float scale;
  696. int width;
  697. windForce = rnd.CRandomFloat() * ( MAX_WINDFORCE * 0.75f );
  698. if (windForce > 0) {
  699. windForce += ( MAX_WINDFORCE * 0.25f );
  700. wind->rotation = 0;
  701. } else {
  702. windForce -= ( MAX_WINDFORCE * 0.25f );
  703. wind->rotation = 180;
  704. }
  705. scale = 1.f - (( MAX_WINDFORCE - idMath::Fabs(windForce) ) / MAX_WINDFORCE);
  706. width = 100*scale;
  707. if ( windForce < 0 ) {
  708. wind->position.x = 500 - width + 1;
  709. } else {
  710. wind->position.x = 500;
  711. }
  712. wind->SetSize( width, 40 );
  713. windUpdateTime = current_time + 7000 + rnd.RandomInt(5000);
  714. }
  715. // Update turret rotation angle
  716. if ( turret ) {
  717. turretAngle = bearTurretAngle.GetFloat();
  718. turret->rotation = turretAngle;
  719. }
  720. for( i = 0; i < entities.Num(); i++ ) {
  721. entities[i]->Update( timeSlice );
  722. }
  723. // Update countdown timer
  724. timeRemaining -= timeSlice;
  725. timeRemaining = idMath::ClampFloat( 0.f, 99999.f, timeRemaining );
  726. gui->SetStateString( "time_remaining", va("%2.1f", timeRemaining ) );
  727. if ( timeRemaining <= 0.f && !gameOver ) {
  728. gameOver = true;
  729. updateScore = true;
  730. }
  731. if ( updateScore ) {
  732. UpdateScore();
  733. updateScore = false;
  734. }
  735. }
  736. }