0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

broken commit

This commit is contained in:
ezveee 2024-10-12 20:23:18 +02:00
parent 2ecd56fad9
commit 476ee5cd7a
16 changed files with 118 additions and 18 deletions

View File

@ -26,4 +26,6 @@ public:
void removeCollision();
void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider);
std::string componentName() override { return "ColliderComponent"; }
};

View File

@ -1,5 +1,7 @@
#pragma once
#include <string>
class Entity;
class Component
@ -11,5 +13,7 @@ public:
virtual void update() {}
virtual void draw() {}
virtual std::string componentName() = 0;
virtual ~Component() = default;
};

View File

@ -22,6 +22,7 @@ public:
void refreshHearts();
void createHeartComponents(int x);
std::string componentName() override { return "HealthComponent"; }
private:

View File

@ -3,16 +3,38 @@
#include <functional>
#include <vector>
#include <string>
#include "Key.h"
#include "InputActionType.h"
struct InputAction
{
std::string name;
std::vector<Key> keys;
std::function<void()> callback;
InputActionType type;
InputValue value;
InputAction() = default;
InputAction(const std::string& actionName, std::function<void()> actionCallback)
: name(actionName), callback(actionCallback) {}
InputAction(const std::string& actionName, std::function<void()> actionCallback, InputActionType actionType)
: name(actionName), callback(actionCallback), type(actionType)
{
switch (type)
{
case InputActionType::Boolean:
value = false;
break;
case InputActionType::Float:
value = 0.0f;
break;
default:
value = false;
break;
}
}
};

11
include/InputActionType.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <variant>
using InputValue = std::variant<bool, float>;
enum class InputActionType
{
Boolean,
Float
};

View File

@ -17,6 +17,8 @@ public:
bool isKeyDown(Key key);
std::string componentName() override { return "InputComponent"; }
private:
const Uint8* m_keyStates;
SDL_Scancode mapKeyToSDL(Key key);

View File

@ -2,11 +2,13 @@
#include <map>
#include <vector>
#include <string>
#include <functional>
#include "Component.h"
#include "InputComponent.h"
#include "InputAction.h"
#include "InputActionType.h"
class InputSystemComponent : public InputComponent
{
@ -17,19 +19,19 @@ public:
void init() override;
void update() override;
void bindAction(const std::string& actionName, Key key, std::function<void()> callback);
void bindAction(const std::string& actionName, std::vector<Key> keys, std::function<void()> callback);
void bindAction(const std::string& actionName, Key key, std::function<void()> callback, InputActionType type);
void bindAction(const std::string& actionName, std::vector<Key> keys, std::function<void()> callback, InputActionType type);
// template<typename... Keys>
// void bindAction(const std::string& actionName, std::function<void()> callback, Keys... keys)
// {
// static_assert((std::is_same<Keys, Key>::value && ...), "A passed argument for 'Keys' is not of type 'Key'");
//
// if (m_actions.find(actionName) == m_actions.end())
// {
// m_actions[actionName] = InputAction(actionName, callback);
// }
//
// (m_actions[actionName].keys.push_back(keys), ...);
// (m_keyToActionsMap[keys].push_back(m_actions[actionName]), ...);
// }
@ -41,7 +43,7 @@ public:
// void unbindAction(const std::string& actionName, Keys... keys)
// {
// static_assert((std::is_same<Keys, Key>::value && ...), "A passed argument for 'Keys' is not of type 'Key'");
//
// auto actionIt = m_actions.find(actionName);
// if (actionIt != m_actions.end())
// {
@ -52,10 +54,16 @@ public:
// }
// }
InputValue getValue(const std::string& actionName);
std::string componentName() override { return "InputSystemComponent"; }
private:
InputComponent* m_inputComponent;
std::map<Key, std::vector<InputAction>> m_keyToActionsMap;
std::map<std::string, InputAction> m_actions;
std::map<Key, std::vector<std::reference_wrapper<InputAction>>> m_keyToActionsMap;
float m_floatChangeRate = 0.1f;
void handleActions();
};

