home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / SprocketInvaders / Source / ObjectActions.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  9.6 KB  |  423 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ObjectActions.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                xxx put dri here xxx
  13.  
  14.         Other Contact:        xxx put other contact here xxx
  15.  
  16.         Technology:            xxx put technology here xxx
  17.  
  18.     Writers:
  19.  
  20.         (cjd)    Chris DeSalvo
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <8>      7/1/98    cjd        Cleaned up
  25. */
  26.  
  27. //•    ------------------------------------------------------------------------------------------    •
  28. //•
  29. //•    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  30. //•
  31. //•
  32. //•        You may incorporate this sample code into your applications without
  33. //•        restriction, though the sample code has been provided "AS IS" and the
  34. //•        responsibility for its operation is 100% yours.  However, what you are
  35. //•        not permitted to do is to redistribute the source as "DSC Sample Code"
  36. //•        after having made changes. If you're going to re-distribute the source,
  37. //•        we require that you make it clear in the source that the code was
  38. //•        descended from Apple Sample Code, but that you've made changes.
  39. //•
  40. //•        Authors:
  41. //•            Chris De Salvo
  42. //•
  43. //•    ------------------------------------------------------------------------------------------    •
  44.  
  45. //•    ------------------------------    Includes
  46.  
  47. #include "EventHandler.h"
  48. #include "Graphics.h"
  49. #include "ObjectActions.h"
  50. #include "Particles.h"
  51. #include "SprocketInvaders.h"
  52. #include "CommonStuff.h"
  53.  
  54. //•    ------------------------------    Private Definitions
  55.  
  56. #define kPlayerTime                    1L    //•    How much time should pass between player updates
  57. #define kPlayerShotTime                2L    //•    How much time should pass between player shot updates
  58. #define kPointsTime                    2L    //•    How much time should pass beteen points updates
  59.  
  60. #define kEnemyShotTime                2L    //•    How much time should pass between enemy shot updates
  61. #define kEnemyVDrop                    25    //•    Speed at which enemy shots fall
  62.  
  63. #define kPointsFrames                20    //•    Number of frames that points sprite will stay visible
  64. #define kNumParticleFrames            200
  65.  
  66. //•    ------------------------------    Private Types
  67. //•    ------------------------------    Private Variables
  68. //•    ------------------------------    Private Functions
  69. //•    ------------------------------    Public Variables
  70.  
  71. //•    --------------------    ObjectIdle
  72. //•
  73. //•    This object action just moves the object at its current speed
  74.  
  75. void
  76. ObjectIdle(GameObjectPtr theObject)
  77. {
  78.     theObject->time += gDeltaTime;
  79.     if (theObject->time < 1L)
  80.         return;
  81.         
  82.     theObject->time = 0L;
  83.  
  84.     theObject->screenX += theObject->velocityH;
  85.     theObject->screenY += theObject->velocityV;
  86. }
  87.  
  88. //•    --------------------    ObjectBounce
  89. //•
  90. //•    This object action movies the object and bounces it off of its bounds
  91.  
  92. void
  93. ObjectBounce(GameObjectPtr theObject)
  94. {
  95.     theObject->time += gDeltaTime;
  96.     if (theObject->time < 1L)
  97.         return;
  98.         
  99.     theObject->time = 0L;
  100.  
  101.     theObject->screenX += theObject->velocityH;
  102.     theObject->screenY += theObject->velocityV;
  103.  
  104.     //•    Check left bounds
  105.     if ((theObject->screenX < theObject->bounds.left) && (theObject->velocityH < 0))
  106.         theObject->velocityH *= -1;
  107.  
  108.     //•    Check top bounds
  109.     if ((theObject->screenY < theObject->bounds.top) && (theObject->velocityV < 0))
  110.         theObject->velocityV *= -1;
  111.  
  112.     //•    Check right bounds
  113.     if ((theObject->screenX > theObject->bounds.right) && (theObject->velocityH > 0))
  114.         theObject->velocityH *= -1;
  115.  
  116.     //•    Check bottom bounds
  117.     if ((theObject->screenY > theObject->bounds.bottom) && (theObject->velocityV > 0))
  118.         theObject->velocityV *= -1;
  119. }
  120.  
  121. //•    --------------------    GreenPlayerAction
  122.  
  123. void
  124. GreenPlayerAction(GameObjectPtr theObject)
  125. {
  126. SInt16    velocity;
  127.  
  128.     theObject->time += gDeltaTime;
  129.     if (theObject->time < kPlayerTime)
  130.         return;
  131.         
  132.     theObject->time = 0L;
  133.  
  134.     if (theObject->refCon > 0)
  135.     {
  136.         theObject->refCon--;
  137.         return;
  138.     }
  139.  
  140.     velocity = 0;
  141.     if (gGameKeys.greenLeft)
  142.         velocity -= theObject->velocityH;
  143.         
  144.     if (gGameKeys.greenRight)
  145.         velocity += theObject->velocityH;
  146.  
  147.     theObject->screenX += velocity;
  148.  
  149.     //•    Check left bounds
  150.     if (theObject->screenX < theObject->bounds.left)
  151.         theObject->screenX = theObject->bounds.left;
  152.  
  153.     //•    Check right bounds
  154.     if (theObject->screenX > theObject->bounds.right)
  155.         theObject->screenX = theObject->bounds.right;
  156.  
  157.     //•    Did player fire?
  158.     if (gGameKeys.greenFire)
  159.         PlayerShoot(theObject);
  160. }
  161.  
  162. //•    --------------------    RedPlayerAction
  163.  
  164. void
  165. RedPlayerAction(GameObjectPtr theObject)
  166. {
  167. SInt16    velocity;
  168.  
  169.     theObject->time += gDeltaTime;
  170.     if (theObject->time < kPlayerTime)
  171.         return;
  172.         
  173.     theObject->time = 0L;
  174.  
  175.     if (theObject->refCon > 0)
  176.     {
  177.         theObject->refCon--;
  178.         return;
  179.     }
  180.  
  181.     velocity = 0;
  182.     if (gGameKeys.redLeft)
  183.         velocity -= theObject->velocityH;
  184.         
  185.     if (gGameKeys.redRight)
  186.         velocity += theObject->velocityH;
  187.  
  188.     theObject->screenX += velocity;
  189.  
  190.     //•    Check left bounds
  191.     if (theObject->screenX < theObject->bounds.left)
  192.         theObject->screenX = theObject->bounds.left;
  193.  
  194.     //•    Check right bounds
  195.     if (theObject->screenX > theObject->bounds.right)
  196.         theObject->screenX = theObject->bounds.right;
  197.  
  198.     //•    Did player fire?
  199.     if (gGameKeys.redFire)
  200.         PlayerShoot(theObject);
  201. }
  202.  
  203. //•    --------------------    PlayerShotAction
  204.  
  205. void
  206. PlayerShotAction(GameObjectPtr theObject)
  207. {
  208.     theObject->time += gDeltaTime;
  209.     if (theObject->time < kPlayerShotTime)
  210.         return;
  211.         
  212.     theObject->time = 0L;
  213.     theObject->screenY -= theObject->velocityV;
  214.     
  215.     if (theObject->screenY < theObject->bounds.top)
  216.         theObject->action = PlayerShotDestroy;
  217. }
  218.  
  219. //•    --------------------    PlayerShotDestroy
  220.  
  221. void
  222. PlayerShotDestroy(GameObjectPtr theObject)
  223. {
  224.     AddParticles(theObject->screenX, theObject->screenY, objectMissileParticles);
  225.  
  226.     if (theObject->kind == objectRedPlayerShot)
  227.         GameObjectRemoveFromList(&gRedPlayerShotList, theObject);
  228.     else
  229.         GameObjectRemoveFromList(&gGreenPlayerShotList, theObject);
  230.  
  231.     GameObjectDispose(theObject);
  232. }
  233.  
  234. //•    --------------------    EnemyDestroy
  235.  
  236. void
  237. EnemyDestroy(GameObjectPtr theObject)
  238. {
  239.     AddParticles(theObject->screenX, theObject->screenY, objectEnemyParticles);
  240.  
  241.     GameObjectRemoveFromList(&gEnemyList, theObject);
  242.     GameObjectDispose(theObject);
  243.     gNumEnemies--;
  244. }
  245.  
  246. //•    --------------------    EnemyShotDestroy
  247.  
  248. void
  249. EnemyShotDestroy(GameObjectPtr theObject)
  250. {
  251.     AddParticles(theObject->screenX, theObject->screenY, objectMissileParticles);
  252.  
  253.     GameObjectRemoveFromList(&gEnemyShotList, theObject);
  254.     GameObjectDispose(theObject);
  255. }
  256.  
  257. //•    --------------------    PointsDestroy
  258.  
  259. void
  260. PointsDestroy(GameObjectPtr theObject)
  261. {
  262.     GameObjectRemoveFromList(&gMiscObjectList, theObject);
  263.     GameObjectDispose(theObject);
  264. }
  265.  
  266. //•    --------------------    PointsAction
  267.  
  268. void
  269. PointsAction(GameObjectPtr theObject)
  270. {
  271.     theObject->time += gDeltaTime;
  272.     if (theObject->time < kPointsTime)
  273.         return;
  274.         
  275.     theObject->time = 0L;
  276.  
  277.     theObject->screenX += theObject->velocityH;
  278.     theObject->screenY += theObject->velocityV;
  279.     
  280.     theObject->refCon++;
  281.  
  282.     if (theObject->refCon > kPointsFrames)
  283.         theObject->action = PointsDestroy;
  284. }
  285.  
  286. //•    --------------------    EnemyAction
  287.  
  288. void
  289. EnemyAction(GameObjectPtr theObject)
  290. {
  291.     if (theObject->refCon > gEnemyLevel)
  292.         return;
  293.         
  294.     theObject->refCon++;
  295.     gNumEnemiesProcessed++;
  296.     
  297.     if (theObject->refCon & 1)
  298.         theObject->frame = 1;
  299.     else
  300.         theObject->frame = 0;
  301.  
  302.     //•    Determine if this enemy needs to shoot
  303.     if (Game_Random((2 * kNumEnemies) - gNumEnemies) == 1)
  304.         EnemyShoot(theObject);
  305.  
  306.     //•    Adjust screen position depending on whether we're moving left or right
  307.     if (gEnemyTask == kEnemyMovingRight)
  308.     {
  309.         theObject->screenX += gEnemyVelocity;
  310.     }
  311.     else if (gEnemyTask == kEnemyMovingLeft)
  312.     {
  313.         theObject->screenX -= gEnemyVelocity;
  314.     }
  315.     else if (gEnemyTask == kEnemyDropping)
  316.     {
  317.         theObject->screenY += kEnemyVDrop;
  318.     }
  319.         
  320.     //•    Check for bottom bound
  321.     if (theObject->screenY > theObject->bounds.bottom)
  322.     {
  323.         gNumRedPlayerLives = 0;
  324.         gNumGreenPlayerLives = 0;
  325.         return;
  326.     }
  327.  
  328.     //•    Check left bounds
  329.     if (theObject->screenX < theObject->bounds.left && (gEnemyTask == kEnemyMovingLeft))
  330.     {
  331.         gEnemiesChangeDirection = kEnemyMovingRight;
  332.         return;
  333.     }
  334.  
  335.     //•    Check right bounds
  336.     if (theObject->screenX > theObject->bounds.right && (gEnemyTask == kEnemyMovingRight))
  337.     {
  338.         gEnemiesChangeDirection = kEnemyMovingLeft;
  339.         return;
  340.     }
  341. }
  342.  
  343. //•    --------------------    EnemyShotAction
  344.  
  345. void
  346. EnemyShotAction(GameObjectPtr theObject)
  347. {
  348.     theObject->time += gDeltaTime;
  349.     if (theObject->time < kEnemyShotTime)
  350.         return;
  351.         
  352.     theObject->time = 0L;
  353.     theObject->screenY += theObject->velocityV;
  354.     
  355.     if (theObject->screenY > theObject->bounds.bottom)
  356.         theObject->action = EnemyShotDestroy;
  357. }
  358.  
  359. //•    --------------------    PlayerDestroy
  360.  
  361. void
  362. PlayerDestroy(GameObjectPtr theObject)
  363. {
  364.     AddParticles(theObject->screenX, theObject->screenY, objectPlayerParticles);
  365.  
  366.     theObject->refCon = kPlayerDeathDelay;
  367.  
  368.     if (theObject->kind == objectRedPlayer)
  369.     {
  370.         theObject->screenX = theObject->bounds.right - 20;
  371.         theObject->action = RedPlayerAction;
  372.     }
  373.     else
  374.     {
  375.         theObject->screenX = theObject->bounds.left + 20;
  376.         theObject->action = GreenPlayerAction;
  377.     }
  378. }
  379.  
  380. //•    --------------------    ParticleAction
  381.  
  382. void
  383. ParticleAction(GameObjectPtr theObject)
  384. {
  385. UInt32            i, numParticlesAlive;
  386. ParticlesPtr    theParticles;
  387. SpeckPtr        theSpecks;
  388.  
  389.     //•    Don't process the particles more than 60 times per second
  390.     theObject->time += gDeltaTime;
  391.     if (theObject->time < 1L)
  392.         return;
  393.         
  394.     theObject->time = 0L;
  395.  
  396.     //•    Get the particle object
  397.     theParticles = theObject->objectData.particles;
  398.     theSpecks = theParticles->specks;
  399.  
  400.     numParticlesAlive = 0;
  401.     
  402.     //•    Advance the position of all the living particles
  403.     for (i = 0; i < kNumSpecks; i++)
  404.     {
  405.         theSpecks[i].age++;
  406.         
  407.         if (theSpecks[i].age < theSpecks[i].lifeTime)
  408.         {
  409.             theSpecks[i].h += theSpecks[i].speedH;
  410.             theSpecks[i].v += theSpecks[i].speedV;
  411.             
  412.             numParticlesAlive++;
  413.         }
  414.     }
  415.     
  416.     //•    If all the particles are dead, kill the object
  417.     if (0 == numParticlesAlive)
  418.     {
  419.         GameObjectRemoveFromList(&gMiscObjectList, theObject);
  420.         GameObjectDispose(theObject);
  421.     }
  422. }
  423.