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

View File

@ -630,5 +630,5 @@ void Hud::setPause(bool pause) {
}
Player* Hud::getPlayer() const {
return frontend->getLevel()->player;
return frontend->getLevel()->player.get();
}

View File

@ -8,10 +8,9 @@ class Level;
class Object {
private:
Level* level;
public:
int64_t objectUID;
uint64_t objectUID;
bool shouldUpdate = true;
public:
@ -21,10 +20,6 @@ public:
virtual void spawned() { }
virtual void update(float delta) { }
virtual void destroyed() { }
public:
Level* getLevel() { return level; }
void setLevel(Level* lvl) { level = lvl; }
};
#endif /* OBJECT_H */

View File

@ -6,7 +6,7 @@
#include "ChunksController.h"
#include "scripting/scripting.h"
#include "../objects/Object.h"
#include "../interfaces/Object.h"
LevelController::LevelController(EngineSettings& 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);
// erease null pointers
level->objects.remove_if([](Object* obj) { return obj == nullptr; });
level->objects.remove_if([](auto obj) { return obj == nullptr; });
if (!pause) {
// update all objects that needed
for(Object *obj : level->objects)
for(auto obj : level->objects)
{
if(obj) {
if(obj->shouldUpdate) {

View File

@ -31,7 +31,7 @@ const float C_ZOOM = 0.1f;
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),
camera(player->camera),
currentViewCamera(player->currentCamera),
@ -208,7 +208,7 @@ void PlayerController::updateControls(float delta){
void PlayerController::updateInteraction(){
auto indices = level->content->getIndices();
Chunks* chunks = level->chunks;
Player* player = level->player;
Player* player = level->player.get();
Lighting* lighting = level->lighting;
Camera* camera = player->camera.get();
glm::vec3 end;

View File

@ -12,7 +12,7 @@ class Level;
class BlocksController;
class CameraControl {
Player* player;
std::shared_ptr<Player> player;
std::shared_ptr<Camera> camera, currentViewCamera;
const CameraSettings& settings;
glm::vec3 offset;
@ -20,7 +20,7 @@ class CameraControl {
float shakeTimer = 0.0f;
glm::vec3 interpVel {0.0f};
public:
CameraControl(Player* player, const CameraSettings& settings);
CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings);
void updateMouse(PlayerInput& input);
void update(PlayerInput& input, float delta, Chunks* chunks);
void refresh();
@ -28,7 +28,7 @@ public:
class PlayerController {
Level* level;
Player* player;
std::shared_ptr<Player> player;
PlayerInput input;
CameraControl camControl;
BlocksController* blocksController;

View File

@ -191,7 +191,7 @@ int l_player_get_inv(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
Player* player = scripting::level->player;
auto player = scripting::level->player;
lua_pushinteger(L, player->getInventory()->getId());
lua_pushinteger(L, player->getChosenSlot());
return 2;

View File

@ -8,7 +8,7 @@
#include "../voxels/voxel.h"
#include "../settings.h"
#include "../interfaces/Serializable.h"
#include "../objects/Object.h"
#include "../interfaces/Object.h"
class Camera;
class Hitbox;

View File

@ -8,7 +8,7 @@
#include "../voxels/ChunksStorage.h"
#include "../physics/Hitbox.h"
#include "../physics/PhysicsSolver.h"
#include "../objects/Object.h"
#include "../interfaces/Object.h"
#include "../objects/Player.h"
#include "../items/Inventory.h"
#include "../items/Inventories.h"
@ -25,11 +25,10 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
events(new LevelEvents()) ,
settings(settings)
{
objCounter = 0;
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
player = spawnObjectOfClass<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
uint matrixSize = (settings.chunks.loadDistance+
settings.chunks.padding) * 2;
@ -42,15 +41,20 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
});
inventories = std::make_unique<Inventories>(*this);
inventories->store(player->getInventory());
}
Level::~Level(){
delete chunks;
delete events;
delete physics;
delete player;
delete lighting;
delete chunksStorage;
for(auto obj : objects)
{
obj.reset();
}
}
void Level::update() {
@ -68,18 +72,17 @@ World* Level::getWorld() {
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>
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");
T* tObj = new T(args...);
Object* obj = reinterpret_cast<Object*>(tObj);
std::shared_ptr<T> tObj = std::make_shared<T>(args...);
std::shared_ptr<Object> obj = std::dynamic_pointer_cast<Object, T>(tObj);
objects.push_back(obj);
obj->objectUID = std::rand();
obj->setLevel(this);
obj->objectUID = objCounter;
obj->spawned();
objCounter += 1;
return tObj;
}

View File

@ -6,6 +6,8 @@
#include "../typedefs.h"
#include "../settings.h"
#include <list>
#include <vector>
#include <chrono>
class Content;
class World;
@ -20,11 +22,13 @@ class PhysicsSolver;
class ChunksStorage;
class Level {
private:
int objCounter;
public:
std::unique_ptr<World> world;
const Content* const content;
std::list<Object*> objects;
Player* player;
std::list<std::shared_ptr<Object>> objects;
std::shared_ptr<Player> player;
Chunks* chunks;
ChunksStorage* chunksStorage;
std::unique_ptr<Inventories> inventories;
@ -45,7 +49,7 @@ public:
World* getWorld();
template<class T, typename... Args>
T* spawnObjectOfClass(Args&&... args);
std::shared_ptr<T> spawnObject(Args&&... args);
};
#endif /* WORLD_LEVEL_H_ */

View File

@ -61,20 +61,17 @@ void World::write(Level* level) {
}
wfile->write(this, content);
wfile->writePlayer(level->player);
wfile->writePlayer(level->player.get());
}
Level* World::create(std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
auto world = new World(name, directory, seed, settings, content, packs);
auto level = new Level(world, content, settings);
level->inventories->store(level->player->getInventory());
return level;
}
@ -91,9 +88,8 @@ Level* World::load(fs::path directory,
throw world_load_error("could not to find world.json");
}
Level* level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player);
level->inventories->store(level->player->getInventory());
auto level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player.get());
world.release();
return level;
}