1
0

Make PlayerController inherit from entity

This commit is contained in:
Sky Johnson 2025-09-09 13:09:37 -05:00
parent e0171963c5
commit ed939adbfc
5 changed files with 73 additions and 16 deletions

View File

@ -3,7 +3,7 @@ CXXFLAGS = -std=c++20 -Wall -Wextra -O2 -I.
LDFLAGS = -lraylib -lboost_system -lpthread -lGL -lm -ldl -lrt -lX11 LDFLAGS = -lraylib -lboost_system -lpthread -lGL -lm -ldl -lrt -lX11
TARGET = game TARGET = game
SOURCES = main.cpp entity/player/PlayerController.cpp net/NetworkManager.cpp SOURCES = main.cpp entity/Entity.cpp entity/player/PlayerController.cpp net/NetworkManager.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
all: $(TARGET) all: $(TARGET)
@ -20,7 +20,4 @@ clean:
run: $(TARGET) run: $(TARGET)
./$(TARGET) ./$(TARGET)
packets: .PHONY: all clean run
cd .. && lua generate_packets.lua
.PHONY: all clean run packets

22
client/entity/Entity.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "Entity.hpp"
Entity::Entity(const Vector3& pos, uint32_t entityId)
: position(pos), id(entityId) {
}
void Entity::update(float deltaTime) {
if (!active) return;
position = Vector3Add(position, Vector3Scale(velocity, deltaTime));
}
void Entity::render() {
}
void Entity::move(const Vector3& delta) {
position = Vector3Add(position, delta);
}
void Entity::rotate(const Vector3& delta) {
rotation = Vector3Add(rotation, delta);
}

37
client/entity/Entity.hpp Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <raylib.h>
#include <raymath.h>
#include <cstdint>
class Entity {
protected:
Vector3 position{0, 0, 0};
Vector3 rotation{0, 0, 0};
Vector3 velocity{0, 0, 0};
uint32_t id{0};
bool active{true};
public:
Entity() = default;
Entity(const Vector3& pos, uint32_t entityId = 0);
virtual ~Entity() = default;
virtual void update(float deltaTime);
virtual void render();
void setPosition(const Vector3& pos) { position = pos; }
void setRotation(const Vector3& rot) { rotation = rot; }
void setVelocity(const Vector3& vel) { velocity = vel; }
void setId(uint32_t entityId) { id = entityId; }
void setActive(bool isActive) { active = isActive; }
Vector3 getPosition() const { return position; }
Vector3 getRotation() const { return rotation; }
Vector3 getVelocity() const { return velocity; }
uint32_t getId() const { return id; }
bool isActive() const { return active; }
void move(const Vector3& delta);
void rotate(const Vector3& delta);
};

View File

@ -14,13 +14,14 @@ PlayerController::PlayerController(float distance, float height, float speed)
} }
void PlayerController::update(float deltaTime) { void PlayerController::update(float deltaTime) {
Entity::update(deltaTime);
handleCameraRotation(); handleCameraRotation();
handleCameraZoom(); handleCameraZoom();
updateCameraPosition(); updateCameraPosition();
} }
void PlayerController::setPlayerPosition(const Vector3& position) { void PlayerController::setPlayerPosition(const Vector3& pos) {
playerPosition = position; position = pos;
} }
Vector3 PlayerController::getMoveInput() const { Vector3 PlayerController::getMoveInput() const {
@ -68,7 +69,7 @@ Vector3 PlayerController::getMoveInput() const {
void PlayerController::updateCameraPosition() { void PlayerController::updateCameraPosition() {
// Update camera target to player position // Update camera target to player position
camera.target = playerPosition; camera.target = position;
// Calculate camera position based on spherical coordinates // Calculate camera position based on spherical coordinates
float cosYaw = cosf(cameraYaw); float cosYaw = cosf(cameraYaw);
@ -76,9 +77,9 @@ void PlayerController::updateCameraPosition() {
float cosPitch = cosf(cameraPitch); float cosPitch = cosf(cameraPitch);
float sinPitch = sinf(cameraPitch); float sinPitch = sinf(cameraPitch);
camera.position.x = playerPosition.x - sinYaw * cosPitch * cameraDistance; camera.position.x = position.x - sinYaw * cosPitch * cameraDistance;
camera.position.y = playerPosition.y + sinPitch * cameraDistance; camera.position.y = position.y + sinPitch * cameraDistance;
camera.position.z = playerPosition.z - cosYaw * cosPitch * cameraDistance; camera.position.z = position.z - cosYaw * cosPitch * cameraDistance;
} }
void PlayerController::handleCameraRotation() { void PlayerController::handleCameraRotation() {

View File

@ -2,11 +2,11 @@
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include "../Entity.hpp"
class PlayerController { class PlayerController : public Entity {
private: private:
Camera3D camera{}; Camera3D camera{};
Vector3 playerPosition{0, 0, 0};
float cameraDistance; float cameraDistance;
float cameraHeight; float cameraHeight;
float moveSpeed; float moveSpeed;
@ -20,9 +20,9 @@ private:
public: public:
PlayerController(float distance = 10.0f, float height = 5.0f, float speed = 5.0f); PlayerController(float distance = 10.0f, float height = 5.0f, float speed = 5.0f);
void update(float deltaTime); void update(float deltaTime) override;
void setPlayerPosition(const Vector3& position); void setPlayerPosition(const Vector3& pos);
Vector3 getPlayerPosition() const { return playerPosition; } Vector3 getPlayerPosition() const { return position; }
Vector3 getMoveInput() const; Vector3 getMoveInput() const;
Camera3D& getCamera() { return camera; } Camera3D& getCamera() { return camera; }