Skip to content

Commit

Permalink
added enemies sway
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 8, 2024
1 parent f490201 commit c3b97a9
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 18 deletions.
1 change: 1 addition & 0 deletions FluffyEngine/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Fluffy
OnCollisionExit,
OnBulletHit,
RemoveLevelStartText,
OnEnemiesIdleStart,
};

class Event
Expand Down
5 changes: 5 additions & 0 deletions FluffyEngine/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ namespace Fluffy
return m_LocalTransform.GetPosition();
}

glm::vec2 GameObject::GetLocalPosition() const
{
return m_LocalTransform.GetPosition();
}

void GameObject::SetWorldPosition(const float x, const float y)
{
SetWorldPosition(glm::vec2(x, y));
Expand Down
2 changes: 2 additions & 0 deletions FluffyEngine/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ namespace Fluffy
inline void SetCurrentScene(class Scene* pScene) { m_pScene = pScene; }

void SetParent(GameObject* pParent, const bool keepWorldPosition);
inline GameObject* GetParent() { return m_pParent; }

inline Transform& GetTransform() { return m_LocalTransform; }
inline const Transform& GetTransform() const { return m_LocalTransform; }
glm::vec2 GetWorldPosition() const;
glm::vec2 GetLocalPosition() const;
void SetWorldPosition(const float x, const float y);
void SetWorldPosition(const glm::vec2& position);
void SetLocalPosition(const float x, const float y);
Expand Down
20 changes: 10 additions & 10 deletions FluffyEngine/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ namespace Fluffy

}

void Transform::SetPosition(const float x, const float y)
void Transform::Translate(const glm::vec2& translation)
{
m_Position.x = x;
m_Position.y = y;
m_Position += translation;
}

void Transform::SetPosition(const glm::vec2& position)
void Transform::Translate(const float x, const float y)
{
m_Position = position;
m_Position.x += x;
m_Position.y += y;
}

void Transform::Translate(const glm::vec2& translation)
void Transform::SetPosition(const float x, const float y)
{
m_Position += translation;
m_Position.x = x;
m_Position.y = y;
}

void Transform::Translate(const float x, const float y)
void Transform::SetPosition(const glm::vec2& position)
{
m_Position.x += x;
m_Position.y += y;
m_Position = position;
}
}
9 changes: 7 additions & 2 deletions FluffyEngine/Transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

namespace Fluffy
{
class GameObject;

class Transform final
{
friend GameObject;

public:
Transform() = default;
Transform(const glm::vec2& position);
inline const glm::vec2& GetPosition() const { return m_Position; }
void SetPosition(const float x, const float y);
void SetPosition(const glm::vec2& position);
void Translate(const glm::vec2& translation);
void Translate(const float x, const float y);

private:
glm::vec2 m_Position;

void SetPosition(const float x, const float y);
void SetPosition(const glm::vec2& position);
};
}
6 changes: 6 additions & 0 deletions Galaga/CharactersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ void CharactersManager::CreatePlayerCharacters()
pPlayerComponent->GetOnDeath().AddListener(this);

m_PlayerCharacters.push_back(pPlayerComponent);

std::shared_ptr<Fluffy::GameObject> pEnemiesSquadron{ std::make_shared<Fluffy::GameObject>(SCREEN_SIZE / 2.0f) };
m_pCurrentScene->Add(pEnemiesSquadron);
m_pEnemiesSquadron = pEnemiesSquadron.get();
}

EnemyCharacter* CharactersManager::SpawnEnemy(const EnemyEnteringData& data)
{
std::shared_ptr<Fluffy::GameObject> pEnemy{ std::make_shared<Fluffy::GameObject>(data.path.GetStartPoint()) };
pEnemy->SetParent(m_pEnemiesSquadron, true);

std::string fileName;

switch (data.type)
Expand Down
3 changes: 3 additions & 0 deletions Galaga/CharactersManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Fluffy
{
struct IEventParam;
class GameObject;
class Scene;
}

Expand All @@ -27,6 +28,7 @@ class CharactersManager final : public Fluffy::Singleton<CharactersManager>, pub
void OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam* param) override;

class PlayerCharacter* GetPlayer(const int playerIndex);
Fluffy::GameObject* GetEnemiesSquadron() const { return m_pEnemiesSquadron; }
inline bool AreAllEnemiesDead() const { return m_Enemies.empty(); }

inline Fluffy::Event& GetOnPlayerKilled() { return m_OnPlayerKilled; }
Expand All @@ -43,4 +45,5 @@ class CharactersManager final : public Fluffy::Singleton<CharactersManager>, pub
std::vector<class EnemyCharacter*> m_Enemies{};

Fluffy::Scene* m_pCurrentScene{};
Fluffy::GameObject* m_pEnemiesSquadron{};
};
41 changes: 41 additions & 0 deletions Galaga/EnemiesSquadron.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "EnemiesSquadron.h"
#include "GameObject.h"
#include "Event.h"

