home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / demo4 / ship.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-11  |  4.7 KB  |  252 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    Ship
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    06/05/00
  8. //
  9. // Base:    CActor
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "demo4.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. CShip::CShip()
  20. {
  21.     m_weapon = 0;
  22.     m_weapon_type = NO_WEAPON;
  23.  
  24.     m_max_speed = 200.f;
  25.     m_acceleration = 1000.f;
  26.     m_damping = 1500.f;
  27. }
  28.  
  29. //-------------------------------------------------------------
  30.  
  31. CShip::~CShip()
  32. {
  33. }
  34.  
  35. //-------------------------------------------------------------
  36.  
  37. bool CShip::activate()
  38. {
  39.     if (!isActive()) {
  40.         m_roll = 0;
  41.         setWeapon(NO_WEAPON);
  42.         setVelocity(gsCVector(0.f,0.f));
  43.  
  44.         m_timer.reset();
  45.         }
  46.  
  47.     return CActor::activate();
  48. }
  49.  
  50. //-------------------------------------------------------------
  51.  
  52. void CShip::explode()
  53. {
  54.     CActor::explode();
  55. }
  56.  
  57. //-------------------------------------------------------------
  58.  
  59. void CShip::kill()
  60. {
  61.     if (m_weapon) {
  62.         m_weapon->kill();
  63.         m_weapon = 0;
  64.         }
  65.  
  66.     CActor::kill();
  67. }
  68.  
  69. //-------------------------------------------------------------
  70.  
  71. bool CShip::update(Controls *controls)
  72. {
  73.     if (m_shield == 0)
  74.         return true;
  75.  
  76.     gsCVector thrust(0.f,0.f);
  77.  
  78.     int required_roll = 0;
  79.  
  80.     float t = gsCTimer::getDeltaTime();
  81.  
  82.     if (controls) {
  83.  
  84.         if (controls->left) {
  85.             thrust.setX(-m_acceleration);
  86.             required_roll = -SHIP_ROLL_FRAMES;
  87.             }
  88.         if (controls->right) {
  89.             thrust.setX(m_acceleration);
  90.             required_roll = SHIP_ROLL_FRAMES;
  91.             }
  92.         if (controls->up) {
  93.             thrust.setY(-m_acceleration);
  94.             }
  95.         if (controls->down) {
  96.             thrust.setY(m_acceleration);
  97.             }
  98.  
  99.         float x = m_velocity.getX();
  100.         
  101.         if (thrust.getX() != 0.f) {
  102.             x += thrust.getX() * t;
  103.             if (x < -m_max_speed)
  104.                 x = -m_max_speed;
  105.             if (x > m_max_speed)
  106.                 x = m_max_speed;
  107.             }
  108.         else {
  109.             if (x > 0.f) {
  110.                 x -= m_damping * t;
  111.                 if (x < 0.f)
  112.                     x = 0.f;
  113.                 }
  114.             else {
  115.                 x += m_damping * t;
  116.                 if (x > 0.f)
  117.                     x = 0.f;
  118.                 }
  119.             }
  120.  
  121.         float y = m_velocity.getY();
  122.         
  123.         if (thrust.getY() != 0.f) {
  124.             y += thrust.getY() * t;
  125.             if (y < -m_max_speed)
  126.                 y = -m_max_speed;
  127.             if (y > m_max_speed)
  128.                 y = m_max_speed;
  129.             }
  130.         else {
  131.             if (y > 0.f) {
  132.                 y -= m_damping * t;
  133.                 if (y < 0.f)
  134.                     y = 0.f;
  135.                 }
  136.             else {
  137.                 y += m_damping * t;
  138.                 if (y > 0.f)
  139.                     y = 0.f;
  140.                 }
  141.             }
  142.  
  143.         m_velocity = gsCVector(x,y);
  144.         }
  145.  
  146.     m_position += m_velocity * gsCVector(t,t);
  147.  
  148.     m_position += gsCVector(0.f,(float) -CPlayGameState::getYScroll());
  149.  
  150.     int map_y = m_scene->getMap()->getPosition().getY();
  151.  
  152.     gsCScreen *screen = gsCApplication::getScreen();
  153.  
  154.     float minx = 0.f;
  155.     float maxx = (float) screen->getSize().getX();
  156.     float miny = (float) (-map_y);
  157.     float maxy = miny + (float) screen->getSize().getY();
  158.  
  159.     m_position.clamp(minx + 32.f,maxx - 32.f,miny + 32.f,maxy - 32.f);
  160.  
  161.     if (m_roll == required_roll)
  162.         m_timer.reset();
  163.     else {
  164.         if (m_timer.getState() == gsTIMER_RESET)
  165.             m_timer.start();
  166.         else {
  167.             if (m_timer.getTime() >= 1.f / SHIP_ROLL_RATE) {
  168.                 if (required_roll < m_roll)
  169.                     m_roll--;
  170.                 else
  171.                     m_roll++;
  172.                 if (m_roll != required_roll)
  173.                     m_timer.start();
  174.                 }
  175.             }
  176.         }
  177.  
  178.     m_sprite.setFrame(SHIP_CENTRE_FRAME + m_roll);
  179.  
  180.     return true;
  181. }
  182.  
  183. //-------------------------------------------------------------
  184.  
  185. gsCRect CShip::getCollisionRect()
  186. {
  187.     gsCRect r = m_sprite.getRect();
  188.  
  189.     r.expand(-8);
  190.  
  191.     return r;
  192. }
  193.  
  194. //-------------------------------------------------------------
  195.  
  196. void CShip::registerHit(int energy,CActor *hitter)
  197. {
  198.     CActor::registerHit(energy,hitter);
  199. }
  200.  
  201. //-------------------------------------------------------------
  202.  
  203. void CShip::onCollisionWithActor(CActor *actor)
  204. {
  205.     switch (actor->getActorInfo().m_type) {
  206.         case ACTOR_TYPE_ALIEN:
  207.             registerHit(1,this);
  208.             break;
  209.         }
  210. }
  211.  
  212. //-------------------------------------------------------------
  213.  
  214. void CShip::onCollisionWithMap(gsCMap *map,int hits)
  215. {
  216.     registerHit(SHIP_MAP_HIT,this);
  217. }
  218.  
  219. //-------------------------------------------------------------
  220.  
  221. void CShip::setWeapon(WeaponType type,WeaponGrade grade)
  222. {
  223.     if (m_weapon) {
  224.         m_weapon->kill();
  225.         m_weapon = 0;
  226.         }
  227.  
  228.     m_weapon_type = type;
  229.  
  230.     switch (m_weapon_type) {
  231.         case MISSILE_WEAPON:
  232.             m_weapon = new CMissileWeapon;
  233.             break;
  234.         }
  235.  
  236.     if (m_weapon) {
  237.         m_scene->addActor(m_weapon);
  238.         m_weapon->activate();
  239.         m_weapon->setOwner(this);
  240.         m_weapon->setGrade(grade);
  241.         }
  242. }
  243.  
  244. //-------------------------------------------------------------
  245.  
  246. WeaponType CShip::getWeaponType()
  247. {
  248.     return m_weapon_type;
  249. }
  250.  
  251. //-------------------------------------------------------------
  252.