Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Weapon.h
Go to the documentation of this file.
1
2#pragma once
3
4#include "KatanaEngine.h"
5#include "Projectile.h"
6#include "TriggerType.h"
7
8using namespace KatanaEngine;
9
11class Weapon : public IAttachment
12{
13
14public:
15
18 Weapon(const std::string &key, bool isAttachedToPlayer = true, bool isActive = true, TriggerType triggerType = TriggerType::Primary)
19 {
20 m_key = key;
21 m_isAttachedToPlayer = isAttachedToPlayer;
22 m_isActive = isActive;
23 SetTriggerType(triggerType);
24 }
25 virtual ~Weapon() { }
26
29 virtual void Update(const GameTime& gameTime) { };
30
34 virtual void Draw(SpriteBatch& spriteBatch) { };
35
40 virtual void Fire(TriggerType triggerType) = 0;
41
43 // @param pGameObject A pointer to the game object. */
44 //virtual void SetGameObject(GameObject *pGameObject) { m_pGameObject = pGameObject; }
45
47 // @param offset The offset of the weapon. */
48 //virtual void SetOffset(Vector2 offset) { m_offset = offset; }
49
53 virtual void SetTriggerType(TriggerType triggerType) { m_triggerType = triggerType; }
54
58 virtual void SetProjectilePool(std::vector<Projectile *> *pProjectiles) { m_pProjectiles = pProjectiles; }
59
61 virtual void Activate() { m_isActive = true; }
62
64 virtual void Dectivate() { m_isActive = false; }
65
68 virtual bool IsActive() const { return m_isActive && m_pGameObject->IsActive(); }
69
72 virtual void SetFireSound(AudioSample* pSound) { m_pFireSound = pSound; }
73
76 virtual AudioSample* GetFireSound() { return m_pFireSound; }
77
80 virtual bool IsAttachedToPlayer() const { return m_isAttachedToPlayer; }
81
85 virtual void AttachTo(IAttachable* pAttachable, Vector2& position) {
86 m_pGameObject = dynamic_cast<GameObject*>(pAttachable);
87 m_offset = position;
88 }
89
90
91 virtual std::string GetKey() const { return m_key; }
92
93 virtual std::string GetAttachmentType() const { return "Weapon"; }
94
95
96protected:
97
100 virtual TriggerType GetTriggerType() const { return m_triggerType; }
101
104 virtual Vector2 GetPosition() const { return m_pGameObject->GetPosition() + m_offset; }
105
110 {
111 m_projectileIt = m_pProjectiles->begin();
112 for (; m_projectileIt != m_pProjectiles->end(); m_projectileIt++)
113 {
114 Projectile *pProjectile = *m_projectileIt;
115 if (!pProjectile->IsActive()) return pProjectile;
116 }
117
118 return nullptr;
119 }
120
121
122private:
123
124 bool m_isActive = true;
125 bool m_isAttachedToPlayer = true;
126
127 std::string m_key;
128
129 GameObject *m_pGameObject = nullptr;
130
131 Vector2 m_offset;
132
133 TriggerType m_triggerType = TriggerType::Primary;
134
135 std::vector<Projectile *>::iterator m_projectileIt;
136 std::vector<Projectile *> *m_pProjectiles;
137
138 AudioSample* m_pFireSound = nullptr;
139
140};
141
Represents a game object in the game. This is the base class for all objects that can be updated,...
Definition GameObject.h:15
virtual bool IsActive() const
Flag to determine if the object is active.
Definition GameObject.h:42
virtual Vector2 & GetPosition()
Get the position of the object.
Definition GameObject.h:52
Represents a 2D grid of texels.
Definition AudioSample.h:21
Contains timing values for game updates and rendering.
Definition GameTime.h:21
Interface for an object that can have items attached to it.
Definition IAttachable.h:27
Interface for a particle emitter.
Definition IAttachment.h:25
Enables a group of sprites to be drawn using the same settings.
Definition SpriteBatch.h:48
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
Represents a projectile that can be fired by a weapon.
Definition Projectile.h:9
Represents a type of trigger, which can be used to activate weapons.
Definition TriggerType.h:8
static const TriggerType Primary
Definition TriggerType.h:17
Base class for all weapons that can be fired by a game object.
Definition Weapon.h:12
virtual void AttachTo(IAttachable *pAttachable, Vector2 &position)
Attach the weapon to a game object.
Definition Weapon.h:85
Weapon(const std::string &key, bool isAttachedToPlayer=true, bool isActive=true, TriggerType triggerType=TriggerType::Primary)
Instantiate a weapon object.
Definition Weapon.h:18
virtual void Draw(SpriteBatch &spriteBatch)
Render the weapon.
Definition Weapon.h:34
virtual std::string GetAttachmentType() const
Definition Weapon.h:93
virtual void Dectivate()
Deactivate the weapon.
Definition Weapon.h:64
virtual bool IsAttachedToPlayer() const
Determine if the weapon is attached to the player.
Definition Weapon.h:80
virtual void Activate()
Activate the weapon.
Definition Weapon.h:61
virtual void SetProjectilePool(std::vector< Projectile * > *pProjectiles)
Set the pool of projectiles that the weapon can fire.
Definition Weapon.h:58
virtual TriggerType GetTriggerType() const
Get the game object that the weapon is attached to.
Definition Weapon.h:100
virtual void Update(const GameTime &gameTime)
Update the weapon.
Definition Weapon.h:29
virtual AudioSample * GetFireSound()
Get the sound that will be played when the weapon is fired.
Definition Weapon.h:76
virtual std::string GetKey() const
Definition Weapon.h:91
virtual Vector2 GetPosition() const
Get the screen position of the weapon.
Definition Weapon.h:104
virtual void SetFireSound(AudioSample *pSound)
Set the sound that will be played when the weapon is fired.
Definition Weapon.h:72
virtual ~Weapon()
Definition Weapon.h:25
virtual Projectile * GetProjectile()
Get a projectile from the pool.
Definition Weapon.h:109
virtual bool IsActive() const
Determine if the weapon is active.
Definition Weapon.h:68
virtual void Fire(TriggerType triggerType)=0
Attempt to fire the weapon.
virtual void SetTriggerType(TriggerType triggerType)
Set the game object that the weapon is attached to.
Definition Weapon.h:53
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18