spawning objects

This commit is contained in:
DanielProl1xy
2024-02-19 11:43:53 +03:00
parent c6e3869176
commit f717e1c109
10 changed files with 40 additions and 42 deletions
+1 -1
View File
@@ -630,5 +630,5 @@ void Hud::setPause(bool pause) {
} }
Player* Hud::getPlayer() const { Player* Hud::getPlayer() const {
return frontend->getLevel()->player; return frontend->getLevel()->player.get();
} }
@@ -8,10 +8,9 @@ class Level;
class Object { class Object {
private: private:
Level* level;
public: public:
int64_t objectUID; uint64_t objectUID;
bool shouldUpdate = true; bool shouldUpdate = true;
public: public:
@@ -21,10 +20,6 @@ public:
virtual void spawned() { } virtual void spawned() { }
virtual void update(float delta) { } virtual void update(float delta) { }
virtual void destroyed() { } virtual void destroyed() { }
public:
Level* getLevel() { return level; }
void setLevel(Level* lvl) { level = lvl; }
}; };
#endif /* OBJECT_H */ #endif /* OBJECT_H */
+3 -3
View File
@@ -6,7 +6,7 @@
#include "ChunksController.h" #include "ChunksController.h"
#include "scripting/scripting.h" #include "scripting/scripting.h"
#include "../objects/Object.h" #include "../interfaces/Object.h"
LevelController::LevelController(EngineSettings& settings, Level* level) LevelController::LevelController(EngineSettings& settings, Level* level)
: settings(settings), level(level) { : settings(settings), level(level) {
@@ -26,11 +26,11 @@ void LevelController::update(float delta, bool input, bool pause) {
chunks->update(settings.chunks.loadSpeed); chunks->update(settings.chunks.loadSpeed);
// erease null pointers // erease null pointers
level->objects.remove_if([](Object* obj) { return obj == nullptr; }); level->objects.remove_if([](auto obj) { return obj == nullptr; });
if (!pause) { if (!pause) {
// update all objects that needed // update all objects that needed
for(Object *obj : level->objects) for(auto obj : level->objects)
{ {
if(obj) { if(obj) {
if(obj->shouldUpdate) { if(obj->shouldUpdate) {
+2 -2
View File
@@ -31,7 +31,7 @@ const float C_ZOOM = 0.1f;
const float CROUCH_SHIFT_Y = -0.2f; const float CROUCH_SHIFT_Y = -0.2f;
CameraControl::CameraControl(Player* player, const CameraSettings& settings) CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
: player(player), : player(player),
camera(player->camera), camera(player->camera),
currentViewCamera(player->currentCamera), currentViewCamera(player->currentCamera),
@@ -208,7 +208,7 @@ void PlayerController::updateControls(float delta){
void PlayerController::updateInteraction(){ void PlayerController::updateInteraction(){
auto indices = level->content->getIndices(); auto indices = level->content->getIndices();
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
Player* player = level->player; Player* player = level->player.get();
Lighting* lighting = level->lighting; Lighting* lighting = level->lighting;
Camera* camera = player->camera.get(); Camera* camera = player->camera.get();
glm::vec3 end; glm::vec3 end;
+3 -3
View File
@@ -12,7 +12,7 @@ class Level;
class BlocksController; class BlocksController;
class CameraControl { class CameraControl {
Player* player; std::shared_ptr<Player> player;
std::shared_ptr<Camera> camera, currentViewCamera; std::shared_ptr<Camera> camera, currentViewCamera;
const CameraSettings& settings; const CameraSettings& settings;
glm::vec3 offset; glm::vec3 offset;
@@ -20,7 +20,7 @@ class CameraControl {
float shakeTimer = 0.0f; float shakeTimer = 0.0f;
glm::vec3 interpVel {0.0f}; glm::vec3 interpVel {0.0f};
public: public:
CameraControl(Player* player, const CameraSettings& settings); CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings);
void updateMouse(PlayerInput& input); void updateMouse(PlayerInput& input);
void update(PlayerInput& input, float delta, Chunks* chunks); void update(PlayerInput& input, float delta, Chunks* chunks);
void refresh(); void refresh();
@@ -28,7 +28,7 @@ public:
class PlayerController { class PlayerController {
Level* level; Level* level;
Player* player; std::shared_ptr<Player> player;
PlayerInput input; PlayerInput input;
CameraControl camControl; CameraControl camControl;
BlocksController* blocksController; BlocksController* blocksController;
+1 -1
View File
@@ -191,7 +191,7 @@ int l_player_get_inv(lua_State* L) {
int playerid = lua_tointeger(L, 1); int playerid = lua_tointeger(L, 1);
if (playerid != 1) if (playerid != 1)
return 0; return 0;
Player* player = scripting::level->player; auto player = scripting::level->player;
lua_pushinteger(L, player->getInventory()->getId()); lua_pushinteger(L, player->getInventory()->getId());
lua_pushinteger(L, player->getChosenSlot()); lua_pushinteger(L, player->getChosenSlot());
return 2; return 2;
+1 -1
View File
@@ -8,7 +8,7 @@
#include "../voxels/voxel.h" #include "../voxels/voxel.h"
#include "../settings.h" #include "../settings.h"
#include "../interfaces/Serializable.h" #include "../interfaces/Serializable.h"
#include "../objects/Object.h" #include "../interfaces/Object.h"
class Camera; class Camera;
class Hitbox; class Hitbox;
+17 -14
View File
@@ -8,7 +8,7 @@
#include "../voxels/ChunksStorage.h" #include "../voxels/ChunksStorage.h"
#include "../physics/Hitbox.h" #include "../physics/Hitbox.h"
#include "../physics/PhysicsSolver.h" #include "../physics/PhysicsSolver.h"
#include "../objects/Object.h" #include "../interfaces/Object.h"
#include "../objects/Player.h" #include "../objects/Player.h"
#include "../items/Inventory.h" #include "../items/Inventory.h"
#include "../items/Inventories.h" #include "../items/Inventories.h"
@@ -25,11 +25,10 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
events(new LevelEvents()) , events(new LevelEvents()) ,
settings(settings) settings(settings)
{ {
objCounter = 0;
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0)); physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE); player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
player = spawnObjectOfClass<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
uint matrixSize = (settings.chunks.loadDistance+ uint matrixSize = (settings.chunks.loadDistance+
settings.chunks.padding) * 2; settings.chunks.padding) * 2;
@@ -42,15 +41,20 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
}); });
inventories = std::make_unique<Inventories>(*this); inventories = std::make_unique<Inventories>(*this);
inventories->store(player->getInventory());
} }
Level::~Level(){ Level::~Level(){
delete chunks; delete chunks;
delete events; delete events;
delete physics; delete physics;
delete player;
delete lighting; delete lighting;
delete chunksStorage; delete chunksStorage;
for(auto obj : objects)
{
obj.reset();
}
} }
void Level::update() { void Level::update() {
@@ -68,18 +72,17 @@ World* Level::getWorld() {
return world.get(); return world.get();
} }
// Spawns object of class T and returns pointer to it.
// @param T class that derives the Object class
// @param args pass arguments needed for T class constructor
template<class T, typename... Args> template<class T, typename... Args>
T* Level::spawnObjectOfClass(Args&&... args) std::shared_ptr<T> Level::spawnObject(Args&&... args)
{ {
static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class"); static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class");
T* tObj = new T(args...); std::shared_ptr<T> tObj = std::make_shared<T>(args...);
Object* obj = reinterpret_cast<Object*>(tObj);
std::shared_ptr<Object> obj = std::dynamic_pointer_cast<Object, T>(tObj);
objects.push_back(obj); objects.push_back(obj);
obj->objectUID = std::rand(); obj->objectUID = objCounter;
obj->setLevel(this);
obj->spawned(); obj->spawned();
objCounter += 1;
return tObj; return tObj;
} }
+7 -3
View File
@@ -6,6 +6,8 @@
#include "../typedefs.h" #include "../typedefs.h"
#include "../settings.h" #include "../settings.h"
#include <list> #include <list>
#include <vector>
#include <chrono>
class Content; class Content;
class World; class World;
@@ -20,11 +22,13 @@ class PhysicsSolver;
class ChunksStorage; class ChunksStorage;
class Level { class Level {
private:
int objCounter;
public: public:
std::unique_ptr<World> world; std::unique_ptr<World> world;
const Content* const content; const Content* const content;
std::list<Object*> objects; std::list<std::shared_ptr<Object>> objects;
Player* player; std::shared_ptr<Player> player;
Chunks* chunks; Chunks* chunks;
ChunksStorage* chunksStorage; ChunksStorage* chunksStorage;
std::unique_ptr<Inventories> inventories; std::unique_ptr<Inventories> inventories;
@@ -45,7 +49,7 @@ public:
World* getWorld(); World* getWorld();
template<class T, typename... Args> template<class T, typename... Args>
T* spawnObjectOfClass(Args&&... args); std::shared_ptr<T> spawnObject(Args&&... args);
}; };
#endif /* WORLD_LEVEL_H_ */ #endif /* WORLD_LEVEL_H_ */
+3 -7
View File
@@ -61,20 +61,17 @@ void World::write(Level* level) {
} }
wfile->write(this, content); wfile->write(this, content);
wfile->writePlayer(level->player); wfile->writePlayer(level->player.get());
} }
Level* World::create(std::string name, Level* World::create(std::string name,
fs::path directory, fs::path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {
auto world = new World(name, directory, seed, settings, content, packs); auto world = new World(name, directory, seed, settings, content, packs);
auto level = new Level(world, content, settings); auto level = new Level(world, content, settings);
level->inventories->store(level->player->getInventory());
return level; return level;
} }
@@ -91,9 +88,8 @@ Level* World::load(fs::path directory,
throw world_load_error("could not to find world.json"); throw world_load_error("could not to find world.json");
} }
Level* level = new Level(world.get(), content, settings); auto level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player); wfile->readPlayer(level->player.get());
level->inventories->store(level->player->getInventory());
world.release(); world.release();
return level; return level;
} }