Skip to content
Snippets Groups Projects
Commit 65589fbd authored by BATON Theau's avatar BATON Theau
Browse files

Add collision to player

parent 7d4425d9
No related branches found
No related tags found
No related merge requests found
Showing
with 84 additions and 21 deletions
...@@ -65,6 +65,21 @@ namespace megu { ...@@ -65,6 +65,21 @@ namespace megu {
} }
} }
bool PhysicEngine::makeCollision(const SquareBox & box, Priority priority) const {
for(const auto & source : this->_statics.at(priority)) {
if(source.get().isColliding(box)) {
return true;
}
}
for(const auto & source : this->_dynamic.at(priority)) {
if(source.get().isColliding(box)) {
return true;
}
}
return false;
}
std::optional<std::reference_wrapper<const Tangible>> PhysicEngine::get(const Identifiable & id) const { std::optional<std::reference_wrapper<const Tangible>> PhysicEngine::get(const Identifiable & id) const {
for(auto & [priotiy, objects] : this->_statics) { for(auto & [priotiy, objects] : this->_statics) {
for(auto & object : objects) { for(auto & object : objects) {
......
...@@ -28,6 +28,8 @@ namespace megu { ...@@ -28,6 +28,8 @@ namespace megu {
void step(double); void step(double);
void step(double, Priority); void step(double, Priority);
bool makeCollision(const SquareBox &, Priority) const;
std::optional<std::reference_wrapper<const Tangible>> get(const Identifiable &) const; std::optional<std::reference_wrapper<const Tangible>> get(const Identifiable &) const;
inline void clearCollision() {this->_collisions.clear();} inline void clearCollision() {this->_collisions.clear();}
......
...@@ -24,11 +24,11 @@ namespace megu { ...@@ -24,11 +24,11 @@ namespace megu {
inline void move(const Direction & direction) {this->_box.move(direction);} inline void move(const Direction & direction) {this->_box.move(direction);}
inline void move(float x, float y, float z = 0.f) {return this->move(Direction(x, y, z));} inline void move(float x, float y, float z = 0.f) {return this->move(Direction(x, y, z));}
bool isColliding(const Tangible &) const; virtual bool isColliding(const Tangible &) const;
bool isColliding(const SquareBox &) const; virtual bool isColliding(const SquareBox &) const;
bool operator==(const Tangible &) const; bool operator==(const Tangible &) const;
//bool operator!=(const Tangible &) const;
using UpdateLambda = std::function<void(double)>; using UpdateLambda = std::function<void(double)>;
......
...@@ -33,9 +33,9 @@ namespace megu::game { ...@@ -33,9 +33,9 @@ namespace megu::game {
Terrain terrain(0.f, 0.f, 32.f, 32.f, "assets/tilemap.png", 4, 4, 32.f, 16); Terrain terrain(0.f, 0.f, 32.f, 32.f, "assets/tilemap.png", 4, 4, 32.f, 16);
terrain.setTileEvent(1, new TileSolide(32.f)); terrain.setTileEvent(2, new TileSolide(32.f));
terrain.setValue(1, 1, 1); terrain.setValue(1, 1, 1);
terrain.setValue(0, 1, 2); terrain.setValue(1, 0, 2);
Level level("STZ"); Level level("STZ");
......
...@@ -8,6 +8,7 @@ namespace megu::game { ...@@ -8,6 +8,7 @@ namespace megu::game {
: kernel::PropsPlayable(this->_sprite, this->_movable), GameProps(this), _sprite(path), _movable(x, y, w, h) { : kernel::PropsPlayable(this->_sprite, this->_movable), GameProps(this), _sprite(path), _movable(x, y, w, h) {
this->_sprite.setPosition({x, y}); this->_sprite.setPosition({x, y});
this->_sprite.setLayerObject(2); this->_sprite.setLayerObject(2);
this->_movable.setLayer(2);
} }
void Player::move(float x, float y) { void Player::move(float x, float y) {
...@@ -16,7 +17,7 @@ namespace megu::game { ...@@ -16,7 +17,7 @@ namespace megu::game {
} }
void Player::setup(kernel::Kernel & kernel, Level & level) { void Player::setup(kernel::Kernel & kernel, Level & level) {
this->setControl(kernel.window(), new PlayerKeyProfile(*this)); this->setControl(kernel.window(), new PlayerKeyProfile(*this, kernel));
this->_sprite.setFrame({0.f, 0.f, 16.f, 16.f}); this->_sprite.setFrame({0.f, 0.f, 16.f, 16.f});
this->_sprite.setSize({32.f, 32.f}); this->_sprite.setSize({32.f, 32.f});
...@@ -33,9 +34,9 @@ namespace megu::game { ...@@ -33,9 +34,9 @@ namespace megu::game {
}); });
} }
void Player::on(const kernel::Prop &, const Event & event) { void Player::on(const kernel::Prop & props, const Event & event) {
if((event & Event::Type::SOLID) != 0) { if((event & Event::Type::SOLID) != 0) {
this->onSolide(event); this->onSolide(props);
} }
if((event & Event::Type::DAMAGE) != 0) { if((event & Event::Type::DAMAGE) != 0) {
...@@ -60,7 +61,7 @@ namespace megu::game { ...@@ -60,7 +61,7 @@ namespace megu::game {
std::cout << "I take " << b.get(0).value_or(0) << " damage !" << std::endl; std::cout << "I take " << b.get(0).value_or(0) << " damage !" << std::endl;
} }
void Player::onSolide(const Event &) { void Player::onSolide(const kernel::Prop & props) {
std::cout << "Player Got Solide !" << std::endl;
} }
} }
\ No newline at end of file
...@@ -20,7 +20,10 @@ namespace megu::game { ...@@ -20,7 +20,10 @@ namespace megu::game {
std::optional<Event> on() const override; std::optional<Event> on() const override;
void onDamage(const Event &); void onDamage(const Event &);
void onSolide(const Event &); void onSolide(const kernel::Prop &);
kernel::Movable & getPhysic() {return this->_movable;}
kernel::Sprite & getGraphic() {return this->_sprite;}
private: private:
kernel::Sprite _sprite; kernel::Sprite _sprite;
......
#include "PlayerKeys.hpp" #include "PlayerKeys.hpp"
namespace megu::game { namespace megu::game {
PlayerKeyProfile::PlayerKeyProfile(Player & player) PlayerKeyProfile::PlayerKeyProfile(Player & player, kernel::Kernel & kernel)
: _player(player) {} : _player(player), _kernel(kernel) {}
void PlayerKeyProfile::on(Key key, int code, Action action, Mod mode) { void PlayerKeyProfile::on(Key key, int code, Action action, Mod mode) {
if(key == Keyboard::Key::ARROW_UP) { if(key == Keyboard::Key::ARROW_UP) {
this->_player.move(0.f, 0.5f); SquareBox box = this->_player.getPhysic().getBox();
box.move({0.f, 1.f, 0.f});
if(!this->_kernel.getPhysicEngine().get().makeCollision(box, 0)) {
this->_player.move(0.f, 1.f);
}
} }
if(key == Keyboard::Key::ARROW_DOWN) { if(key == Keyboard::Key::ARROW_DOWN) {
this->_player.move(0.f, -0.5f); SquareBox box = this->_player.getPhysic().getBox();
box.move({0.f, -1.f, 0.f});
if(!this->_kernel.getPhysicEngine().get().makeCollision(box, 0)) {
this->_player.move(0.f, -1.f);
}
} }
if(key == Keyboard::Key::ARROW_LEFT) { if(key == Keyboard::Key::ARROW_LEFT) {
this->_player.move(-0.5f, 0.f); SquareBox box = this->_player.getPhysic().getBox();
box.move({-1.f, 0.f, 0.f});
if(!this->_kernel.getPhysicEngine().get().makeCollision(box, 0)) {
this->_player.move(-1.f, 0.f);
}
} }
if(key == Keyboard::Key::ARROW_RIGHT) { if(key == Keyboard::Key::ARROW_RIGHT) {
this->_player.move(0.5f, 0.f); SquareBox box = this->_player.getPhysic().getBox();
box.move({1.f, 0.f, 0.f});
if(!this->_kernel.getPhysicEngine().get().makeCollision(box, 0)) {
this->_player.move(1.f, 0.f);
}
} }
} }
} }
\ No newline at end of file
...@@ -8,11 +8,12 @@ ...@@ -8,11 +8,12 @@
namespace megu::game { namespace megu::game {
class PlayerKeyProfile : public Keyboard { class PlayerKeyProfile : public Keyboard {
public: public:
PlayerKeyProfile(Player &); PlayerKeyProfile(Player &, kernel::Kernel &);
virtual void on(Key, int, Action, Mod); virtual void on(Key, int, Action, Mod);
private: private:
Player & _player; Player & _player;
kernel::Kernel & _kernel;
}; };
} }
\ No newline at end of file
...@@ -4,13 +4,22 @@ ...@@ -4,13 +4,22 @@
#include <functional> #include <functional>
#include <utility/Identifiable.hpp> #include <utility/Identifiable.hpp>
#include <engine/utility/Priority.hpp>
namespace megu::kernel { namespace megu::kernel {
template <class Pe> template <class Pe>
class Physical : public Component<Pe> { class Physical : public Component<Pe> {
public: public:
inline Physical(Priority p = 0)
: _layer(p) {}
virtual void on_collide(Kernel &, Pe &, Identifiable &, Physical &, double) = 0; virtual void on_collide(Kernel &, Pe &, Identifiable &, Physical &, double) = 0;
using CollideLambda = std::function<void(Kernel &, Pe &, Identifiable &, Physical &, double)>; using CollideLambda = std::function<void(Kernel &, Pe &, Identifiable &, Physical &, double)>;
inline void setLayer(Priority p) {this->_layer = p;}
inline Priority getLayer() const {return this->_layer;}
private:
Priority _layer;
}; };
} }
\ No newline at end of file
...@@ -19,7 +19,7 @@ namespace megu::kernel { ...@@ -19,7 +19,7 @@ namespace megu::kernel {
} }
void Fixed::apply(Kernel & kernel, PhysicEngine & engine) { void Fixed::apply(Kernel & kernel, PhysicEngine & engine) {
engine.get().push(0, *this); engine.get().push(this->getLayer(), *this);
} }
void Fixed::unapply(Kernel & kernel, PhysicEngine & engine) { void Fixed::unapply(Kernel & kernel, PhysicEngine & engine) {
......
...@@ -17,7 +17,7 @@ namespace megu::kernel { ...@@ -17,7 +17,7 @@ namespace megu::kernel {
} }
void Movable::apply(Kernel & kernel, PhysicEngine & engine) { void Movable::apply(Kernel & kernel, PhysicEngine & engine) {
engine.get().push(0, *this); engine.get().push(this->getLayer(), *this);
} }
void Movable::unapply(Kernel & kernel, PhysicEngine & engine) { void Movable::unapply(Kernel & kernel, PhysicEngine & engine) {
......
...@@ -45,6 +45,16 @@ namespace megu::kernel { ...@@ -45,6 +45,16 @@ namespace megu::kernel {
} }
} }
bool TileArray::isColliding(const SquareBox & box) const {
for(auto & [positon, tile] : this->_tiles) {
SquareBox tbox = SquareBox(positon, tile.get().getBox().dimension());
if(box.intersect(tbox)) {
return true;
}
}
return false;
}
void TileArray::update_physic(double time) const { void TileArray::update_physic(double time) const {
if(this->_update != nullptr) { if(this->_update != nullptr) {
this->_update(time); this->_update(time);
...@@ -52,7 +62,7 @@ namespace megu::kernel { ...@@ -52,7 +62,7 @@ namespace megu::kernel {
} }
void TileArray::apply(Kernel & kernel, PhysicEngine & engine) { void TileArray::apply(Kernel & kernel, PhysicEngine & engine) {
engine.get().push(0, *this); engine.get().push(this->getLayer(), *this);
} }
void TileArray::unapply(Kernel & kernel, PhysicEngine & engine) { void TileArray::unapply(Kernel & kernel, PhysicEngine & engine) {
......
...@@ -27,6 +27,8 @@ namespace megu::kernel { ...@@ -27,6 +27,8 @@ namespace megu::kernel {
void unapply(Kernel & k, PhysicEngine &) override; void unapply(Kernel & k, PhysicEngine &) override;
void on_collide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override; void on_collide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override;
virtual bool isColliding(const SquareBox &) const override;
private: private:
std::map<Position, std::reference_wrapper<Tile>> _tiles; std::map<Position, std::reference_wrapper<Tile>> _tiles;
TileCollideLambda _collide; TileCollideLambda _collide;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment