Peoplemon  0.1.0
Peoplemon 3 game source documentation
Player.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Logging.hpp>
4 #include <BLIB/Util/Random.hpp>
8 #include <Core/Items/Item.hpp>
9 #include <Core/Maps/Map.hpp>
10 #include <Core/Properties.hpp>
11 #include <Core/Systems/Systems.hpp>
12 
13 namespace core
14 {
15 namespace system
16 {
17 namespace
18 {
19 constexpr float LanternRadius = 32.f * 6.f;
20 constexpr float MaxVariance = 19.f;
21 constexpr float MaxVarianceHoldtime = 0.22f;
22 constexpr float VarianceMinConvergeRate = MaxVariance / 0.25f;
23 constexpr float VarianceMaxConvergeRate = MaxVariance / 0.15f;
24 constexpr glm::vec3 LanternColor(0.94f, 0.94f, 0.05f);
25 } // namespace
26 
27 using Serializer = bl::serial::json::Serializer<Player>;
28 
30 : owner(owner) {}
31 
32 bool Player::spawnPlayer(const bl::tmap::Position& pos, map::Map& map) {
33  playerId = owner.engine().ecs().createEntity(bl::ecs::Flags::WorldObject);
34  BL_LOG_INFO << "New player id: " << playerId;
35 
36  _position = owner.engine().ecs().addComponent<bl::tmap::Position>(playerId, pos);
37  owner.engine().ecs().addComponent<component::Collision>(playerId, {});
38 
39  movable = owner.engine().ecs().addComponent<component::Movable>(
40  playerId,
42 
43  owner.engine().ecs().addComponent<component::Controllable>(playerId, {owner, playerId});
44 
45  if (!makePlayerControlled(playerId)) { return false; }
46 
48  owner.engine(), playerId, map.getScene(), Properties::PlayerAnimations(data.sex));
49 
50  map.setupEntityPosition(playerId);
51 
52  return true;
53 }
54 
55 bl::ecs::Entity Player::player() const { return playerId; }
56 
57 const bl::tmap::Position& Player::position() const { return *_position; }
58 
59 void Player::newGame(const std::string& n, player::Gender g) {
60  data.name = n;
61  data.sex = g;
62  data.bag.clear();
63  data.monei = 0;
64  data.peoplemon.clear();
65 
66 #ifdef PEOPLEMON_DEBUG
67  data.monei = 100000;
68  data.peoplemon.emplace_back(pplmn::Id::AnnaA, 30);
69  data.peoplemon.back().learnMove(pplmn::MoveId::Awkwardness, 0);
70  data.peoplemon.back().learnMove(pplmn::MoveId::Confuse, 1);
71  data.peoplemon.back().learnMove(pplmn::MoveId::MedicalAttention, 2);
72  data.peoplemon.back().learnMove(pplmn::MoveId::Oblivious, 3);
73 #endif
74 }
75 
76 bool Player::makePlayerControlled(bl::ecs::Entity entity) {
77  component::Controllable* controllable =
78  owner.engine().ecs().getComponent<component::Controllable>(entity);
79  if (!controllable) {
80  BL_LOG_ERROR << "Failed to get controllable component handle for " << entity;
81  return false;
82  }
83 
84  component::PlayerControlled* p = owner.engine().ecs().addComponent<component::PlayerControlled>(
85  entity, {owner, *controllable});
86  p->start();
87 
88  return true;
89 }
90 
91 void Player::removePlayerControlled(bl::ecs::Entity e) {
92  owner.engine().ecs().removeComponent<component::PlayerControlled>(e);
93 }
94 
95 void Player::init(bl::engine::Engine&) { bl::event::Dispatcher::subscribe(this); }
96 
98  data.healPeoplemon();
99  owner.world().whiteout(data.whiteoutMap, data.whiteoutSpawn);
100 }
101 
102 void Player::update(std::mutex&, float dt, float, float, float) {
103  if (lantern.isValid()) { updateLantern(dt); }
104 }
105 
107  if (!lantern.isValid()) {
108  lanternVariance = 0.f;
109  lanternTargetVariance = 0.f;
110  lantern = owner.world().activeMap().getSceneLighting().addLight(
111  _position->getWorldPosition(Properties::PixelsPerTile()), LanternRadius, LanternColor);
112  startLanternVarianceHold();
113  }
114 }
115 
116 void Player::updateLantern(float dt) {
117  auto& lighting = owner.world().activeMap().lightingSystem();
118 
119  if (lighting.lightsAreOn()) {
120  if (lanternVariance != lanternTargetVariance) {
121  if (lanternVariance < lanternTargetVariance) {
122  lanternVariance += varianceConvergeRate * dt;
123  if (lanternVariance >= lanternTargetVariance) { startLanternVarianceHold(); }
124  }
125  else {
126  lanternVariance -= varianceConvergeRate * dt;
127  if (lanternVariance <= lanternTargetVariance) { startLanternVarianceHold(); }
128  }
129  }
130  else {
131  varianceSwitchTime -= dt;
132  if (varianceSwitchTime <= 0.f) { startLanternVarianceChange(); }
133  }
134 
135  glm::vec2 lpos = _position->getWorldPosition(Properties::PixelsPerTile());
136  lpos.x += Properties::PixelsPerTile();
137  lantern.setPosition(lpos);
138  lantern.setRadius(LanternRadius + lanternVariance);
139  }
140  else { lantern.removeFromScene(); }
141 }
142 
143 void Player::startLanternVarianceHold() {
144  lanternVariance = lanternTargetVariance;
145  varianceSwitchTime = bl::util::Random::get<float>(0.1f, MaxVarianceHoldtime);
146 }
147 
148 void Player::startLanternVarianceChange() {
149  lanternTargetVariance = bl::util::Random::get<float>(-MaxVariance, MaxVariance);
151  bl::util::Random::get<float>(VarianceMinConvergeRate, VarianceMaxConvergeRate);
152 }
153 
154 player::State& Player::state() { return data; }
155 
156 const player::State& Player::state() const { return data; }
157 
158 void Player::observe(const event::GameSaveInitializing& save) {
159  save.gameSave.player.inventory = &data.bag;
160  save.gameSave.player.monei = &data.monei;
161  save.gameSave.player.peoplemon = &data.peoplemon;
162  save.gameSave.player.playerName = &data.name;
163  save.gameSave.player.sex = &data.sex;
164  save.gameSave.player.whiteoutMap = &data.whiteoutMap;
166  save.gameSave.player.repelSteps = &data.repelSteps;
167  save.gameSave.player.storage = &data.storage.boxes;
169  save.gameSave.player.seenPeoplemon = &data.peopledex.seenCounts;
170  save.gameSave.player.firstSightingLocations = &data.peopledex.firstSawLocations;
171 }
172 
173 void Player::observe(const event::EntityMoveFinished& ent) {
174  if (ent.entity == playerId) {
175  if (data.repelSteps > 0) {
176  --data.repelSteps;
177  if (data.repelSteps == 0) { owner.hud().displayMessage("Repel wore off"); }
178  }
179 
180  // MrExtra ability
181  for (auto& ppl : data.peoplemon) {
182  if (ppl.holdItem() != item::Id::None) continue;
183 
184  if (ppl.ability() == pplmn::SpecialAbility::MrExtra) {
185  if (bl::util::Random::get<int>(0, 1000) < 5) {
186  item::Id pickedUp;
187  do {
188  pickedUp = item::Item::validIds()[bl::util::Random::get<unsigned int>(
189  0, item::Item::validIds().size() - 1)];
190  } while (item::Item::getCategory(pickedUp) == item::Category::Key);
191 
192  ppl.holdItem() = pickedUp;
193  owner.hud().displayMessage(ppl.name() + " picked up a " +
194  item::Item::getName(pickedUp) +
195  " they found on the ground.");
196  }
197  }
198  }
199  }
200 }
201 
202 } // namespace system
203 } // namespace core
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
@ Key
Key items. Covers ids [101, 200].
Gender
Possible genders for the player.
Definition: Gender.hpp:24
Core classes and functionality for both the editor and game.
bl::serial::json::Serializer< Player > Serializer
Definition: Player.cpp:27
Empty component to indicate that an entity cannot be moved through.
Definition: Collision.hpp:14
Adding this component to an entity allows it to be controlled.
Adding this component to an entity will allow it to move.
Definition: Movable.hpp:26
Add this component to an entity to make it controlled by player input.
void start()
Activate this listener and receive player input. Does not have effect if already activated but anothe...
static Renderable & createFromFastMoveAnims(bl::engine::Engine &engine, bl::ecs::Entity entity, bl::rc::Scene *scene, const std::string &path)
Creates a renderable component for movement animations with running.
Definition: Renderable.cpp:49
Fired when an entity completes a move from one tile to another.
Definition: EntityMoved.hpp:52
const bl::ecs::Entity entity
The entity that moved.
Definition: EntityMoved.hpp:54
Fired when the game is saving or loading. Allows systems to hook in their data.
Definition: GameSave.hpp:22
file::GameSave & gameSave
Game save being initialized.
Definition: GameSave.hpp:24
struct core::file::GameSave::PlayerDataPointers player
std::unordered_map< pplmn::Id, std::string > * firstSightingLocations
Definition: GameSave.hpp:50
std::array< std::vector< pplmn::StoredPeoplemon >, player::StorageSystem::BoxCount > * storage
Definition: GameSave.hpp:47
std::unordered_set< std::string > * visitedTowns
Definition: GameSave.hpp:48
std::vector< pplmn::OwnedPeoplemon > * peoplemon
Definition: GameSave.hpp:43
std::unordered_map< pplmn::Id, std::uint32_t > * seenPeoplemon
Definition: GameSave.hpp:49
static Category getCategory(Id item)
Returns the category of the given item.
Definition: Item.cpp:49
static const std::string & getName(item::Id item)
Returns the name of the given item.
Definition: Item.cpp:91
static const std::vector< Id > & validIds()
Returns the list of valid item ids.
Definition: Item.cpp:125
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
LightingSystem & lightingSystem()
Returns a reference to the lighting system in this map.
Definition: Map.cpp:184
bl::rc::lgt::Scene2DLighting & getSceneLighting()
Returns the scene lighting for this map. Only valid after enter() is called.
Definition: Map.hpp:285
bl::rc::SceneRef getScene()
Returns the scene for this map.
Definition: Map.hpp:280
void setupEntityPosition(bl::ecs::Entity entity)
Performs the final setup of the position components for the given entity. Must already have a bl::tma...
Definition: Map.cpp:792
void clear()
Removes all items.
Definition: Bag.cpp:64
Stores all player state.
Definition: State.hpp:21
StorageSystem storage
Definition: State.hpp:47
unsigned int whiteoutSpawn
Definition: State.hpp:53
std::vector< pplmn::OwnedPeoplemon > peoplemon
Definition: State.hpp:46
std::string name
Definition: State.hpp:42
void healPeoplemon()
Restores HP and removes all ailments.
Definition: State.cpp:16
std::string whiteoutMap
Definition: State.hpp:52
unsigned int repelSteps
Definition: State.hpp:54
player::Gender sex
Definition: State.hpp:43
Peopledex peopledex
Definition: State.hpp:49
player::Bag bag
Definition: State.hpp:44
std::unordered_set< std::string > visitedTowns
Definition: State.hpp:48
static int PixelsPerTile()
Definition: Properties.cpp:279
static const std::string & PlayerAnimations(player::Gender gender)
Definition: Properties.cpp:573
static float CharacterMoveSpeed()
Definition: Properties.cpp:563
static float FastCharacterMoveSpeed()
Definition: Properties.cpp:568
void displayMessage(const std::string &message, const Callback &cb=[](const std::string &) {})
Displays a message in the HUD textbox. Messages are queued in order that they arrive.
Definition: HUD.cpp:92
float varianceConvergeRate
Definition: Player.hpp:128
float varianceSwitchTime
Definition: Player.hpp:129
void showLantern()
Creates a light for the player's lantern. Keeps it updated as well.
Definition: Player.cpp:106
player::State & state()
Returns the state of the player.
Definition: Player.cpp:154
Player(Systems &owner)
Construct a new Player system.
Definition: Player.cpp:29
void newGame(const std::string &name, player::Gender gender)
Initializes all player data structures for a new game.
Definition: Player.cpp:59
const bl::tmap::Position & position() const
Returns the current position of the player.
Definition: Player.cpp:57
void whiteout()
Heals all Peoplemon and respawns at the last PC center.
Definition: Player.cpp:97
void removePlayerControlled(bl::ecs::Entity entity)
Removes the PlayerControlled component from the given entity, if any.
Definition: Player.cpp:91
bool spawnPlayer(const bl::tmap::Position &position, map::Map &map)
Spawns the player into the world.
Definition: Player.cpp:32
bl::ecs::Entity player() const
Returns the id of the player entity.
Definition: Player.cpp:55
bool makePlayerControlled(bl::ecs::Entity entity)
Makes the given entity controlled by the player. Only one entity may be player controlled at a time.
Definition: Player.cpp:76
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Definition: Systems.cpp:35
HUD & hud()
Returns the HUD.
Definition: Systems.cpp:69
World & world()
Modifiable accessor for the world system.
Definition: Systems.cpp:43
void whiteout(const std::string &newMap, int spawnId)
Switches to the new map and resets the last map to empty.
Definition: World.cpp:72
map::Map & activeMap()
Returns a reference to the active map.
Definition: World.cpp:84