diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c70608..01861ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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(COMMANDS src/GameCommands/GameCommand.h src/GameCommands/GameCommand.h src/GameCommands/MoveUpCommand.h src/GameCommands/EndGameCommand.h ) 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}) target_compile_features(Witch-Game PRIVATE cxx_std_17) diff --git a/src/Actors/Actor.cpp b/src/Actors/Actor.cpp index 80271ca..bfbf0dc 100644 --- a/src/Actors/Actor.cpp +++ b/src/Actors/Actor.cpp @@ -1,13 +1,17 @@ #include "Actor.h" -void Actor::SetPosition(float x, float y) +void Actor::SetPosition(Vector2D position) { - m_Hitbox.x = x; - m_Hitbox.y = y; + //differenct = velocity vector to new position + 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_Hitbox.y += y; + m_MoveHitbox.ApplyVelocity(velocity); + m_CombatHitbox.ApplyVelocity(velocity); + m_Position += velocity; } \ No newline at end of file diff --git a/src/Actors/Actor.h b/src/Actors/Actor.h index 14066d7..50ce4df 100644 --- a/src/Actors/Actor.h +++ b/src/Actors/Actor.h @@ -3,21 +3,43 @@ #include #include +#include "../DataStructures/Vector2D.h" +#include "../DataStructures/Hitbox.h" class Actor : public std::enable_shared_from_this { public: virtual ~Actor() {}; //virtual void Update(float deltaTime) {}; - void SetPosition(float x, float y); - void MoveBy(float x, float y); - SDL_FRect GetHitbox(){return m_Hitbox;} + void SetPosition(Vector2D position); + void MoveBy(Vector2D velocity); + + Vector2D GetPosition() {return m_Position;} + Hitbox GetMovementHitbox() {return m_MoveHitbox;} + Hitbox GetCombatHitbox() {return m_CombatHitbox;} friend class ActorFactory; protected: virtual void Init() {}; - Actor(SDL_FRect hitbox) : m_Hitbox(hitbox) {}; - SDL_FRect m_Hitbox; + Actor(Vector2D position, Hitbox moveHitbox, Hitbox combatHitbox) : m_Position(position), m_MoveHitbox(moveHitbox), m_CombatHitbox(combatHitbox) {}; + + Vector2D m_Position; + Hitbox m_MoveHitbox; + Hitbox m_CombatHitbox; + std::shared_ptr m_InputComponent; //Input or AI + std::shared_ptr m_MoveComponent; + std::shared_ptr m_ActionComponent; //Skills, hit with Axe, Fiery rush,... + std::shared_ptr 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 diff --git a/src/Components/MoveComponent.cpp b/src/Components/MoveComponent.cpp index 1f35380..40e67e9 100644 --- a/src/Components/MoveComponent.cpp +++ b/src/Components/MoveComponent.cpp @@ -18,8 +18,7 @@ void MoveComponent::Update(float deltaTime) m_ForcedDistance -= distance; } } - float xDistance = m_Direction.GetX() * distance; - float yDistance = m_Direction.GetY() * distance; + Vector2D velocity = m_Direction.GetDirection() * distance; //TODO: collision bool collision = false; @@ -29,7 +28,7 @@ void MoveComponent::Update(float deltaTime) } else { - m_Actor.lock()->MoveBy(xDistance, yDistance); + m_Actor.lock()->MoveBy(velocity); } if(m_ForcedDistance == 0) { diff --git a/src/Components/MoveComponent.h b/src/Components/MoveComponent.h index 844eb90..5a6c054 100644 --- a/src/Components/MoveComponent.h +++ b/src/Components/MoveComponent.h @@ -10,7 +10,7 @@ class MoveComponent { public: - MoveComponent(std::weak_ptr actor): m_Actor(actor), m_Speed(10) {}; //TODO: speed + MoveComponent(std::weak_ptr actor): m_Actor(actor), m_Speed(50) {}; //TODO: speed void Update(float deltaTime); void SetDirection(const NormalizedDirection & direction); void SetSpeed(float speed); diff --git a/src/DataStructures/Hitbox.cpp b/src/DataStructures/Hitbox.cpp new file mode 100644 index 0000000..73d7ddd --- /dev/null +++ b/src/DataStructures/Hitbox.cpp @@ -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; +} \ No newline at end of file diff --git a/src/DataStructures/Hitbox.h b/src/DataStructures/Hitbox.h new file mode 100644 index 0000000..6828201 --- /dev/null +++ b/src/DataStructures/Hitbox.h @@ -0,0 +1,20 @@ +#ifndef HITBOX_H +#define HITBOX_H + +#include +#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 \ No newline at end of file diff --git a/src/DataStructures/NormalizedDirection.cpp b/src/DataStructures/NormalizedDirection.cpp index 9320480..e319d6a 100644 --- a/src/DataStructures/NormalizedDirection.cpp +++ b/src/DataStructures/NormalizedDirection.cpp @@ -5,13 +5,23 @@ NormalizedDirection::NormalizedDirection(float x, float y, bool isNormalized) { if(isNormalized || (x == 0 && y == 0)) { - m_x = x; - m_y = y; + m_Direction.x = x; + m_Direction.y = y; } else { float normalizeValue = 1 / sqrt(pow(x,2) + pow(y,2)); - m_x = x * normalizeValue; - m_y = y * normalizeValue; + m_Direction.x = x * 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); +} \ No newline at end of file diff --git a/src/DataStructures/NormalizedDirection.h b/src/DataStructures/NormalizedDirection.h index aacd496..457f7f7 100644 --- a/src/DataStructures/NormalizedDirection.h +++ b/src/DataStructures/NormalizedDirection.h @@ -1,17 +1,23 @@ #ifndef NORMALIZED_DIRECTION_H #define NORMALIZED_DIRECTION_H +#include "Vector2D.h" +#include +#include "Vector2D.h" + class NormalizedDirection { public: - NormalizedDirection() : m_x(0), m_y(0) {}; + NormalizedDirection() {}; NormalizedDirection(float x, float y, bool isNormalized); + NormalizedDirection(SDL_FPoint direction, bool isNormalized); + NormalizedDirection(Vector2D direction, bool isNormalized); - float GetX() {return m_x;} - float GetY() {return m_y;} + float GetX() {return m_Direction.x;} + float GetY() {return m_Direction.y;} + Vector2D GetDirection() {return m_Direction;}; private: - float m_x; - float m_y; + Vector2D m_Direction; }; #endif \ No newline at end of file diff --git a/src/DataStructures/Vector2D.cpp b/src/DataStructures/Vector2D.cpp new file mode 100644 index 0000000..15c1f0e --- /dev/null +++ b/src/DataStructures/Vector2D.cpp @@ -0,0 +1,54 @@ +#include "Vector2D.h" +#include + +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; +} \ No newline at end of file diff --git a/src/DataStructures/Vector2D.h b/src/DataStructures/Vector2D.h new file mode 100644 index 0000000..ed0fb3f --- /dev/null +++ b/src/DataStructures/Vector2D.h @@ -0,0 +1,24 @@ +#ifndef VECTOR_2D_H +#define VECTOR_2D_H + +#include + +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 \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 1b99b58..d798315 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -11,6 +11,9 @@ #include "GameCommands/MoveXCommand.h" #include "GameCommands/MoveYCommand.h" +#include "DataStructures/Vector2D.h" +#include "DataStructures/Hitbox.h" + Game::Game(SDL_Window *window, SDL_Renderer *renderer) : m_Window(window, SDL_DestroyWindow), m_Renderer(renderer, SDL_DestroyRenderer), @@ -55,7 +58,7 @@ void Game::Run() SDL_SetRenderDrawColor(m_Renderer.get(), 255, 255, 255, 255); SDL_RenderDebugTextFormat(m_Renderer.get(), 10, 440, "%" SDL_PRIu64 " fps", fps); 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_RenderPresent(m_Renderer.get()); diff --git a/src/World/Tile.h b/src/World/Tile.h index c7ca2aa..6a013d2 100644 --- a/src/World/Tile.h +++ b/src/World/Tile.h @@ -9,10 +9,8 @@ class Tile public: private: - int8_t tile_id_; - SDL_Texture* texture; - SDL_FRect tile_position; - int i = SDL_RenderTexture() + uint16_t m_TileId; + SDL_Texture* m_Texture; };