Skip to content

Commit

Permalink
removed Blackboard from FSMStates and made enemy dives loop infinitely
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 5, 2024
1 parent f5a5eba commit 8b6cd92
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 199 deletions.
99 changes: 0 additions & 99 deletions Galaga/Blackboard.h

This file was deleted.

14 changes: 3 additions & 11 deletions Galaga/CharactersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ void CharactersManager::StartLevel1()
BezierCurve firstCurve{ { -32.0f, -32.0f }, { -32.0f, -32.0f }, { 290.0f, 200.0f }, { 290.0f, 200.0f } };
firstPath.AddCurve(firstCurve, 1);

enemiesData.push({ 4.0f, firstPath });
enemiesData.push({ 4.0f, firstPath, EnemyType::Boss });

BezierPath secondPath{};
BezierCurve secondCurve{ { 640.0f, -32.0f }, { 640.0f, -32.0f }, { 330.0f, 200.0f }, { 330.0f, 200.0f } };
secondPath.AddCurve(secondCurve, 1);

enemiesData.push({ 4.3f, secondPath });
enemiesData.push({ 4.3f, secondPath, EnemyType::Bee });

m_Level.StartLevel(enemiesData);

Expand Down Expand Up @@ -69,14 +69,6 @@ void CharactersManager::CreatePlayerCharacters(Fluffy::Scene& scene)
pPlayer1Component->GetOnDeath().AddListener(this);

m_PlayerCharacters.push_back(pPlayer1Component);

/*std::shared_ptr<Fluffy::GameObject> pPlayer2{ std::make_shared<Fluffy::GameObject>(SCREEN_SIZE.x / 2.0f, SCREEN_SIZE.y - 80.0f) };
pPlayer2->AddComponent<Fluffy::Sprite>("galaga_player2.png");
PlayerCharacter* pPlayer2Component{ pPlayer2->AddComponent<PlayerCharacter>(3, int(m_PlayerCharacters.size())) };
scene.Add(pPlayer2);
m_PlayerCharacters.push_back(pPlayer2Component);*/
}

EnemyCharacter* CharactersManager::SpawnEnemy(const EnemyEnteringData& data)
Expand All @@ -95,7 +87,7 @@ EnemyCharacter* CharactersManager::SpawnEnemy(const EnemyEnteringData& data)
break;

case EnemyType::Bee:
fileName = "galaga_wasp.png";
fileName = "galaga_bee.png";
break;
}

Expand Down
9 changes: 6 additions & 3 deletions Galaga/EnemyCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ EnemyCharacter::EnemyCharacter(Fluffy::GameObject* pOwner, const EnemyType type,
: Character(pOwner, 1)
, m_Type{ type }
, m_Speed{ speed }
, m_EnteringState{ this }
, m_IdleState{ this }
, m_ExitingState{ this }
{
m_FSM.GetBlackboard().SetData(OWNER_PARAM, this);

CreateExitingPath();
m_FSM.GetBlackboard().SetData(EXITING_PATH_PARAM, m_ExitingPath);
}

void EnemyCharacter::Update(const float deltaTime)
Expand Down Expand Up @@ -60,5 +60,8 @@ void EnemyCharacter::CreateExitingPath()
{
const BezierCurve lineDown{ { 0.0f, 0.0f }, { 0.0f, 0.0f },
{ 0.0f, 480.0f }, { 0.0f, 480.0f } };
const BezierCurve lineUp{ { 0.0f, 480.0f }, { 0.0f, 480.0f },
{ 0.0f, 0.0f }, { 0.0f, 0.0f } };
m_ExitingPath.AddCurve(lineDown, 1);
m_ExitingPath.AddCurve(lineUp, 1);
}
11 changes: 7 additions & 4 deletions Galaga/EnemyCharacter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class EnemyCharacter final : public Character
void SetState(EnemyStates newState);
FiniteStateMachine& GetFSM() { return m_FSM; }
inline float GetSpeed() const { return m_Speed; }
inline const BezierPath& GetEnteringPath() const { return m_EnteringPath; }
inline const BezierPath& GetExitingPath() const { return m_ExitingPath; }
inline void SetEnteringPath(const BezierPath& path) { m_EnteringPath = path; }

void Kill(int killerIndex = INVALID_PLAYER_INDEX) override;

Expand All @@ -35,18 +38,18 @@ class EnemyCharacter final : public Character
void OnCollisionEnter(Fluffy::GameObject* pOtherGameObject) override;

private:
BezierPath m_EnteringPath{};
BezierPath m_ExitingPath{};

EnemyType m_Type{};
EnemyStates m_State{ EnemyStates::None };
FiniteStateMachine m_FSM{};

EnemyEnteringState m_EnteringState{};
EnemyIdleState m_IdleState{};
EnemyExitingState m_ExitingState{};
EnemyEnteringState m_EnteringState;
EnemyIdleState m_IdleState;
EnemyExitingState m_ExitingState;

float m_Speed{ 10.0f };

void CreateExitingPath();
};

87 changes: 41 additions & 46 deletions Galaga/EnemyStates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,99 @@
#include "EnemyCharacter.h"
#include "GameObject.h"

void EnemyEnteringState::OnEnter(Blackboard& blackboard) const
EnemyEnteringState::EnemyEnteringState(class EnemyCharacter* pOwner)
: m_pOwner(pOwner)
{
blackboard.SetData(CURRENT_PATH_CURVE_INDEX_PARAM, 0);

}

void EnemyEnteringState::Update(Blackboard& blackboard, const float deltaTime) const
void EnemyEnteringState::OnEnter()
{
EnemyCharacter* pOwner;
BezierPath path;
int currentPathCurveIndex;
m_CurrentPathCurveIndex = 0;
}

