Skip to content

Commit ab375df

Browse files
committed
added shooting ability to players
1 parent f981989 commit ab375df

7 files changed

+155
-0
lines changed

Data/galaga_player_bullet.png

2.31 KB
Loading

Galaga/Bullet.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "Bullet.h"
2+
#include "GameObject.h"
3+
#include "Sprite.h"
4+
#include "SpriteColliderComponent.h"
5+
6+
Bullet::Bullet(Fluffy::GameObject* pOwner, int ownerIndex, const glm::vec2& position, const glm::vec2& speed)
7+
: Component(pOwner)
8+
, m_OwnerIndex{ ownerIndex }
9+
, m_Speed{ speed }
10+
{
11+
Fluffy::Sprite* pSprite{ m_pOwner->AddComponent<Fluffy::Sprite>(ownerIndex == 0 ? "enemyBullet.png" : "galaga_player_bullet.png") };
12+
m_pOwner->AddComponent<SpriteColliderComponent>(pSprite->GetTextureSize());
13+
m_pOwner->SetActive(true);
14+
m_pOwner->SetWorldPosition(position);
15+
}
16+
17+
void Bullet::Update(const float deltaTime)
18+
{
19+
m_pOwner->SetWorldPosition(m_pOwner->GetWorldPosition() + (m_Speed * deltaTime));
20+
}
21+
22+
void Bullet::Initialize(int ownerIndex, const glm::vec2& position)
23+
{
24+
m_pOwner->GetComponentOfType<Fluffy::Sprite>()->SetTexture(ownerIndex == 0 ? "enemyBullet.png" : "galaga_player_bullet.png");
25+
26+
m_pOwner->SetActive(true);
27+
m_pOwner->SetWorldPosition(position);
28+
}

Galaga/Bullet.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "Component.h"
3+
#include <glm/glm.hpp>
4+
5+
namespace Fluffy
6+
{
7+
class GameObject;
8+
}
9+
10+
class Bullet final : public Fluffy::Component
11+
{
12+
public:
13+
Bullet(Fluffy::GameObject* pOwner, int ownerIndex, const glm::vec2& position, const glm::vec2& speed = { 0.0f, -100.0f });
14+
~Bullet() = default;
15+
16+
void Update(const float deltaTime) override;
17+
std::string GetTypeName() override { return typeid(*this).name(); }
18+
void Initialize(int ownerIndex, const glm::vec2& position);
19+
20+
private:
21+
int m_OwnerIndex;
22+
glm::vec2 m_Speed;
23+
};
24+

Galaga/BulletsManager.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "BulletsManager.h"
2+
#include "GameObject.h"
3+
4+
void BulletsManager::Shoot(int ownerIndex, const glm::vec2& position)
5+
{
6+
if (!m_BulletsPool.empty())
7+
{
8+
Bullet* pBullet;
9+
pBullet = m_BulletsPool.front();
10+
m_ActiveBullets.push_back(pBullet);
11+
m_BulletsPool.pop();
12+
13+
pBullet->Initialize(ownerIndex, position);
14+
}
15+
else
16+
{
17+
std::shared_ptr<Fluffy::GameObject> pBulletObject{ std::make_shared<Fluffy::GameObject>(position.x, position.y) };
18+
Bullet* pBullet{ pBulletObject->AddComponent<Bullet>(ownerIndex, position) };
19+
pBullet->Initialize(ownerIndex, position);
20+
m_Scene->Add(pBulletObject);
21+
}
22+
}
23+
24+
void BulletsManager::PoolBullet(int index) // pool the bullet if it collides with a thing or is out of screen -> add events
25+
{
26+
Bullet* pBullet{ m_ActiveBullets[index] };
27+
m_BulletsPool.push(pBullet);
28+
29+
m_ActiveBullets.erase(m_ActiveBullets.begin() + index);
30+
31+
pBullet->GetGameObject()->SetActive(false);
32+
}

Galaga/BulletsManager.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#include "Singleton.h"
3+
#include "Scene.h"
4+
#include <glm/glm.hpp>
5+
#include <queue>
6+
#include <unordered_map>
7+
#include "Bullet.h"
8+
9+
namespace Fluffy
10+
{
11+
class GameObject;
12+
}
13+
14+
class BulletsManager : public Fluffy::Singleton<BulletsManager>
15+
{
16+
public:
17+
void SetScene(Fluffy::Scene* pScene) { m_Scene = pScene; }
18+
void Shoot(int ownerIndex, const glm::vec2& position);
19+
20+
private:
21+
Fluffy::Scene* m_Scene;
22+
std::queue<Bullet*> m_BulletsPool;
23+
std::vector<Bullet*> m_ActiveBullets;
24+
25+
void PoolBullet(int index);
26+
};
27+

Galaga/ShootCommand.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include "ShootCommand.h"
3+
#include "BulletsManager.h"
4+
#include "CharactersManager.h"
5+
#include "PlayerCharacter.h"
6+
#include "GameObject.h"
7+
8+
ShootCommand::ShootCommand(int playerIndex)
9+
: m_PlayerIndex{ playerIndex }
10+
{
11+
12+
}
13+
14+
void ShootCommand::Execute()
15+
{
16+
const glm::vec2 position{ CharactersManager::GetInstance().GetPlayer(m_PlayerIndex)->GetGameObject()->GetWorldPosition() };
17+
BulletsManager::GetInstance().Shoot(m_PlayerIndex, position);
18+
}

Galaga/ShootCommand.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include "Command.h"
3+
#include "glm/glm.hpp"
4+
5+
namespace Fluffy
6+
{
7+
class Command;
8+
}
9+
10+
class ShootCommand final : public Fluffy::Command
11+
{
12+
public:
13+
ShootCommand(int playerIndex);
14+
~ShootCommand() = default;
15+
16+
ShootCommand(const ShootCommand& other) = default;
17+
ShootCommand& operator=(const ShootCommand& other) = default;
18+
ShootCommand(ShootCommand&& other) = default;
19+
ShootCommand& operator=(ShootCommand&& other) = default;
20+
21+
void Execute() override;
22+
23+
private:
24+
const int m_PlayerIndex;
25+
};
26+

0 commit comments

Comments
 (0)