Movable Object | Moby

Not every entity needs to be able to be moved, So it wouldn't make sense to have to store values associated with movement for an entity which will not move.
This commit is contained in:
2023-11-18 19:40:41 -05:00
parent 4eedd52004
commit 25bc4ee2b7
7 changed files with 30 additions and 20 deletions

View File

@@ -15,7 +15,8 @@ add_executable(SDL3D src/main.cpp
src/engine/tick.h
src/types/vertex.h
src/types/skybox.h
src/types/geometry.h)
src/types/geometry.h
src/types/moby.h)
find_package(OpenGL)
target_link_libraries(SDL3D SDL2 freeimage ${OPENGL_LIBRARIES})

View File

@@ -1,12 +1,12 @@
#pragma once
#include "entity.h"
#include "moby.h"
#include "../engine/engine.h"
enum class cameramode: uint8_t {
FREECAM = 0,
FOLLOW_PLAYER = 1
};
class Camera : public entity {
class Camera : public Moby {
public:
uint8_t mode = 0;
void move(vector3 offset) {

View File

@@ -2,18 +2,16 @@
#include <cstdint>
#include "vector3.h"
class entity {
class Entity {
public:
bool draw;
bool collidable;
uint32_t ticksAlive; //At 64tps it'd take 776 days to overflow.
Position position; //X Y Z
angle angles = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
angle velAngles = {0,0,0}; //The angle of an entities velocity.
bool draw = true;
bool collidable = true;
uint32_t ticksAlive = 0; //At 64tps it'd take 776 days to overflow.
Position position = {0,0,0}; //X Y Z
void destruct() {
//TODO: Search entity list for this entity and remove it to avoid use-after-free.
delete this;
}
virtual ~entity() {}
virtual ~Entity() {}
};

View File

@@ -3,14 +3,15 @@
#include "entity.h"
#include "player.h"
#include "camera.h"
class entitylist {
public:
std::vector<entity*> list;
std::vector<Entity*> list;
inline player* getPlayer() {
inline Player* getPlayer() {
for (auto& e : this->list)
if (auto* p = dynamic_cast<player*>(e) )
return dynamic_cast<player *>(e);
if (auto* p = dynamic_cast<Player*>(e) )
return dynamic_cast<Player *>(e);
}
inline Camera* getCamera() {
@@ -19,7 +20,7 @@ public:
return dynamic_cast<Camera *>(e);
}
inline void storeEntity(entity* e) {
inline void storeEntity(Entity* e) {
this->list.push_back(e);
}
};

10
src/types/moby.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include "entity.h"
//Movable Object.
class Moby : public Entity {
public:
float velocity = 0;
angle angles = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
angle velAngles = {0,0,0}; //The angle of an entities velocity.
};

View File

@@ -1,7 +1,7 @@
#pragma once
#include "entity.h"
#include "moby.h"
class player : public entity {
class Player : public Moby {
public:
bool alive;
uint8_t health;

View File

@@ -1,9 +1,9 @@
#pragma once
#include <SDL2/SDL_opengl.h>
#include "entity.h"
#include "moby.h"
#include "geometry.h"
class skybox : public entity {
class skybox : public Moby {
public:
const Geometry geometry = {
{0.000f, 1.000f, 0.000f,