void EnemiesSquadron::Initialize(Fluffy::GameObject* pSquadronObject, Fluffy::Event& onEnemiesIdleStartEvent)
{
m_pEnemiesSquadron = pSquadronObject;
m_StartingPos = pSquadronObject->GetWorldPosition();

onEnemiesIdleStartEvent.AddListener(this);
}

void EnemiesSquadron::Update(const float deltaTime)
{
if (!m_ShouldSway)
return;

m_pEnemiesSquadron->GetTransform().Translate(m_CurrentDirection * m_SwaySpeed * deltaTime, 0.0f);

const float swayDistance{ m_pEnemiesSquadron->GetWorldPosition().x - m_StartingPos.x };

// squared for absolute distance
if (swayDistance * swayDistance >= m_MaxSwayDistance * m_MaxSwayDistance)
m_CurrentDirection *= -1.0f;
}

void EnemiesSquadron::Reset()
{
m_pEnemiesSquadron->SetWorldPosition(m_StartingPos);
m_ShouldSway = false;
}

void EnemiesSquadron::OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam* /*= nullptr*/)
{
switch (eventType)
{
case Fluffy::EventType::OnEnemiesIdleStart:
m_ShouldSway = true;
break;
}
}
32 changes: 32 additions & 0 deletions Galaga/EnemiesSquadron.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include "Event.h"
#include "IEventListener.h"
#include "glm\glm.hpp"

namespace Fluffy
{
class GameObject;
struct IEventParam;
}

class EnemiesSquadron final : public Fluffy::IEventListener
{
public:
inline bool IsInitialized() const { return m_pEnemiesSquadron != nullptr; }
void Initialize(Fluffy::GameObject* pSquadronObject, Fluffy::Event& onEnemiesIdleStartEvent);
inline void SetMaxSwayDistance(const float maxSwayDistance) { m_MaxSwayDistance = maxSwayDistance; }
void Update(const float deltaTime);
void Reset();

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

private:
Fluffy::GameObject* m_pEnemiesSquadron{};
glm::vec2 m_StartingPos{};
float m_MaxSwayDistance{};

const float m_SwaySpeed{ 50.0f };
float m_CurrentDirection{ 1.0f };

bool m_ShouldSway{ false };
};
26 changes: 21 additions & 5 deletions Galaga/EnemyStates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void EnemyEnteringState::Update(const float deltaTime)
else
{
// We arrived!
pOwnerObject->GetTransform().SetPosition(curveEndPoint);
pOwnerObject->SetWorldPosition(curveEndPoint);
m_pOwner->SetState(EnemyStates::Idle);
}
}
Expand Down Expand Up @@ -71,20 +71,30 @@ void EnemyDivingState::OnEnter()
{
m_CurrentPathCurveIndex = 0;

m_Squadron = m_pOwner->GetGameObject()->GetParent();
m_StartPosition = m_pOwner->GetGameObject()->GetWorldPosition();
m_StartPositionInSquadron = m_pOwner->GetGameObject()->GetLocalPosition();

m_pOwner->GetGameObject()->SetParent(nullptr, true);

const glm::vec2 playerPosition{ CharactersManager::GetInstance().GetPlayer(0)->GetGameObject()->GetWorldPosition() };
const glm::vec2 shootDirection{ glm::normalize(playerPosition - m_StartPosition) };
const glm::vec2 shootDirection{ glm::normalize(playerPosition - m_StartPositionInSquadron) };

BulletsManager::GetInstance().Shoot(INVALID_PLAYER_INDEX, m_StartPosition, shootDirection);
BulletsManager::GetInstance().Shoot(INVALID_PLAYER_INDEX, m_StartPositionInSquadron, shootDirection);
}

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

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

const glm::vec2 curveEndPoint
{
m_IsReturningToStartPos ?
m_StartPositionInSquadron + m_Squadron->GetWorldPosition() :
m_StartPosition + path.GetCurve(m_CurrentPathCurveIndex).GetEndPoint()
};

const glm::vec2 distance{ curveEndPoint - pOwnerObject->GetWorldPosition() };
pOwnerObject->GetTransform().Translate(glm::normalize(distance) * (m_pOwner->GetSpeed() * deltaTime));
Expand All @@ -98,11 +108,17 @@ void EnemyDivingState::Update(const float deltaTime)
if (path.HasCurve(m_CurrentPathCurveIndex + 1))
{
++m_CurrentPathCurveIndex;
m_IsReturningToStartPos = path.GetCurve(m_CurrentPathCurveIndex).GetEndPoint() == glm::vec2{ 0.0f, 0.0f };
}
else
{
// We arrived!
pOwnerObject->GetTransform().SetPosition(curveEndPoint);
pOwnerObject->SetWorldPosition(curveEndPoint);
m_pOwner->SetState(EnemyStates::Idle);
}
}

