Peoplemon  0.1.0
Peoplemon 3 game source documentation
Renderable.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Util/FileUtil.hpp>
4 #include <BLIB/Util/Visitor.hpp>
5 #include <Core/Properties.hpp>
6 #include <Core/Resources.hpp>
7 
8 namespace core
9 {
10 namespace component
11 {
12 Renderable& Renderable::createFromSprite(bl::engine::Engine& engine, bl::ecs::Entity entity,
13  bl::rc::Scene* scene, const std::string& path) {
14  Renderable& rc = *engine.ecs().emplaceComponent<Renderable>(entity);
15  rc.srcType = Sprite;
16 
17  bl::gfx::Sprite sprite;
18  sprite.create(engine, entity, engine.renderer().texturePool().getOrLoadTexture(path));
19  sprite.deleteEntityOnDestroy(false);
20  sprite.getTransform().setOrigin(sprite.getTexture()->size());
21  sprite.addToScene(scene, bl::rc::UpdateSpeed::Static);
22  rc.transform = &sprite.getTransform();
23  rc.drawable = &sprite.component();
24 
25  return rc;
26 }
27 
28 Renderable& Renderable::createFromMoveAnims(bl::engine::Engine& engine, bl::ecs::Entity entity,
29  bl::rc::Scene* scene, const std::string& path) {
30  Renderable& rc = *engine.ecs().emplaceComponent<Renderable>(entity);
31  rc.srcType = Walk;
32 
33  bl::resource::Ref<res::WalkAnimations> data = WalkAnimationManager::getOrCreateGenerated(path);
34  bl::gfx::Slideshow slideshow;
35  slideshow.createWithUniquePlayer(engine, entity, data);
36  slideshow.deleteEntityOnDestroy(false);
37  slideshow.addToScene(scene, bl::rc::UpdateSpeed::Dynamic);
38  slideshow.getTransform().setOrigin(
39  slideshow.getLocalSize().x - static_cast<float>(Properties::PixelsPerTile()),
40  slideshow.getLocalSize().y - static_cast<float>(Properties::PixelsPerTile()));
41  rc.drawable = &slideshow.component();
42  rc.transform = &slideshow.getTransform();
43  rc.player = &slideshow.getPlayer();
44  rc.walkSrc = data.get();
45 
46  return rc;
47 }
48 
49 Renderable& Renderable::createFromFastMoveAnims(bl::engine::Engine& engine, bl::ecs::Entity entity,
50  bl::rc::Scene* scene, const std::string& path) {
51  Renderable& rc = *engine.ecs().emplaceComponent<Renderable>(entity);
52  rc.srcType = Run;
53 
54  bl::resource::Ref<res::RunWalkAnimations> data =
55  RunWalkAnimationManager::getOrCreateGenerated(path);
56  bl::gfx::Slideshow slideshow;
57  slideshow.createWithUniquePlayer(engine, entity, data);
58  slideshow.deleteEntityOnDestroy(false);
59  slideshow.addToScene(scene, bl::rc::UpdateSpeed::Dynamic);
60  slideshow.getTransform().setOrigin(
61  slideshow.getLocalSize().x - static_cast<float>(Properties::PixelsPerTile()),
62  slideshow.getLocalSize().y - static_cast<float>(Properties::PixelsPerTile()));
63  rc.drawable = &slideshow.component();
64  rc.transform = &slideshow.getTransform();
65  rc.player = &slideshow.getPlayer();
66  rc.runSrc = data.get();
67 
68  return rc;
69 }
70 
71 Renderable& Renderable::createFromAnimation(bl::engine::Engine& engine, bl::ecs::Entity entity,
72  bl::rc::Scene* scene, const std::string& path) {
73  Renderable& rc = *engine.ecs().emplaceComponent<Renderable>(entity);
74  rc.srcType = SingleAnim;
75 
76  bl::resource::Ref<bl::gfx::a2d::AnimationData> data = AnimationManager::load(path);
77  rc.animSrc = data.get();
78  if (data->isSlideshow()) {
79  bl::gfx::Slideshow slideshow;
80  slideshow.createWithUniquePlayer(engine, entity, data);
81  slideshow.deleteEntityOnDestroy(false);
82  slideshow.addToScene(scene, bl::rc::UpdateSpeed::Static);
83  rc.drawable = &slideshow.component();
84  rc.transform = &slideshow.getTransform();
85  rc.player = &slideshow.getPlayer();
86  }
87  else {
88  bl::gfx::Animation2D anim;
89  anim.createWithUniquePlayer(engine, entity, data);
90  anim.deleteEntityOnDestroy(false);
91  anim.addToScene(scene, bl::rc::UpdateSpeed::Static);
92  rc.drawable = &anim.component();
93  rc.transform = &anim.getTransform();
94  rc.player = &anim.getPlayer();
95  }
96 
97  return rc;
98 }
99 
101 : srcType(Sprite)
102 , walkSrc(nullptr)
103 , transform(nullptr)
104 , player(nullptr)
105 , shadow(bl::ecs::InvalidEntity)
106 , isMoving(false) {}
107 
108 void Renderable::setAngle(float a) { transform->setRotation(a); }
109 
110 float Renderable::animLength() const {
111  switch (srcType) {
112  case Walk:
113  return walkSrc->getMaxStateLength();
114  case Run:
115  return runSrc->getMaxStateLength();
116  case SingleAnim:
117  return animSrc->getLength();
118  default:
119  return 0.f;
120  }
121 }
122 
123 void Renderable::triggerAnim(bool loop) {
124  if (player) {
125  if (loop) { player->playLooping(); }
126  else { player->play(); }
127  }
128 }
129 
130 void Renderable::notifyMoveState(bl::tmap::Direction dir, bool moving, bool running) {
131  if (player) {
132  if (srcType == Walk) {
133  player->setState(res::WalkAnimations::getStateFromDirection(dir), false);
134  }
135  else if (srcType == Run) {
136  player->setState(res::RunWalkAnimations::getStateFromDirection(dir, running), false);
137  }
138  if (moving && !isMoving) { player->playLooping(); }
139  else if (!moving) { player->stop(); }
140  isMoving = moving;
141  }
142 }
143 
144 void Renderable::setHidden(bool hide) {
145  if (drawable) { drawable->setHidden(hide); }
146 }
147 
148 } // namespace component
149 } // namespace core
Core classes and functionality for both the editor and game.
Adding this component to an entity will allow it to be rendered.
Definition: Renderable.hpp:28
res::WalkAnimations * walkSrc
Definition: Renderable.hpp:126
bl::gfx::a2d::AnimationData * animSrc
Definition: Renderable.hpp:128
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
void setHidden(bool hide)
Set whether the entity is hidden or not.
Definition: Renderable.cpp:144
void setAngle(float angle)
Sets the angle to render the entity at.
Definition: Renderable.cpp:108
float animLength() const
Returns the length of the contained animation, or 0.f if no animation.
Definition: Renderable.cpp:110
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
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
res::RunWalkAnimations * runSrc
Definition: Renderable.hpp:127
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
void triggerAnim(bool loop)
Triggers the current animation if any.
Definition: Renderable.cpp:123
void notifyMoveState(bl::tmap::Direction dir, bool moving, bool running)
Call when the entity starts or stops moving or changes direction.
Definition: Renderable.cpp:130
static int PixelsPerTile()
Definition: Properties.cpp:279
static std::size_t getStateFromDirection(bl::tmap::Direction dir, bool running)
Maps the movement direction to the state index.
float getMaxStateLength() const
Returns the maximum length of any substate animation in seconds.
float getMaxStateLength() const
Returns the maximum length of any substate animation in seconds.
static std::size_t getStateFromDirection(bl::tmap::Direction dir)
Maps the movement direction to the state index.