if (!blackboard.GetData(OWNER_PARAM, pOwner) ||
!blackboard.GetData(ENTERING_PATH_PARAM, path) ||
!blackboard.GetData(CURRENT_PATH_CURVE_INDEX_PARAM, currentPathCurveIndex))
return;
void EnemyEnteringState::Update(const float deltaTime)
{
const BezierPath path{ m_pOwner->GetEnteringPath() };

Fluffy::GameObject* pOwnerObject{ pOwner->GetGameObject() };
const glm::vec2 curveEndPoint{ path.GetCurve(currentPathCurveIndex).GetEndPoint() };
Fluffy::GameObject* pOwnerObject{ m_pOwner->GetGameObject() };
const glm::vec2 curveEndPoint{ path.GetCurve(m_CurrentPathCurveIndex).GetEndPoint() };

const glm::vec2 distance{ curveEndPoint - pOwnerObject->GetWorldPosition() };
pOwnerObject->GetTransform().Translate(glm::normalize(distance) * (pOwner->GetSpeed() * deltaTime));
pOwnerObject->GetTransform().Translate(glm::normalize(distance) * (m_pOwner->GetSpeed() * deltaTime));

const glm::vec2 newDistance{ curveEndPoint - pOwnerObject->GetWorldPosition() };
const float squaredMagnitude{ glm::dot<2, float>(newDistance, newDistance) };

if (squaredMagnitude > 50.0f)
return;

++currentPathCurveIndex;

if (path.HasCurve(currentPathCurveIndex))
if (path.HasCurve(m_CurrentPathCurveIndex + 1))
{
blackboard.SetData(CURRENT_PATH_CURVE_INDEX_PARAM, currentPathCurveIndex);
++m_CurrentPathCurveIndex;
}
else
{
// We arrived!
pOwnerObject->GetTransform().SetPosition(curveEndPoint);
pOwner->SetState(EnemyStates::Idle);
m_pOwner->SetState(EnemyStates::Idle);
}
}

void EnemyIdleState::OnEnter(Blackboard& /*blackboard*/) const
EnemyIdleState::EnemyIdleState(class EnemyCharacter* pOwner)
: m_pOwner(pOwner)
{

}

void EnemyIdleState::OnEnter()
{

}

void EnemyIdleState::Update(Blackboard& /*blackboard*/, const float /*deltaTime*/) const
void EnemyIdleState::Update(const float /*deltaTime*/)
{
// Do the idle movement/formation here
}

void EnemyExitingState::OnEnter(Blackboard& blackboard) const
EnemyExitingState::EnemyExitingState(class EnemyCharacter* pOwner)
: m_pOwner(pOwner)
{
blackboard.SetData(CURRENT_PATH_CURVE_INDEX_PARAM, 0);

EnemyCharacter* pOwner;
if (blackboard.GetData(OWNER_PARAM, pOwner))
{
blackboard.SetData(STARTING_POSITION_PARAM, pOwner->GetGameObject()->GetWorldPosition());
}
}

void EnemyExitingState::Update(Blackboard& blackboard, const float deltaTime) const
void EnemyExitingState::OnEnter()
{
EnemyCharacter* pOwner;
BezierPath path;
glm::vec2 startPosition;
int currentPathCurveIndex;

if (!blackboard.GetData(OWNER_PARAM, pOwner) ||
!blackboard.GetData(EXITING_PATH_PARAM, path) ||
!blackboard.GetData(STARTING_POSITION_PARAM, startPosition) ||
!blackboard.GetData(CURRENT_PATH_CURVE_INDEX_PARAM, currentPathCurveIndex))
return;
m_CurrentPathCurveIndex = 0;

if (m_pOwner != nullptr)
m_StartPosition = m_pOwner->GetGameObject()->GetWorldPosition();
}

void EnemyExitingState::Update(const float deltaTime)
{
const BezierPath path{ m_pOwner->GetExitingPath() };

Fluffy::GameObject* pOwnerObject{ pOwner->GetGameObject() };
const glm::vec2 curveEndPoint{ startPosition + path.GetCurve(currentPathCurveIndex).GetEndPoint() };
Fluffy::GameObject* pOwnerObject{ m_pOwner->GetGameObject() };
const glm::vec2 curveEndPoint{ m_StartPosition + path.GetCurve(m_CurrentPathCurveIndex).GetEndPoint() };

const glm::vec2 distance{ curveEndPoint - pOwnerObject->GetWorldPosition() };
pOwnerObject->GetTransform().Translate(glm::normalize(distance) * (pOwner->GetSpeed() * deltaTime));
pOwnerObject->GetTransform().Translate(glm::normalize(distance) * (m_pOwner->GetSpeed() * deltaTime));

const glm::vec2 newDistance{ curveEndPoint - pOwnerObject->GetWorldPosition() };

// Squared magnitude
if (glm::dot(newDistance, newDistance) > 50.0f)
return;

++currentPathCurveIndex;

if (path.HasCurve(currentPathCurveIndex))
if (path.HasCurve(m_CurrentPathCurveIndex + 1))
{
blackboard.SetData(CURRENT_PATH_CURVE_INDEX_PARAM, currentPathCurveIndex);
++m_CurrentPathCurveIndex;
}
else
{
// We arrived!
pOwnerObject->GetTransform().SetPosition(curveEndPoint);
pOwner->Kill();
m_pOwner->SetState(EnemyStates::Idle);
}
}
Loading

0 comments on commit 8b6cd92

Please sign in to comment.