View File

@ -1,9 +1,12 @@
#pragma once
#include "Component.h"
#include <string>
class PlayerComponent : public Component
{
public:
std::string componentName() override { return "PlayerComponent"; }
private:
};

View File

@ -11,6 +11,8 @@ public:
void update() override;
std::string componentName() override { return "PowerupComponent"; }
private:
std::function<void (Entity*)> pickupFunc;
};

View File

@ -17,6 +17,8 @@ public:
void init() override;
void update() override;
std::string componentName() override { return "ProjectileComponent"; }
private:
TransformComponent* transformComponent;

View File

@ -18,6 +18,8 @@ public:
std::map<std::string, std::unique_ptr<Animation>>* animations = nullptr;
std::string componentName() override { return "SpriteComponent"; }
private:
TransformComponent* transform;
SDL_Texture* texture;

View File

@ -23,6 +23,8 @@ public:
void modifyStatValue(Stats stat, int modifier);
void resetStatValue(Stats stat);
std::string componentName() override { return "StatEffectsComponent"; }
private:
std::array<int, MAX_STATS> buffs = { 0 };
};

View File

@ -27,6 +27,9 @@ public:
bool hasCollision(){return this->collision;}
std::string getName(){return this->tileName;}
std::string componentName() override { return "TileComponent"; }
private:
bool collision;
std::string tileName;

View File

@ -28,6 +28,8 @@ public:
void setPositionAfterCollision(Vector2D& positionChange);
void modifySpeed(int8_t modifier);
std::string componentName() override { return "TransformComponent"; }
private:
int speed = 3;
int speedMod = 0;

View File

@ -6,7 +6,10 @@
void Entity::update() const
{
for (auto const& c : components) c->update();
for (auto const& c : components){
std::cout << "whoop info: " << c->componentName() << std::endl;
c->update();
}
}
void Entity::draw() const

View File

@ -12,11 +12,11 @@ void InputSystemComponent::update()
}
// alternative to bindAction template
void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function<void()> callback)
void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function<void()> callback, InputActionType type)
{
if (m_actions.find(actionName) == m_actions.end())
{
m_actions[actionName] = InputAction(actionName, callback);
m_actions[actionName] = InputAction(actionName, callback, type);
}
m_actions[actionName].keys.push_back(key);
@ -25,11 +25,11 @@ void InputSystemComponent::bindAction(const std::string& actionName, Key key, st
}
// alternative to bindAction template
void InputSystemComponent::bindAction(const std::string& actionName, std::vector<Key> keys, std::function<void()> callback)
void InputSystemComponent::bindAction(const std::string& actionName, std::vector<Key> keys, std::function<void()> callback, InputActionType type)
{
if (m_actions.find(actionName) == m_actions.end())
{
m_actions[actionName] = InputAction(actionName, callback);
m_actions[actionName] = InputAction(actionName, callback, type);
}
for (Key key : keys)
@ -73,15 +73,46 @@ void InputSystemComponent::handleActions()
for (auto& keyActionsPair : m_keyToActionsMap)
{
Key key = keyActionsPair.first;
if (isKeyDown(key))
{
bool keyDown = isKeyDown(key);
for (auto& action : keyActionsPair.second)
{
if (action.callback)
switch (action.get().type)
{
action.callback();
case InputActionType::Boolean:
std::get<bool>(action.get().value) = keyDown;
break;
case InputActionType::Float:
float& floatValue = std::get<float>(action.get().value);
if (keyDown && floatValue < 1.0f)
{
floatValue += m_floatChangeRate;
}
else
{
if (floatValue > 0.1f)
{
floatValue -= m_floatChangeRate;
}
}
break;
}
if (keyDown && action.get().callback)
{
action.get().callback();
}
}
}
}
InputValue InputSystemComponent::getValue(const std::string& actionName)
{
if (m_actions.find(actionName) != m_actions.end())
{
return m_actions[actionName].value;
}
return InputValue();
}