void EnemyDivingState::OnExit()
{
m_pOwner->GetGameObject()->SetParent(m_Squadron, true);
}
9 changes: 9 additions & 0 deletions Galaga/EnemyStates.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ enum class EnemyStates
Diving,
};

namespace Fluffy
{
class GameObject;
}

class EnemyEnteringState final : public FSMState
{
public:
Expand Down Expand Up @@ -46,10 +51,14 @@ class EnemyDivingState final : public FSMState
~EnemyDivingState() {};
void OnEnter() override;
void Update(const float deltaTime) override;
void OnExit() override;
inline EnemyStates GetType() const { return EnemyStates::Diving; }
private:
class EnemyCharacter* const m_pOwner;

Fluffy::GameObject* m_Squadron{};
glm::vec2 m_StartPosition{};
glm::vec2 m_StartPositionInSquadron{};
int m_CurrentPathCurveIndex{ 0 };
bool m_IsReturningToStartPos{ false };
};
2 changes: 2 additions & 0 deletions Galaga/Galaga.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<ClCompile Include="BulletsManager.cpp" />
<ClCompile Include="Character.cpp" />
<ClCompile Include="CharactersManager.cpp" />
<ClCompile Include="EnemiesSquadron.cpp" />
<ClCompile Include="EnemyCharacter.cpp" />
<ClCompile Include="FiniteStateMachine.cpp" />
<ClCompile Include="FPSCounter.cpp" />
Expand Down Expand Up @@ -196,6 +197,7 @@
<ClInclude Include="Character.h" />
<ClInclude Include="CharactersManager.h" />
<ClInclude Include="CollisionLayers.h" />
<ClInclude Include="EnemiesSquadron.h" />
<ClInclude Include="EnemyCharacter.h" />
<ClInclude Include="EventParams.h" />
<ClInclude Include="FiniteStateMachine.h" />
Expand Down
6 changes: 6 additions & 0 deletions Galaga/Galaga.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
<ClCompile Include="BackgroundScroller.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="EnemiesSquadron.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AchievementComponent.h">
Expand Down Expand Up @@ -170,5 +173,8 @@
<ClInclude Include="BackgroundScroller.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EnemiesSquadron.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
21 changes: 20 additions & 1 deletion Galaga/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ GameManager::~GameManager()

GameEvents::OnPlayerShoot.RemoveListener(this);
GameEvents::OnBulletHit.RemoveListener(this);

m_ActiveLevel.GetOnEnemiesIdleStartEvent().RemoveListener(this);
}

void GameManager::StartLevel(const int levelIndex)
{
m_CurrentLevelIndex = levelIndex;

const float screenMidPoint{ SCREEN_SIZE.x / 2.0f };
float biggestEnemyDistanceFromCenter{ 0.0f };

CharactersManager::GetInstance().CreatePlayerCharacters();

if (m_EnemiesSquadron.IsInitialized())
m_EnemiesSquadron.Reset();
else
m_EnemiesSquadron.Initialize(CharactersManager::GetInstance().GetEnemiesSquadron(), m_ActiveLevel.GetOnEnemiesIdleStartEvent());

std::vector<EnemyEnteringData> enemiesData;
Parser::GetInstance().ParseEnemyLayoutData(std::format("D:/Repos/FluffyEngine/Data/Formations/galaga_level_{}.csv", levelIndex), enemiesData);
std::ranges::sort(enemiesData, [](const auto& a, const auto& b) { return a.time < b.time; });
Expand All @@ -62,9 +72,17 @@ void GameManager::StartLevel(const int levelIndex)
const glm::vec2& spawnPos{ int(data.time) % 2 == 0 ? spawnPosLeft : spawnPosRight };
#endif
data.path.AddCurve({ spawnPos, spawnPos, data.position, data.position }, 1);

const float distanceFromCenter{ data.position.x - screenMidPoint };

if (distanceFromCenter * distanceFromCenter > biggestEnemyDistanceFromCenter * biggestEnemyDistanceFromCenter)
biggestEnemyDistanceFromCenter = distanceFromCenter;
}

std::queue<EnemyEnteringData> enemiesQueue{ };
const float estimatedEnemyHalfWidth{ 16.0f };
m_EnemiesSquadron.SetMaxSwayDistance(screenMidPoint - std::abs(biggestEnemyDistanceFromCenter) - estimatedEnemyHalfWidth);

std::queue<EnemyEnteringData> enemiesQueue{};
std::ranges::for_each(enemiesData, [&enemiesQueue](const auto& data) { enemiesQueue.push(data); });

m_ActiveLevel.StartLevel(enemiesQueue);
Expand All @@ -91,6 +109,7 @@ void GameManager::Update(const float deltaTime)
{
// if the level has started
m_ActiveLevel.Update(deltaTime);
m_EnemiesSquadron.Update(deltaTime);

UpdatePlayerRespawnTimers(deltaTime);

Expand Down
Loading

0 comments on commit c3b97a9

Please sign in to comment.