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

Add Tilemap collision

parent c7f07668
Branches
No related tags found
No related merge requests found
Showing
with 156 additions and 108 deletions
assets/player.png

1.35 KiB

File added
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
#include <iostream> #include <iostream>
namespace megu { namespace megu {
TileArray::TileArray(const std::filesystem::path & path, size_t width, size_t height, float size, size_t lenght) TileArray::TileArray(const std::filesystem::path & path, size_t width, size_t height, float tileSize, size_t lenght)
: _width(width), _height(height), _size(size) { : _width(width), _height(height), _tileSize(tileSize) {
megu::TextureBuffer buffer(path); megu::TextureBuffer buffer(path);
this->_texture.store(buffer); this->_texture.store(buffer);
......
...@@ -27,7 +27,8 @@ namespace megu { ...@@ -27,7 +27,8 @@ namespace megu {
inline size_t width() const {return this->_width;} inline size_t width() const {return this->_width;}
inline size_t height() const {return this->_height;} inline size_t height() const {return this->_height;}
inline float size() const {return this->_size;} inline float getTileSize() const {return this->_tileSize;}
inline const Vec3 & getSize() const {return this->_transformation.scaling();}
inline void setUv(size_t x, size_t y, const glm::vec4 & uv) {this->_uvs[y][x] = uv;} inline void setUv(size_t x, size_t y, const glm::vec4 & uv) {this->_uvs[y][x] = uv;}
...@@ -41,7 +42,7 @@ namespace megu { ...@@ -41,7 +42,7 @@ namespace megu {
Transformable _transformation; Transformable _transformation;
Texture _texture; Texture _texture;
size_t _width, _height; size_t _width, _height;
float _size; float _tileSize;
std::vector<std::vector<glm::vec4>> _uvs; std::vector<std::vector<glm::vec4>> _uvs;
}; };
} }
\ No newline at end of file
#include "SquareBox.hpp" #include "SquareBox.hpp"
#include <iostream>
namespace megu { namespace megu {
SquareBox::SquareBox(const Position & position, const Dimension & dimension) SquareBox::SquareBox(const Position & position, const Dimension & dimension)
: _position(position), _dimension(dimension) {} : _position(position), _dimension(dimension) {}
...@@ -31,21 +33,21 @@ namespace megu { ...@@ -31,21 +33,21 @@ namespace megu {
this->_position.move(direction.x(), direction.y(), direction.z()); this->_position.move(direction.x(), direction.y(), direction.z());
} }
bool SquareBox::contain(const Position & position) const { bool SquareBox::intersect(const SquareBox & squareBox) const {
if(position >= this->_position && position <= (this->_position + Position(this->_dimension))) { const Position & a = squareBox.position();
return true; const Dimension & as = squareBox.dimension();
} const Position & b = this->_position;
return false; const Dimension & bs = this->_dimension;
}
bool SquareBox::intersect(const SquareBox & SquareBox) const { bool x = std::max(a.x(), b.x()) < std::min(a.x() + as.x, b.x() + bs.x);
const Cube cube = SquareBox.asCube(); bool y = std::max(a.y(), b.y()) < std::min(a.y() + as.y, b.y() + bs.y);
for(const auto & position : cube) {
if(this->contain(position)) { bool z = true;
return true; if(a.z() != 0 && b.z() != 0) {
} z = std::max(a.z(), b.z()) < std::min(a.z() + as.z, b.z() + bs.z);
} }
return false;
return x && y && z;
} }
bool SquareBox::operator==(const SquareBox & squareBox) const { bool SquareBox::operator==(const SquareBox & squareBox) const {
......
...@@ -28,8 +28,6 @@ namespace megu { ...@@ -28,8 +28,6 @@ namespace megu {
Cube asCube() const; Cube asCube() const;
void move(const Direction &); void move(const Direction &);
bool contain(const Position &) const;
bool intersect(const SquareBox &) const; bool intersect(const SquareBox &) const;
bool operator==(const SquareBox &) const; bool operator==(const SquareBox &) const;
......
...@@ -53,6 +53,7 @@ namespace megu { ...@@ -53,6 +53,7 @@ namespace megu {
for(auto & target : this->_statics[priority]) { for(auto & target : this->_statics[priority]) {
if(source.get().isColliding(target)) { if(source.get().isColliding(target)) {
this->_collisions.insert(Collision(source, target)); this->_collisions.insert(Collision(source, target));
this->_collisions.insert(Collision(target, source));
} }
} }
......
#include "Tangible.hpp" #include "Tangible.hpp"
#include <iostream>
namespace megu { namespace megu {
Tangible::Tangible(const Position & position, const Dimension & dimension) Tangible::Tangible(const Position & position, const Dimension & dimension)
: _box(position, dimension) {} : _box(position, dimension) {}
...@@ -11,6 +13,10 @@ namespace megu { ...@@ -11,6 +13,10 @@ namespace megu {
return false; return false;
} }
bool Tangible::isColliding(const SquareBox & box) const {
return this->_box.intersect(box);
}
bool Tangible::operator==(const Tangible & entity) const { bool Tangible::operator==(const Tangible & entity) const {
return this->_box == entity._box; return this->_box == entity._box;
} }
......
...@@ -25,6 +25,7 @@ namespace megu { ...@@ -25,6 +25,7 @@ namespace megu {
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; bool isColliding(const Tangible &) const;
bool isColliding(const SquareBox &) const;
bool operator==(const Tangible &) const; bool operator==(const Tangible &) const;
//bool operator!=(const Tangible &) const; //bool operator!=(const Tangible &) const;
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <game/back/object/Player.hpp> #include <game/back/object/Player.hpp>
#include <game/back/object/Enemy.hpp> #include <game/back/object/Enemy.hpp>
#include <game/back/object/Level.hpp> #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/object/Test.hpp>
#include <game/utility/FrameCouter.hpp> #include <game/utility/FrameCouter.hpp>
...@@ -23,23 +25,31 @@ namespace megu::game { ...@@ -23,23 +25,31 @@ namespace megu::game {
FrameCounter counter; FrameCounter counter;
kernel::Kernel kernel(window); kernel::Kernel kernel(window);
auto path = std::filesystem::path("assets/textures/Neera.png"); auto path = std::filesystem::path("assets/player.png");
megu::game::Object object(path); //megu::game::Object object(path);
//kernel.add(&object);
Player player(16, 16, 16, 16, path); Player player(0, 0, 32, 32, path);
Enemy enemy(64, 64, 16, 16, path); Enemy enemy(64, 64, 16, 16, path);
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.setValue(1, 1, 1);
terrain.setValue(0, 1, 2);
Level level("STZ"); Level level("STZ");
level.add(&player);
level.add(&enemy);
terrain.setup(kernel, level);
terrain.apply(kernel);
//level.add(&enemy);
level.add(&player);
player.setup(kernel, level); player.setup(kernel, level);
player.apply(kernel); player.apply(kernel);
enemy.setup(kernel, level); //enemy.setup(kernel, level);
enemy.apply(kernel); //enemy.apply(kernel);
std::cout << "..." << std::endl; std::cout << "..." << std::endl;
......
#pragma once #pragma once
#include <kernel/front/Kernel.hpp> #include <kernel/front/Kernel.hpp>
#include <game/back/message/Behavior.hpp> #include <game/back/message/Event.hpp>
namespace megu::game { namespace megu::game {
class Level; class Level;
...@@ -13,14 +13,17 @@ namespace megu::game { ...@@ -13,14 +13,17 @@ namespace megu::game {
virtual void apply(kernel::Kernel &) = 0; virtual void apply(kernel::Kernel &) = 0;
}; };
class GameProps : public GameObject { class GameEvent {
public:
virtual void on(const kernel::Prop &, const Event &) = 0;
virtual std::optional<Event> on() const = 0;
};
class GameProps : public GameObject, public GameEvent {
public: public:
GameProps(kernel::Prop * prop) GameProps(kernel::Prop * prop)
: _props(prop) {} : _props(prop) {}
virtual void on(const Behavior &) = 0;
virtual std::optional<Behavior> on() const = 0;
inline kernel::Prop * get() {return this->_props;} inline kernel::Prop * get() {return this->_props;}
private: private:
......
#include "Behavior.hpp"
namespace megu::game {
Behavior::Behavior(const kernel::Prop & author, uint32_t type)
: _type(type), _stats(author) {}
uint32_t Behavior::get(uint32_t key) const {
return this->_stats.get(key);
}
void Behavior::set(uint32_t key, uint32_t value) {
this->_stats.set(key, value);
}
uint32_t Behavior::operator&(const uint32_t & type) const {
return this->_type & type;
}
}
\ No newline at end of file
#include "Event.hpp"
namespace megu::game {
Event::Event(uint32_t type)
: _type(type) {}
std::optional<uint32_t> Event::get(size_t key) const {
return this->_stats.get(key);
}
void Event::set(size_t key, uint32_t value) {
this->_stats.set(key, value);
}
bool Event::operator&(const uint32_t & type) const {
return (this->_type & type) != 0;
}
}
\ No newline at end of file
...@@ -3,25 +3,24 @@ ...@@ -3,25 +3,24 @@
#include "StatsAlterator.hpp" #include "StatsAlterator.hpp"
namespace megu::game { namespace megu::game {
class Behavior { class Event {
public: public:
enum Type : uint32_t { enum Type : uint32_t {
SOLID = 1, SOLID = 1,
DAMAGE = 2, DAMAGE = 2,
}; };
Behavior() = delete; Event() = delete;
Behavior(const kernel::Prop &, uint32_t); Event(uint32_t);
uint32_t get(uint32_t) const; std::optional<uint32_t> get(size_t) const;
void set(uint32_t, uint32_t); void set(size_t, uint32_t);
bool operator&(const uint32_t &) const;
uint32_t operator&(const uint32_t &) const;
private: private:
uint32_t _type; uint32_t _type;
StatsAlterator _stats; StatsAlterator<uint32_t> _stats;
}; };
} }
......
#include "StatsAlterator.hpp"
namespace megu::game {
StatsAlterator::StatsAlterator(const kernel::Prop & author)
: _author(author) {}
uint32_t StatsAlterator::get(uint32_t key) const {
if(this->_stats.contains(key)) {
return this->_stats.at(key);
}
return 0;
}
void StatsAlterator::set(uint32_t key, uint32_t value) {
this->_stats[key] = value;
}
const uint32_t & StatsAlterator::operator[](const uint32_t & key) const {
if(this->_stats.contains(key)) {
return this->_stats.at(key);
}
throw std::runtime_error("Cannot get stats value");
}
uint32_t & StatsAlterator::operator[](const uint32_t & key) {
if(this->_stats.contains(key)) {
return this->_stats.at(key);
}
throw std::runtime_error("Cannot get stats value");
}
}
\ No newline at end of file
...@@ -3,19 +3,19 @@ ...@@ -3,19 +3,19 @@
#include <kernel/front/props/Props.hpp> #include <kernel/front/props/Props.hpp>
namespace megu::game { namespace megu::game {
template <class D>
class StatsAlterator { class StatsAlterator {
public: public:
StatsAlterator() = delete; StatsAlterator() = default;
StatsAlterator(const kernel::Prop &);
uint32_t get(uint32_t) const; std::optional<D> get(size_t) const;
void set(uint32_t, uint32_t); void set(size_t, D);
const uint32_t & operator[](const uint32_t &) const; std::optional<D> operator[](size_t) const;
uint32_t & operator[](const uint32_t &);
private: private:
std::map<uint32_t, uint32_t> _stats; std::map<size_t, D> _stats;
const kernel::Prop & _author;
}; };
} }
#include "StatsAlterator.tpp"
\ No newline at end of file
#include "StatsAlterator.hpp"
namespace megu::game {
template <class D>
std::optional<D> StatsAlterator<D>::get(size_t key) const {
if(this->_stats.contains(key)) {
return this->_stats.at(key);
}
return {};
}
template <class D>
void StatsAlterator<D>::set(size_t key, D value) {
this->_stats[key] = value;
}
template <class D>
std::optional<D> StatsAlterator<D>::operator[](size_t key) const {
return this->get(key);
}
}
\ No newline at end of file
...@@ -18,25 +18,28 @@ namespace megu::game { ...@@ -18,25 +18,28 @@ namespace megu::game {
this->_sprite.setSize({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) { this->_movable.setCollideLambda([this, &level](kernel::Kernel &, kernel::PhysicEngine &, Identifiable & id, kernel::Physical<kernel::PhysicEngine> & comp, double) {
auto event = level.get(id)->on(); auto object = level.get(id);
if(object.has_value()) {
auto event = object->get().on();
if(event.has_value()) { if(event.has_value()) {
this->on(event.value()); this->on(*object->get().get(), event.value());
}
} }
}); });
} }
void Enemy::on(const Behavior & event) { void Enemy::on(const kernel::Prop &, const Event & event) {
if(event & Behavior::Type::SOLID) { if(event & Event::Type::SOLID) {
this->onSolide(event); this->onSolide(event);
} }
if(event & Behavior::Type::DAMAGE) { if(event & Event::Type::DAMAGE) {
this->onDamage(event); this->onDamage(event);
} }
} }
std::optional<Behavior> Enemy::on() const { std::optional<Event> Enemy::on() const {
Behavior b(*this, Behavior::DAMAGE); Event b(Event::DAMAGE);
b.set(0, 10); b.set(0, 10);
return b; return b;
...@@ -50,11 +53,11 @@ namespace megu::game { ...@@ -50,11 +53,11 @@ namespace megu::game {
kernel.add(this); kernel.add(this);
} }
void Enemy::onDamage(const Behavior &) { void Enemy::onDamage(const Event &) {
std::cout << "Enemy Got Damage !" << std::endl; std::cout << "Enemy Got Damage !" << std::endl;
} }
void Enemy::onSolide(const Behavior &) { void Enemy::onSolide(const Event &) {
std::cout << "Enemy Got Solide !" << std::endl; std::cout << "Enemy Got Solide !" << std::endl;
} }
} }
\ No newline at end of file
#pragma once #pragma once
#include <game/back/GameObject.hpp>
#include <kernel/front/props/PropsDynamic.hpp> #include <kernel/front/props/PropsDynamic.hpp>
#include <game/back/GameObject.hpp>
#include <game/back/object/Level.hpp> #include <game/back/object/Level.hpp>
namespace megu::game { namespace megu::game {
...@@ -16,11 +16,11 @@ namespace megu::game { ...@@ -16,11 +16,11 @@ namespace megu::game {
void apply(kernel::Kernel &) override; void apply(kernel::Kernel &) override;
void on(const Behavior &) override; void on(const kernel::Prop &, const Event &) override;
std::optional<Behavior> on() const override; std::optional<Event> on() const override;
void onDamage(const Behavior &); void onDamage(const Event &);
void onSolide(const Behavior &); void onSolide(const Event &);
private: private:
kernel::Sprite _sprite; kernel::Sprite _sprite;
......
...@@ -4,8 +4,13 @@ namespace megu::game { ...@@ -4,8 +4,13 @@ namespace megu::game {
Level::Level(const std::string & name) Level::Level(const std::string & name)
: _name(name) {} : _name(name) {}
GameProps * Level::get(Identifiable & id) const { std::optional<std::reference_wrapper<GameProps>> Level::get(Identifiable & id) const {
return this->_objecs.at(id.id()); for(auto it = this->_objecs.begin(); it != this->_objecs.end(); ++it) {
if(it->first == id) {
return *it->second;
}
}
return {};
} }
void Level::add(GameProps * prop) { void Level::add(GameProps * prop) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment