fixed player spawn point
This commit is contained in:
@@ -542,6 +542,12 @@ void WorldFiles::writePlayer(Player* player) {
|
|||||||
rotarr.put(player->cam.x);
|
rotarr.put(player->cam.x);
|
||||||
rotarr.put(player->cam.y);
|
rotarr.put(player->cam.y);
|
||||||
|
|
||||||
|
auto& sparr = root.putList("spawnpoint");
|
||||||
|
glm::vec3 spawnpoint = player->getSpawnPoint();
|
||||||
|
sparr.put(spawnpoint.x);
|
||||||
|
sparr.put(spawnpoint.y);
|
||||||
|
sparr.put(spawnpoint.z);
|
||||||
|
|
||||||
root.put("flight", player->flight);
|
root.put("flight", player->flight);
|
||||||
root.put("noclip", player->noclip);
|
root.put("noclip", player->noclip);
|
||||||
root.put("chosen-slot", player->getChosenSlot());
|
root.put("chosen-slot", player->getChosenSlot());
|
||||||
@@ -569,6 +575,17 @@ bool WorldFiles::readPlayer(Player* player) {
|
|||||||
player->cam.x = rotarr->num(0);
|
player->cam.x = rotarr->num(0);
|
||||||
player->cam.y = rotarr->num(1);
|
player->cam.y = rotarr->num(1);
|
||||||
|
|
||||||
|
if (root->has("spawnpoint")) {
|
||||||
|
auto sparr = root->list("spawnpoint");
|
||||||
|
player->setSpawnPoint(glm::vec3(
|
||||||
|
sparr->num(0),
|
||||||
|
sparr->num(1),
|
||||||
|
sparr->num(2)
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
player->setSpawnPoint(position);
|
||||||
|
}
|
||||||
|
|
||||||
root->flag("flight", player->flight);
|
root->flag("flight", player->flight);
|
||||||
root->flag("noclip", player->noclip);
|
root->flag("noclip", player->noclip);
|
||||||
player->setChosenSlot(root->getInt("chosen-slot", player->getChosenSlot()));
|
player->setChosenSlot(root->getInt("chosen-slot", player->getChosenSlot()));
|
||||||
|
|||||||
@@ -117,12 +117,33 @@ void Player::update(
|
|||||||
|
|
||||||
input.noclip = false;
|
input.noclip = false;
|
||||||
input.flight = false;
|
input.flight = false;
|
||||||
|
|
||||||
|
if (spawnpoint.y <= 0.1) {
|
||||||
|
attemptToFindSpawnpoint(level);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::teleport(glm::vec3 position) {
|
void Player::teleport(glm::vec3 position) {
|
||||||
hitbox->position = position;
|
hitbox->position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Player::attemptToFindSpawnpoint(Level* level) {
|
||||||
|
glm::vec3 ppos = hitbox->position;
|
||||||
|
glm::vec3 newpos {ppos.x + (rand() % 200 - 100),
|
||||||
|
rand() % 80 + 100,
|
||||||
|
ppos.z + (rand() % 200 - 100)};
|
||||||
|
while (newpos.y > 0 && !level->chunks->isObstacleBlock(newpos.x, newpos.y-2, newpos.z)) {
|
||||||
|
newpos.y--;
|
||||||
|
}
|
||||||
|
|
||||||
|
voxel* headvox = level->chunks->get(newpos.x, newpos.y+1, newpos.z);
|
||||||
|
if (level->chunks->isObstacleBlock(newpos.x, newpos.y, newpos.z) ||
|
||||||
|
headvox == nullptr || headvox->id != 0)
|
||||||
|
return;
|
||||||
|
spawnpoint = newpos;
|
||||||
|
teleport(spawnpoint);
|
||||||
|
}
|
||||||
|
|
||||||
void Player::setChosenSlot(int index) {
|
void Player::setChosenSlot(int index) {
|
||||||
chosenSlot = index;
|
chosenSlot = index;
|
||||||
}
|
}
|
||||||
@@ -138,3 +159,11 @@ float Player::getSpeed() const {
|
|||||||
std::shared_ptr<Inventory> Player::getInventory() const {
|
std::shared_ptr<Inventory> Player::getInventory() const {
|
||||||
return inventory;
|
return inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Player::setSpawnPoint(glm::vec3 spawnpoint) {
|
||||||
|
this->spawnpoint = spawnpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Player::getSpawnPoint() const {
|
||||||
|
return spawnpoint;
|
||||||
|
}
|
||||||
|
|||||||
+31
-25
@@ -15,47 +15,53 @@ class Chunks;
|
|||||||
class Level;
|
class Level;
|
||||||
|
|
||||||
struct PlayerInput {
|
struct PlayerInput {
|
||||||
bool zoom;
|
bool zoom;
|
||||||
bool cameraMode;
|
bool cameraMode;
|
||||||
bool moveForward;
|
bool moveForward;
|
||||||
bool moveBack;
|
bool moveBack;
|
||||||
bool moveRight;
|
bool moveRight;
|
||||||
bool moveLeft;
|
bool moveLeft;
|
||||||
bool sprint;
|
bool sprint;
|
||||||
bool shift;
|
bool shift;
|
||||||
bool cheat;
|
bool cheat;
|
||||||
bool jump;
|
bool jump;
|
||||||
bool noclip;
|
bool noclip;
|
||||||
bool flight;
|
bool flight;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Player {
|
class Player {
|
||||||
float speed;
|
float speed;
|
||||||
int chosenSlot;
|
int chosenSlot;
|
||||||
|
glm::vec3 spawnpoint {};
|
||||||
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;
|
||||||
std::unique_ptr<Hitbox> hitbox;
|
std::unique_ptr<Hitbox> hitbox;
|
||||||
std::shared_ptr<Inventory> inventory;
|
std::shared_ptr<Inventory> inventory;
|
||||||
bool flight = false;
|
bool flight = false;
|
||||||
bool noclip = false;
|
bool noclip = false;
|
||||||
bool debug = false;
|
bool debug = false;
|
||||||
voxel selectedVoxel {0, 0};
|
voxel selectedVoxel {0, 0};
|
||||||
|
|
||||||
glm::vec2 cam = {};
|
glm::vec2 cam = {};
|
||||||
|
|
||||||
Player(glm::vec3 position, float speed);
|
Player(glm::vec3 position, float speed);
|
||||||
~Player();
|
~Player();
|
||||||
|
|
||||||
void teleport(glm::vec3 position);
|
void teleport(glm::vec3 position);
|
||||||
void update(Level* level, PlayerInput& input, float delta);
|
void update(Level* level, PlayerInput& input, float delta);
|
||||||
|
|
||||||
|
void attemptToFindSpawnpoint(Level* level);
|
||||||
|
|
||||||
void setChosenSlot(int index);
|
void setChosenSlot(int index);
|
||||||
|
|
||||||
int getChosenSlot() const;
|
int getChosenSlot() const;
|
||||||
float getSpeed() const;
|
float getSpeed() const;
|
||||||
|
|
||||||
std::shared_ptr<Inventory> getInventory() const;
|
std::shared_ptr<Inventory> getInventory() const;
|
||||||
|
|
||||||
|
void setSpawnPoint(glm::vec3 point);
|
||||||
|
glm::vec3 getSpawnPoint() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SRC_OBJECTS_PLAYER_H_ */
|
#endif /* SRC_OBJECTS_PLAYER_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user