Peoplemon  0.1.0
Peoplemon 3 game source documentation
Entity.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Util/FileUtil.hpp>
10 #include <Core/Files/NPC.hpp>
11 #include <Core/Files/Trainer.hpp>
12 #include <Core/Items/Item.hpp>
14 #include <Core/Maps/Map.hpp>
15 #include <Core/Properties.hpp>
16 #include <Core/Systems/Systems.hpp>
17 
18 namespace core
19 {
20 namespace system
21 {
23 : owner(owner) {}
24 
25 bl::ecs::Entity Entity::spawnCharacter(const map::CharacterSpawn& spawn, map::Map& map) {
26  bl::ecs::Entity entity = owner.engine().ecs().createEntity(bl::ecs::Flags::WorldObject);
27  std::string animation;
28  BL_LOG_DEBUG << "Created character entity " << entity;
29 
30  bl::ecs::Cleaner cleaner(owner.engine().ecs(), entity);
31 
32  // Common components
33  bl::tmap::Position* pos =
34  owner.engine().ecs().addComponent<bl::tmap::Position>(entity, spawn.position);
35  owner.engine().ecs().addComponent<component::Collision>(entity, {});
36  component::Movable* mover = owner.engine().ecs().addComponent<component::Movable>(
37  entity, {*pos, Properties::CharacterMoveSpeed(), 0.f});
38  owner.engine().ecs().addComponent<component::Controllable>(entity, {owner, entity});
39 
41  if (bl::util::FileUtil::getExtension(spawn.file) == Properties::NpcFileExtension()) {
42  file::NPC data;
43  if (!data.load(bl::util::FileUtil::joinPath(Properties::NpcPath(), spawn.file),
44  spawn.position.direction)) {
45  BL_LOG_ERROR << "Failed to load NPC: " << spawn.file;
46  return bl::ecs::InvalidEntity;
47  }
48  BL_LOG_INFO << "Spawning NPC " << data.name() << " at ("
49  << static_cast<int>(spawn.position.level) << ", " << spawn.position.position.x
50  << ", " << spawn.position.position.y << ") id=" << entity;
51 
52  if (!owner.ai().addBehavior(entity, data.behavior())) {
53  BL_LOG_ERROR << "Failed to add behavior to spawned npc: " << entity;
54  return bl::ecs::InvalidEntity;
55  }
56 
57  animation = data.animation();
58  owner.engine().ecs().addComponent<component::NPC>(entity, component::NPC(data));
59  }
60 
61  // Trainer
62  else if (bl::util::FileUtil::getExtension(spawn.file) == Properties::TrainerFileExtension()) {
63  file::Trainer data;
64  if (!data.load(bl::util::FileUtil::joinPath(Properties::TrainerPath(), spawn.file),
65  spawn.position.direction)) {
66  BL_LOG_ERROR << "Failed to load trainer: " << spawn.file;
67  return bl::ecs::InvalidEntity;
68  }
69  BL_LOG_INFO << "Spawning trainer " << data.name << " at ("
70  << static_cast<int>(spawn.position.level) << ", " << spawn.position.position.x
71  << ", " << spawn.position.position.y << ") id=" << entity;
72 
73  if (!owner.ai().addBehavior(entity, data.behavior)) {
74  BL_LOG_ERROR << "Failed to add behavior to spawned npc: " << entity;
75  return bl::ecs::InvalidEntity;
76  }
77 
78  animation = data.animation;
79  owner.engine().ecs().emplaceComponent<component::Trainer>(entity, std::move(data));
80  }
81  else {
82  BL_LOG_ERROR << "Unknown character file type: " << spawn.file;
83  return bl::ecs::InvalidEntity;
84  }
85 
88  owner.engine(),
89  entity,
90  map.getScene(),
91  bl::util::FileUtil::joinPath(Properties::CharacterAnimationPath(), animation));
92  map.setupEntityPosition(entity);
93 
94  cleaner.disarm();
95  return entity;
96 }
97 
98 bool Entity::spawnItem(const map::Item& item, map::Map& map) {
99  const item::Id id = item::Item::cast(item.id);
100  BL_LOG_INFO << "Spawning item " << item.id << " (" << item::Item::getName(id) << ") at ("
101  << item.position.x << " , " << item.position.y << ")";
102  if (id == item::Id::Unknown) {
103  BL_LOG_ERROR << "Unknown item id: " << item.id;
104  return false;
105  }
106 
107  const bl::ecs::Entity entity = owner.engine().ecs().createEntity(bl::ecs::Flags::WorldObject);
108 
109  bl::tmap::Position* pos = owner.engine().ecs().emplaceComponent<bl::tmap::Position>(
110  entity,
111  item.level,
112  glm::i32vec2(item.position.x, item.position.y),
113  bl::tmap::Direction::Up);
114  owner.engine().ecs().addComponent<component::Item>(entity, {id});
115 
116  if (item.visible || Properties::InEditor()) {
117  owner.engine().ecs().addComponent<component::Collision>(entity, {});
119  owner.engine(), entity, map.getScene(), Properties::ItemSprite());
120  owner.engine().ecs().getComponent<bl::com::Transform2D>(entity)->setOrigin(0.f, 0.f);
121  map.setupEntityPosition(entity);
122  }
123 
124  return true;
125 }
126 
127 bl::ecs::Entity Entity::spawnGeneric(std::uint8_t level, const glm::i32vec2& tiles, bool collidable,
128  const std::string& gfx, map::Map& map) {
129  bl::ecs::Entity entity = owner.engine().ecs().createEntity(bl::ecs::Flags::WorldObject);
130  bl::ecs::Cleaner cleaner(owner.engine().ecs(), entity);
131  BL_LOG_DEBUG << "Created generic entity " << entity;
132 
133  bl::tmap::Position* pos = owner.engine().ecs().emplaceComponent<bl::tmap::Position>(
134  entity, level, tiles, bl::tmap::Direction::Up);
135  const bool isAnim = bl::util::FileUtil::getExtension(gfx) == "anim";
136  if (isAnim) {
137  component::Renderable::createFromAnimation(owner.engine(), entity, map.getScene(), gfx);
138  }
139  else {
141  owner.engine(),
142  entity,
143  map.getScene(),
144  bl::util::FileUtil::joinPath(Properties::ImagePath(), gfx));
145  }
146 
147  map.setupEntityPosition(entity);
148 
149  if (collidable) { owner.engine().ecs().addComponent<component::Collision>(entity, {}); }
150 
151  cleaner.disarm();
152  return entity;
153 }
154 
155 bl::ecs::Entity Entity::spawnAnimation(std::uint8_t level, const glm::vec2& worldPos,
156  const std::string& gfx, map::Map& map) {
157  bl::ecs::Entity entity = owner.engine().ecs().createEntity(bl::ecs::Flags::WorldObject);
158  bl::ecs::Cleaner cleaner(owner.engine().ecs(), entity);
159  BL_LOG_DEBUG << "Created animation entity " << entity;
160 
161  component::Renderable::createFromAnimation(owner.engine(), entity, map.getScene(), gfx);
162 
163  cleaner.disarm();
164  return entity;
165 }
166 
167 } // namespace system
168 } // namespace core
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
Core classes and functionality for both the editor and game.
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.
Add this component to an entity to make it give an item when interacted with. On interact the owning ...
Definition: Item.hpp:17
Adding this component to an entity will allow it to move.
Definition: Movable.hpp:26
Adding this to an entity will allow it to be interacted with and talked to.
Definition: NPC.hpp:17
static Renderable & createFromAnimation(bl::engine::Engine &engine, bl::ecs::Entity entity, bl::rc::Scene *scene, const std::string &path)
Creates a renderable component from a single animation.
Definition: Renderable.cpp:71
static Renderable & createFromSprite(bl::engine::Engine &engine, bl::ecs::Entity entity, bl::rc::Scene *scene, const std::string &path)
Creates a renderable component for a static sprite.
Definition: Renderable.cpp:12
static Renderable & createFromMoveAnims(bl::engine::Engine &engine, bl::ecs::Entity entity, bl::rc::Scene *scene, const std::string &path)
Creates a renderable component for movement animations.
Definition: Renderable.cpp:28
Adding this to an entity will make it a trainer that can battle.
Definition: Trainer.hpp:18
File data for NPCs.
Definition: NPC.hpp:26
Behavior & behavior()
The behavior of the NPC.
Definition: NPC.cpp:62
std::string & name()
The name of the NPC.
Definition: NPC.cpp:50
bool load(const std::string &file, bl::tmap::Direction spawnDir=bl::tmap::Direction::Up)
Loads the NPC data from the given file.
Definition: NPC.cpp:36
std::string & animation()
The directory of the movement animations.
Definition: NPC.cpp:54
Static data for trainers in the world.
Definition: Trainer.hpp:20
std::string animation
Definition: Trainer.hpp:72
std::string name
Definition: Trainer.hpp:71
bool load(const std::string &file, bl::tmap::Direction spawnDir=bl::tmap::Direction::Down)
Loads the trainer data from the given file.
Definition: Trainer.cpp:43
Behavior behavior
Definition: Trainer.hpp:77
static const std::string & getName(item::Id item)
Returns the name of the given item.
Definition: Item.cpp:91
static Id cast(unsigned int id)
Helper function to cast a raw id to an item Id.
Definition: Item.cpp:31
Represents a character to be spawned into a map on load.
bl::tmap::Position position
Basic struct representing a pickup-able item in Map.
Definition: Item.hpp:16
std::uint16_t id
Definition: Item.hpp:17
std::uint8_t level
Definition: Item.hpp:20
sf::Vector2i position
Definition: Item.hpp:19
bool visible
Definition: Item.hpp:21
The primary map class that represents a usable map in the game.
Definition: Map.hpp:49
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
static const std::string & ItemSprite()
Definition: Properties.cpp:596
static bool InEditor()
Definition: Properties.cpp:266
static const std::string & NpcFileExtension()
Definition: Properties.cpp:515
static const std::string & TrainerPath()
Definition: Properties.cpp:533
static float CharacterMoveSpeed()
Definition: Properties.cpp:563
static const std::string & NpcPath()
Definition: Properties.cpp:521
static const std::string & CharacterAnimationPath()
Definition: Properties.cpp:557
static const std::string & TrainerFileExtension()
Definition: Properties.cpp:527
static const std::string & ImagePath()
Definition: Properties.cpp:333
bool addBehavior(bl::ecs::Entity entity, const file::Behavior &behavior)
Helper function for adding the given behavior to the given entity.
Definition: AI.cpp:100
Entity(Systems &owner)
Construct a new Entity system.
Definition: Entity.cpp:22
bool spawnItem(const map::Item &item, map::Map &map)
Spawns an item into the world.
Definition: Entity.cpp:98
bl::ecs::Entity spawnAnimation(std::uint8_t level, const glm::vec2 &position, const std::string &gfx, map::Map &map)
Spawns an animation-only entity at the given position. The animation is not played until manually tri...
Definition: Entity.cpp:155
bl::ecs::Entity spawnGeneric(std::uint8_t level, const glm::i32vec2 &tiles, bool collidable, const std::string &gfx, map::Map &map)
Spawns a generic entity with some basic components.
Definition: Entity.cpp:127
bl::ecs::Entity spawnCharacter(const map::CharacterSpawn &spawn, map::Map &map)
Spawns a trainer or an npc from the given spawn information.
Definition: Entity.cpp:25
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
AI & ai()
Returns the AI system.
Definition: Systems.cpp:65