MCPcopy Create free account
hub / github.com/alecthomas/entityx / BounceSystem

Class BounceSystem

examples/example.cc:137–155  ·  view source on GitHub ↗

Bounce bodies off the edge of the screen.

Source from the content-addressed store, hash-verified

135
136// Bounce bodies off the edge of the screen.
137class BounceSystem : public ex::System<BounceSystem> {
138public:
139 explicit BounceSystem(sf::RenderTarget &target) : size(target.getSize()) {}
140
141 void update(ex::EntityManager &es, ex::EventManager &events, ex::TimeDelta dt) override {
142 ex::ComponentHandle<Body> body;
143 for (ex::Entity entity : es.entities_with_components(body)) {
144 if (body->position.x + body->direction.x < 0 ||
145 body->position.x + body->direction.x >= size.x)
146 body->direction.x = -body->direction.x;
147 if (body->position.y + body->direction.y < 0 ||
148 body->position.y + body->direction.y >= size.y)
149 body->direction.y = -body->direction.y;
150 }
151 }
152
153private:
154 sf::Vector2u size;
155};
156
157
158// Determines if two Collideable bodies have collided. If they have it emits a

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected