GameBustOutWindow.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../framework/Session_local.h"
  23. #include "../renderer/Image.h"
  24. #include "DeviceContext.h"
  25. #include "Window.h"
  26. #include "UserInterfaceLocal.h"
  27. #include "GameBustOutWindow.h"
  28. #define BALL_RADIUS 12.f
  29. #define BALL_SPEED 250.f
  30. #define BALL_MAXSPEED 450.f
  31. #define S_UNIQUE_CHANNEL 6
  32. /*
  33. *****************************************************************************
  34. * BOEntity
  35. ****************************************************************************
  36. */
  37. BOEntity::BOEntity(idGameBustOutWindow* _game) {
  38. game = _game;
  39. visible = true;
  40. materialName = "";
  41. material = NULL;
  42. width = height = 8;
  43. color = colorWhite;
  44. powerup = POWERUP_NONE;
  45. position.Zero();
  46. velocity.Zero();
  47. removed = false;
  48. fadeOut = 0;
  49. }
  50. BOEntity::~BOEntity() {
  51. }
  52. /*
  53. ======================
  54. BOEntity::WriteToSaveGame
  55. ======================
  56. */
  57. void BOEntity::WriteToSaveGame( idFile *savefile ) {
  58. savefile->Write( &visible, sizeof(visible) );
  59. game->WriteSaveGameString( materialName, savefile );
  60. savefile->Write( &width, sizeof(width) );
  61. savefile->Write( &height, sizeof(height) );
  62. savefile->Write( &color, sizeof(color) );
  63. savefile->Write( &position, sizeof(position) );
  64. savefile->Write( &velocity, sizeof(velocity) );
  65. savefile->Write( &powerup, sizeof(powerup) );
  66. savefile->Write( &removed, sizeof(removed) );
  67. savefile->Write( &fadeOut, sizeof(fadeOut) );
  68. }
  69. /*
  70. ======================
  71. BOEntity::ReadFromSaveGame
  72. ======================
  73. */
  74. void BOEntity::ReadFromSaveGame( idFile *savefile, idGameBustOutWindow* _game ) {
  75. game = _game;
  76. savefile->Read( &visible, sizeof(visible) );
  77. game->ReadSaveGameString( materialName, savefile );
  78. SetMaterial( materialName );
  79. savefile->Read( &width, sizeof(width) );
  80. savefile->Read( &height, sizeof(height) );
  81. savefile->Read( &color, sizeof(color) );
  82. savefile->Read( &position, sizeof(position) );
  83. savefile->Read( &velocity, sizeof(velocity) );
  84. savefile->Read( &powerup, sizeof(powerup) );
  85. savefile->Read( &removed, sizeof(removed) );
  86. savefile->Read( &fadeOut, sizeof(fadeOut) );
  87. }
  88. /*
  89. ======================
  90. BOEntity::SetMaterial
  91. ======================
  92. */
  93. void BOEntity::SetMaterial(const char* name) {
  94. materialName = name;
  95. material = declManager->FindMaterial( name );
  96. material->SetSort( SS_GUI );
  97. }
  98. /*
  99. ======================
  100. BOEntity::SetSize
  101. ======================
  102. */
  103. void BOEntity::SetSize( float _width, float _height ) {
  104. width = _width;
  105. height = _height;
  106. }
  107. /*
  108. ======================
  109. BOEntity::SetVisible
  110. ======================
  111. */
  112. void BOEntity::SetColor( float r, float g, float b, float a ) {
  113. color.x = r;
  114. color.y = g;
  115. color.z = b;
  116. color.w = a;
  117. }
  118. /*
  119. ======================
  120. BOEntity::SetVisible
  121. ======================
  122. */
  123. void BOEntity::SetVisible( bool isVisible ) {
  124. visible = isVisible;
  125. }
  126. /*
  127. ======================
  128. BOEntity::Update
  129. ======================
  130. */
  131. void BOEntity::Update( float timeslice, int guiTime ) {
  132. if ( !visible ) {
  133. return;
  134. }
  135. // Move the entity
  136. position += velocity * timeslice;
  137. // Fade out the ent
  138. if ( fadeOut ) {
  139. color.w -= timeslice * 2.5;
  140. if ( color.w <= 0.f ) {
  141. color.w = 0.f;
  142. removed = true;
  143. }
  144. }
  145. }
  146. /*
  147. ======================
  148. BOEntity::Draw
  149. ======================
  150. */
  151. void BOEntity::Draw(idDeviceContext *dc) {
  152. if ( visible ) {
  153. dc->DrawMaterialRotated( position.x, position.y, width, height, material, color, 1.0f, 1.0f, DEG2RAD(0.f) );
  154. }
  155. }
  156. /*
  157. *****************************************************************************
  158. * BOBrick
  159. ****************************************************************************
  160. */
  161. BOBrick::BOBrick( void ) {
  162. ent = NULL;
  163. x = y = width = height = 0;
  164. powerup = POWERUP_NONE;
  165. isBroken = false;
  166. }
  167. BOBrick::BOBrick( BOEntity *_ent, float _x, float _y, float _width, float _height ) {
  168. ent = _ent;
  169. x = _x;
  170. y = _y;
  171. width = _width;
  172. height = _height;
  173. powerup = POWERUP_NONE;
  174. isBroken = false;
  175. ent->position.x = x;
  176. ent->position.y = y;
  177. ent->SetSize( width, height );
  178. ent->SetMaterial( "game/bustout/brick" );
  179. ent->game->entities.Append( ent );
  180. }
  181. BOBrick::~BOBrick( void ) {
  182. }
  183. /*
  184. ======================
  185. BOBrick::WriteToSaveGame
  186. ======================
  187. */
  188. void BOBrick::WriteToSaveGame( idFile *savefile ) {
  189. savefile->Write( &x, sizeof(x) );
  190. savefile->Write( &y, sizeof(y) );
  191. savefile->Write( &width, sizeof(width) );
  192. savefile->Write( &height, sizeof(height) );
  193. savefile->Write( &powerup, sizeof(powerup) );
  194. savefile->Write( &isBroken, sizeof(isBroken) );
  195. int index = ent->game->entities.FindIndex( ent );
  196. savefile->Write( &index, sizeof(index) );
  197. }
  198. /*
  199. ======================
  200. BOBrick::ReadFromSaveGame
  201. ======================
  202. */
  203. void BOBrick::ReadFromSaveGame( idFile *savefile, idGameBustOutWindow *game ) {
  204. savefile->Read( &x, sizeof(x) );
  205. savefile->Read( &y, sizeof(y) );
  206. savefile->Read( &width, sizeof(width) );
  207. savefile->Read( &height, sizeof(height) );
  208. savefile->Read( &powerup, sizeof(powerup) );
  209. savefile->Read( &isBroken, sizeof(isBroken) );
  210. int index;
  211. savefile->Read( &index, sizeof(index) );
  212. ent = game->entities[index];
  213. }
  214. /*
  215. ======================
  216. BOBrick::SetColor
  217. ======================
  218. */
  219. void BOBrick::SetColor( idVec4 bcolor ) {
  220. ent->SetColor( bcolor.x, bcolor.y, bcolor.z, bcolor.w );
  221. }
  222. /*
  223. ======================
  224. BOBrick::checkCollision
  225. ======================
  226. */
  227. collideDir_t BOBrick::checkCollision( idVec2 pos, idVec2 vel ) {
  228. idVec2 ptA, ptB;
  229. float dist;
  230. collideDir_t result = COLLIDE_NONE;
  231. if ( isBroken ) {
  232. return result;
  233. }
  234. // Check for collision with each edge
  235. idVec2 vec;
  236. // Bottom
  237. ptA.x = x;
  238. ptA.y = y + height;
  239. ptB.x = x + width;
  240. ptB.y = y + height;
  241. if ( vel.y < 0 && pos.y > ptA.y ) {
  242. if( pos.x > ptA.x && pos.x < ptB.x ) {
  243. dist = pos.y - ptA.y;
  244. if ( dist < BALL_RADIUS ) {
  245. result = COLLIDE_DOWN;
  246. }
  247. } else {
  248. if ( pos.x <= ptA.x ) {
  249. vec = pos - ptA;
  250. } else {
  251. vec = pos - ptB;
  252. }
  253. if ( (idMath::Fabs(vec.y) > idMath::Fabs(vec.x)) && (vec.LengthFast() < BALL_RADIUS) ) {
  254. result = COLLIDE_DOWN;
  255. }
  256. }
  257. }
  258. if ( result == COLLIDE_NONE ) {
  259. // Top
  260. ptA.y = y;
  261. ptB.y = y;
  262. if ( vel.y > 0 && pos.y < ptA.y ) {
  263. if( pos.x > ptA.x && pos.x < ptB.x ) {
  264. dist = ptA.y - pos.y;
  265. if ( dist < BALL_RADIUS ) {
  266. result = COLLIDE_UP;
  267. }
  268. } else {
  269. if ( pos.x <= ptA.x ) {
  270. vec = pos - ptA;
  271. } else {
  272. vec = pos - ptB;
  273. }
  274. if ( (idMath::Fabs(vec.y) > idMath::Fabs(vec.x)) && (vec.LengthFast() < BALL_RADIUS) ) {
  275. result = COLLIDE_UP;
  276. }
  277. }
  278. }
  279. if ( result == COLLIDE_NONE ) {
  280. // Left side
  281. ptA.x = x;
  282. ptA.y = y;
  283. ptB.x = x;
  284. ptB.y = y + height;
  285. if ( vel.x > 0 && pos.x < ptA.x ) {
  286. if( pos.y > ptA.y && pos.y < ptB.y ) {
  287. dist = ptA.x - pos.x;
  288. if ( dist < BALL_RADIUS ) {
  289. result = COLLIDE_LEFT;
  290. }
  291. } else {
  292. if ( pos.y <= ptA.y ) {
  293. vec = pos - ptA;
  294. } else {
  295. vec = pos - ptB;
  296. }
  297. if ( (idMath::Fabs(vec.x) >= idMath::Fabs(vec.y)) && (vec.LengthFast() < BALL_RADIUS) ) {
  298. result = COLLIDE_LEFT;
  299. }
  300. }
  301. }
  302. if ( result == COLLIDE_NONE ) {
  303. // Right side
  304. ptA.x = x + width;
  305. ptB.x = x + width;
  306. if ( vel.x < 0 && pos.x > ptA.x ) {
  307. if( pos.y > ptA.y && pos.y < ptB.y ) {
  308. dist = pos.x - ptA.x;
  309. if ( dist < BALL_RADIUS ) {
  310. result = COLLIDE_LEFT;
  311. }
  312. } else {
  313. if ( pos.y <= ptA.y ) {
  314. vec = pos - ptA;
  315. } else {
  316. vec = pos - ptB;
  317. }
  318. if ( (idMath::Fabs(vec.x) >= idMath::Fabs(vec.y)) && (vec.LengthFast() < BALL_RADIUS) ) {
  319. result = COLLIDE_LEFT;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. return result;
  327. }
  328. /*
  329. *****************************************************************************
  330. * idGameBustOutWindow
  331. ****************************************************************************
  332. */
  333. idGameBustOutWindow::idGameBustOutWindow(idDeviceContext *d, idUserInterfaceLocal *g) : idWindow(d, g) {
  334. dc = d;
  335. gui = g;
  336. CommonInit();
  337. }
  338. idGameBustOutWindow::idGameBustOutWindow(idUserInterfaceLocal *g) : idWindow(g) {
  339. gui = g;
  340. CommonInit();
  341. }
  342. idGameBustOutWindow::~idGameBustOutWindow() {
  343. entities.DeleteContents(true);
  344. Mem_Free( levelBoardData );
  345. }
  346. /*
  347. =============================
  348. idGameBustOutWindow::WriteToSaveGame
  349. =============================
  350. */
  351. void idGameBustOutWindow::WriteToSaveGame( idFile *savefile ) {
  352. idWindow::WriteToSaveGame( savefile );
  353. gamerunning.WriteToSaveGame( savefile );
  354. onFire.WriteToSaveGame( savefile );
  355. onContinue.WriteToSaveGame( savefile );
  356. onNewGame.WriteToSaveGame( savefile );
  357. onNewLevel.WriteToSaveGame( savefile );
  358. savefile->Write( &timeSlice, sizeof(timeSlice) );
  359. savefile->Write( &gameOver, sizeof(gameOver) );
  360. savefile->Write( &numLevels, sizeof(numLevels) );
  361. // Board Data is loaded when GUI is loaded, don't need to save
  362. savefile->Write( &numBricks, sizeof(numBricks) );
  363. savefile->Write( &currentLevel, sizeof(currentLevel) );
  364. savefile->Write( &updateScore, sizeof(updateScore) );
  365. savefile->Write( &gameScore, sizeof(gameScore) );
  366. savefile->Write( &nextBallScore, sizeof(nextBallScore) );
  367. savefile->Write( &bigPaddleTime, sizeof(bigPaddleTime) );
  368. savefile->Write( &paddleVelocity, sizeof(paddleVelocity) );
  369. savefile->Write( &ballSpeed, sizeof(ballSpeed) );
  370. savefile->Write( &ballsRemaining, sizeof(ballsRemaining) );
  371. savefile->Write( &ballsInPlay, sizeof(ballsInPlay) );
  372. savefile->Write( &ballHitCeiling, sizeof(ballHitCeiling) );
  373. // Write Entities
  374. int i;
  375. int numberOfEnts = entities.Num();
  376. savefile->Write( &numberOfEnts, sizeof(numberOfEnts) );
  377. for ( i=0; i<numberOfEnts; i++ ) {
  378. entities[i]->WriteToSaveGame( savefile );
  379. }
  380. // Write Balls
  381. numberOfEnts = balls.Num();
  382. savefile->Write( &numberOfEnts, sizeof(numberOfEnts) );
  383. for ( i=0; i<numberOfEnts; i++ ) {
  384. int ballIndex = entities.FindIndex( balls[i] );
  385. savefile->Write( &ballIndex, sizeof(ballIndex) );
  386. }
  387. // Write Powerups
  388. numberOfEnts = powerUps.Num();
  389. savefile->Write( &numberOfEnts, sizeof(numberOfEnts) );
  390. for ( i=0; i<numberOfEnts; i++ ) {
  391. int powerIndex = entities.FindIndex( powerUps[i] );
  392. savefile->Write( &powerIndex, sizeof(powerIndex) );
  393. }
  394. // Write paddle
  395. paddle->WriteToSaveGame( savefile );
  396. // Write Bricks
  397. int row;
  398. for ( row=0; row<BOARD_ROWS; row++ ) {
  399. numberOfEnts = board[row].Num();
  400. savefile->Write( &numberOfEnts, sizeof(numberOfEnts) );
  401. for ( i=0; i<numberOfEnts; i++ ) {
  402. board[row][i]->WriteToSaveGame( savefile );
  403. }
  404. }
  405. }
  406. /*
  407. =============================
  408. idGameBustOutWindow::ReadFromSaveGame
  409. =============================
  410. */
  411. void idGameBustOutWindow::ReadFromSaveGame( idFile *savefile ) {
  412. idWindow::ReadFromSaveGame( savefile );
  413. // Clear out existing paddle and entities from GUI load
  414. delete paddle;
  415. entities.DeleteContents( true );
  416. gamerunning.ReadFromSaveGame( savefile );
  417. onFire.ReadFromSaveGame( savefile );
  418. onContinue.ReadFromSaveGame( savefile );
  419. onNewGame.ReadFromSaveGame( savefile );
  420. onNewLevel.ReadFromSaveGame( savefile );
  421. savefile->Read( &timeSlice, sizeof(timeSlice) );
  422. savefile->Read( &gameOver, sizeof(gameOver) );
  423. savefile->Read( &numLevels, sizeof(numLevels) );
  424. // Board Data is loaded when GUI is loaded, don't need to save
  425. savefile->Read( &numBricks, sizeof(numBricks) );
  426. savefile->Read( &currentLevel, sizeof(currentLevel) );
  427. savefile->Read( &updateScore, sizeof(updateScore) );
  428. savefile->Read( &gameScore, sizeof(gameScore) );
  429. savefile->Read( &nextBallScore, sizeof(nextBallScore) );
  430. savefile->Read( &bigPaddleTime, sizeof(bigPaddleTime) );
  431. savefile->Read( &paddleVelocity, sizeof(paddleVelocity) );
  432. savefile->Read( &ballSpeed, sizeof(ballSpeed) );
  433. savefile->Read( &ballsRemaining, sizeof(ballsRemaining) );
  434. savefile->Read( &ballsInPlay, sizeof(ballsInPlay) );
  435. savefile->Read( &ballHitCeiling, sizeof(ballHitCeiling) );
  436. int i;
  437. int numberOfEnts;
  438. // Read entities
  439. savefile->Read( &numberOfEnts, sizeof(numberOfEnts) );
  440. for ( i=0; i<numberOfEnts; i++ ) {
  441. BOEntity *ent;
  442. ent = new BOEntity( this );
  443. ent->ReadFromSaveGame( savefile, this );
  444. entities.Append( ent );
  445. }
  446. // Read balls
  447. savefile->Read( &numberOfEnts, sizeof(numberOfEnts) );
  448. for ( i=0; i<numberOfEnts; i++ ) {
  449. int ballIndex;
  450. savefile->Read( &ballIndex, sizeof(ballIndex) );
  451. balls.Append( entities[ballIndex] );
  452. }
  453. // Read powerups
  454. savefile->Read( &numberOfEnts, sizeof(numberOfEnts) );
  455. for ( i=0; i<numberOfEnts; i++ ) {
  456. int powerIndex;
  457. savefile->Read( &powerIndex, sizeof(powerIndex) );
  458. balls.Append( entities[powerIndex] );
  459. }
  460. // Read paddle
  461. paddle = new BOBrick();
  462. paddle->ReadFromSaveGame( savefile, this );
  463. // Read board
  464. int row;
  465. for ( row=0; row<BOARD_ROWS; row++ ) {
  466. savefile->Read( &numberOfEnts, sizeof(numberOfEnts) );
  467. for ( i=0; i<numberOfEnts; i++ ) {
  468. BOBrick *brick = new BOBrick();
  469. brick->ReadFromSaveGame( savefile, this );
  470. board[row].Append( brick );
  471. }
  472. }
  473. }
  474. /*
  475. =============================
  476. idGameBustOutWindow::ResetGameState
  477. =============================
  478. */
  479. void idGameBustOutWindow::ResetGameState() {
  480. gamerunning = false;
  481. gameOver = false;
  482. onFire = false;
  483. onContinue = false;
  484. onNewGame = false;
  485. onNewLevel = false;
  486. // Game moves forward 16 milliseconds every frame
  487. timeSlice = 0.016f;
  488. ballsRemaining = 3;
  489. ballSpeed = BALL_SPEED;
  490. ballsInPlay = 0;
  491. updateScore = false;
  492. numBricks = 0;
  493. currentLevel = 1;
  494. gameScore = 0;
  495. bigPaddleTime = 0;
  496. nextBallScore = gameScore + 10000;
  497. ClearBoard();
  498. }
  499. /*
  500. =============================
  501. idGameBustOutWindow::CommonInit
  502. =============================
  503. */
  504. void idGameBustOutWindow::CommonInit() {
  505. BOEntity *ent;
  506. // Precache images
  507. declManager->FindMaterial( "game/bustout/ball" );
  508. declManager->FindMaterial( "game/bustout/doublepaddle" );
  509. declManager->FindMaterial( "game/bustout/powerup_bigpaddle" );
  510. declManager->FindMaterial( "game/bustout/powerup_multiball" );
  511. declManager->FindMaterial( "game/bustout/brick" );
  512. // Precache sounds
  513. declManager->FindSound( "arcade_ballbounce" );
  514. declManager->FindSound( "arcade_brickhit" );
  515. declManager->FindSound( "arcade_missedball" );
  516. declManager->FindSound( "arcade_sadsound" );
  517. declManager->FindSound( "arcade_extraball" );
  518. declManager->FindSound( "arcade_powerup" );
  519. ResetGameState();
  520. numLevels = 0;
  521. boardDataLoaded = false;
  522. levelBoardData = NULL;
  523. // Create Paddle
  524. ent = new BOEntity( this );
  525. paddle = new BOBrick( ent, 260.f, 440.f, 96.f, 24.f );
  526. paddle->ent->SetMaterial( "game/bustout/paddle" );
  527. }
  528. /*
  529. =============================
  530. idGameBustOutWindow::HandleEvent
  531. =============================
  532. */
  533. const char *idGameBustOutWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
  534. int key = event->evValue;
  535. // need to call this to allow proper focus and capturing on embedded children
  536. const char *ret = idWindow::HandleEvent(event, updateVisuals);
  537. if ( event->evType == SE_KEY ) {
  538. if ( !event->evValue2 ) {
  539. return ret;
  540. }
  541. if ( key == K_MOUSE1) {
  542. // Mouse was clicked
  543. if ( ballsInPlay == 0 ) {
  544. BOEntity *ball = CreateNewBall();
  545. ball->SetVisible( true );
  546. ball->position.x = paddle->ent->position.x + 48.f;
  547. ball->position.y = 430.f;
  548. ball->velocity.x = ballSpeed;
  549. ball->velocity.y = -ballSpeed*2.f;
  550. ball->velocity.NormalizeFast();
  551. ball->velocity *= ballSpeed;
  552. }
  553. } else {
  554. return ret;
  555. }
  556. }
  557. return ret;
  558. }
  559. /*
  560. =============================
  561. idGameBustOutWindow::ParseInternalVar
  562. =============================
  563. */
  564. bool idGameBustOutWindow::ParseInternalVar(const char *_name, idParser *src) {
  565. if ( idStr::Icmp(_name, "gamerunning") == 0 ) {
  566. gamerunning = src->ParseBool();
  567. return true;
  568. }
  569. if ( idStr::Icmp(_name, "onFire") == 0 ) {
  570. onFire = src->ParseBool();
  571. return true;
  572. }
  573. if ( idStr::Icmp(_name, "onContinue") == 0 ) {
  574. onContinue = src->ParseBool();
  575. return true;
  576. }
  577. if ( idStr::Icmp(_name, "onNewGame") == 0 ) {
  578. onNewGame = src->ParseBool();
  579. return true;
  580. }
  581. if ( idStr::Icmp(_name, "onNewLevel") == 0 ) {
  582. onNewLevel = src->ParseBool();
  583. return true;
  584. }
  585. if ( idStr::Icmp(_name, "numLevels") == 0 ) {
  586. numLevels = src->ParseInt();
  587. // Load all the level images
  588. LoadBoardFiles();
  589. return true;
  590. }
  591. return idWindow::ParseInternalVar(_name, src);
  592. }
  593. /*
  594. =============================
  595. idGameBustOutWindow::GetWinVarByName
  596. =============================
  597. */
  598. idWinVar *idGameBustOutWindow::GetWinVarByName(const char *_name, bool winLookup, drawWin_t** owner) {
  599. idWinVar *retVar = NULL;
  600. if ( idStr::Icmp(_name, "gamerunning") == 0 ) {
  601. retVar = &gamerunning;
  602. } else if ( idStr::Icmp(_name, "onFire") == 0 ) {
  603. retVar = &onFire;
  604. } else if ( idStr::Icmp(_name, "onContinue") == 0 ) {
  605. retVar = &onContinue;
  606. } else if ( idStr::Icmp(_name, "onNewGame") == 0 ) {
  607. retVar = &onNewGame;
  608. } else if ( idStr::Icmp(_name, "onNewLevel") == 0 ) {
  609. retVar = &onNewLevel;
  610. }
  611. if(retVar) {
  612. return retVar;
  613. }
  614. return idWindow::GetWinVarByName(_name, winLookup, owner);
  615. }
  616. /*
  617. =============================
  618. idGameBustOutWindow::PostParse
  619. =============================
  620. */
  621. void idGameBustOutWindow::PostParse() {
  622. idWindow::PostParse();
  623. }
  624. /*
  625. =============================
  626. idGameBustOutWindow::Draw
  627. =============================
  628. */
  629. void idGameBustOutWindow::Draw(int time, float x, float y) {
  630. int i;
  631. //Update the game every frame before drawing
  632. UpdateGame();
  633. for( i = entities.Num()-1; i >= 0; i-- ) {
  634. entities[i]->Draw(dc);
  635. }
  636. }
  637. /*
  638. =============================
  639. idGameBustOutWindow::Activate
  640. =============================
  641. */
  642. const char *idGameBustOutWindow::Activate(bool activate) {
  643. return "";
  644. }
  645. /*
  646. =============================
  647. idGameBustOutWindow::UpdateScore
  648. =============================
  649. */
  650. void idGameBustOutWindow::UpdateScore() {
  651. if ( gameOver ) {
  652. gui->HandleNamedEvent( "GameOver" );
  653. return;
  654. }
  655. // Check for level progression
  656. if ( numBricks == 0 ) {
  657. ClearBalls();
  658. gui->HandleNamedEvent( "levelComplete" );
  659. }
  660. // Check for new ball score
  661. if ( gameScore >= nextBallScore ) {
  662. ballsRemaining++;
  663. gui->HandleNamedEvent( "extraBall" );
  664. // Play sound
  665. session->sw->PlayShaderDirectly( "arcade_extraball", S_UNIQUE_CHANNEL );
  666. nextBallScore = gameScore + 10000;
  667. }
  668. gui->SetStateString( "player_score", va("%i", gameScore ) );
  669. gui->SetStateString( "balls_remaining", va("%i", ballsRemaining ) );
  670. gui->SetStateString( "current_level", va("%i", currentLevel ) );
  671. gui->SetStateString( "next_ball_score", va("%i", nextBallScore ) );
  672. }
  673. /*
  674. =============================
  675. idGameBustOutWindow::ClearBoard
  676. =============================
  677. */
  678. void idGameBustOutWindow::ClearBoard( void ) {
  679. int i,j;
  680. ClearPowerups();
  681. ballHitCeiling = false;
  682. for ( i=0; i<BOARD_ROWS; i++ ) {
  683. for ( j=0; j<board[i].Num(); j++ ) {
  684. BOBrick *brick = board[i][j];
  685. brick->ent->removed = true;
  686. }
  687. board[i].DeleteContents( true );
  688. }
  689. }
  690. /*
  691. =============================
  692. idGameBustOutWindow::ClearPowerups
  693. =============================
  694. */
  695. void idGameBustOutWindow::ClearPowerups( void ) {
  696. while ( powerUps.Num() ) {
  697. powerUps[0]->removed = true;
  698. powerUps.RemoveIndex( 0 );
  699. }
  700. }
  701. /*
  702. =============================
  703. idGameBustOutWindow::ClearBalls
  704. =============================
  705. */
  706. void idGameBustOutWindow::ClearBalls( void ) {
  707. while ( balls.Num() ) {
  708. balls[0]->removed = true;
  709. balls.RemoveIndex( 0 );
  710. }
  711. ballsInPlay = 0;
  712. }
  713. /*
  714. =============================
  715. idGameBustOutWindow::LoadBoardFiles
  716. =============================
  717. */
  718. void idGameBustOutWindow::LoadBoardFiles( void ) {
  719. int i;
  720. int w,h;
  721. ID_TIME_T time;
  722. int boardSize;
  723. byte *currentBoard;
  724. if ( boardDataLoaded ) {
  725. return;
  726. }
  727. boardSize = 9 * 12 * 4;
  728. levelBoardData = (byte*)Mem_Alloc( boardSize * numLevels );
  729. currentBoard = levelBoardData;
  730. for ( i=0; i<numLevels; i++ ) {
  731. byte *pic;
  732. idStr name = "guis/assets/bustout/level";
  733. name += (i+1);
  734. name += ".tga";
  735. R_LoadImage( name, &pic, &w, &h, &time, false );
  736. if ( pic != NULL ) {
  737. if ( w != 9 || h != 12 ) {
  738. common->DWarning( "Hell Bust-Out level image not correct dimensions! (%d x %d)", w, h );
  739. }
  740. memcpy( currentBoard, pic, boardSize );
  741. Mem_Free(pic);
  742. }
  743. currentBoard += boardSize;
  744. }
  745. boardDataLoaded = true;
  746. }
  747. /*
  748. =============================
  749. idGameBustOutWindow::SetCurrentBoard
  750. =============================
  751. */
  752. void idGameBustOutWindow::SetCurrentBoard( void ) {
  753. int i,j;
  754. int realLevel = ((currentLevel-1) % numLevels);
  755. int boardSize;
  756. byte *currentBoard;
  757. float bx = 11.f;
  758. float by = 24.f;
  759. float stepx = 619.f / 9.f;
  760. float stepy = ( 256 / 12.f );
  761. boardSize = 9 * 12 * 4;
  762. currentBoard = levelBoardData + ( realLevel * boardSize );
  763. for ( j=0; j<BOARD_ROWS; j++ ) {
  764. bx = 11.f;
  765. for ( i=0; i<9; i++ ) {
  766. int pixelindex = (j*9*4) + (i*4);
  767. if ( currentBoard[pixelindex + 3] ) {
  768. idVec4 bcolor;
  769. float pType = 0.f;
  770. BOEntity *bent = new BOEntity( this );
  771. BOBrick *brick = new BOBrick( bent, bx, by, stepx, stepy );
  772. bcolor.x = currentBoard[pixelindex + 0] / 255.f;
  773. bcolor.y = currentBoard[pixelindex + 1] / 255.f;
  774. bcolor.z = currentBoard[pixelindex + 2] / 255.f;
  775. bcolor.w = 1.f;
  776. brick->SetColor( bcolor );
  777. pType = currentBoard[pixelindex + 3] / 255.f;
  778. if ( pType > 0.f && pType < 1.f ) {
  779. if ( pType < 0.5f ) {
  780. brick->powerup = POWERUP_BIGPADDLE;
  781. } else {
  782. brick->powerup = POWERUP_MULTIBALL;
  783. }
  784. }
  785. board[j].Append( brick );
  786. numBricks++;
  787. }
  788. bx += stepx;
  789. }
  790. by += stepy;
  791. }
  792. }
  793. /*
  794. =============================
  795. idGameBustOutWindow::CreateNewBall
  796. =============================
  797. */
  798. BOEntity * idGameBustOutWindow::CreateNewBall( void ) {
  799. BOEntity *ball;
  800. ball = new BOEntity( this );
  801. ball->position.x = 300.f;
  802. ball->position.y = 416.f;
  803. ball->SetMaterial( "game/bustout/ball" );
  804. ball->SetSize( BALL_RADIUS*2.f, BALL_RADIUS*2.f );
  805. ball->SetVisible( false );
  806. ballsInPlay++;
  807. balls.Append( ball );
  808. entities.Append( ball );
  809. return ball;
  810. }
  811. /*
  812. =============================
  813. idGameBustOutWindow::CreatePowerup
  814. =============================
  815. */
  816. BOEntity * idGameBustOutWindow::CreatePowerup( BOBrick *brick ) {
  817. BOEntity *powerEnt = new BOEntity( this );
  818. powerEnt->position.x = brick->x;
  819. powerEnt->position.y = brick->y;
  820. powerEnt->velocity.x = 0.f;
  821. powerEnt->velocity.y = 64.f;
  822. powerEnt->powerup = brick->powerup;
  823. switch( powerEnt->powerup ) {
  824. case POWERUP_BIGPADDLE:
  825. powerEnt->SetMaterial( "game/bustout/powerup_bigpaddle" );
  826. break;
  827. case POWERUP_MULTIBALL:
  828. powerEnt->SetMaterial( "game/bustout/powerup_multiball" );
  829. break;
  830. default:
  831. powerEnt->SetMaterial( "textures/common/nodraw" );
  832. break;
  833. }
  834. powerEnt->SetSize( 619/9, 256/12 );
  835. powerEnt->SetVisible( true );
  836. powerUps.Append( powerEnt );
  837. entities.Append( powerEnt );
  838. return powerEnt;
  839. }
  840. /*
  841. =============================
  842. idGameBustOutWindow::UpdatePowerups
  843. =============================
  844. */
  845. void idGameBustOutWindow::UpdatePowerups( void ) {
  846. idVec2 pos;
  847. for ( int i=0; i < powerUps.Num(); i++ ) {
  848. BOEntity *pUp = powerUps[i];
  849. // Check for powerup falling below screen
  850. if ( pUp->position.y > 480 ) {
  851. powerUps.RemoveIndex( i );
  852. pUp->removed = true;
  853. continue;
  854. }
  855. // Check for the paddle catching a powerup
  856. pos.x = pUp->position.x + ( pUp->width / 2 );
  857. pos.y = pUp->position.y + ( pUp->height / 2 );
  858. collideDir_t collision = paddle->checkCollision( pos, pUp->velocity );
  859. if ( collision != COLLIDE_NONE ) {
  860. BOEntity *ball;
  861. // Give the powerup to the player
  862. switch( pUp->powerup ) {
  863. case POWERUP_BIGPADDLE:
  864. bigPaddleTime = gui->GetTime() + 15000;
  865. break;
  866. case POWERUP_MULTIBALL:
  867. // Create 2 new balls in the spot of the existing ball
  868. for ( int b=0; b<2; b++ ) {
  869. ball = CreateNewBall();
  870. ball->position = balls[0]->position;
  871. ball->velocity = balls[0]->velocity;
  872. if ( b == 0 ) {
  873. ball->velocity.x -= 35.f;
  874. } else {
  875. ball->velocity.x += 35.f;
  876. }
  877. ball->velocity.NormalizeFast();
  878. ball->velocity *= ballSpeed;
  879. ball->SetVisible( true );
  880. }
  881. break;
  882. default:
  883. break;
  884. }
  885. // Play the sound
  886. session->sw->PlayShaderDirectly( "arcade_powerup", S_UNIQUE_CHANNEL );
  887. // Remove it
  888. powerUps.RemoveIndex( i );
  889. pUp->removed = true;
  890. }
  891. }
  892. }
  893. /*
  894. =============================
  895. idGameBustOutWindow::UpdatePaddle
  896. =============================
  897. */
  898. void idGameBustOutWindow::UpdatePaddle( void ) {
  899. idVec2 cursorPos;
  900. float oldPos = paddle->x;
  901. cursorPos.x = gui->CursorX();
  902. cursorPos.y = gui->CursorY();
  903. if ( bigPaddleTime > gui->GetTime() ) {
  904. paddle->x = cursorPos.x - 80.f;
  905. paddle->width = 160;
  906. paddle->ent->width = 160;
  907. paddle->ent->SetMaterial( "game/bustout/doublepaddle" );
  908. } else {
  909. paddle->x = cursorPos.x - 48.f;
  910. paddle->width = 96;
  911. paddle->ent->width = 96;
  912. paddle->ent->SetMaterial( "game/bustout/paddle" );
  913. }
  914. paddle->ent->position.x = paddle->x;
  915. paddleVelocity = (paddle->x - oldPos);
  916. }
  917. /*
  918. =============================
  919. idGameBustOutWindow::UpdateBall
  920. =============================
  921. */
  922. void idGameBustOutWindow::UpdateBall( void ) {
  923. int ballnum,i,j;
  924. bool playSoundBounce = false;
  925. bool playSoundBrick = false;
  926. static int bounceChannel = 1;
  927. if ( ballsInPlay == 0 ) {
  928. return;
  929. }
  930. for ( ballnum = 0; ballnum < balls.Num(); ballnum++ ) {
  931. BOEntity *ball = balls[ballnum];
  932. // Check for ball going below screen, lost ball
  933. if ( ball->position.y > 480.f ) {
  934. ball->removed = true;
  935. continue;
  936. }
  937. // Check world collision
  938. if ( ball->position.y < 20 && ball->velocity.y < 0 ) {
  939. ball->velocity.y = -ball->velocity.y;
  940. // Increase ball speed when it hits ceiling
  941. if ( !ballHitCeiling ) {
  942. ballSpeed *= 1.25f;
  943. ballHitCeiling = true;
  944. }
  945. playSoundBounce = true;
  946. }
  947. if ( ball->position.x > 608 && ball->velocity.x > 0 ) {
  948. ball->velocity.x = -ball->velocity.x;
  949. playSoundBounce = true;
  950. } else if ( ball->position.x < 8 && ball->velocity.x < 0 ) {
  951. ball->velocity.x = -ball->velocity.x;
  952. playSoundBounce = true;
  953. }
  954. // Check for Paddle collision
  955. idVec2 ballCenter = ball->position + idVec2( BALL_RADIUS, BALL_RADIUS );
  956. collideDir_t collision = paddle->checkCollision( ballCenter, ball->velocity );
  957. if ( collision == COLLIDE_UP ) {
  958. if ( ball->velocity.y > 0 ) {
  959. idVec2 paddleVec( paddleVelocity*2, 0 );
  960. float centerX;
  961. if ( bigPaddleTime > gui->GetTime() ) {
  962. centerX = paddle->x + 80.f;
  963. } else {
  964. centerX = paddle->x + 48.f;
  965. }
  966. ball->velocity.y = -ball->velocity.y;
  967. paddleVec.x += (ball->position.x - centerX) * 2;
  968. ball->velocity += paddleVec;
  969. ball->velocity.NormalizeFast();
  970. ball->velocity *= ballSpeed;
  971. playSoundBounce = true;
  972. }
  973. } else if ( collision == COLLIDE_LEFT || collision == COLLIDE_RIGHT ) {
  974. if ( ball->velocity.y > 0 ) {
  975. ball->velocity.x = -ball->velocity.x;
  976. playSoundBounce = true;
  977. }
  978. }
  979. collision = COLLIDE_NONE;
  980. // Check for collision with bricks
  981. for ( i=0; i<BOARD_ROWS; i++ ) {
  982. int num = board[i].Num();
  983. for ( j=0; j<num; j++ ) {
  984. BOBrick *brick = (board[i])[j];
  985. collision = brick->checkCollision( ballCenter, ball->velocity );
  986. if ( collision ) {
  987. // Now break the brick if there was a collision
  988. brick->isBroken = true;
  989. brick->ent->fadeOut = true;
  990. if ( brick->powerup > POWERUP_NONE ) {
  991. BOEntity *pUp = CreatePowerup( brick );
  992. }
  993. numBricks--;
  994. gameScore += 100;
  995. updateScore = true;
  996. // Go ahead an forcibly remove the last brick, no fade
  997. if ( numBricks == 0 ) {
  998. brick->ent->removed = true;
  999. }
  1000. board[i].Remove( brick );
  1001. break;
  1002. }
  1003. }
  1004. if ( collision ) {
  1005. playSoundBrick = true;
  1006. break;
  1007. }
  1008. }
  1009. if ( collision == COLLIDE_DOWN || collision == COLLIDE_UP ) {
  1010. ball->velocity.y *= -1;
  1011. } else if ( collision == COLLIDE_LEFT || collision == COLLIDE_RIGHT ) {
  1012. ball->velocity.x *= -1;
  1013. }
  1014. if ( playSoundBounce ) {
  1015. session->sw->PlayShaderDirectly( "arcade_ballbounce", bounceChannel );
  1016. } else if ( playSoundBrick ) {
  1017. session->sw->PlayShaderDirectly( "arcade_brickhit", bounceChannel );
  1018. }
  1019. if ( playSoundBounce || playSoundBrick ) {
  1020. bounceChannel++;
  1021. if ( bounceChannel == 4 ) {
  1022. bounceChannel = 1;
  1023. }
  1024. }
  1025. }
  1026. // Check to see if any balls were removed from play
  1027. for ( ballnum=0; ballnum<balls.Num(); ballnum++ ) {
  1028. if ( balls[ballnum]->removed ) {
  1029. ballsInPlay--;
  1030. balls.RemoveIndex( ballnum );
  1031. }
  1032. }
  1033. // If all the balls were removed, update the game accordingly
  1034. if ( ballsInPlay == 0 ) {
  1035. if ( ballsRemaining == 0 ) {
  1036. gameOver = true;
  1037. // Game Over sound
  1038. session->sw->PlayShaderDirectly( "arcade_sadsound", S_UNIQUE_CHANNEL );
  1039. } else {
  1040. ballsRemaining--;
  1041. // Ball was lost, but game is not over
  1042. session->sw->PlayShaderDirectly( "arcade_missedball", S_UNIQUE_CHANNEL );
  1043. }
  1044. ClearPowerups();
  1045. updateScore = true;
  1046. }
  1047. }
  1048. /*
  1049. =============================
  1050. idGameBustOutWindow::UpdateGame
  1051. =============================
  1052. */
  1053. void idGameBustOutWindow::UpdateGame() {
  1054. int i;
  1055. if ( onNewGame ) {
  1056. ResetGameState();
  1057. // Create Board
  1058. SetCurrentBoard();
  1059. gamerunning = true;
  1060. }
  1061. if ( onContinue ) {
  1062. gameOver = false;
  1063. ballsRemaining = 3;
  1064. onContinue = false;
  1065. }
  1066. if ( onNewLevel ) {
  1067. currentLevel++;
  1068. ClearBoard();
  1069. SetCurrentBoard();
  1070. ballSpeed = BALL_SPEED * ( 1.f + ((float)currentLevel/5.f) );
  1071. if ( ballSpeed > BALL_MAXSPEED ) {
  1072. ballSpeed = BALL_MAXSPEED;
  1073. }
  1074. updateScore = true;
  1075. onNewLevel = false;
  1076. }
  1077. if(gamerunning == true) {
  1078. UpdatePaddle();
  1079. UpdateBall();
  1080. UpdatePowerups();
  1081. for( i = 0; i < entities.Num(); i++ ) {
  1082. entities[i]->Update( timeSlice, gui->GetTime() );
  1083. }
  1084. // Delete entities that need to be deleted
  1085. for( i = entities.Num()-1; i >= 0; i-- ) {
  1086. if( entities[i]->removed ) {
  1087. BOEntity* ent = entities[i];
  1088. delete ent;
  1089. entities.RemoveIndex(i);
  1090. }
  1091. }
  1092. if ( updateScore ) {
  1093. UpdateScore();
  1094. updateScore = false;
  1095. }
  1096. }
  1097. }