Skip to content

Commit

Permalink
stable version w/ score, shooting and player respawn
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 5, 2024
1 parent c5788e6 commit f5a5eba
Show file tree
Hide file tree
Showing 22 changed files with 872 additions and 42 deletions.
24 changes: 24 additions & 0 deletions FluffyEngine/Singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ namespace Fluffy
protected:
Singleton() = default;
};

template <typename T>
class ComponentSingleton
{
public:
inline static T* GetInstance() { return m_pInstance; }

virtual ~ComponentSingleton()
{
if (m_pInstance == this)
m_pInstance = nullptr;
}

ComponentSingleton(const ComponentSingleton& other) = delete;
ComponentSingleton(ComponentSingleton&& other) = delete;
ComponentSingleton& operator=(const ComponentSingleton& other) = delete;
ComponentSingleton& operator=(ComponentSingleton&& other) = delete;

protected:
inline static T* m_pInstance{ nullptr };

ComponentSingleton() = default;
virtual void RegisterInstance() = 0;
};
}
99 changes: 99 additions & 0 deletions Galaga/Blackboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#pragma once
#include <string>
#include <unordered_map>
#include <stdexcept>

#pragma region BlackboardTypes

static const std::string OWNER_PARAM{ "Owner" };

#pragma endregion

class IBlackBoardField
{
public:
IBlackBoardField() = default;
virtual ~IBlackBoardField() = default;
};

//BlackboardField does not take ownership of pointers whatsoever!
template<typename T>
class BlackboardField : public IBlackBoardField
{
public:
explicit BlackboardField(const std::string& name, const T& data) : m_Name{ name }, m_Data{ data } { }

inline const std::string& GetName() const { return m_Name; }
inline const T& GetData() const { return m_Data; };
inline void SetData(const T& data) { m_Data = data; }

private:
const std::string m_Name;
T m_Data;
};

class Blackboard final
{
public:
Blackboard() = default;
~Blackboard()
{
for (auto& el : m_BlackboardData)
delete el.second;
m_BlackboardData.clear();
}

Blackboard(const Blackboard& other) = delete;
Blackboard& operator=(const Blackboard& other) = delete;
Blackboard(Blackboard&& other) = delete;
Blackboard& operator=(Blackboard&& other) = delete;

template<typename T>
bool GetData(const std::string& name, T& data) const
{
const auto& it = m_BlackboardData.find(name);
if (it != m_BlackboardData.end())
{
if (const BlackboardField<T>* p = dynamic_cast<BlackboardField<T>*>((*it).second))
{
data = p->GetData();
return true;
}
}

return false;
}

template<typename T>
bool SetData(const std::string& name, const T& data)
{
auto it = m_BlackboardData.find(name);
if (it == m_BlackboardData.end())
{
m_BlackboardData[name] = new BlackboardField<T>(name, data);
return true;
}

if (auto* p = dynamic_cast<BlackboardField<T>*>((*it).second))
{
p->SetData(data);
return true;
}

return false;
}

bool GetBoolData(const std::string& name) const
{
bool value;
return GetData(name, value) ? value : false;
}

void RemoveData(const std::string& name)
{
m_BlackboardData.erase(name);
}

private:
std::unordered_map<std::string, IBlackBoardField*> m_BlackboardData;
};
28 changes: 27 additions & 1 deletion Galaga/Character.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "Character.h"
#include "IEventParam.h"
#include "EventParams.h"
#include "GameObject.h"
#include "Sprite.h"
#include "SpriteColliderComponent.h"
#include "PlayerCharacter.h"

Character::Character(Fluffy::GameObject* pOwner, int livesCount)
: Fluffy::Component(pOwner)
Expand All @@ -8,10 +13,31 @@ Character::Character(Fluffy::GameObject* pOwner, int livesCount)

}

void Character::Start()
{
Fluffy::Sprite* pSpriteComponent{ m_pOwner->GetComponent<Fluffy::Sprite>() };
Fluffy::SpriteColliderComponent* pCollider{ m_pOwner->AddComponent<Fluffy::SpriteColliderComponent>(pSpriteComponent, GetCollisionLayer()) };
pCollider->GetOnCollisionEnter().AddListener(this);

m_SpriteSize = pSpriteComponent->GetTextureSize();
}

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

const Fluffy::OnCharacterDeathParam eventParam{ killerIndex };
const CharacterDeathParam eventParam{ this, killerIndex };
m_OnDeath.Invoke(&eventParam);
}

void Character::OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam* pParam)
{
switch (eventType)
{
case Fluffy::EventType::OnCollisionEnter:
if (const Fluffy::CollisionParam* pCollisionParam{ static_cast<const Fluffy::CollisionParam*>(pParam) })
OnCollisionEnter(pCollisionParam->GetOtherCollider()->GetGameObject());

break;
}
}
25 changes: 21 additions & 4 deletions Galaga/Character.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
#pragma once
#include "Component.h"
#include "Event.h"
#include "IEventListener.h"
#include <string>
#include <glm\glm.hpp>

namespace Fluffy
{
class GameObject;
class Event;
class SpriteColliderComponent;
struct IEventParam;
}
class Character : public Fluffy::Component

const int INVALID_PLAYER_INDEX{ -1 };

class Character : public Fluffy::Component, public Fluffy::IEventListener
{
public:
virtual ~Character() = default;

void Start() override;

inline int GetLivesCount() const { return m_LivesCount; }
inline const glm::vec2& GetSpriteSize() const { return m_SpriteSize; }
inline Fluffy::Event& GetOnDamageTaken() { return m_OnDamageTaken; }
inline Fluffy::Event& GetOnDeath() { return m_OnDeath; }

void Kill(int killerIndex = 0);
virtual int GetPlayerIndex() const { return INVALID_PLAYER_INDEX; }
virtual void Kill(int killerIndex = INVALID_PLAYER_INDEX);

void OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam* pParam) override;

protected:
Character(Fluffy::GameObject* pOwner, int livesCount);


virtual const std::string& GetCollisionLayer() const = 0;
virtual void OnCollisionEnter(Fluffy::GameObject* pOtherGameObject) = 0;

glm::vec2 m_SpriteSize{};
int m_LivesCount;

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

Loading

0 comments on commit f5a5eba

Please sign in to comment.