Skip to content

Commit

Permalink
added player and enemy classes
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Mar 25, 2024
1 parent 086ff18 commit 702705b
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Minigin/Character.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "Character.h"
#include "IEventParam.h"

Character::Character(const std::weak_ptr<GameObject> pOwner, int livesCount)
: Component(pOwner)
, m_LivesCount{ livesCount }
{

}

void Character::Kill(int killerIndex)
{
--m_LivesCount;

const OnCharacterDeathParam eventParam{ killerIndex };
m_OnDeath.Invoke(&eventParam);
}
23 changes: 23 additions & 0 deletions Minigin/Character.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "Component.h"
#include "Event.h"
class Character : public Component
{
public:
virtual ~Character() = default;

inline int GetLivesCount() const { return m_LivesCount; }
inline Event& GetOnDamageTaken() { return m_OnDamageTaken; }
inline Event& GetOnDeath() { return m_OnDeath; }

void Kill(int killerIndex = 0);

protected:
Character(const std::weak_ptr<GameObject> pOwner, int livesCount);

int m_LivesCount;

Event m_OnDamageTaken{ EventType::OnDamageTaken };
Event m_OnDeath{ EventType::OnCharacterDeath };
};

69 changes: 69 additions & 0 deletions Minigin/CharactersManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "CharactersManager.h"
#include "Character.h"
#include "EnemyCharacter.h"
#include "PlayerCharacter.h"
#include "Scene.h"
#include "GameObject.h"
#include "Sprite.h"
#include <memory>

void CharactersManager::CreatePlayerCharacters(Scene& scene)
{
std::shared_ptr<GameObject> pPlayer1{ std::make_shared<GameObject>(270.0f, 350.0f) };
std::shared_ptr<Sprite> pPlayer1Sprite{ std::make_shared<Sprite>(pPlayer1, "galaga_player1.png") };
pPlayer1->AddComponent<Sprite>(pPlayer1Sprite);
std::shared_ptr<PlayerCharacter> pPlayer1Component{ std::make_shared<PlayerCharacter>(pPlayer1, 3, 1) };
pPlayer1->AddComponent<PlayerCharacter>(pPlayer1Component);
scene.Add(pPlayer1);

m_PlayerCharacters.push_back(pPlayer1Component);

std::shared_ptr<GameObject> pPlayer2{ std::make_shared<GameObject>(330.0f, 350.0f) };
std::shared_ptr<Sprite> pPlayer2Sprite{ std::make_shared<Sprite>(pPlayer2, "galaga_player2.png") };
std::shared_ptr<PlayerCharacter> pPlayer2Component{ std::make_shared<PlayerCharacter>(pPlayer2, 3, 2) };
pPlayer2->AddComponent<Sprite>(pPlayer2Sprite);
scene.Add(pPlayer2);

m_PlayerCharacters.push_back(pPlayer2Component);
}

void CharactersManager::SpawnEnemies(Scene& scene)
{
std::shared_ptr<GameObject> enemy{ std::make_shared<GameObject>(290.0f, 60.0f) };
std::shared_ptr<Sprite> enemySprite{ std::make_shared<Sprite>(enemy, "galaga_blue_enemy.png")};
std::shared_ptr<EnemyCharacter> enemyCharacter{ std::make_shared<EnemyCharacter>(enemy) };
enemy->AddComponent<Sprite>(enemySprite);
enemy->AddComponent<EnemyCharacter>(enemyCharacter);
scene.Add(enemy);

enemyCharacter->GetOnDeath().AddListener(this);

m_Enemies.push_back(enemyCharacter);
}

void CharactersManager::KillEnemy(int killerIndex)
{
if (!m_Enemies.empty())
{
m_Enemies.back()->Kill(killerIndex);
m_Enemies.back()->GetGameObject()->Destroy();
m_Enemies.pop_back();
}
}

void CharactersManager::OnNotify(const EventType& eventType, const IEventParam* param)
{
switch (eventType)
{
case EventType::OnCharacterDeath:
m_OnEnemyKilled.Invoke(param);
break;
}
}

std::shared_ptr<PlayerCharacter> CharactersManager::GetPlayer(int playerIndex)
{
return playerIndex > 0 && playerIndex <= m_PlayerCharacters.size() ?
m_PlayerCharacters[playerIndex - 1] :
nullptr;
}
23 changes: 23 additions & 0 deletions Minigin/CharactersManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "Singleton.h"
#include "Event.h"
#include "IEventListener.h"
#include <memory>
#include <vector>

class CharactersManager : public Singleton<CharactersManager>, IEventListener
{
public:
void CreatePlayerCharacters(class Scene& scene);
void SpawnEnemies(class Scene& scene);
void KillEnemy(int killerIndex);
void OnNotify(const EventType& eventType, const IEventParam* param) override;
std::shared_ptr<class PlayerCharacter> GetPlayer(int playerIndex);
inline Event& GetOnEnemyKilled() { return m_OnEnemyKilled; }

private:
Event m_OnEnemyKilled{ EventType::OnEnemyKilled };

std::vector<std::shared_ptr<class PlayerCharacter>> m_PlayerCharacters{};
std::vector<std::shared_ptr<class EnemyCharacter>> m_Enemies{};
};
8 changes: 8 additions & 0 deletions Minigin/EnemyCharacter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "EnemyCharacter.h"
#include "GameObject.h"

EnemyCharacter::EnemyCharacter(const std::weak_ptr<GameObject> pOwner)
: Character(pOwner, 1)
{

}
17 changes: 17 additions & 0 deletions Minigin/EnemyCharacter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "Character.h"

class EnemyCharacter final : public Character
{
public:
EnemyCharacter(const std::weak_ptr<class GameObject> pOwner);
~EnemyCharacter() = default;

/*EnemyCharacter(const EnemyCharacter&) = delete;
EnemyCharacter(EnemyCharacter&&) = delete;
EnemyCharacter& operator=(const EnemyCharacter&) = delete;
EnemyCharacter& operator=(EnemyCharacter&&) = delete;*/

std::string GetTypeName() override { return typeid(*this).name(); }
};

9 changes: 9 additions & 0 deletions Minigin/PlayerCharacter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "PlayerCharacter.h"
#include "GameObject.h"

PlayerCharacter::PlayerCharacter(const std::weak_ptr<GameObject> pOwner, int livesCount, int playerIndex)
: Character(pOwner, livesCount)
, m_PlayerIndex{ playerIndex }
{

}

0 comments on commit 702705b

Please sign in to comment.