Skip to content

Commit

Permalink
added WASD key bindings to movement
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Mar 17, 2024
1 parent e8b9d18 commit b80e438
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Minigin/Command.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once
#include <SDL.h>

class Command
{
public:
virtual ~Command() = default;
virtual void Execute(class GameObject*) {};
virtual void Execute(class GameObject*, SDL_Scancode) {};
};
7 changes: 5 additions & 2 deletions Minigin/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

dae::InputManager::~InputManager()
{
//delete command pointers
for (auto& item : m_MappedInputs)
{
delete item.second;
}
}

bool dae::InputManager::ProcessInput()
Expand All @@ -27,7 +30,7 @@ bool dae::InputManager::ProcessInput()
const auto& command = m_MappedInputs.find(e.key.keysym.scancode);

if (command != m_MappedInputs.end())
command->second->Execute(m_pInputReceiver);
command->second->Execute(m_pInputReceiver, command->first);

ImGui_ImplSDL2_ProcessEvent(&e);
}
Expand Down
1 change: 1 addition & 0 deletions Minigin/InputManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "Singleton.h"
#include "MoveCommand.h"
#include <SDL.h>

namespace dae
{
Expand Down
Binary file modified Minigin/Main.cpp
Binary file not shown.
15 changes: 13 additions & 2 deletions Minigin/MoveCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
class MoveCommand final : public Command
{
public:
void Execute(GameObject* pGameObject) override
void Execute(GameObject* pGameObject, SDL_Scancode input) override
{
pGameObject->GetComponentOfType<MoveComponent>()->Move(glm::vec2(0.0f, 1.0f));
glm::vec2 movement{0.0f, 0.0f};

if (input == SDL_SCANCODE_W)
++movement.y;
else if (input == SDL_SCANCODE_A)
++movement.x;
else if (input == SDL_SCANCODE_S)
--movement.y;
else if (input == SDL_SCANCODE_D)
--movement.x;

pGameObject->GetComponentOfType<MoveComponent>()->Move(movement);
}
};
8 changes: 7 additions & 1 deletion Minigin/MoveComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MoveComponent.h"
#include "GameObject.h"
#include <vcruntime_typeinfo.h>
#include <iostream>

MoveComponent::MoveComponent(const std::weak_ptr<class GameObject> pOwner, float verticalSpeed, float horizontalSpeed)
: Component(pOwner)
Expand All @@ -23,5 +24,10 @@ void MoveComponent::Update(const float deltaTime)

void MoveComponent::Move(const glm::vec2& input)
{
m_Movement = glm::vec2((input.x * m_HorizontalSpeed), (input.y * m_VerticalSpeed));
std::cout << "movement:" << std::endl;
std::cout << m_Movement.x << ", " << m_Movement.y << std::endl;

m_Movement += glm::vec2((input.x * m_HorizontalSpeed), (input.y * m_VerticalSpeed));

std::cout << m_Movement.x << ", " << m_Movement.y << std::endl;
}

0 comments on commit b80e438

Please sign in to comment.