Skip to content

Commit

Permalink
Add uuid interface to game objects
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Aug 15, 2024
1 parent 1819c53 commit c1796aa
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Characters/include/Characters/enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#define CHARACTERS_ENEMY_H_

#include "character.h"
#include <Utility/uuid_generator.h>

namespace wolfenstein {

class Enemy : public ICharacter, public IGameObject
{
public:
explicit Enemy();
~Enemy() = default;

void Update(double delta_time) override;
Expand All @@ -28,11 +30,13 @@ class Enemy : public ICharacter, public IGameObject
vector2d GetPose() const override;
void SetPosition(Position2D position) override;
Position2D GetPosition() const override;
std::string GetId() const override;

private:
void Move(double delta_time);

Position2D position_;
std::string id_;
};

} // namespace wolfenstein
Expand Down
3 changes: 3 additions & 0 deletions src/Characters/include/Characters/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define CHARACTERS_PLAYER_H

#include "character.h"
#include <Utility/uuid_generator.h>
#include <functional>

namespace wolfenstein {
Expand All @@ -31,6 +32,7 @@ class Player : public ICharacter, public IGameObject
vector2d GetPose() const override;
void SetPosition(Position2D position) override;
Position2D GetPosition() const override;
std::string GetId() const override;

void SetCameraPositionUpdator(std::function<void(Position2D)> updator);

Expand All @@ -41,6 +43,7 @@ class Player : public ICharacter, public IGameObject
Position2D position_;
double rotation_speed_;
double translation_speed_;
std::string id_;
std::function<void(Position2D)> camera_position_updator_;
};

Expand Down
8 changes: 8 additions & 0 deletions src/Characters/src/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace wolfenstein {

Enemy::Enemy() {
id_ = UuidGenerator::GetInstance().GenerateUuid().bytes();
}

void Enemy::Update(double delta_time) {
Move(delta_time);
}
Expand All @@ -26,6 +30,10 @@ Position2D Enemy::GetPosition() const {
return position_;
}

std::string Enemy::GetId() const {
return id_;
}

void Enemy::Move(double delta_time) {
(void)delta_time;
// Move enemy
Expand Down
17 changes: 15 additions & 2 deletions src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace wolfenstein {
Player::Player(CharacterConfig& config)
: position_(config.initial_position),
rotation_speed_(config.rotation_speed),
translation_speed_(config.translation_speed) {}
translation_speed_(config.translation_speed) {
id_ = UuidGenerator::GetInstance().GenerateUuid().bytes();
}

void Player::Update(double delta_time) {
Move(delta_time);
Expand Down Expand Up @@ -36,6 +38,10 @@ Position2D Player::GetPosition() const {
return position_;
}

std::string Player::GetId() const {
return id_;
}

void Player::SetCameraPositionUpdator(std::function<void(Position2D)> updator) {
camera_position_updator_ = updator;
}
Expand Down Expand Up @@ -77,7 +83,14 @@ void Player::Rotate(double delta_time) {
if (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE) {
int x, y;
SDL_GetMouseState(&x, &y);
position_.theta = SumRadian(position_.theta, (x - 400) * 0.001);
position_.theta = SumRadian(position_.theta,
(x - 400) * rotation_speed_ * delta_time);
if (position_.theta > M_PI) {
position_.theta -= 2 * M_PI;
}
else if (position_.theta < -M_PI) {
position_.theta += 2 * M_PI;
}
SDL_WarpMouseInWindow(nullptr, 400, 300);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/GameObjects/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}
)

# Game object library
Expand All @@ -15,11 +16,13 @@ add_library(
target_include_directories(game_object PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}
)

target_link_libraries(
game_object
PUBLIC
vector
character
uuid_generator
)
3 changes: 3 additions & 0 deletions src/GameObjects/include/GameObjects/dynamic_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define GAME_OBJECTS_DYNAMIC_OBJECT_H

#include "GameObjects/game_object.h"
#include <Utility/uuid_generator.h>

namespace wolfenstein {

Expand All @@ -27,11 +28,13 @@ class DynamicObject : public IGameObject
void SetPose(const vector2d& pose) override;
ObjectType GetObjectType() const override;
vector2d GetPose() const override;
std::string GetId() const override;

protected:
// Dynamic object specific data
vector2d pose;
std::string texture_path;
std::string id;
};
} // namespace wolfenstein

Expand Down
2 changes: 2 additions & 0 deletions src/GameObjects/include/GameObjects/game_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define GAME_OBJECTS_GAME_OBJECT_H

#include <Math/vector.h>
#include <string>

namespace wolfenstein {

Expand All @@ -33,6 +34,7 @@ class IGameObject
virtual void SetPose(const vector2d& pose) = 0;
virtual ObjectType GetObjectType() const = 0;
virtual vector2d GetPose() const = 0;
virtual std::string GetId() const = 0;
};
} // namespace wolfenstein

Expand Down
4 changes: 4 additions & 0 deletions src/GameObjects/include/GameObjects/static_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#include "GameObjects/game_object.h"
#include "Math/vector.h"
#include "third-party/uuid_v4/uuid_v4.h"
#include <Characters/character.h>
#include <Utility/uuid_generator.h>

namespace wolfenstein {

Expand All @@ -32,6 +34,7 @@ class StaticObject : public IGameObject

ObjectType GetObjectType() const override;
vector2d GetPose() const override;
std::string GetId() const override;

int GetTextureId() const;
double GetWidth() const;
Expand All @@ -42,6 +45,7 @@ class StaticObject : public IGameObject
int texture_id;
double width;
double height;
std::string id;
};

} // namespace wolfenstein
Expand Down
8 changes: 7 additions & 1 deletion src/GameObjects/src/dynamic_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace wolfenstein {

DynamicObject::DynamicObject(const vector2d& pose_, std::string texture_path_)
: pose(pose_), texture_path(texture_path_) {}
: pose(pose_), texture_path(texture_path_) {
id = UuidGenerator::GetInstance().GenerateUuid().bytes();
}

DynamicObject::~DynamicObject() {}

Expand All @@ -24,4 +26,8 @@ vector2d DynamicObject::GetPose() const {
return pose;
}

std::string DynamicObject::GetId() const {
return id;
}

} // namespace wolfenstein
9 changes: 8 additions & 1 deletion src/GameObjects/src/static_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace wolfenstein {

StaticObject::StaticObject(const vector2d& pose_, const int texture_id_,
const double width_, const double height_)
: pose(pose_), texture_id(texture_id_), width(width_), height(height_) {}
: pose(pose_), texture_id(texture_id_), width(width_), height(height_) {
id = UuidGenerator::GetInstance().GenerateUuid().bytes();
}

StaticObject::~StaticObject() {}

Expand Down Expand Up @@ -33,6 +35,11 @@ vector2d StaticObject::GetPose() const {
int StaticObject::GetTextureId() const {
return texture_id;
}

std::string StaticObject::GetId() const {
return id;
}

double StaticObject::GetWidth() const {
return width;
}
Expand Down

0 comments on commit c1796aa

Please sign in to comment.