Skip to content

Commit

Permalink
keep player in level bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 5, 2024
1 parent e2f9115 commit 91953f4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Galaga/MoveCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "CharactersManager.h"
#include "PlayerCharacter.h"
#include "GameObject.h"
#include "Structs.h"
#include <iostream>
#include <algorithm>

MoveCommand::MoveCommand(int playerIndex, glm::vec2 direction, float speed)
: m_PlayerIndex(playerIndex)
Expand All @@ -14,12 +17,16 @@ MoveCommand::MoveCommand(int playerIndex, glm::vec2 direction, float speed)

void MoveCommand::Execute()
{
if (PlayerCharacter * pPlayer{ CharactersManager::GetInstance().GetPlayer(m_PlayerIndex) })
if (const PlayerCharacter* pPlayer{ CharactersManager::GetInstance()->GetPlayer(m_PlayerIndex) })
{
if (Fluffy::GameObject * pPlayerGameObject{ pPlayer->GetGameObject() })
if (Fluffy::GameObject* pPlayerGameObject{ pPlayer->GetGameObject() })
{
glm::vec2 position{ pPlayerGameObject->GetWorldPosition() };
position += m_Direction * (m_Speed * Fluffy::FluffyTime::DeltaTime());

const float halfSpriteSize{ pPlayer->GetSpriteSize().x / 2.0f };
position.x = std::clamp(position.x, halfSpriteSize, SCREEN_SIZE.x - halfSpriteSize);

pPlayerGameObject->SetWorldPosition(position);
}
}
Expand Down

0 comments on commit 91953f4

Please sign in to comment.