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

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    CWeapon
  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 "demo2.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. CWeapon::CWeapon()
  20. {
  21.     m_grade = WEAPON_STANDARD;
  22.     m_offset = gsCVector(0.f,0.f);
  23.     m_mode = WEAPON_AUTOMATIC;
  24.     m_direction = WEAPON_FORWARD;
  25. }
  26.  
  27. //-------------------------------------------------------------
  28.  
  29. CWeapon::~CWeapon()
  30. {
  31. }
  32.  
  33. //-------------------------------------------------------------
  34.  
  35. bool CWeapon::activate()
  36. {
  37.     if (!isActive()) {
  38.         m_delay_fire = false;
  39.         m_autofire = false;
  40.         }
  41.         
  42.     return CActor::activate();
  43. }
  44.  
  45. //-------------------------------------------------------------
  46.  
  47. bool CWeapon::update(Controls *controls)
  48. {
  49.     if (!controls ||
  50.         !getOwner())
  51.         return false;
  52.  
  53.     m_position = getOwner()->getPosition() + m_offset;
  54.  
  55.     if (m_mode == WEAPON_MANUAL)
  56.         return true;
  57.  
  58.     switch (getOwner()->getActorInfo().m_type) {
  59.         case ACTOR_TYPE_SHIP:
  60.         case ACTOR_TYPE_UPGRADE:
  61.             {
  62.                 bool do_fire = false;
  63.  
  64.                 if (m_autofire) {
  65.                     if (controls->fire) {
  66.                         if (m_autofire_timer.getTime() >= getActorInfo().m_autofire_delay) {
  67.                             do_fire = true;
  68.                             m_autofire_timer.start();
  69.                             }
  70.                         }
  71.                     else {
  72.                         m_autofire = false;
  73.                         m_delay_fire = false;
  74.                         }
  75.                     }
  76.  
  77.                 if (controls->firePressed ||
  78.                     (controls->fire && !m_autofire)) {
  79.                     if (m_delay_fire) {
  80.                         if (m_fire_timer.getTime() >= getActorInfo().m_fire_delay)
  81.                             m_delay_fire = false;
  82.                         }
  83.                     if (!m_delay_fire) {
  84.                         do_fire = true;
  85.                         m_delay_fire = true;
  86.                         m_fire_timer.start();
  87.                         if (getActorInfo().m_autofire_delay == 0.f)
  88.                             m_autofire = false;
  89.                         else {
  90.                             m_autofire = true;
  91.                             m_autofire_timer.start();
  92.                             }
  93.                         }
  94.                     }
  95.  
  96.                 if (do_fire)
  97.                     fire();
  98.             }
  99.             break;
  100.  
  101.         case ACTOR_TYPE_ALIEN:
  102.             {
  103.                 if (!m_delay_fire || m_fire_timer.getTime() >= getActorInfo().m_fire_delay) {
  104.                     m_delay_fire = true;
  105.                     m_fire_timer.start();
  106.                     fire();
  107.                     }
  108.             }
  109.             break;
  110.         }
  111.  
  112.     return true;
  113. }
  114.  
  115. //-------------------------------------------------------------
  116.  
  117. void CWeapon::setGrade(WeaponGrade grade)
  118. {
  119.     m_grade = grade;
  120. }
  121.  
  122. //-------------------------------------------------------------
  123.  
  124. bool CWeapon::upgrade()
  125. {
  126.     switch (m_grade) {
  127.         case WEAPON_STANDARD:
  128.             setGrade(WEAPON_MEDIUM);
  129.             return true;
  130.         case WEAPON_MEDIUM:
  131.             setGrade(WEAPON_BEST);
  132.             return true;
  133.         }
  134.  
  135.     return false;
  136. }
  137.  
  138. //-------------------------------------------------------------
  139.  
  140. void CWeapon::setOffset(const gsCVector& offset)
  141. {
  142.     m_offset = offset;
  143. }
  144.  
  145. //-------------------------------------------------------------
  146.  
  147. void CWeapon::setFiringMode(WeaponFiringMode mode)
  148. {
  149.     m_mode = mode;
  150. }
  151.  
  152. //-------------------------------------------------------------
  153.  
  154. bool CWeapon::isValidFiringPosition()
  155. {
  156.     gsCScreen *screen = gsCApplication::getScreen();
  157.  
  158.     if (!screen)
  159.         return false;
  160.  
  161.     return true;
  162.  
  163.     // NYI
  164. /*
  165.     gsCPoint pos = getOwner()->getPosition() + m_offset + m_map->getPosition();
  166.  
  167.     gsCRect rect(pos - gsCPoint(WEAPON_ONSCREEN_RADIUS,WEAPON_ONSCREEN_RADIUS),
  168.                  pos + gsCPoint(WEAPON_ONSCREEN_RADIUS,WEAPON_ONSCREEN_RADIUS));
  169.  
  170.     return screen->getRect().contains(rect);
  171. */
  172. }
  173.  
  174. //-------------------------------------------------------------
  175.  
  176. void CWeapon::setDirection(WeaponDirection direction)
  177. {
  178.     m_direction = direction;
  179. }
  180.  
  181. //-------------------------------------------------------------
  182.  
  183. WeaponDirection CWeapon::getDirection()
  184. {
  185.     return m_direction;
  186. }
  187.  
  188. //-------------------------------------------------------------
  189.