mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
parent
2e7a1b45cf
commit
aa83280b6a
@ -11,8 +11,9 @@
|
||||
|
||||
// TODO: remove here if possible
|
||||
// temporary fix: addComponent function template doesnt know TransformComponent -> error undefined type
|
||||
// #include "KeyboardController.h"
|
||||
#include "InputComponent.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "KeyboardController.h"
|
||||
#include "SpriteComponent.h"
|
||||
|
||||
class Manager;
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
#pragma once
|
||||
#include <SDL.h>
|
||||
#include <map>
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
enum class Key
|
||||
{
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
FIRE,
|
||||
SPACE,
|
||||
ENTER,
|
||||
ESCAPE,
|
||||
TAB,
|
||||
@ -84,13 +87,16 @@ enum class Key
|
||||
GRAVE
|
||||
};
|
||||
|
||||
class Input
|
||||
class InputComponent : public Component
|
||||
{
|
||||
public:
|
||||
Input();
|
||||
~Input();
|
||||
InputComponent();
|
||||
~InputComponent();
|
||||
|
||||
void pollEvents();
|
||||
void init() override;
|
||||
void update() override;
|
||||
|
||||
// void pollEvents();
|
||||
bool isKeyDown(Key key);
|
||||
|
||||
private:
|
||||
@ -1,40 +0,0 @@
|
||||
#pragma once
|
||||
#include <SDL.h>
|
||||
|
||||
#include "Component.h"
|
||||
#include "Vector2D.h"
|
||||
#include "Constants.h" // TODO: change so gamespecific constants are in own file
|
||||
#include "SoundManager.h"
|
||||
#include "Input.h"
|
||||
|
||||
class TransformComponent;
|
||||
class SpriteComponent;
|
||||
|
||||
class KeyboardController : public Component
|
||||
{
|
||||
public:
|
||||
KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity);
|
||||
~KeyboardController() = default;
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void modifyAtkSpeed(int8_t modifier);
|
||||
|
||||
private:
|
||||
Input* m_input;
|
||||
Key m_up;
|
||||
Key m_down;
|
||||
Key m_left;
|
||||
Key m_right;
|
||||
Key m_fire;
|
||||
|
||||
TransformComponent* m_transform;
|
||||
SpriteComponent* m_sprite;
|
||||
|
||||
TransformComponent* m_player; //for starting position of projectile
|
||||
Vector2D m_fireVelocity; //decide source of projectile and flying direction
|
||||
|
||||
//for attack cooldown in between shots
|
||||
uint32_t m_lastFireTime = 0;
|
||||
uint32_t m_fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
|
||||
};
|
||||
12
src/Game.cpp
12
src/Game.cpp
@ -5,7 +5,7 @@
|
||||
#include "CollisionHandler.h"
|
||||
#include "AssetManager.h"
|
||||
#include "SoundManager.h"
|
||||
#include "KeyboardController.h"
|
||||
// #include "KeyboardController.h"
|
||||
#include "TileComponent.h"
|
||||
#include "Direction.h"
|
||||
#include "Entity.h"
|
||||
@ -135,7 +135,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
return;
|
||||
}
|
||||
|
||||
engine::init();
|
||||
// engine::init(); // temporarily moved down to access groups at engine init call
|
||||
|
||||
// character selection
|
||||
const char* player1Sprite;
|
||||
@ -167,7 +167,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
player1.setTeam(Entity::TeamLabel::BLUE);
|
||||
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
||||
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
|
||||
player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added
|
||||
// player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added
|
||||
player1.addComponent<InputComponent>();
|
||||
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
|
||||
player1.addComponent<HealthComponent>(5, Direction::LEFT);
|
||||
player1.addComponent<StatEffectsComponent>();
|
||||
@ -177,11 +178,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
player2.setTeam(Entity::TeamLabel::RED);
|
||||
player2.addComponent<TransformComponent>(600, 500, 2);
|
||||
player2.addComponent<SpriteComponent>(player2Sprite, true);
|
||||
player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
|
||||
// player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
|
||||
player2.addComponent<InputComponent>();
|
||||
player2.addComponent<ColliderComponent>("enemy", 0.8f);
|
||||
player2.addComponent<HealthComponent>(5, Direction::RIGHT);
|
||||
player2.addComponent<StatEffectsComponent>();
|
||||
player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
|
||||
|
||||
engine::init();
|
||||
}
|
||||
|
||||
void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)
|
||||
|
||||
@ -1,24 +1,30 @@
|
||||
#include "Input.h"
|
||||
#include "InputComponent.h"
|
||||
|
||||
Input::Input()
|
||||
InputComponent::InputComponent()
|
||||
{
|
||||
m_keyStates = SDL_GetKeyboardState(NULL);
|
||||
InitKeyMappings();
|
||||
}
|
||||
|
||||
Input::~Input() = default;
|
||||
InputComponent::~InputComponent() = default;
|
||||
|
||||
void Input::pollEvents()
|
||||
void InputComponent::init()
|
||||
{
|
||||
// m_keyStates = SDL_GetKeyboardState(NULL);
|
||||
// InitKeyMappings();
|
||||
}
|
||||
|
||||
void InputComponent::update()
|
||||
{
|
||||
SDL_PumpEvents();
|
||||
}
|
||||
|
||||
bool Input::isKeyDown(Key key)
|
||||
bool InputComponent::isKeyDown(Key key)
|
||||
{
|
||||
return m_keyStates[mapKeyToSDL(key)];
|
||||
}
|
||||
|
||||
SDL_Scancode Input::mapKeyToSDL(Key key)
|
||||
SDL_Scancode InputComponent::mapKeyToSDL(Key key)
|
||||
{
|
||||
auto it = m_keyMappings.find(key);
|
||||
if (it == m_keyMappings.end())
|
||||
@ -28,7 +34,7 @@ SDL_Scancode Input::mapKeyToSDL(Key key)
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void Input::InitKeyMappings()
|
||||
void InputComponent::InitKeyMappings()
|
||||
{
|
||||
m_keyMappings =
|
||||
{
|
||||
@ -36,7 +42,7 @@ void Input::InitKeyMappings()
|
||||
{Key::DOWN, SDL_SCANCODE_DOWN},
|
||||
{Key::LEFT, SDL_SCANCODE_LEFT},
|
||||
{Key::RIGHT, SDL_SCANCODE_RIGHT},
|
||||
{Key::FIRE, SDL_SCANCODE_SPACE},
|
||||
{Key::SPACE, SDL_SCANCODE_SPACE},
|
||||
{Key::ENTER, SDL_SCANCODE_RETURN},
|
||||
{Key::ESCAPE, SDL_SCANCODE_ESCAPE},
|
||||
{Key::TAB, SDL_SCANCODE_TAB},
|
||||
@ -1,86 +0,0 @@
|
||||
#include "KeyboardController.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "AssetManager.h"
|
||||
#include "SpriteComponent.h"
|
||||
|
||||
KeyboardController::KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity)
|
||||
: m_input(input), m_up(up), m_down(down), m_left(left), m_right(right), m_fire(fire) {}
|
||||
|
||||
void KeyboardController::init()
|
||||
{
|
||||
m_sprite = &entity->getComponent<SpriteComponent>();
|
||||
m_transform = &entity->getComponent<TransformComponent>();
|
||||
}
|
||||
|
||||
void KeyboardController::update()
|
||||
{
|
||||
m_transform->direction.x = 0;
|
||||
m_transform->direction.y = 0;
|
||||
m_sprite->playAnimation(IDLE);
|
||||
|
||||
if (m_input->isKeyDown(m_left))
|
||||
{
|
||||
m_transform->direction.x = -1;
|
||||
m_sprite->playAnimation(WALK);
|
||||
m_sprite->setDirection(Direction::LEFT);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
|
||||
}
|
||||
|
||||
if (m_input->isKeyDown(m_right))
|
||||
{
|
||||
m_transform->direction.x = 1;
|
||||
m_sprite->playAnimation(WALK);
|
||||
m_sprite->setDirection(Direction::RIGHT);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
|
||||
}
|
||||
|
||||
if (m_input->isKeyDown(m_up))
|
||||
{
|
||||
m_transform->direction.y = -1;
|
||||
m_sprite->playAnimation(WALK);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
|
||||
}
|
||||
|
||||
if (m_input->isKeyDown(m_down))
|
||||
{
|
||||
m_transform->direction.y = 1;
|
||||
m_sprite->playAnimation(WALK);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
|
||||
}
|
||||
|
||||
if (m_input->isKeyDown(m_fire))
|
||||
{
|
||||
Uint32 currentTicks = SDL_GetTicks();
|
||||
|
||||
if (currentTicks - m_lastFireTime >= m_fireCooldown)
|
||||
{
|
||||
|
||||
m_player = &entity->getComponent<TransformComponent>();
|
||||
|
||||
//checks player source via the firing velocity
|
||||
//TODO: adding actual projectile textures
|
||||
if (m_fireVelocity.x > 0)
|
||||
{
|
||||
m_sprite->setDirection(Direction::RIGHT);
|
||||
this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity,
|
||||
1, 180, 2, "assets/egg.png", this->entity->getTeam());
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
m_sprite->setDirection(Direction::LEFT);
|
||||
this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity,
|
||||
1, 180, 2, "assets/egg.png", this->entity->getTeam());
|
||||
}
|
||||
|
||||
m_lastFireTime = currentTicks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardController::modifyAtkSpeed(int8_t modifier)
|
||||
{
|
||||
this->m_fireCooldown -= modifier * 400;
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
#include "StatEffectsComponent.h"
|
||||
#include "Entity.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "KeyboardController.h"
|
||||
// #include "KeyboardController.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
@ -35,7 +35,7 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier
|
||||
this->entity->getComponent<TransformComponent>().modifySpeed(modifier);
|
||||
break;
|
||||
case Stats::ATTACK_SPEED:
|
||||
this->entity->getComponent<KeyboardController>().modifyAtkSpeed(modifier);
|
||||
// this->entity->getComponent<KeyboardController>().modifyAtkSpeed(modifier);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user