Arcade Shooter
Gun.h
1 
2 /* . ,'`. .
3  . .." _.-;' ⁄‚ `. . `
4  _.-"`.##%"_.--" ,' ⁄` `. "#" ___,,od000
5  ,'"-_ _.-.--"\ ,' `-_ '%#%',,/00000000000
6  ,' |_.' )`/- __..--""`-_`-._ J L/00000000000000
7  . + ,' _.-" / / _-"" `-._`-_/___\///0000 000M
8  .'_.-"" ' :_/_.-' _,`-/__V__\0000 00MMM
9  . _-"" . ' _,/000\ | /000 0MMMMM
10 _-" . ' . . ,/ 000\ | /000000000MMMMM
11  ` Shooter Library ' ,/ 000\|/000000000MMMMMM
12 . © 2017 - Shuriken Studios LLC ,/0 00000|0000000000MMMMMM */
13 
14 #pragma once
15 
16 namespace ShooterLibrary
17 {
18 
20  template <typename T>
21  class Gun : public Weapon
22  {
23 
24  public:
25 
28  Gun(const bool isActive) : Weapon(isActive)
29  {
30  m_cooldown = 0;
31  m_cooldownSeconds = 0.25;
32  }
33 
34  virtual ~Gun() { }
35 
38  virtual void Update(const GameTime *pGameTime)
39  {
40  if (m_cooldown > 0) m_cooldown -= pGameTime->GetTimeElapsed();
41  }
42 
45  virtual void Fire(TriggerType triggerType)
46  {
47  if (IsActive() && CanFire())
48  {
49  if (triggerType.Contains(GetTriggerType()))
50  {
51  Projectile *pProjectile = m_pProjectilePool->GetInactiveProjectile<T>();
52  if (pProjectile)
53  {
54  pProjectile->Activate(GetPosition(), true);
55  m_cooldown = m_cooldownSeconds;
56  }
57  }
58  }
59  }
60 
63  virtual void SetProjectilePool(ProjectilePool *pPool) { m_pProjectilePool = pPool; }
64 
67  virtual bool CanFire() const { return m_cooldown <= 0; }
68 
71  virtual void SetCooldownSeconds(const float seconds) { m_cooldownSeconds = seconds; }
72 
74  virtual void ResetCooldown() { m_cooldown = 0; }
75 
76 
77  private:
78 
79  ProjectilePool *m_pProjectilePool;
80 
81  float m_cooldown;
82  float m_cooldownSeconds;
83 
84  };
85 }
Used to recycle projectiles.
Definition: ProjectilePool.h:20
A weapon type that shoots.
Definition: Gun.h:21
virtual bool IsActive() const
Determines if the weapon is active.
Definition: Weapon.h:69
Defines how different types of weapons should be triggered.
Definition: TriggerType.h:20
virtual void SetCooldownSeconds(const float seconds)
Sets the time in seconds that the weapon takes between firing.
Definition: Gun.h:71
Shooter Library is a library of classes, interfaces, and value types that provides a foundation for d...
Definition: Background.h:16
virtual void Activate(const Vector2 &position, bool wasShotByPlayer=true)
Activates the projectile.
Definition: Projectile.cpp:58
virtual Vector2 GetPosition() const
Gets the weapon&#39;s position.
Definition: Weapon.h:80
virtual TriggerType GetTriggerType() const
Gets the action that triggers the weapon.
Definition: Weapon.h:76
Projectile * GetInactiveProjectile()
Get the next available projectile.
Definition: ProjectilePool.h:54
bool Contains(const TriggerType &type)
Determines if the trigger type contains another trigger type.
Definition: TriggerType.h:136
virtual void ResetCooldown()
Instantly cools the weapon, making it ready to fire.
Definition: Gun.h:74
Base class for all Weapons.
Definition: Weapon.h:19
Base class for all projectile types.
Definition: Projectile.h:20
Gun(const bool isActive)
Instantiates a new weapon object.
Definition: Gun.h:28
virtual bool CanFire() const
Determines if the weapon is ready to fire.
Definition: Gun.h:67
virtual void Update(const GameTime *pGameTime)
Called when the game determines it is time to draw a frame.
Definition: Gun.h:38
virtual void SetProjectilePool(ProjectilePool *pPool)
Sets the pool where the weapon gets it&#39;s projectiles.
Definition: Gun.h:63
virtual void Fire(TriggerType triggerType)
Fires the weapon.
Definition: Gun.h:45