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);
updateCamera(delta, input);
updateControls(delta);
updatePlayer(delta);
}
camControl.refresh();
@ -288,7 +288,8 @@ void PlayerController::resetKeyboard() {
input.jump = false;
}
void PlayerController::updateControls(float delta){
void PlayerController::updatePlayer(float delta) {
player->updateEntity(level);
player->updateInput(level, input, delta);
}

View File

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

View File

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

View File

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

View File

@ -31,7 +31,7 @@ Level::Level(
this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
);
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 = (

View File

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