diff --git a/assets/game/levels/BSZ/tilemap.png b/assets/game/levels/BSZ/tilemap.png new file mode 100644 index 0000000000000000000000000000000000000000..33c2c4713acf3de01bd3a0e8cfc4dee773cf847b Binary files /dev/null and b/assets/game/levels/BSZ/tilemap.png differ diff --git a/assets/game/objects/klinck.png b/assets/game/objects/klinck.png new file mode 100644 index 0000000000000000000000000000000000000000..f5ea178d3f0064be5995ff7e946b67c8d22ed468 Binary files /dev/null and b/assets/game/objects/klinck.png differ diff --git a/assets/player.png.old b/assets/player.png.old deleted file mode 100644 index 70456833ec2e399ebc897d12cc6abd8f1423db91..0000000000000000000000000000000000000000 Binary files a/assets/player.png.old and /dev/null differ diff --git a/assets/shaders/Sprite.frag b/assets/shaders/Sprite.frag index 31804e3da3883884bfa9d2a1f808344f53aad50b..95a92eb4fde85d8d7f461b848e34aa067282948c 100644 --- a/assets/shaders/Sprite.frag +++ b/assets/shaders/Sprite.frag @@ -11,8 +11,8 @@ flat in int Id; void main() { vec2 coord = vec2( - (uFrames[Id].x / uSizes[Id].x) + (uFrames[Id].z / uSizes[Id].x) * Texture.x, - (uFrames[Id].y / uSizes[Id].y) + (uFrames[Id].w / uSizes[Id].y) * Texture.y + (uFrames[Id].x / uSizes[Id].x) + (uFrames[Id].z / uSizes[Id].x) * Texture.x, + (uFrames[Id].y / uSizes[Id].y) + (uFrames[Id].w / uSizes[Id].y) * Texture.y ); FragColor = texture(uSampler[uTextures[Id]], coord); } \ No newline at end of file diff --git a/source/engine/graphics/front/engine/Layer.hpp b/source/engine/graphics/front/engine/Layer.hpp index 3efa4ddc636103b8e727effb4be5e58346fed159..3fde2cf84020b61863e40729a9f5e17bc70f9368 100644 --- a/source/engine/graphics/front/engine/Layer.hpp +++ b/source/engine/graphics/front/engine/Layer.hpp @@ -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 }}); diff --git a/source/engine/graphics/front/object/Sprite.hpp b/source/engine/graphics/front/object/Sprite.hpp index 43b9a8b88e40f24e82e0e39e38d9554ef3049932..a0b5b23075888188c6548e92a0ba3a19672684d6 100644 --- a/source/engine/graphics/front/object/Sprite.hpp +++ b/source/engine/graphics/front/object/Sprite.hpp @@ -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;} diff --git a/source/engine/physic/front/engine/Engine.cpp b/source/engine/physic/front/engine/Engine.cpp index 480db59a46eb08a20ac17aee233fd63d53daa2e8..2e39c9fae3c4d2b04c92e38979512901bb9cb7f5 100644 --- a/source/engine/physic/front/engine/Engine.cpp +++ b/source/engine/physic/front/engine/Engine.cpp @@ -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 { diff --git a/source/engine/physic/front/engine/Engine.hpp b/source/engine/physic/front/engine/Engine.hpp index ae19a1bba6730f451fdf1ae20d2305163de607f8..7c8304d12fb3357a1812c798cc64227e1a7f5952 100644 --- a/source/engine/physic/front/engine/Engine.hpp +++ b/source/engine/physic/front/engine/Engine.hpp @@ -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; diff --git a/source/engine/physic/front/object/Tangible.cpp b/source/engine/physic/front/object/Tangible.cpp index 5915036ae78d16ee36f5064c4c3401e732adf10d..58708a0e6f2b6bc34977fedfbc9ad17ef991e8c9 100644 --- a/source/engine/physic/front/object/Tangible.cpp +++ b/source/engine/physic/front/object/Tangible.cpp @@ -1,20 +1,24 @@ #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 { diff --git a/source/engine/physic/front/object/Tangible.hpp b/source/engine/physic/front/object/Tangible.hpp index 9cc4d73bce969d5a15e4f9771e968f2524a08b17..9b98a61ddf136d62c7c597b2a3b407d3776388d8 100644 --- a/source/engine/physic/front/object/Tangible.hpp +++ b/source/engine/physic/front/object/Tangible.hpp @@ -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,11 +25,10 @@ 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)>; diff --git a/source/game/Game.cpp b/source/game/Game.cpp index 8a14bbfb8766a5676ba413ba2246227c382f6fd9..e0022680178b27197094d113b6a18a4b64666017 100644 --- a/source/game/Game.cpp +++ b/source/game/Game.cpp @@ -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}); + } - Terrain terrain(0.f, 0.f, 32.f, 32.f, "assets/tilemap.png", 4, 4, 32.f, 16); + 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.setTileEvent(2, new TileSolide(32.f)); - terrain.setValue(1, 1, 1); - terrain.setValue(1, 0, 2); + 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)); + } + } - Level level("STZ"); + terrain_2.getGraphicComponent()->setLayerPriority(graphic::TERRAIN_UP); + terrain_2.getPhysicComponent()->setLayer(physic::TERRAIN); + + + 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); diff --git a/source/game/back/object/Player.cpp b/source/game/back/object/Player.cpp index 4371a1d68cc5fcd5200dfac61925a080b7957bab..f78ddf3b1871d7f4d94709831ffc494e6dc19954 100644 --- a/source/game/back/object/Player.cpp +++ b/source/game/back/object/Player.cpp @@ -1,27 +1,25 @@ #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) { @@ -43,25 +47,12 @@ namespace megu::game { this->onDamage(event); } } - - 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 diff --git a/source/game/back/object/Player.hpp b/source/game/back/object/Player.hpp index febcc85da217b8d5ad7837fd1ee17179933da587..9859acff8db2fc60d1fec773aba4a5b4bdd9032d 100644 --- a/source/game/back/object/Player.hpp +++ b/source/game/back/object/Player.hpp @@ -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;} diff --git a/source/game/back/object/Terrain.cpp b/source/game/back/object/Terrain.cpp index dceb37eb2f5c00682d1bfded001ca43fd97fe3e0..eea0a8613cd0b31e7b982e6fafeea631495eab72 100644 --- a/source/game/back/object/Terrain.cpp +++ b/source/game/back/object/Terrain.cpp @@ -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] + ); } } diff --git a/source/game/back/object/Terrain.hpp b/source/game/back/object/Terrain.hpp index 8fa99ec8fe29eada72f9686506f29034f2bc1c4a..44e91279949165ea7040670c9b27e8625af8691a 100644 --- a/source/game/back/object/Terrain.hpp +++ b/source/game/back/object/Terrain.hpp @@ -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; diff --git a/source/game/back/object/tile/Tile.cpp b/source/game/back/object/tile/Tile.cpp index e25518716f412192697f627c492ec7d302408bfc..8cdabacd1068dc871cccd27af595177f9ade99e9 100644 --- a/source/game/back/object/tile/Tile.cpp +++ b/source/game/back/object/tile/Tile.cpp @@ -1,6 +1,6 @@ #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 diff --git a/source/game/back/object/tile/Tile.hpp b/source/game/back/object/tile/Tile.hpp index ec35a38075d97b86e828ec338a788a8ce3c943b7..09e3dbd65dd94bc998b90d0fd412745ed1f315e2 100644 --- a/source/game/back/object/tile/Tile.hpp +++ b/source/game/back/object/tile/Tile.hpp @@ -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 diff --git a/source/game/back/object/tile/TileSolide.cpp b/source/game/back/object/tile/TileSolide.cpp index 91d1ae735a504fb834897df6c207ac81988a19d0..b0bcd078f81a58eef2206d23281d441ba75b708f 100644 --- a/source/game/back/object/tile/TileSolide.cpp +++ b/source/game/back/object/tile/TileSolide.cpp @@ -1,8 +1,8 @@ #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 &) { //... diff --git a/source/game/back/object/tile/TileSolide.hpp b/source/game/back/object/tile/TileSolide.hpp index 84424f23fc812f297583af0de5817c6e20ae03f0..c8db88e1e847c07aa725e176c49727a94b62e1fc 100644 --- a/source/game/back/object/tile/TileSolide.hpp +++ b/source/game/back/object/tile/TileSolide.hpp @@ -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; diff --git a/source/game/front/Layer.hpp b/source/game/front/Layer.hpp new file mode 100644 index 0000000000000000000000000000000000000000..88ca9e9611b9324d23f582c98e378a0fe5600c59 --- /dev/null +++ b/source/game/front/Layer.hpp @@ -0,0 +1,18 @@ +#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 diff --git a/source/game/front/Stats.hpp b/source/game/front/Stats.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ca02b26f61925570453c5f35dcef9f23bdf35d03 --- /dev/null +++ b/source/game/front/Stats.hpp @@ -0,0 +1,9 @@ +#pragma once + +namespace megu::game { + enum Stats { + LIFE, + ATK, + DEF, + }; +} \ No newline at end of file diff --git a/source/game/front/object/Klinck.cpp b/source/game/front/object/Klinck.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b668e0b5383d09d20081ef19ddfcb0931eb7f497 --- /dev/null +++ b/source/game/front/object/Klinck.cpp @@ -0,0 +1,132 @@ +#include "Klinck.hpp" + +#include <game/front/Stats.hpp> +#include <game/front/Layer.hpp> +#include <game/front/profile/KlinckKeys.hpp> + +namespace megu::game { + Klinck::Klinck(kernel::Kernel & k, float x, float y) + : Player(0.f, 0.f, 32.f, 32.f, "assets/game/objects/klinck.png"), _kernel(k), _life(100), _atk(10), _def(25), _vx(0.f), _vy(0.f) { + this->getGraphic().setLayerObject(graphic::Layer::ENTITY); + this->getPhysic().setLayer(physic::Layer::ENTITY); + this->move(x, y); + } + + void Klinck::onSetup(kernel::Kernel & kernel, Level &) { + auto & sprite = this->getGraphic(); + + sprite.push(Animation::IDL_BACK, 0, 32, 16, 16); + sprite.push(Animation::IDL_FRONT, 16, 32, 16, 16); + sprite.push(Animation::IDL_LEFT, 32, 32, 16, 16); + sprite.push(Animation::IDL_RIGHT, 48, 32, 16, 16); + + sprite.push(Animation::WALK_BACK, {{0, 16, 16, 16}, { 16, 16, 16, 16}}); + sprite.push(Animation::WALK_FRONT, {{32, 16, 16, 16}, { 48, 16, 16, 16}}); + sprite.push(Animation::WALK_LEFT, {{0, 0, 16, 16}, { 16, 0, 16, 16}}); + sprite.push(Animation::WALK_RIGHT, {{32, 0, 16, 16}, { 48, 0, 16, 16}}); + + sprite.setAnimation(Animation::IDL_BACK); + sprite.setSize({32.f, 32.f}); + + this->setControl(kernel.window(), new KlinckKeyProfile(*this, kernel)); + + this->_moving[UP] = false; + this->_moving[DOWN] = false; + this->_moving[LEFT] = false; + this->_moving[RIGHT] = false; + + } + + void Klinck::onDestroy(kernel::Kernel &, Level &) { + + } + + void Klinck::onDamage(const Event &) { + + } + + void Klinck::onSolide(const kernel::Prop &) { + + } + + void Klinck::onPhysic(double) { + if(abs(this->_vx) > 0.05f) { + this->_vx = this->_vx > 0 ? 0.05f : -0.05f; + } + + if(abs(this->_vy) > 0.05f) { + this->_vy = this->_vy > 0 ? 0.05f : -0.05f; + } + + float vx = 0.f, vy = 0.f; + if(this->_moving[UP]) { + vy += 0.01f; + } + + if(this->_moving[DOWN]) { + vy -= 0.01f; + } + + if(this->_moving[RIGHT]) { + vx += 0.01f; + } + + if(this->_moving[LEFT]) { + vx -= 0.01f; + } + + this->_vx += vx; + this->_vy += vy; + + if(abs(this->_vy) > 0) { + SquareBox box = this->getPhysic().getBox(); + box.move({0.f, this->_vy, 0.f}); + + std::optional<SquareBox> obox = this->_kernel.getPhysicEngine().get().makeCollision(box, physic::TERRAIN); + if(obox.has_value()) { + float pos_y = this->_vy > 0 ? obox.value().position().y() - obox.value().height() : obox.value().position().y() + obox.value().height(); + this->setPosition(this->getPosition().x(), pos_y); + this->_vy = 0.f; + } + else { + this->move(0.0, this->_vy); + } + } + + if(abs(this->_vx) > 0) { + SquareBox box = this->getPhysic().getBox(); + box.move({this->_vx, 0.f, 0.f}); + + std::optional<SquareBox> obox = this->_kernel.getPhysicEngine().get().makeCollision(box, physic::TERRAIN); + if(obox.has_value()) { + float pos_x = this->_vx > 0 ? obox.value().position().x() - obox.value().width() : obox.value().position().x() + obox.value().width(); + this->setPosition(pos_x, this->getPosition().y()); + this->_vx = 0.f; + } + else { + this->move(this->_vx, 0.0); + } + } + + if(abs(this->_vy) > 0.001f) { + this->_vy += this->_vy > 0.f ? -0.0002f : 0.0002f; + } + else { + this->_vy = 0.f; + } + + if(abs(this->_vx) > 0.001f) { + this->_vx += this->_vx > 0.f ? -0.0002f : 0.0002f; + } + else { + this->_vx = 0.f; + } + } + + std::optional<Event> Klinck::on() const { + Event event(Event::DAMAGE); + event.set(Stats::ATK, this->_atk); + + return event; + } +} \ No newline at end of file diff --git a/source/game/front/object/Klinck.hpp b/source/game/front/object/Klinck.hpp new file mode 100644 index 0000000000000000000000000000000000000000..178046966f70a4804c008d0f98093eade1f792ed --- /dev/null +++ b/source/game/front/object/Klinck.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include <game/back/object/Player.hpp> + +namespace megu::game { + class Klinck : public Player { + public: + enum Animation { + IDL_FRONT = 1, + IDL_BACK = 2, + IDL_LEFT = 3, + IDL_RIGHT = 4, + + WALK_FRONT = 5, + WALK_BACK = 6, + WALK_LEFT = 7, + WALK_RIGHT = 8 + }; + + enum Direction { + UP, + DOWN, + LEFT, + RIGHT + }; + + Klinck(kernel::Kernel &, float, float); + + void onSetup(kernel::Kernel &, Level &) override; + void onDestroy(kernel::Kernel &, Level &) override; + void onDamage(const Event &) override; + void onSolide(const kernel::Prop &) override; + void onPhysic(double) override; + std::optional<Event> on() const override; + + inline void setVelocity(float x, float y) {this->_vx = x; this->_vy = y;} + inline void addVelocity(float x, float y) {this->_vx += x; this->_vy += y;} + inline float vx() const {return this->_vx;} + inline float vy() const {return this->_vy;} + + inline void setMovingDirection(Direction dir, bool state) {this->_moving[dir] = state;} + + private: + kernel::Kernel & _kernel; + unsigned int _life; + unsigned int _atk; + unsigned int _def; + float _vx, _vy; + std::map<Direction, bool> _moving; + }; +} \ No newline at end of file diff --git a/source/game/front/profile/KlinckKeys.cpp b/source/game/front/profile/KlinckKeys.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2da47f3d96240ddb2989f7786506262203cfd3fb --- /dev/null +++ b/source/game/front/profile/KlinckKeys.cpp @@ -0,0 +1,35 @@ +#include "KlinckKeys.hpp" + +#include <game/front/Layer.hpp> +#include <optional> + +namespace megu::game { + KlinckKeyProfile::KlinckKeyProfile(Klinck & player, kernel::Kernel & kernel) + : _player(player), _kernel(kernel) {} + + void KlinckKeyProfile::on(Key key, int code, Action action, Mod mode) { + if(key == Keyboard::Key::ARROW_LEFT) { + this->_player.setMovingDirection(Klinck::Direction::LEFT, action != Action::RELEASE); + //this->_player.getGraphic().setAnimation(action == Action::RELEASE ? Klinck::Animation::IDL_LEFT : Klinck::Animation::WALK_LEFT); + this->_player.getGraphic().setAnimation(Klinck::Animation::IDL_LEFT); + } + + if(key == Keyboard::Key::ARROW_RIGHT) { + this->_player.setMovingDirection(Klinck::Direction::RIGHT, action != Action::RELEASE); + //this->_player.getGraphic().setAnimation(action == Action::RELEASE ? Klinck::Animation::IDL_RIGHT : Klinck::Animation::WALK_RIGHT); + this->_player.getGraphic().setAnimation(Klinck::Animation::IDL_RIGHT); + } + + if(key == Keyboard::Key::ARROW_UP) { + this->_player.setMovingDirection(Klinck::Direction::UP, action != Action::RELEASE); + //this->_player.getGraphic().setAnimation(action == Action::RELEASE ? Klinck::Animation::IDL_FRONT : Klinck::Animation::WALK_FRONT); + this->_player.getGraphic().setAnimation(Klinck::Animation::IDL_FRONT); + } + + if(key == Keyboard::Key::ARROW_DOWN) { + this->_player.setMovingDirection(Klinck::Direction::DOWN, action != Action::RELEASE); + //this->_player.getGraphic().setAnimation(action == Action::RELEASE ? Klinck::Animation::IDL_BACK : Klinck::Animation::WALK_BACK); + this->_player.getGraphic().setAnimation(Klinck::Animation::IDL_BACK); + } + } +} \ No newline at end of file diff --git a/source/game/front/profile/PlayerKeys.hpp b/source/game/front/profile/KlinckKeys.hpp similarity index 57% rename from source/game/front/profile/PlayerKeys.hpp rename to source/game/front/profile/KlinckKeys.hpp index 8acb995be2c023b8523c3cd62660f9566da08b06..7521b1baa1357b81f4887e0411e5b6e851847475 100644 --- a/source/game/front/profile/PlayerKeys.hpp +++ b/source/game/front/profile/KlinckKeys.hpp @@ -1,19 +1,19 @@ #pragma once -#include <game/back/object/Player.hpp> +#include <game/front/object/Klinck.hpp> #include <engine/io/Keyboard.hpp> #include <engine/io/Mouse.hpp> namespace megu::game { - class PlayerKeyProfile : public Keyboard { + class KlinckKeyProfile : public Keyboard { public: - PlayerKeyProfile(Player &, kernel::Kernel &); + KlinckKeyProfile(Klinck &, kernel::Kernel &); virtual void on(Key, int, Action, Mod); private: - Player & _player; + Klinck & _player; kernel::Kernel & _kernel; }; } \ No newline at end of file diff --git a/source/game/front/profile/PlayerKeys.cpp b/source/game/front/profile/PlayerKeys.cpp deleted file mode 100644 index 1f3300135a1a147bb2502a1fe71ea8755f619f6e..0000000000000000000000000000000000000000 --- a/source/game/front/profile/PlayerKeys.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "PlayerKeys.hpp" - -namespace megu::game { - PlayerKeyProfile::PlayerKeyProfile(Player & player, kernel::Kernel & kernel) - : _player(player), _kernel(kernel) {} - - void PlayerKeyProfile::on(Key key, int code, Action action, Mod mode) { - if(key == Keyboard::Key::ARROW_UP) { - 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) { - 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) { - 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) { - 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 diff --git a/source/kernel/back/engine/GraphicEngine.cpp b/source/kernel/back/engine/GraphicEngine.cpp index efb84180ef06817205aed822de9f627433d1a0ba..1aa1931a565523b5d408b065b6e5d2ffb066efc8 100644 --- a/source/kernel/back/engine/GraphicEngine.cpp +++ b/source/kernel/back/engine/GraphicEngine.cpp @@ -8,6 +8,8 @@ namespace megu::kernel { void GraphicEngine::boot(Kernel &) { this->_engine.push(0, this->_renderer); + this->_engine.push(1, this->_renderer); + this->_engine.push(2, this->_renderer); } void GraphicEngine::stop(Kernel &) { diff --git a/source/kernel/front/component/graphic/Sprite.cpp b/source/kernel/front/component/graphic/Sprite.cpp index ba3cbb9fcbb8b1f790975a8ca4d8637050591798..4545186dfc24023da15107321bf0e4d7d10ace94 100644 --- a/source/kernel/front/component/graphic/Sprite.cpp +++ b/source/kernel/front/component/graphic/Sprite.cpp @@ -10,20 +10,20 @@ namespace megu::kernel { : Sprite(path, {0, 0, 0, 0}) {} Sprite::Sprite(const std::filesystem::path & path, const Frame & frame) - : megu::Sprite(path, frame), _animation(""), _index(0) { + : megu::Sprite(path, frame), _animation(0), _index(0) { if(!Sprite::_Linker.haveModule()) { Sprite::_Linker.setModule(new Sprite_Module{}); } } - const Frame_List & Sprite::getAnimations(const std::string & name) const { + const Frame_List & Sprite::getAnimations(size_t name) const { if(this->_frames.contains(name)) { return this->_frames.at(name); } throw std::runtime_error("Cannot get inexisting animation"); } - const Frame & Sprite::getFrame(const std::string & name, size_t index) const { + const Frame & Sprite::getFrame(size_t name, size_t index) const { auto & frames = this->getAnimations(name); if(frames.size() > index) { return frames.at(index); @@ -31,36 +31,44 @@ namespace megu::kernel { throw std::runtime_error("Cannot get inexisting frame"); } - void Sprite::setAnimation(const std::string & name) { + void Sprite::setAnimation(size_t name) { if(this->_frames.contains(name)) { this->_animation = name; this->_previous = 0.0; } - throw std::runtime_error("Cannot set inexisting animation"); + else { + throw std::runtime_error("Cannot set inexisting animation"); + } } - void Sprite::push(const std::string & name, const Frame_List & frames) { - this->_frames[name] = frames; + void Sprite::push(size_t name, const Frame_List & frames) { + for(auto & frame : frames) { + this->push(name, frame); + } } - void Sprite::push(const std::string & name, const Frame & frame) { + void Sprite::push(size_t name, const Frame & frame) { this->_frames[name].push_back(frame); } - void Sprite::remove(const std::string & name) { + void Sprite::push(size_t name, float x, float y, float w, float h) { + this->_frames[name].push_back({x, y, w, h}); + } + + void Sprite::remove(size_t name) { if(this->_frames.contains(name)) { this->_frames.erase(name); } } - void Sprite::remove(const std::string & name, size_t index) { + void Sprite::remove(size_t name, size_t index) { if(this->_frames.contains(name) && this->_frames[name].size() > index) { this->_frames[name].erase(this->_frames[name].begin() + index); } } void Sprite::update(double time) { - /*if(!this->_animation.empty()) { + if(this->_animation != 0) { if(this->_previous == 0.0) { this->setFrame(this->_frames[this->_animation][this->_index]); this->_previous = time; @@ -72,7 +80,7 @@ namespace megu::kernel { this->setFrame(this->_frames[this->_animation][this->_index]); } } - }*/ + } } void Sprite::apply(Kernel & kernel, GraphicEngine & engine) { diff --git a/source/kernel/front/component/graphic/Sprite.hpp b/source/kernel/front/component/graphic/Sprite.hpp index b3bcc561717674f1588cb53bdb2800fd7f4a50fa..f3e5e57c1ca157b6015c7a3981c6cd53943f707e 100644 --- a/source/kernel/front/component/graphic/Sprite.hpp +++ b/source/kernel/front/component/graphic/Sprite.hpp @@ -23,16 +23,18 @@ namespace megu::kernel { inline size_t getCurrentFrameIndex() const {return this->_index;} inline double getDuration() const {return this->_duration;} + inline void setDuration(double d) {this->_duration = d;} - const Frame_List & getAnimations(const std::string &) const; - const Frame & getFrame(const std::string &, size_t) const; + const Frame_List & getAnimations(size_t) const; + const Frame & getFrame(size_t, size_t) const; - void setAnimation(const std::string &); - void push(const std::string &, const Frame_List & = {}); - void push(const std::string &, const Frame &); + void setAnimation(size_t); + void push(size_t, const Frame_List & = {}); + void push(size_t, const Frame &); + void push(size_t, float, float, float, float); - void remove(const std::string &); - void remove(const std::string &, size_t); + void remove(size_t); + void remove(size_t, size_t); void update(double) override; void apply(Kernel &, GraphicEngine &) override; @@ -41,11 +43,11 @@ namespace megu::kernel { static const GraphicLinker<megu::Sprite> & linker() {return Sprite::_Linker;} private: - std::map<std::string, Frame_List> _frames; - std::string _animation; + std::map<size_t, Frame_List> _frames; + size_t _animation; size_t _index; double _duration; - double _previous; + double _previous; static GraphicLinker<megu::Sprite> _Linker; }; diff --git a/source/kernel/front/component/graphic/TileMap.cpp b/source/kernel/front/component/graphic/TileMap.cpp index e0df788668b17bc628f0f9896eccbf71c6ec370a..4e4acd33524d465bbe2e9c68f366306ead4b7803 100644 --- a/source/kernel/front/component/graphic/TileMap.cpp +++ b/source/kernel/front/component/graphic/TileMap.cpp @@ -5,9 +5,9 @@ namespace megu::kernel { Tilemap::Tilemap(const std::filesystem::path & path, size_t width, size_t height, float size, size_t lenght, double aUpdate) : TileArray(path, width , height, size, lenght), _tileSize(lenght), _duration(aUpdate) { - if(lenght % width != 0 && lenght % height != 0) { + /*if(lenght % width != 0 && lenght % height != 0) { throw std::runtime_error("Tilemap dimension not matching tiles size."); - } + }*/ size_t tileWidth = this->texture().width() / this->_tileSize; size_t tileHeight = this->texture().height() / this->_tileSize; diff --git a/source/kernel/front/component/physic/Tile.cpp b/source/kernel/front/component/physic/Tile.cpp index eafd5df0f733f3e601c00029beef051cba19a950..47e0e34045dcb27c8444ae6e015d58760100e42b 100644 --- a/source/kernel/front/component/physic/Tile.cpp +++ b/source/kernel/front/component/physic/Tile.cpp @@ -1,8 +1,10 @@ #include "Tile.hpp" namespace megu::kernel { - Tile::Tile(float x, float y, float d) - : Fixed(x, y, d, d) {} + Tile::Tile(float x, float y, float d, Priority p) + : Fixed(x, y, d, d) { + this->setLayer(p); + } bool Tile::operator==(const Tile & tile) const { return this->id() == tile.id(); diff --git a/source/kernel/front/component/physic/Tile.hpp b/source/kernel/front/component/physic/Tile.hpp index 112ac25c647e46aea37cadcaf5cd5e9177f7a831..3b914c426b5da3ab3a0ef735bdfd2cff6ad19f48 100644 --- a/source/kernel/front/component/physic/Tile.hpp +++ b/source/kernel/front/component/physic/Tile.hpp @@ -5,7 +5,7 @@ namespace megu::kernel { class Tile : public Fixed { public: - Tile(float x, float y, float d); + Tile(float x, float y, float d, Priority p); bool operator==(const Tile &) const; bool operator>=(const Tile &) const; diff --git a/source/kernel/front/component/physic/TileArray.cpp b/source/kernel/front/component/physic/TileArray.cpp index 1ce7961dd5cdfcbaad0247bb432576fc5ebef79e..7a2e45584642ae2a6b01ba3f13e957a4592b180a 100644 --- a/source/kernel/front/component/physic/TileArray.cpp +++ b/source/kernel/front/component/physic/TileArray.cpp @@ -45,14 +45,14 @@ namespace megu::kernel { } } - bool TileArray::isColliding(const SquareBox & box) const { + std::optional<SquareBox> 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 tbox; } } - return false; + return {}; } void TileArray::update_physic(double time) const { diff --git a/source/kernel/front/component/physic/TileArray.hpp b/source/kernel/front/component/physic/TileArray.hpp index 418d8d02b3ffdf32de23ee3e38d198e1160f7e1b..3bd463130ac1b14eac78c4bc000646d11f29accb 100644 --- a/source/kernel/front/component/physic/TileArray.hpp +++ b/source/kernel/front/component/physic/TileArray.hpp @@ -27,10 +27,10 @@ namespace megu::kernel { void unapply(Kernel & k, PhysicEngine &) override; void on_collide(Kernel &, PhysicEngine &, Identifiable &, Physical &, double) override; - virtual bool isColliding(const SquareBox &) const override; + virtual std::optional<SquareBox> isColliding(const SquareBox &) const override; private: - std::map<Position, std::reference_wrapper<Tile>> _tiles; + std::map<Position, std::reference_wrapper<Tile>, reference_sorter<Position>> _tiles; TileCollideLambda _collide; UpdateLambda _update; };