add player entity (draft)

This commit is contained in:
MihailRis 2024-07-07 22:19:15 +03:00
parent 939cfdd851
commit 77c9c5cdb7
6 changed files with 39 additions and 13 deletions

View File

@ -242,7 +242,7 @@ void PlayerController::update(float delta, bool input, bool pause) {
} }
updateFootsteps(delta); updateFootsteps(delta);
updateCamera(delta, input); updateCamera(delta, input);
updateControls(delta); updatePlayer(delta);
} }
camControl.refresh(); camControl.refresh();
@ -288,7 +288,8 @@ void PlayerController::resetKeyboard() {
input.jump = false; input.jump = false;
} }
void PlayerController::updateControls(float delta){ void PlayerController::updatePlayer(float delta) {
player->updateEntity(level);
player->updateInput(level, input, delta); player->updateInput(level, input, delta);
} }

View File

@ -65,7 +65,7 @@ class PlayerController {
void updateKeyboard(); void updateKeyboard();
void updateCamera(float delta, bool movement); void updateCamera(float delta, bool movement);
void resetKeyboard(); void resetKeyboard();
void updateControls(float delta); void updatePlayer(float delta);
void updateInteraction(); void updateInteraction();
void onBlockInteraction( void onBlockInteraction(
glm::ivec3 pos, glm::ivec3 pos,

View File

@ -8,6 +8,7 @@
#include "../window/Events.hpp" #include "../window/Events.hpp"
#include "../window/Camera.hpp" #include "../window/Camera.hpp"
#include "../items/Inventory.hpp" #include "../items/Inventory.hpp"
#include "../objects/Entities.hpp"
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <utility> #include <utility>
@ -20,10 +21,13 @@ const float FLIGHT_SPEED_MUL = 4.0f;
const float CHEAT_SPEED_MUL = 5.0f; const float CHEAT_SPEED_MUL = 5.0f;
const float JUMP_FORCE = 8.0f; const float JUMP_FORCE = 8.0f;
Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv) : Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv,
entityid_t eid) :
speed(speed), speed(speed),
chosenSlot(0), chosenSlot(0),
position(position),
inventory(std::move(inv)), inventory(std::move(inv)),
entity(eid),
camera(std::make_shared<Camera>(position, glm::radians(90.0f))), camera(std::make_shared<Camera>(position, glm::radians(90.0f))),
spCamera(std::make_shared<Camera>(position, glm::radians(90.0f))), spCamera(std::make_shared<Camera>(position, glm::radians(90.0f))),
tpCamera(std::make_shared<Camera>(position, glm::radians(90.0f))), tpCamera(std::make_shared<Camera>(position, glm::radians(90.0f))),
@ -35,11 +39,15 @@ Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv)
Player::~Player() { Player::~Player() {
} }
void Player::updateInput( void Player::updateEntity(Level* level) {
Level* level, if (entity == 0) {
PlayerInput& input, // spawn entity
float delta } else {
) { // check entity, respawn if despawned
}
}
void Player::updateInput(Level* level, PlayerInput& input, float delta) {
bool crouch = input.shift && hitbox->grounded && !input.sprint; bool crouch = input.shift && hitbox->grounded && !input.sprint;
float speed = this->speed; float speed = this->speed;
if (flight){ if (flight){
@ -185,6 +193,14 @@ void Player::setNoclip(bool flag) {
this->noclip = flag; this->noclip = flag;
} }
entityid_t Player::getEntity() const {
return entity;
}
void Player::setEntity(entityid_t eid) {
entity = eid;
}
std::shared_ptr<Inventory> Player::getInventory() const { std::shared_ptr<Inventory> Player::getInventory() const {
return inventory; return inventory;
} }
@ -218,6 +234,7 @@ std::unique_ptr<dynamic::Map> Player::serialize() const {
root->put("flight", flight); root->put("flight", flight);
root->put("noclip", noclip); root->put("noclip", noclip);
root->put("chosen-slot", chosenSlot); root->put("chosen-slot", chosenSlot);
root->put("entity", entity);
root->put("inventory", inventory->serialize()); root->put("inventory", inventory->serialize());
return root; return root;
} }
@ -251,13 +268,13 @@ void Player::deserialize(dynamic::Map *src) {
src->flag("flight", flight); src->flag("flight", flight);
src->flag("noclip", noclip); src->flag("noclip", noclip);
setChosenSlot(src->get("chosen-slot", getChosenSlot())); setChosenSlot(src->get("chosen-slot", getChosenSlot()));
src->num("enitity", entity);
if (auto invmap = src->map("inventory")) { if (auto invmap = src->map("inventory")) {
getInventory()->deserialize(invmap.get()); getInventory()->deserialize(invmap.get());
} }
} }
void Player::convert(dynamic::Map* data, const ContentLUT* lut) { void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
auto players = data->list("players"); auto players = data->list("players");
if (players) { if (players) {

View File

@ -43,10 +43,12 @@ struct BlockSelection {
class Player : public Object, public Serializable { class Player : public Object, public Serializable {
float speed; float speed;
int chosenSlot; int chosenSlot;
glm::vec3 position;
glm::vec3 spawnpoint {}; glm::vec3 spawnpoint {};
std::shared_ptr<Inventory> inventory; std::shared_ptr<Inventory> inventory;
bool flight = false; bool flight = false;
bool noclip = false; bool noclip = false;
entityid_t entity;
public: public:
std::shared_ptr<Camera> camera, spCamera, tpCamera; std::shared_ptr<Camera> camera, spCamera, tpCamera;
std::shared_ptr<Camera> currentCamera; std::shared_ptr<Camera> currentCamera;
@ -55,10 +57,12 @@ public:
glm::vec3 cam {}; glm::vec3 cam {};
BlockSelection selection {}; BlockSelection selection {};
Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv); Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv,
entityid_t eid);
~Player(); ~Player();
void teleport(glm::vec3 position); void teleport(glm::vec3 position);
void updateEntity(Level* level);
void updateInput(Level* level, PlayerInput& input, float delta); void updateInput(Level* level, PlayerInput& input, float delta);
void attemptToFindSpawnpoint(Level* level); void attemptToFindSpawnpoint(Level* level);
@ -74,6 +78,9 @@ public:
bool isNoclip() const; bool isNoclip() const;
void setNoclip(bool flag); void setNoclip(bool flag);
entityid_t getEntity() const;
void setEntity(entityid_t eid);
std::shared_ptr<Inventory> getInventory() const; std::shared_ptr<Inventory> getInventory() const;
void setSpawnPoint(glm::vec3 point); void setSpawnPoint(glm::vec3 point);

View File

@ -31,7 +31,7 @@ Level::Level(
this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
); );
auto player = spawnObject<Player>( auto player = spawnObject<Player>(
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv, 0
); );
uint matrixSize = ( uint matrixSize = (

View File

@ -111,7 +111,8 @@ std::unique_ptr<Level> World::load(
auto player = level->spawnObject<Player>( auto player = level->spawnObject<Player>(
glm::vec3(0, DEF_PLAYER_Y, 0), glm::vec3(0, DEF_PLAYER_Y, 0),
DEF_PLAYER_SPEED, DEF_PLAYER_SPEED,
level->inventories->create(DEF_PLAYER_INVENTORY_SIZE) level->inventories->create(DEF_PLAYER_INVENTORY_SIZE),
0
); );
player->deserialize(players->map(i).get()); player->deserialize(players->map(i).get());
level->inventories->store(player->getInventory()); level->inventories->store(player->getInventory());