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
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)
all: $(TARGET)
@ -20,7 +20,4 @@ clean:
run: $(TARGET)
./$(TARGET)
packets:
cd .. && lua generate_packets.lua
.PHONY: all clean run packets
.PHONY: all clean run

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

View File

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