added Vector2D and Hitbox

This commit is contained in:
Sarah Faey 2025-05-01 20:46:58 +02:00
parent eabeb55115
commit d5da82f287
13 changed files with 192 additions and 30 deletions

View file

@ -38,7 +38,7 @@ FetchContent_MakeAvailable(SDL3 SDL3_image SDL3_ttf)
set(ACTORS src/Actors/Actor.cpp src/Actors/MovingActor.cpp src/Actors/ActorFactory.cpp) set(ACTORS src/Actors/Actor.cpp src/Actors/MovingActor.cpp src/Actors/ActorFactory.cpp)
#set(COMMANDS src/GameCommands/GameCommand.h src/GameCommands/GameCommand.h src/GameCommands/MoveUpCommand.h src/GameCommands/EndGameCommand.h ) #set(COMMANDS src/GameCommands/GameCommand.h src/GameCommands/GameCommand.h src/GameCommands/MoveUpCommand.h src/GameCommands/EndGameCommand.h )
set(COMPONENTS src/Components/MoveComponent.cpp ) set(COMPONENTS src/Components/MoveComponent.cpp )
set(DATA_STRUCTURES src/DataStructures/NormalizedDirection.cpp src/DataStructures/DirectionButtonStatus.cpp) set(DATA_STRUCTURES src/DataStructures/NormalizedDirection.cpp src/DataStructures/DirectionButtonStatus.cpp src/DataStructures/Vector2D.cpp src/DataStructures/Hitbox.cpp)
add_executable(Witch-Game main.cpp src/Game.cpp ${ACTORS} ${COMPONENTS} ${DATA_STRUCTURES}) add_executable(Witch-Game main.cpp src/Game.cpp ${ACTORS} ${COMPONENTS} ${DATA_STRUCTURES})
target_compile_features(Witch-Game PRIVATE cxx_std_17) target_compile_features(Witch-Game PRIVATE cxx_std_17)

View file

@ -1,13 +1,17 @@
#include "Actor.h" #include "Actor.h"
void Actor::SetPosition(float x, float y) void Actor::SetPosition(Vector2D position)
{ {
m_Hitbox.x = x; //differenct = velocity vector to new position
m_Hitbox.y = y; Vector2D difference = position - m_Position;
m_MoveHitbox.ApplyVelocity(difference);
m_CombatHitbox.ApplyVelocity(difference);
m_Position += difference;
} }
void Actor::MoveBy(float x, float y) void Actor::MoveBy(Vector2D velocity)
{ {
m_Hitbox.x += x; m_MoveHitbox.ApplyVelocity(velocity);
m_Hitbox.y += y; m_CombatHitbox.ApplyVelocity(velocity);
m_Position += velocity;
} }

View file

@ -3,21 +3,43 @@
#include <memory> #include <memory>
#include <SDL3/SDL_rect.h> #include <SDL3/SDL_rect.h>
#include "../DataStructures/Vector2D.h"
#include "../DataStructures/Hitbox.h"
class Actor : public std::enable_shared_from_this<Actor> class Actor : public std::enable_shared_from_this<Actor>
{ {
public: public:
virtual ~Actor() {}; virtual ~Actor() {};
//virtual void Update(float deltaTime) {}; //virtual void Update(float deltaTime) {};
void SetPosition(float x, float y); void SetPosition(Vector2D position);
void MoveBy(float x, float y); void MoveBy(Vector2D velocity);
SDL_FRect GetHitbox(){return m_Hitbox;}
Vector2D GetPosition() {return m_Position;}
Hitbox GetMovementHitbox() {return m_MoveHitbox;}
Hitbox GetCombatHitbox() {return m_CombatHitbox;}
friend class ActorFactory; friend class ActorFactory;
protected: protected:
virtual void Init() {}; virtual void Init() {};
Actor(SDL_FRect hitbox) : m_Hitbox(hitbox) {}; Actor(Vector2D position, Hitbox moveHitbox, Hitbox combatHitbox) : m_Position(position), m_MoveHitbox(moveHitbox), m_CombatHitbox(combatHitbox) {};
SDL_FRect m_Hitbox;
Vector2D m_Position;
Hitbox m_MoveHitbox;
Hitbox m_CombatHitbox;
std::shared_ptr<InputComponent> m_InputComponent; //Input or AI
std::shared_ptr<MoveComponent> m_MoveComponent;
std::shared_ptr<ActionComponent> m_ActionComponent; //Skills, hit with Axe, Fiery rush,...
std::shared_ptr<SpriteComponent> m_SpriteComponent; //Sprites or anim Sprites
//every other action needs a sprite
//move needs input, input maybe needs move?
//action needs input, input maybe needs action
//all of them don't belong into the base Actor
//Actor, Visible Actor, ActionActor???
//Base Component pointer in Vector?
//Dialogue Component? - makes sense
//Interaction component? - allows interaction - dialogue, open chest, read sign (is dialogue?)
//Damage component for taking damage? part of interaction? also talking?
}; };
#endif // ACTOR_H #endif // ACTOR_H

