Advertisement
Guest User

PrimeScroll

a guest
Dec 7th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. /*
  2.  * PrimeScroll.cpp
  3.  *
  4.  *  Created on: Dec 7, 2012
  5.  *      Author: duenez
  6.  */
  7.  
  8.  
  9. #define __SCROLL_IMPL__
  10. #include "PrimeScroll.h"
  11. #undef __SCROLL_IMPL__
  12.  
  13. #include <vector>
  14. #include "orx.h"
  15. #include "CCharacter.h"
  16.  
  17. #include <sys/time.h>
  18. #include <time.h>
  19.  
  20. CPlayer *Player;
  21.  
  22. orxFLOAT Speed;
  23. orxCLOCK *Clock;
  24.  
  25. int gameState;
  26.  
  27. void movePlayer();
  28. void showMenu();
  29.  
  30.  
  31. enum game_state
  32. {
  33.     GAME_MENU, GAME_OVER, GAME_PLAY
  34. };
  35.  
  36. void PrimeScroll::Update( const orxCLOCK_INFO *_pstClockInfo )
  37. {
  38.     orxLOG("\nUpdate" );
  39.  
  40.     //Process all pending callbacks
  41.     CTimer::getInstance()->update( _pstClockInfo->fDT );
  42.  
  43.     //TODO: Fix prob. of appearance from this exponential!
  44.     // A new NumberBall appears
  45.     if( rand() % 100 == 0 )
  46.     {
  47.         //TODO: Recycle from a pool for efficiency.
  48.         CreateObject( "NumberBall" );
  49.     }
  50.     //TODO: Infinite enemies!!
  51.     if( rand() % 100 == 0 ) //int( orxConfig_GetS32( "NumBugs" )))
  52.     {
  53.         CreateObject( "Bug" );
  54.     }
  55. }
  56.  
  57. void moveCamera()
  58. {
  59.     //TODO: Don't use hard-coded constants
  60.     orxFLOAT dX = 200, dY = 150;
  61.     orxVECTOR c, p;
  62.     orxCAMERA *cam = orxCamera_Get( "Camera" );
  63.     orxObject_GetPosition( Player->GetOrxObject(), &p );
  64.     //TODO: Find out why the position is offset
  65.     //p.fY -= 300;
  66.     orxCamera_GetPosition( cam, &c );
  67.  
  68.     //Clip X coordinate
  69.     if( p.fX > c.fX + dX )
  70.         c.fX = p.fX - dX;
  71.     if( p.fX < c.fX - dX )
  72.         c.fX = p.fX + dX;
  73.  
  74.     //Clip Y coordinate
  75.     if( p.fY > c.fY + dY )
  76.         c.fY = p.fY - dY;
  77.     if( p.fY < c.fY - dY )
  78.         c.fY = p.fY + dY;
  79.  
  80.     orxCamera_SetPosition( cam, &c );
  81. }
  82.  
  83. /** Input update callback
  84.  */
  85. void orxFASTCALL InputUpdate(const orxCLOCK_INFO *_pstClockInfo, void *_pstContext)
  86. {
  87.     if (gameState == GAME_PLAY)
  88.     {
  89.         Player->move();
  90.         moveCamera();
  91.     }
  92.     else if (gameState == GAME_OVER)
  93.     {
  94.         orxOBJECT *text = orxObject_CreateFromConfig( "Te" );
  95.         char str[10];
  96.         sprintf( str, "%s", "GAME OVER" );
  97.         orxSTRING orxStr = str;
  98.         orxObject_SetTextString( text, orxStr );
  99.         gameState = GAME_MENU;
  100.     }
  101.     //else
  102.     //  showMenu();
  103.  
  104.  
  105. }
  106.  
  107. /*
  108. void PrimeScroll::showMenu()
  109. {
  110.     if(orxInput_IsActive("Start"))
  111.     {
  112.         gameState = GAME_PLAY;
  113.         Player = dynamic_cast<CPlayer*>( CreateObject( "Player" ) );
  114.     }
  115. }
  116. */
  117.  
  118. void setupInput()
  119. {
  120.     /* Reloads inputs */
  121. //    orxInput_Load(orxSTRING_EMPTY);
  122.  
  123.     /* Gets main clock */
  124.     orxCLOCK *pstMainClock = orxClock_FindFirst(orx2F(-1.0f), orxCLOCK_TYPE_CORE);
  125.  
  126.     /* Registers our input update callback to it
  127.      * !!IMPORTANT!! *DO NOT* handle inputs in clock callbacks that are *NOT* registered to the main clock
  128.      * you might miss input status updates if the user clock runs slower than the main one
  129.      */
  130.     orxClock_Register(pstMainClock, InputUpdate, orxNULL, orxMODULE_ID_MAIN, orxCLOCK_PRIORITY_NORMAL);
  131. }
  132.  
  133. void PrimeScroll::BindObjects()
  134. {
  135.     ScrollBindObject<CEnemy> ("Bug");
  136.  
  137.     ScrollBindObject<CPlayerBullet> ("Bubble");
  138.     ScrollBindObject<CEnemyBullet> ("EnemyBullet");
  139.     ScrollBindObject<CShrapnel> ("Shrapnel");
  140.  
  141.     ScrollBindObject<CPlayer> ("Player");
  142.  
  143.     ScrollBindObject<CNumberBall> ("NumberBall");
  144. }
  145.  
  146. orxSTATUS PrimeScroll::Init()
  147. {
  148.     //orxS32 i;
  149.     /* Displays a small hint in console */
  150.     orxLOG("\nInit" );
  151.  
  152.     //Initialize the static variables needed for the Health bars
  153.     CCharacter::Init();
  154.  
  155.     setupInput();
  156.  
  157.     orxViewport_CreateFromConfig( "Viewport" );
  158.     Player = CreateObject<CPlayer>( "Player" );
  159.     orxLOG( "\nPlayer %lx", Player->GetOrxObject() );
  160.     Clock = orxClock_CreateFromConfig( "Clock" );
  161.  
  162.     orxConfig_PushSection( "Constants" );
  163.     Speed = orxConfig_GetFloat( "Speed" );
  164.     orxConfig_PopSection();
  165.  
  166.     //orxClock_Register( Clock, update, orxNULL, orxMODULE_ID_MAIN, orxCLOCK_PRIORITY_NORMAL );
  167.  
  168.     //orxEvent_AddHandler( orxEVENT_TYPE_PHYSICS, PhysicsEventHandler );
  169.     //orxEvent_AddHandler( orxEVENT_TYPE_OBJECT, ObjectEventHandler );
  170.  
  171.     gameState = GAME_PLAY;
  172.     return orxSTATUS_SUCCESS;
  173. }
  174.  
  175. orxSTATUS PrimeScroll::Run()
  176. {
  177.     return orxSTATUS_SUCCESS;
  178. }
  179.  
  180. void PrimeScroll::Exit()
  181. {
  182.     //TODO: Save the state! User might want to resume.
  183.     return;
  184. }
  185.  
  186.  
  187. int main( int argc, char **argv )
  188. {
  189.     srand( time(NULL) );
  190.     PrimeScroll::GetInstance().Execute( argc, argv );
  191.     PrimeScroll::GetInstance().StartGame();
  192.     //orx_Execute( argc, argv, Init, Run, Exit );
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement