12 , mainRenderTarget(nullptr)
13 , pool(owner.engine().ecs().getAllComponents<component::Renderable>())
14 , transformPool(owner.engine().ecs().getAllComponents<
bl::com::Transform2D>()) {}
16 void Render::update(std::mutex&,
float,
float,
float,
float) {
17 if (!mainRenderTarget) { mainRenderTarget = &owner.
engine().renderer().getObserver(); }
18 for (
const auto& epair : stopRm) {
24 for (
const bl::ecs::Entity shadow : shadowsToRemove) {
25 owner.
engine().ecs().destroyEntity(shadow);
27 shadowsToRemove.clear();
30 void Render::init(bl::engine::Engine&) { bl::event::Dispatcher::subscribe(
this); }
32 void Render::notifyFrameStart() { std::swap(stopAdd, stopRm); }
34 void Render::observe(
const event::EntityMoved& event) {
35 component::Renderable* rc = pool.get(event.entity);
37 rc->notifyMoveState(event.position.direction,
true, event.running);
38 std::erase_if(stopRm, [&event](
auto& pair) {
return pair.first ==
event.entity; });
39 std::erase_if(stopAdd, [&event](
auto& pair) {
return pair.first ==
event.entity; });
42 bl::com::Transform2D* transform = transformPool.get(event.entity);
45 event.position.level, event.position.position.y));
49 void Render::observe(
const event::EntityMoveFinished& event) {
50 stopAdd.emplace_back(event.entity, event.position.direction);
53 void Render::observe(
const event::EntityRotated& event) {
54 component::Renderable* rc = pool.get(event.entity);
55 if (rc) { rc->notifyMoveState(event.faceDir,
false,
false); }
61 BL_LOG_WARN <<
"Cannot update shadow of entity, missing Renderable component: " << entity;
65 constexpr
float Radius = 100.f;
67 bl::gfx::Circle* shadow =
nullptr;
68 if (rc->shadow != bl::ecs::InvalidEntity) {
69 shadow = owner.
engine().ecs().getComponent<bl::gfx::Circle>(rc->shadow);
71 BL_LOG_ERROR <<
"Entity is missing shadow but one was expected, re-creating";
72 owner.
engine().ecs().destroyEntity(rc->shadow);
76 rc->shadow = owner.
engine().ecs().createEntity(bl::ecs::Flags::WorldObject);
77 owner.
engine().ecs().setEntityParent(rc->shadow, entity);
78 shadow = owner.
engine().ecs().emplaceComponent<bl::gfx::Circle>(rc->shadow);
79 shadow->create(owner.
engine(), rc->shadow, Radius);
80 shadow->deleteEntityOnDestroy(
false);
81 shadow->getTransform().setOrigin(Radius, Radius);
82 shadow->getTransform().setDepth(0.5f);
83 shadow->setFillColor(sf::Color(0, 0, 0, 145));
87 bl::com::Transform2D* parentTransform =
88 owner.
engine().ecs().getComponent<bl::com::Transform2D>(entity);
89 const glm::vec2 parentOrigin =
90 parentTransform ? parentTransform->getOrigin() : glm::vec2{16.f, 32.f};
92 shadow->scaleToSize({radius * 2.f, radius * 2.f});
99 if (rc && rc->shadow != bl::ecs::InvalidEntity) {
100 shadowsToRemove.emplace_back(rc->shadow);
101 rc->shadow = bl::ecs::InvalidEntity;
Core classes and functionality for both the editor and game.
Adding this component to an entity will allow it to be rendered.
void notifyMoveState(bl::tmap::Direction dir, bool moving, bool running)
Call when the entity starts or stops moving or changes direction.
float getDepthForPosition(unsigned int level, unsigned int y, int layer=-1) const
Computes the depth to use at the given y position, taking into account the map size,...
bl::rc::SceneRef getScene()
Returns the scene for this map.
static int PixelsPerTile()
Render(Systems &owner)
Creates the render system.
void setMainRenderTarget(bl::rc::RenderTarget &target)
Sets the main render target. Used by the editor.
void removeShadow(bl::ecs::Entity entity)
Removes the shadow from the given entity.
void updateShadow(bl::ecs::Entity entity, float distance, float radius)
Adds or updates the shadow of the given entity.
Owns all primary systems and a reference to the engine.
const bl::engine::Engine & engine() const
Const accessor for the Engine.
World & world()
Modifiable accessor for the world system.
map::Map & activeMap()
Returns a reference to the active map.