View file

@ -18,8 +18,7 @@ void MoveComponent::Update(float deltaTime)
m_ForcedDistance -= distance; m_ForcedDistance -= distance;
} }
} }
float xDistance = m_Direction.GetX() * distance; Vector2D velocity = m_Direction.GetDirection() * distance;
float yDistance = m_Direction.GetY() * distance;
//TODO: collision //TODO: collision
bool collision = false; bool collision = false;
@ -29,7 +28,7 @@ void MoveComponent::Update(float deltaTime)
} }
else else
{ {
m_Actor.lock()->MoveBy(xDistance, yDistance); m_Actor.lock()->MoveBy(velocity);
} }
if(m_ForcedDistance == 0) if(m_ForcedDistance == 0)
{ {

View file

@ -10,7 +10,7 @@
class MoveComponent class MoveComponent
{ {
public: public:
MoveComponent(std::weak_ptr<Actor> actor): m_Actor(actor), m_Speed(10) {}; //TODO: speed MoveComponent(std::weak_ptr<Actor> actor): m_Actor(actor), m_Speed(50) {}; //TODO: speed
void Update(float deltaTime); void Update(float deltaTime);
void SetDirection(const NormalizedDirection & direction); void SetDirection(const NormalizedDirection & direction);
void SetSpeed(float speed); void SetSpeed(float speed);

View file

@ -0,0 +1,22 @@
#include "Hitbox.h"
#include "Vector2D.h"
void Hitbox::ApplyVelocity(Vector2D vector)
{
x += vector.x;
y += vector.y;
}
void Hitbox::SetPosition(Vector2D vector)
{
x = vector.x;
y = vector.y;
}
Vector2D Hitbox::GetDirectionTo(const Hitbox & other)
{
Vector2D vec;
vec.x = other.x - x;
vec.y = other.y - y;
return vec;
}

View file

@ -0,0 +1,20 @@
#ifndef HITBOX_H
#define HITBOX_H
#include <SDL3/SDL.h>
#include "Vector2D.h"
class Hitbox : public SDL_FRect
{
public:
Hitbox() : SDL_FRect({0,0,0,0}) {};
Hitbox(float x, float y, float w, float h) : SDL_FRect({x,y,w,h}) {};
Hitbox(SDL_FPoint position, float w, float h) : SDL_FRect({position.x, position.y, w, h}) {};
Hitbox(SDL_FRect rectangle) : SDL_FRect(rectangle) {};
void ApplyVelocity(Vector2D velocity);
void SetPosition(Vector2D vector);
Vector2D GetDirectionTo(const Hitbox & other);
};
#endif

View file

@ -5,13 +5,23 @@ NormalizedDirection::NormalizedDirection(float x, float y, bool isNormalized)
{ {
if(isNormalized || (x == 0 && y == 0)) if(isNormalized || (x == 0 && y == 0))
{ {
m_x = x; m_Direction.x = x;
m_y = y; m_Direction.y = y;
} }
else else
{ {
float normalizeValue = 1 / sqrt(pow(x,2) + pow(y,2)); float normalizeValue = 1 / sqrt(pow(x,2) + pow(y,2));
m_x = x * normalizeValue; m_Direction.x = x * normalizeValue;
m_y = y * normalizeValue; m_Direction.y = y * normalizeValue;
} }
} }
NormalizedDirection::NormalizedDirection(SDL_FPoint direction, bool isNormalized)
{
NormalizedDirection(direction.x, direction.y, isNormalized);
}
NormalizedDirection::NormalizedDirection(Vector2D direction, bool isNormalized)
{
NormalizedDirection(direction.x, direction.y, isNormalized);
}

View file

@ -1,17 +1,23 @@
#ifndef NORMALIZED_DIRECTION_H #ifndef NORMALIZED_DIRECTION_H
#define NORMALIZED_DIRECTION_H #define NORMALIZED_DIRECTION_H
#include "Vector2D.h"
#include <SDL3/SDL.h>
#include "Vector2D.h"
class NormalizedDirection class NormalizedDirection
{ {
public: public:
NormalizedDirection() : m_x(0), m_y(0) {}; NormalizedDirection() {};
NormalizedDirection(float x, float y, bool isNormalized); NormalizedDirection(float x, float y, bool isNormalized);
NormalizedDirection(SDL_FPoint direction, bool isNormalized);
NormalizedDirection(Vector2D direction, bool isNormalized);
float GetX() {return m_x;} float GetX() {return m_Direction.x;}
float GetY() {return m_y;} float GetY() {return m_Direction.y;}
Vector2D GetDirection() {return m_Direction;};
private: private:
float m_x; Vector2D m_Direction;
float m_y;
}; };
#endif #endif

View file

@ -0,0 +1,54 @@
#include "Vector2D.h"
#include <SDL3/SDL.h>
Vector2D& Vector2D::operator+=(const Vector2D& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Vector2D operator+(Vector2D lhs, const Vector2D& rhs)
{
lhs += rhs;
return lhs;
}
Vector2D& Vector2D::operator-=(const Vector2D& rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
Vector2D operator-(Vector2D lhs, const Vector2D& rhs)
{
lhs -= rhs;
return lhs;
}
Vector2D& Vector2D::operator*=(const float& rhs)
{
x *= rhs;
y *= rhs;
return *this;
}
Vector2D operator*(Vector2D lhs, const float& rhs)
{
lhs *= rhs;
return lhs;
}
Vector2D& Vector2D::operator/=(const float& rhs)
{
x /= rhs;
y /= rhs;
return *this;
}
Vector2D operator/(Vector2D lhs, const float& rhs)
{
lhs /= rhs;
return lhs;
}

View file

@ -0,0 +1,24 @@
#ifndef VECTOR_2D_H
#define VECTOR_2D_H
#include <SDL3/SDL.h>
class Vector2D : public SDL_FPoint
{
public:
Vector2D() : SDL_FPoint({0,0}) {};
Vector2D(float x, float y) : SDL_FPoint({x,y}) {};
Vector2D(const SDL_FPoint & point) : SDL_FPoint(point) {};
Vector2D& operator+=(const Vector2D& rhs);
friend Vector2D operator+(Vector2D lhs, const Vector2D& rhs);
Vector2D& operator-=(const Vector2D& rhs);
friend Vector2D operator-(Vector2D lhs, const Vector2D& rhs);
Vector2D& operator*=(const float& rhs);
friend Vector2D operator*(Vector2D lhs, const float& rhs);
Vector2D& operator/=(const float& rhs);
friend Vector2D operator/(Vector2D lhs, const float& rhs);
};
#endif

View file

@ -11,6 +11,9 @@
#include "GameCommands/MoveXCommand.h" #include "GameCommands/MoveXCommand.h"
#include "GameCommands/MoveYCommand.h" #include "GameCommands/MoveYCommand.h"
#include "DataStructures/Vector2D.h"
#include "DataStructures/Hitbox.h"
Game::Game(SDL_Window *window, SDL_Renderer *renderer) Game::Game(SDL_Window *window, SDL_Renderer *renderer)
: m_Window(window, SDL_DestroyWindow), : m_Window(window, SDL_DestroyWindow),
m_Renderer(renderer, SDL_DestroyRenderer), m_Renderer(renderer, SDL_DestroyRenderer),
@ -55,7 +58,7 @@ void Game::Run()
SDL_SetRenderDrawColor(m_Renderer.get(), 255, 255, 255, 255); SDL_SetRenderDrawColor(m_Renderer.get(), 255, 255, 255, 255);
SDL_RenderDebugTextFormat(m_Renderer.get(), 10, 440, "%" SDL_PRIu64 " fps", fps); SDL_RenderDebugTextFormat(m_Renderer.get(), 10, 440, "%" SDL_PRIu64 " fps", fps);
SDL_SetRenderDrawColor(m_Renderer.get(), 150, 0, 150, 255); SDL_SetRenderDrawColor(m_Renderer.get(), 150, 0, 150, 255);
SDL_FRect player = m_Player1->GetHitbox(); SDL_FRect player = m_Player1->GetMovementHitbox();
SDL_RenderFillRect(m_Renderer.get(), &player); SDL_RenderFillRect(m_Renderer.get(), &player);
SDL_RenderPresent(m_Renderer.get()); SDL_RenderPresent(m_Renderer.get());

View file

@ -9,10 +9,8 @@ class Tile
public: public:
private: private:
int8_t tile_id_; uint16_t m_TileId;
SDL_Texture* texture; SDL_Texture* m_Texture;
SDL_FRect tile_position;
int i = SDL_RenderTexture()
}; };