gamelaserduckwindow.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "../framework/Session_local.h"
  4. #include "DeviceContext.h"
  5. #include "Window.h"
  6. #include "UserInterfaceLocal.h"
  7. #include "gamelaserduckwindow.h"
  8. #define BEAR_GRAVITY 240
  9. #define BEAR_SIZE 24.f
  10. #define BEAR_SHRINK_TIME 2000.f
  11. #define MAX_WINDFORCE 100.f
  12. /*
  13. *****************************************************************************
  14. * LaserDuckEntity
  15. ****************************************************************************
  16. */
  17. LaserDuckEntity::LaserDuckEntity(idGameLaserDuckWindow* _game) {
  18. game = _game;
  19. visible = true;
  20. entColor = colorWhite;
  21. materialName = "";
  22. material = NULL;
  23. width = height = 8;
  24. rotation = 0.f;
  25. rotationSpeed = 0.f;
  26. fadeIn = false;
  27. fadeOut = false;
  28. position.Zero();
  29. velocity.Zero();
  30. }
  31. LaserDuckEntity::~LaserDuckEntity() {
  32. }
  33. /*
  34. ======================
  35. LaserDuckEntity::WriteToSaveGame
  36. ======================
  37. */
  38. void LaserDuckEntity::WriteToSaveGame( idFile *savefile ) {
  39. game->WriteSaveGameString( materialName, savefile );
  40. savefile->Write( &width, sizeof(width) );
  41. savefile->Write( &height, sizeof(height) );
  42. savefile->Write( &visible, sizeof(visible) );
  43. savefile->Write( &entColor, sizeof(entColor) );
  44. savefile->Write( &position, sizeof(position) );
  45. savefile->Write( &rotation, sizeof(rotation) );
  46. savefile->Write( &rotationSpeed, sizeof(rotationSpeed) );
  47. savefile->Write( &velocity, sizeof(velocity) );
  48. savefile->Write( &fadeIn, sizeof(fadeIn) );
  49. savefile->Write( &fadeOut, sizeof(fadeOut) );
  50. }
  51. /*
  52. ======================
  53. LaserDuckEntity::ReadFromSaveGame
  54. ======================
  55. */
  56. void LaserDuckEntity::ReadFromSaveGame( idFile *savefile, idGameLaserDuckWindow* _game ) {
  57. game = _game;
  58. game->ReadSaveGameString( materialName, savefile );
  59. SetMaterial( materialName );
  60. savefile->Read( &width, sizeof(width) );
  61. savefile->Read( &height, sizeof(height) );
  62. savefile->Read( &visible, sizeof(visible) );
  63. savefile->Read( &entColor, sizeof(entColor) );
  64. savefile->Read( &position, sizeof(position) );
  65. savefile->Read( &rotation, sizeof(rotation) );
  66. savefile->Read( &rotationSpeed, sizeof(rotationSpeed) );
  67. savefile->Read( &velocity, sizeof(velocity) );
  68. savefile->Read( &fadeIn, sizeof(fadeIn) );
  69. savefile->Read( &fadeOut, sizeof(fadeOut) );
  70. }
  71. /*
  72. ======================
  73. LaserDuckEntity::SetMaterial
  74. ======================
  75. */
  76. void LaserDuckEntity::SetMaterial(const char* name)
  77. {
  78. materialName = name;
  79. material = declManager->FindMaterial( name );
  80. material->SetSort( SS_GUI );
  81. }
  82. /*
  83. ======================
  84. LaserDuckEntity::SetSize
  85. ======================
  86. */
  87. void LaserDuckEntity::SetSize( float _width, float _height ) {
  88. width = _width;
  89. height = _height;
  90. }
  91. /*
  92. ======================
  93. LaserDuckEntity::SetVisible
  94. ======================
  95. */
  96. void LaserDuckEntity::SetVisible( bool isVisible ) {
  97. visible = isVisible;
  98. }
  99. /*
  100. ======================
  101. LaserDuckEntity::Update
  102. ======================
  103. */
  104. void LaserDuckEntity::Update( float timeslice )
  105. {
  106. if ( !visible ) {
  107. return;
  108. }
  109. // Fades
  110. if ( fadeIn && entColor.w < 1.f ) {
  111. entColor.w += 1 * timeslice;
  112. if ( entColor.w >= 1.f ) {
  113. entColor.w = 1.f;
  114. fadeIn = false;
  115. }
  116. }
  117. if ( fadeOut && entColor.w > 0.f ) {
  118. entColor.w -= 1 * timeslice;
  119. if ( entColor.w <= 0.f ) {
  120. entColor.w = 0.f;
  121. fadeOut = false;
  122. }
  123. }
  124. // Move the entity
  125. position += velocity * timeslice;
  126. // Rotate Entity
  127. rotation += rotationSpeed * timeslice;
  128. }
  129. /*
  130. ======================
  131. LaserDuckEntity::Draw
  132. ======================
  133. */
  134. void LaserDuckEntity::Draw(idDeviceContext *dc)
  135. {
  136. if ( visible )
  137. {
  138. dc->DrawMaterialRotated( position.x, position.y, width, height, material, entColor, 1.0f, 1.0f, DEG2RAD(rotation) );
  139. }
  140. }
  141. idGameLaserDuckWindow::idGameLaserDuckWindow(idDeviceContext *d, idUserInterfaceLocal *g) : idWindow(d, g) {
  142. dc = d;
  143. gui = g;
  144. CommonInit();
  145. }
  146. idGameLaserDuckWindow::idGameLaserDuckWindow(idUserInterfaceLocal *g) : idWindow(g) {
  147. gui = g;
  148. CommonInit();
  149. }
  150. idGameLaserDuckWindow::~idGameLaserDuckWindow() {
  151. entities.DeleteContents(true);
  152. }
  153. void idGameLaserDuckWindow::WriteToSaveGame( idFile *savefile )
  154. {
  155. }
  156. void idGameLaserDuckWindow::ReadFromSaveGame( idFile *savefile )
  157. {
  158. }
  159. void idGameLaserDuckWindow::ResetGameState() {
  160. gamerunning = false;
  161. onNewGame = false;
  162. }
  163. void idGameLaserDuckWindow::CommonInit() {
  164. LaserDuckEntity * ent;
  165. // Precache sounds
  166. //declManager->FindSound( "arcade_beargroan" );
  167. //declManager->FindMaterial( "game/bearshoot/helicopter_broken" );
  168. declManager->FindMaterial( "game/laserduck/duck" );
  169. ResetGameState();
  170. ent = new LaserDuckEntity( this );
  171. duck = ent;
  172. ent->SetMaterial( "game/laserduck/duck" );
  173. ent->SetSize( BEAR_SIZE, BEAR_SIZE );
  174. ent->SetVisible( true );
  175. ent->position.x = 32;
  176. ent->position.y = 32;
  177. entities.Append( ent );
  178. }
  179. const char *idGameLaserDuckWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals)
  180. {
  181. int key = event->evValue;
  182. // need to call this to allow proper focus and capturing on embedded children
  183. const char *ret = idWindow::HandleEvent(event, updateVisuals);
  184. if ( event->evType == SE_KEY )
  185. {
  186. if ( !event->evValue2 )
  187. {
  188. return ret;
  189. }
  190. if ( key == K_MOUSE1)
  191. {
  192. // Mouse was clicked
  193. }
  194. else
  195. {
  196. return ret;
  197. }
  198. }
  199. return ret;
  200. }
  201. bool idGameLaserDuckWindow::ParseInternalVar(const char *_name, idParser *src) {
  202. if ( idStr::Icmp(_name, "gamerunning") == 0 )
  203. {
  204. gamerunning = src->ParseBool();
  205. return true;
  206. }
  207. if ( idStr::Icmp(_name, "onNewGame") == 0 )
  208. {
  209. onNewGame = src->ParseBool();
  210. return true;
  211. }
  212. return idWindow::ParseInternalVar(_name, src);
  213. }
  214. idWinVar *idGameLaserDuckWindow::GetWinVarByName(const char *_name, bool winLookup, drawWin_t** owner)
  215. {
  216. idWinVar *retVar = NULL;
  217. if ( idStr::Icmp(_name, "gamerunning") == 0 )
  218. {
  219. retVar = &gamerunning;
  220. }
  221. else if ( idStr::Icmp(_name, "onNewGame") == 0 )
  222. {
  223. retVar = &onNewGame;
  224. }
  225. if(retVar)
  226. {
  227. return retVar;
  228. }
  229. return idWindow::GetWinVarByName(_name, winLookup, owner);
  230. }
  231. void idGameLaserDuckWindow::PostParse()
  232. {
  233. idWindow::PostParse();
  234. }
  235. void idGameLaserDuckWindow::Draw(int time, float x, float y)
  236. {
  237. int i;
  238. //Update the game every frame before drawing
  239. UpdateGame();
  240. for( i = entities.Num()-1; i >= 0; i-- )
  241. {
  242. entities[i]->Draw(dc);
  243. }
  244. }
  245. const char *idGameLaserDuckWindow::Activate(bool activate)
  246. {
  247. return "";
  248. }
  249. void idGameLaserDuckWindow::UpdateButtons()
  250. {
  251. }
  252. void idGameLaserDuckWindow::UpdateGame()
  253. {
  254. //this->duck->position.x = this->duck->position.x + 1;
  255. }