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

Refactoring code

parent bd4dfe88
No related branches found
No related tags found
No related merge requests found
Showing
with 125 additions and 149 deletions
......@@ -5,6 +5,6 @@ namespace megu {
: Tangible(position, dimension) {}
void TangibleStatic::update(double delta) {
this->update_physic(delta);
this->updatePhysic(delta);
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ namespace megu {
TangibleStatic(const Position &, const Dimension &);
virtual ~TangibleStatic() = default;
virtual void update_physic(double) const = 0;
virtual void updatePhysic(double) const = 0;
protected:
void update(double) override;
......
......@@ -8,12 +8,9 @@
#include <game/back/object/Level.hpp>
#include <game/back/object/Terrain.hpp>
#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 {
......@@ -54,8 +51,8 @@ namespace megu::game {
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.graphic().pushAnimation(i, 9, {18, 19, 20, 21});
terrain.graphic().pushAnimation(i, 10, {24, 25, 26, 27});
}
for(size_t i = 0; i < 11; ++i) {
......
#include "GameObject.hpp"
namespace megu::game {
GameProps::GameProps(kernel::Prop * props)
: _parent(props) {}
}
\ No newline at end of file
......@@ -21,13 +21,14 @@ namespace megu::game {
class GameProps : public GameObject, public GameEvent {
public:
GameProps(kernel::Prop * prop)
: _props(prop) {}
GameProps() = delete;
GameProps(kernel::Prop *);
virtual ~GameProps() = default;
inline kernel::Prop * get() {return this->_props;}
inline kernel::Prop * parent() const {return this->_parent;}
private:
kernel::Prop * _props;
kernel::Prop * _parent;
};
}
\ No newline at end of file
#include "Damagable.hpp"
namespace megu::game {
Damagable::Damagable(uint32_t life, bool dead)
: _life(life), _dead(dead) {}
void Damagable::damage(uint32_t damage) {
this->_life -= damage;
if(this->_life <= 0) {
this->_life = 0;
this->_dead = true;
}
}
void Damagable::heal(uint32_t heal) {
this->_life += heal;
}
}
\ No newline at end of file
#pragma once
#include <stdint.h>
namespace megu::game {
class Damagable {
public:
Damagable(uint32_t = 0, bool = false);
virtual ~Damagable() = default;
inline uint32_t getLife() const {return this->_life;}
inline bool isAlive() const {return !this->_dead;}
inline bool isDead() const {return this->_dead;}
inline void setLife(uint32_t l) {this->_life = l;}
inline void setDead(bool s) {this->_dead = s;}
inline void kill() {this->_dead = true;}
void damage(uint32_t);
void heal(uint32_t);
private:
uint32_t _life;
bool _dead;
};
}
\ No newline at end of file
#include "Enemy.hpp"
#include <kernel/front/Kernel.hpp>
#include <game/back/object/Level.hpp>
#include <game/front/Stats.hpp>
namespace megu::game {
Enemy::Enemy(float x, float y, float w, float h, std::filesystem::path & path)
: kernel::PropsDynamic(this->_sprite, this->_movable), GameProps(this), _sprite(path), _movable(x, y, w, h) {
this->_sprite.setPosition({x, y});
}
void Enemy::move(float x, float y) {
this->_movable.move(x, y);
this->_movable.move(x, y);
this->_sprite.setPosition(x, y);
}
void Enemy::setup(kernel::Kernel & kernel, Level & level) {
this->_sprite.setFrame({0.f, 0.f, 51.f, 98.f});
this->_sprite.setSize({51.f, 98.f});
this->_sprite.setFrame(0.f, 0.f, 51.f, 98.f);
this->_sprite.setSize(51.f, 98.f);
this->_movable.setCollideLambda([this, &level](kernel::Kernel &, kernel::PhysicEngine &, Identifiable & id, kernel::Physical<kernel::PhysicEngine> & comp, double) {
auto object = level.get(id);
if(object.has_value()) {
auto event = object->get().on();
if(event.has_value()) {
this->on(*object->get().get(), event.value());
this->on(*object->get().parent(), event.value());
}
}
});
}
void Enemy::destroy(kernel::Kernel & kernel, Level & level) {
}
void Enemy::on(const kernel::Prop &, const Event & event) {
if(event & Event::Type::SOLID) {
this->onSolide(event);
......@@ -40,17 +42,13 @@ namespace megu::game {
std::optional<Event> Enemy::on() const {
Event b(Event::DAMAGE);
b.set(0, 10);
b.set(Stats::ATK, 10);
return b;
}
void Enemy::destroy(kernel::Kernel & kernel, Level & level) {
}
void Enemy::apply(kernel::Kernel & kernel) {
kernel.add(this);
kernel.push(*this);
}
void Enemy::onDamage(const Event &) {
......
......@@ -2,23 +2,22 @@
#include <kernel/front/props/PropsDynamic.hpp>
#include <game/back/GameObject.hpp>
#include <game/back/object/Level.hpp>
namespace megu::game {
class Enemy : public kernel::PropsDynamic, public GameProps {
public:
Enemy(float, float, float, float, std::filesystem::path &);
void move(float, float);
void setup(kernel::Kernel &, Level &) override;
void destroy(kernel::Kernel &, Level &) override;
void apply(kernel::Kernel &) override;
void on(const kernel::Prop &, const Event &) override;
std::optional<Event> on() const override;
void apply(kernel::Kernel &) override;
void onDamage(const Event &);
void onSolide(const Event &);
......
......@@ -14,12 +14,12 @@ namespace megu::game {
}
void Level::add(GameProps * prop) {
this->_objecs.insert({prop->get()->id(), prop});
this->_objecs.insert({prop->parent()->id(), prop});
}
void Level::apply(kernel::Kernel & kernel) {
for(auto & [id, prop] : this->_objecs) {
kernel.add(prop->get());
kernel.push(*prop->parent());
}
}
......@@ -29,7 +29,7 @@ namespace megu::game {
void Level::destroy(kernel::Kernel & kernel, Level & level) {
for(auto & [id, prop] : this->_objecs) {
kernel.remove(prop->get());
kernel.remove(*prop->parent());
}
}
}
\ No newline at end of file
......@@ -6,13 +6,13 @@ namespace megu::game {
: 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->_sprite.move(x, y);
this->_movable.move(x, y);
}
void Player::setPosition(float x, float y) {
this->_sprite.setPosition({x, y});
this->_movable.setPosition({x, y});
this->_sprite.setPosition(x, y);
this->_movable.setPosition(x, y);
}
const Position & Player::getPosition() const {
......@@ -26,7 +26,7 @@ namespace megu::game {
if(object.has_value()) {
auto event = object.value().get().on();
if(event.has_value()) {
this->on(*object.value().get().get(), event.value());
this->on(*object.value().get().parent(), event.value());
}
}
});
......@@ -53,6 +53,6 @@ namespace megu::game {
}
void Player::apply(kernel::Kernel & kernel) {
kernel.add(this);
kernel.push(*this);
}
}
\ No newline at end of file
......@@ -9,17 +9,16 @@ namespace megu::game {
public:
Player(float x, float y, float w, float h, std::filesystem::path);
void move(float, float);
void setPosition(float, float);
const Position & getPosition() const;
void setup(kernel::Kernel &, Level &) override final;
void destroy(kernel::Kernel &, Level &) override final;
void apply(kernel::Kernel &) override final;
void setPosition(float, float);
void move(float, float);
const Position & getPosition() const;
void on(const kernel::Prop &, const Event &) override final;
void apply(kernel::Kernel &) override final;
std::optional<Event> on() const = 0;
......
......@@ -7,7 +7,7 @@ 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) {
//! Taille Pour 1 tile !
this->_graphic.setSize({w, h});
this->_graphic.setSize(w, h);
}
void Terrain::setValue(size_t x, size_t y, size_t value) {
......@@ -16,7 +16,7 @@ namespace megu::game {
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->_graphic.getSize().y * (this->_graphic.collumn()- 1)) - static_cast<float>(y) * this->_graphic.getSize().y
},
*this->_event[value]
);
......@@ -52,7 +52,7 @@ namespace megu::game {
}
void Terrain::apply(kernel::Kernel & kernel) {
kernel.add(this);
kernel.push(*this);
}
void Terrain::on(const kernel::Prop &, const Event & event) {
......
#pragma once
#include <kernel/front/props/PropsTileMap.hpp>
#include <kernel/front/component/physic/TileArray.hpp>
#include <kernel/front/component/graphic/TileMap.hpp>
#include <game/back/GameObject.hpp>
#include <game/back/object/tile/Tile.hpp>
......@@ -12,14 +9,16 @@ namespace megu::game {
public:
Terrain() = delete;
Terrain(float, float, float, float, const std::filesystem::path &, size_t, size_t, float, size_t);
virtual ~Terrain() = default;
void setup(kernel::Kernel &, Level &) override;
void destroy(kernel::Kernel &, Level &) override;
void setValue(size_t, size_t, size_t);
void setTileEvent(size_t, Tile *);
void setup(kernel::Kernel &, Level &) override;
void destroy(kernel::Kernel &, Level &) override;
void apply(kernel::Kernel &) override;
void apply(kernel::Kernel &) override;
void on(const kernel::Prop &, const Event &) override;
std::optional<Event> on() const override;
......
......@@ -6,7 +6,7 @@
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) {
: Player(0.f, 0.f, 32.f, 32.f, "assets/game/objects/klinck.png"), Damagable(100), _kernel(k), _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);
......@@ -15,18 +15,18 @@ namespace megu::game {
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.pushAnimation(Animation::IDL_BACK, 0, 32, 16, 16);
sprite.pushAnimation(Animation::IDL_FRONT, 16, 32, 16, 16);
sprite.pushAnimation(Animation::IDL_LEFT, 32, 32, 16, 16);
sprite.pushAnimation(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.pushAnimation(Animation::WALK_BACK, {{0, 16, 16, 16}, { 16, 16, 16, 16}});
sprite.pushAnimation(Animation::WALK_FRONT, {{32, 16, 16, 16}, { 48, 16, 16, 16}});
sprite.pushAnimation(Animation::WALK_LEFT, {{0, 0, 16, 16}, { 16, 0, 16, 16}});
sprite.pushAnimation(Animation::WALK_RIGHT, {{32, 0, 16, 16}, { 48, 0, 16, 16}});
sprite.setAnimation(Animation::IDL_BACK);
sprite.setSize({32.f, 32.f});
sprite.setCurrentAnimation(Animation::IDL_BACK);
sprite.setSize(32.f, 32.f);
this->setControl(kernel.window(), new KlinckKeyProfile(*this, kernel));
......@@ -34,15 +34,14 @@ namespace megu::game {
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::onDamage(const Event & event) {
this->damage(event.get(Stats::ATK).value_or(0));
}
void Klinck::onSolide(const kernel::Prop &) {
......@@ -82,9 +81,9 @@ namespace megu::game {
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);
std::optional<SquareBox> obox = this->_kernel.getPhysicEngine().get().checkCollision(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();
float pos_y = this->_vy > 0 ? obox.value().y() - obox.value().height() : obox.value().y() + obox.value().height();
this->setPosition(this->getPosition().x(), pos_y);
this->_vy = 0.f;
}
......@@ -97,9 +96,9 @@ namespace megu::game {
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);
std::optional<SquareBox> obox = this->_kernel.getPhysicEngine().get().checkCollision(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();
float pos_x = this->_vx > 0 ? obox.value().x() - obox.value().width() : obox.value().x() + obox.value().width();
this->setPosition(pos_x, this->getPosition().y());
this->_vx = 0.f;
}
......
#pragma once
#include <game/back/object/Player.hpp>
#include <game/back/object/Damagable.hpp>
namespace megu::game {
class Klinck : public Player {
class Klinck : public Player, public Damagable {
public:
enum Animation {
IDL_FRONT = 1,
......@@ -42,7 +43,6 @@ namespace megu::game {
private:
kernel::Kernel & _kernel;
unsigned int _life;
unsigned int _atk;
unsigned int _def;
float _vx, _vy;
......
......@@ -11,25 +11,25 @@ namespace megu::game {
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);
this->_player.getGraphic().setCurrentAnimation(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);
this->_player.getGraphic().setCurrentAnimation(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);
this->_player.getGraphic().setCurrentAnimation(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);
this->_player.getGraphic().setCurrentAnimation(Klinck::Animation::IDL_BACK);
}
}
}
\ No newline at end of file
#include "Test.hpp"
namespace megu::game {
Object::Object(std::filesystem::path & path)
: _physic(0, 0, 64, 64), _graphic(path), _map("assets/tilemap.png", 8, 8, 8.f, 16, 0.125) {
this->_graphic.setFrame({0.f, 0.f, 51.f, 98.f});
this->_graphic.setSize({51.f, 98.f});
size_t tab[8][8] = {
{44, 45, 44, 47, 2, 1, 1, 1},
{45, 45, 44, 47, 1, 1, 1, 1},
{45, 44, 44, 47, 1, 1, 1, 1},
{22, 22, 22, 23, 1, 1, 2, 1},
{28, 28, 28, 29, 1, 1, 1, 1},
{ 1, 2, 1, 1, 1, 1, 1, 1},
{ 1, 2, 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 2, 1, 1, 1, 1}
};
for(size_t i = 0; i < 8; ++i) {
for(size_t j = 0; j < 8; ++j) {
this->_map.setValue(j, i, tab[i][j]);
}
}
this->_map.addAnimation(0, 6, {18, 18, 20, 21});
this->_map.addAnimation(1, 6, {18, 18, 20, 21});
this->_map.addAnimation(2, 6, {18, 18, 20, 21});
this->_map.addAnimation(3, 6, {18, 18, 20, 21});
this->_map.addAnimation(4, 6, {18, 18, 20, 21});
this->_map.addAnimation(5, 6, {18, 18, 20, 21});
this->_map.addAnimation(6, 6, {18, 18, 20, 21});
this->_map.addAnimation(7, 6, {18, 18, 20, 21});
this->_map.addAnimation(0, 7, {24, 25, 26, 27});
this->_map.addAnimation(1, 7, {24, 25, 26, 27});
this->_map.addAnimation(2, 7, {24, 25, 26, 27});
this->_map.addAnimation(3, 7, {24, 25, 26, 27});
this->_map.addAnimation(4, 7, {24, 25, 26, 27});
this->_map.addAnimation(5, 7, {24, 25, 26, 27});
this->_map.addAnimation(6, 7, {24, 25, 26, 27});
this->_map.addAnimation(7, 7, {24, 25, 26, 27});
this->_map.setSize({360 / 8, 360 / 8});
}
}
\ No newline at end of file
#pragma once
#include <kernel/front/props/Props.hpp>
#include <kernel/front/component/physic/Fixed.hpp>
#include <kernel/front/component/graphic/Sprite.hpp>
#include <kernel/front/component/graphic/TileMap.hpp>
namespace megu::game {
class Object : public kernel::Prop {
public:
Object(std::filesystem::path &);
inline kernel::Prop::Physical_Component * getPhysicComponent() override {return &this->_physic;}
inline kernel::Prop::Graphical_Component * getGraphicComponent() override {return &this->_map;}
private:
kernel::Fixed _physic;
kernel::Sprite _graphic;
kernel::Tilemap _map;
};
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ namespace megu::kernel {
template <class E>
class Component : public virtual Identifiable {
public:
virtual void apply(Kernel & k, E &) = 0;
virtual void unapply(Kernel & k, E &) = 0;
virtual void apply(E &) = 0;
virtual void unapply(E &) = 0;
};
}
\ 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