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

Add Player collision to world

parent 65589fbd
No related branches found
No related tags found
No related merge requests found
Showing
with 171 additions and 74 deletions
assets/game/levels/BSZ/tilemap.png

5.48 KiB

assets/game/objects/klinck.png

1.54 KiB

File deleted
......@@ -27,8 +27,7 @@ namespace megu {
template <class T>
size_t push(Priority priority, T & object, Module<T> * modul) {
if(!this->_objects.contains(priority)) {
if(this->_objects.contains(priority)) {
this->_objects.erase(priority);
}
this->_objects.insert({priority, Renderable{ object, modul }});
......
......@@ -36,6 +36,8 @@ namespace megu {
inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});}
inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);}
inline void setSize(float w, float h) {this->_transformation.setScaling(w, h);}
inline const Frame & frame() const {return this->_frame;}
inline const Texture & texture() const {return this->_texture;}
inline const Transformable & transformation() const {return this->_transformation;}
......
......@@ -65,19 +65,22 @@ namespace megu {
}
}
bool PhysicEngine::makeCollision(const SquareBox & box, Priority priority) const {
std::optional<SquareBox> PhysicEngine::makeCollision(const SquareBox & box, Priority priority) const {
for(const auto & source : this->_statics.at(priority)) {
if(source.get().isColliding(box)) {
return true;
auto obox = source.get().isColliding(box);
if(obox.has_value()) {
return obox;
}
}
for(const auto & source : this->_dynamic.at(priority)) {
if(source.get().isColliding(box)) {
return true;
auto obox = source.get().isColliding(box);
if(obox.has_value()) {
return obox;
}
}
return false;
return {};
}
std::optional<std::reference_wrapper<const Tangible>> PhysicEngine::get(const Identifiable & id) const {
......
......@@ -28,7 +28,7 @@ namespace megu {
void step(double);
void step(double, Priority);
bool makeCollision(const SquareBox &, Priority) const;
std::optional<SquareBox> makeCollision(const SquareBox &, Priority) const;
std::optional<std::reference_wrapper<const Tangible>> get(const Identifiable &) const;
......
#include "Tangible.hpp"
#include <optional>
#include <iostream>
namespace megu {
Tangible::Tangible(const Position & position, const Dimension & dimension)
: _box(position, dimension) {}
bool Tangible::isColliding(const Tangible & entity) const {
if(this != &entity) {
return this->_box.intersect(entity._box);
std::optional<SquareBox> Tangible::isColliding(const Tangible & entity) const {
if(this != &entity && this->_box.intersect(entity._box)) {
return this->_box;
}
return false;
return {};
}
bool Tangible::isColliding(const SquareBox & box) const {
return this->_box.intersect(box);
std::optional<SquareBox> Tangible::isColliding(const SquareBox & box) const {
if(this->_box.intersect(box)) {
return this->_box;
}
return {};
}
bool Tangible::operator==(const Tangible & entity) const {
......
......@@ -5,6 +5,7 @@
#include <engine/physic/back/Position.hpp>
#include <engine/physic/back/SquareBox.hpp>
#include <functional>
#include <optional>
namespace megu {
class Tangible : virtual public Identifiable {
......@@ -24,12 +25,11 @@ namespace megu {
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));}
virtual bool isColliding(const Tangible &) const;
virtual bool isColliding(const SquareBox &) const;
virtual std::optional<SquareBox> isColliding(const Tangible &) const;
virtual std::optional<SquareBox> isColliding(const SquareBox &) const;
bool operator==(const Tangible &) const;
using UpdateLambda = std::function<void(double)>;
protected:
......
......@@ -10,6 +10,10 @@
#include <game/back/object/tile/TileSolide.hpp>
#include <game/object/Test.hpp>
#include <game/front/Layer.hpp>
#include <game/front/object/Klinck.hpp>
#include <game/utility/FrameCouter.hpp>
namespace megu::game {
......@@ -25,28 +29,87 @@ namespace megu::game {
FrameCounter counter;
kernel::Kernel kernel(window);
auto path = std::filesystem::path("assets/player.png");
//megu::game::Object object(path);
Player player(0, 0, 32, 32, path);
Enemy enemy(64, 64, 16, 16, path);
Klinck klinck(kernel, 170, 170);
Terrain terrain(0.f, 0.f, 32.f, 32.f, "assets/game/levels/BSZ/tilemap.png", 11, 11, 32.f, 16);
std::vector<size_t> layer0 = {
48, 49, 49, 23, 30, 31, 32, 34, 50, 50, 51,
54, 55, 55, 29, 36, 37, 38, 40, 56, 56, 57,
47, 1, 1, 1, 1, 1, 1, 1, 1, 1, 46,
47, 1, 1, 1, 1, 1, 1, 1, 1, 1, 46,
47, 1, 1, 1, 1, 1, 1, 1, 1, 1, 46,
47, 1, 1, 1, 1, 1, 1, 1, 1, 1, 46,
47, 1, 12, 13, 1, 1, 1, 12, 13, 1, 46,
23, 1, 1, 1, 1, 1, 1, 1, 2, 1, 34,
29, 1, 1, 1, 1, 1, 1, 1, 1, 1, 40,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
};
for(size_t i = 7; i < 60; ++i) {
terrain.setTileEvent(i, new TileSolide(32.f, physic::TERRAIN));
}
terrain.setTileEvent(2, new TileSolide(32.f, physic::TERRAIN));
for(size_t i = 0; i < 11; ++i) {
terrain.graphic().addAnimation(i, 9, {18, 19, 20, 21});
terrain.graphic().addAnimation(i, 10, {24, 25, 26, 27});
}
for(size_t i = 0; i < 11; ++i) {
for(size_t j = 0; j < 11; ++j) {
terrain.setValue(j, i, layer0.at(i*11 + j));
}
}
terrain.getGraphicComponent()->setLayerPriority(graphic::TERRAIN);
terrain.getPhysicComponent()->setLayer(physic::TERRAIN);
Terrain terrain_2(0.f, 0.f, 32.f, 32.f, "assets/game/levels/BSZ/tilemap.png", 11, 11, 32.f, 16);
std::vector<size_t> layer1 = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 9, 0, 0, 0, 14, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 6, 7, 0, 0, 0, 6, 7, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
terrain_2.setTileEvent(9, new TileSolide(32.f, physic::TERRAIN));
terrain_2.setTileEvent(14, new TileSolide(32.f, physic::TERRAIN));
terrain_2.setTileEvent(15, new TileSolide(32.f, physic::TERRAIN));
for(size_t i = 0; i < 11; ++i) {
for(size_t j = 0; j < 11; ++j) {
terrain_2.setValue(j, i, layer1.at(i*11 + j));
}
}
Terrain terrain(0.f, 0.f, 32.f, 32.f, "assets/tilemap.png", 4, 4, 32.f, 16);
terrain_2.getGraphicComponent()->setLayerPriority(graphic::TERRAIN_UP);
terrain_2.getPhysicComponent()->setLayer(physic::TERRAIN);
terrain.setTileEvent(2, new TileSolide(32.f));
terrain.setValue(1, 1, 1);
terrain.setValue(1, 0, 2);
Level level("STZ");
Level level("BSZ");
terrain.setup(kernel, level);
terrain.apply(kernel);
terrain_2.setup(kernel, level);
terrain_2.apply(kernel);
//level.add(&enemy);
level.add(&player);
player.setup(kernel, level);
player.apply(kernel);
level.add(&klinck);
klinck.setup(kernel, level);
klinck.apply(kernel);
//enemy.setup(kernel, level);
//enemy.apply(kernel);
......
#include "Player.hpp"
#include <game/front/profile/PlayerKeys.hpp>
#include <game/back/object/Level.hpp>
namespace megu::game {
Player::Player(float x, float y, float w, float h, std::filesystem::path & path)
: kernel::PropsPlayable(this->_sprite, this->_movable), GameProps(this), _sprite(path), _movable(x, y, w, h) {
this->_sprite.setPosition({x, y});
this->_sprite.setLayerObject(2);
this->_movable.setLayer(2);
}
Player::Player(float x, float y, float w, float h, std::filesystem::path path)
: kernel::PropsPlayable(this->_sprite, this->_movable), GameProps(this), _sprite(path), _movable(x, y, w, h) {}
void Player::move(float x, float y) {
this->_sprite.move({x, y});
this->_movable.move(x, y);
}
void Player::setup(kernel::Kernel & kernel, Level & level) {
this->setControl(kernel.window(), new PlayerKeyProfile(*this, kernel));
void Player::setPosition(float x, float y) {
this->_sprite.setPosition({x, y});
this->_movable.setPosition({x, y});
}
this->_sprite.setFrame({0.f, 0.f, 16.f, 16.f});
this->_sprite.setSize({32.f, 32.f});
const Position & Player::getPosition() const {
return this->_movable.getPosition();
}
void Player::setup(kernel::Kernel & kernel, Level & level) {
this->_movable.setCollideLambda([this, &level](kernel::Kernel &, kernel::PhysicEngine &, Identifiable & id, kernel::Physical<kernel::PhysicEngine> & comp, double) {
auto object = level.get(id);
......@@ -32,6 +30,12 @@ namespace megu::game {
}
}
});
this->_movable.setUpdateLambda([this](double delta) {
this->onPhysic(delta);
});
this->onSetup(kernel, level);
}
void Player::on(const kernel::Prop & props, const Event & event) {
......@@ -44,24 +48,11 @@ namespace megu::game {
}
}
std::optional<Event> Player::on() const {
return {};
}
void Player::destroy(kernel::Kernel & kernel, Level & level) {
this->onDestroy(kernel, level);
}
void Player::apply(kernel::Kernel & kernel) {
kernel.add(this);
}
void Player::onDamage(const Event & b) {
std::cout << "Player Got Damage !" << std::endl;
std::cout << "I take " << b.get(0).value_or(0) << " damage !" << std::endl;
}
void Player::onSolide(const kernel::Prop & props) {
}
}
\ No newline at end of file
......@@ -7,20 +7,27 @@
namespace megu::game {
class Player : public kernel::PropsPlayable, public GameProps {
public:
Player(float x, float y, float w, float h, std::filesystem::path &);
Player(float x, float y, float w, float h, std::filesystem::path);
void move(float, float);
void setPosition(float, float);
void setup(kernel::Kernel &, Level &) override;
void destroy(kernel::Kernel &, Level &) override;
const Position & getPosition() const;
void apply(kernel::Kernel &) override;
void setup(kernel::Kernel &, Level &) override final;
void destroy(kernel::Kernel &, Level &) override final;
void on(const kernel::Prop &, const Event &) override;
std::optional<Event> on() const override;
void apply(kernel::Kernel &) override final;
void onDamage(const Event &);
void onSolide(const kernel::Prop &);
void on(const kernel::Prop &, const Event &) override final;
std::optional<Event> on() const = 0;
virtual void onDamage(const Event &) = 0;
virtual void onSolide(const kernel::Prop &) = 0;
virtual void onSetup(kernel::Kernel &, Level &) = 0;
virtual void onDestroy(kernel::Kernel &, Level &) = 0;
virtual void onPhysic(double) = 0;
kernel::Movable & getPhysic() {return this->_movable;}
kernel::Sprite & getGraphic() {return this->_sprite;}
......
......@@ -2,6 +2,7 @@
#include <game/back/object/Level.hpp>
namespace megu::game {
Terrain::Terrain(float x, float y, float w, float h, const std::filesystem::path & path, size_t r, size_t c , float size, size_t tileSize)
: PropsTileMap(this->_graphic, this->_physic), GameProps(this), _graphic(path, r, c, size, tileSize), _physic(x, y, r * w, c * h) {
......@@ -12,7 +13,13 @@ namespace megu::game {
void Terrain::setValue(size_t x, size_t y, size_t value) {
this->_graphic.setValue(x, y, value);
if(this->_event.contains(value)) {
this->_physic.push({static_cast<float>(x) * this->_graphic.getSize().x, (this->_graphic.getSize().y * (this->_graphic.height()- 1)) - static_cast<float>(y) * this->_graphic.getSize().y}, *this->_event[value]);
this->_physic.push(
{
static_cast<float>(x) * this->_graphic.getSize().x,
(this->_graphic.getSize().y * (this->_graphic.height()- 1)) - static_cast<float>(y) * this->_graphic.getSize().y
},
*this->_event[value]
);
}
}
......
......@@ -23,6 +23,9 @@ namespace megu::game {
void on(const kernel::Prop &, const Event &) override;
std::optional<Event> on() const override;
inline kernel::Tilemap & graphic() {return this->_graphic;}
inline kernel::TileArray & phyisc() {return this->_physic;}
private:
kernel::Tilemap _graphic;
kernel::TileArray _physic;
......
#include "Tile.hpp"
namespace megu::game {
Tile::Tile(float d)
: kernel::Tile(0, 0, d) {}
Tile::Tile(float d, Priority p)
: kernel::Tile(0, 0, d, p) {}
}
\ No newline at end of file
......@@ -9,6 +9,6 @@ namespace megu::game {
class Tile : public kernel::Tile, public GameEvent {
public:
Tile() = delete;
Tile(float d);
Tile(float d, Priority p);
};
}
\ No newline at end of file
#include "TileSolide.hpp"
namespace megu::game {
TileSolide::TileSolide(float d)
: Tile(d) {}
TileSolide::TileSolide(float d, Priority p)
: Tile(d, p) {}
void TileSolide::on(const kernel::Prop &, const Event &) {
//...
......
......@@ -5,7 +5,7 @@
namespace megu::game {
class TileSolide : public Tile {
public:
TileSolide(float d);
TileSolide(float d, Priority p);
void on(const kernel::Prop &, const Event &) override;
std::optional<Event> on() const override;
......
#pragma once
namespace megu::game {
namespace physic {
enum Layer {
TERRAIN = 0,
ENTITY = 1,
};
}
namespace graphic {
enum Layer {
TERRAIN = 0,
ENTITY = 1,
TERRAIN_UP = 2
};
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment