Lodestone.Level
Loading...
Searching...
No Matches
lodestone::level::entity::Player Class Referenceabstract

#include <Player.h>

Inheritance diagram for lodestone::level::entity::Player:
Collaboration diagram for lodestone::level::entity::Player:

Public Member Functions

virtual const std::string & getId () const =0
 Can return a UUID, player name, or other depending on the version.
std::string toString () const override
world::World * getWorld () const
bool isInWorld () const
level::Level * getLevel () const
bool isInLevel () const
virtual void respawn (bool inDefaultLevel=true)
bool isOnGround () const
std::unique_ptr< level::properties::AbstractProperty > getProperty (const std::string &name) override
Public Member Functions inherited from lodestone::level::entity::Entity
 Entity ()=default
 ~Entity () override=default
int getHealth () const
void setHealth (int health)
bool isDead () const
virtual int getMaxHealth () const
const types::Vec3d & getMotion () const
virtual void setMotion (const types::Vec3d &motion)
 Sets the entity's motion.
void setMotion (double x, double y, double z)
void resetCoords ()
Public Member Functions inherited from lodestone::level::entity::WorldSpaceObject
 WorldSpaceObject ()
virtual ~WorldSpaceObject ()=default
types::Vec3f getFront () const
types::Vec3f getDirection () const
types::Vec3d getTarget () const
Public Member Functions inherited from lodestone::common::string::StringSerializable
constexpr StringSerializable ()=default
virtual constexpr ~StringSerializable ()=default
Public Member Functions inherited from lodestone::level::properties::ReflectiveProperties
virtual ~ReflectiveProperties ()=default
template<typename T>
std::unique_ptr< TemplatedProperty< T > > getProperty (const std::string &name)
template<typename T>
std::unique_ptr< TemplatedProperty< T > > getPropertyOr (const std::string &name, T value)

Protected Member Functions

void setLevel (level::Level *level, bool resetCoords=true)
 Sets the player's level.
void setWorld (world::World *world, bool resetCoords=true)
 Sets the player's world.
const common::registry::Identifier * getType () const override

Private Attributes

world::World * m_world = nullptr
level::Level * m_currentLevel = nullptr

Friends

class lodestone::level::world::World

Additional Inherited Members

Public Attributes inherited from lodestone::level::entity::WorldSpaceObject
types::Vec3d position = types::VEC3_ZERO<double>
types::Vec2f rotation = types::VEC2_ZERO<float>
Static Public Attributes inherited from lodestone::level::entity::Entity
static constexpr int MAX_HEALTH = 20
Protected Attributes inherited from lodestone::level::entity::Entity
types::Vec3d m_motion {0, 0, 0}
 The motion that the entity has.
int m_health = MAX_HEALTH

Member Function Documentation

◆ getId()

virtual const std::string & lodestone::level::entity::Player::getId ( ) const
pure virtual

Can return a UUID, player name, or other depending on the version.

◆ getLevel()

Level * lodestone::level::entity::Player::getLevel ( ) const
50{ return m_currentLevel; }
level::Level * m_currentLevel
Definition Player.h:54

◆ getProperty()

std::unique_ptr< level::properties::AbstractProperty > lodestone::level::entity::Player::getProperty ( const std::string & name)
overridevirtual

Reimplemented from lodestone::level::properties::ReflectiveProperties.

93 {
94 return nullptr;
95 }

◆ getType()

const common::registry::Identifier * lodestone::level::entity::Player::getType ( ) const
overrideprotectedvirtual

Reimplemented from lodestone::level::entity::Entity.

46 {
47 return &PLAYER;
48 }
static constexpr lodestone::common::registry::Identifier PLAYER
Definition Entities.h:10

◆ getWorld()

world::World * lodestone::level::entity::Player::getWorld ( ) const
10{ return m_world; }
world::World * m_world
Definition Player.h:53

◆ isInLevel()

bool lodestone::level::entity::Player::isInLevel ( ) const
52{ return getLevel(); }
level::Level * getLevel() const
Definition Player.cpp:50

◆ isInWorld()

bool lodestone::level::entity::Player::isInWorld ( ) const
12{ return getWorld(); }
world::World * getWorld() const
Definition Player.cpp:10

◆ isOnGround()

bool lodestone::level::entity::Player::isOnGround ( ) const
70 {
71 const Level *lvl = this->getLevel();
72 if (lvl == nullptr)
73 return false;
74
75 double t;
76 // if there's no fraction then we know the player is standing ON the
77 // block This however will NOT work with blocks that have alt AABBs, as
78 // the fraction most definitely won't be 0. but it should suffice for
79 // most cases.
80 //
81 // Additionally, we don't check for collision, but this *probably* won't
82 // be an issue as the game will reset it afterward anyway.
83 if (std::modf(this->position.y, &t) == 0 &&
84 lvl->getBlock(this->position.x, this->position.y - 1,
85 this->position.z)
86 .getBlock()->hasCollision())
87 return true;
88
89 return false;
90 }
types::Vec3d position
Definition WorldSpaceObject.h:20

◆ respawn()

void lodestone::level::entity::Player::respawn ( bool inDefaultLevel = true)
virtual
54 {
55 this->resetCoords();
56
57 if (const world::World *wld = getWorld();
58 inDefaultLevel && wld && wld->getDefaultLevel()) {
59 this->position =
60 static_cast<types::Vec3d>(wld->getDefaultLevel()->getSpawnPos());
61 } else if (const Level *lvl = this->getLevel(); lvl != nullptr) {
62 this->position = static_cast<types::Vec3d>(lvl->getSpawnPos());
63 } else {
64 this->position = {0, 64, 0};
65 }
66
67 this->m_health = getMaxHealth();
68 }
virtual int getMaxHealth() const
Definition Entity.cpp:17
void resetCoords()
Definition Entity.cpp:31
int m_health
Definition Entity.h:56
virtual Level * getDefaultLevel() const
Definition World.cpp:46
Vec3< double > Vec3d
Definition Vec3.h:177

◆ setLevel()

void lodestone::level::entity::Player::setLevel ( level::Level * level,
bool resetCoords = true )
protected

Sets the player's level.

NOTE: if the level is contained within a different world, the player will be moved to that world.

14 {
15 if (!level)
16 throw std::runtime_error(
17 "Attempted to move player into null level");
18
19 if (this->m_currentLevel != level) {
20 this->m_currentLevel = level;
21
22 this->m_world = level->getWorld();
23
24 if (resetCoords) {
25 this->resetCoords();
26 }
27 }
28 }

◆ setWorld()

void lodestone::level::entity::Player::setWorld ( world::World * world,
bool resetCoords = true )
protected

Sets the player's world.

NOTE: if the current level does not exist within the given world, the player will be moved to the default level.

30 {
31 if (!world)
32 throw std::runtime_error(
33 "Attempted to move player into null world");
34
35 this->m_world = world;
36
37 if (!(this->m_currentLevel &&
38 this->m_currentLevel->getWorld() == world)) {
39 this->m_currentLevel = world->getDefaultLevel();
40 if (resetCoords) {
41 this->resetCoords();
42 }
43 }
44 }

◆ toString()

std::string lodestone::level::entity::Player::toString ( ) const
inlineoverridevirtual

Reimplemented from lodestone::level::entity::Entity.

19 {
20 return std::format("Player[id={}, health={}, pos={}]", this->getId(), this->m_health, this->position.toString());
21 };
virtual const std::string & getId() const =0
Can return a UUID, player name, or other depending on the version.

◆ lodestone::level::world::World

friend class lodestone::level::world::World
friend

Member Data Documentation

◆ m_currentLevel

level::Level* lodestone::level::entity::Player::m_currentLevel = nullptr
private

◆ m_world

world::World* lodestone::level::entity::Player::m_world = nullptr
private

The documentation for this class was generated from the following files: