Commit 16a597d3 authored by Georg Schäfer's avatar Georg Schäfer

Add rigid body component.

parent a5556908
Pipeline #3257 failed with stage
in 2 minutes and 10 seconds
{ {
"Transform":{ "Transform": {
"scale": {"x": 0.5, "y": 0.5, "z": 0.5} "scale": {
"x": 0.5,
"y": 0.5,
"z": 0.5
}
}, },
"Model": { "Model": {
"aid": "model:cube" "aid": "model:cube"
}, },
"Shadowcaster": { "Shadowcaster": {},
},
"Movement": { "Movement": {
"beats_per_step": 1, "beats_per_step": 1,
"distance_per_step": 1, "distance_per_step": 1,
...@@ -14,5 +17,8 @@ ...@@ -14,5 +17,8 @@
"off_beat_threshold": 0.2 "off_beat_threshold": 0.2
}, },
"Input_controller": {}, "Input_controller": {},
"Player": {} "Player": {},
"Rigid_body": {
"radius": 1.0
}
} }
#include "rigid_body_comp.hpp"
#pragma once
#include <mirrage/ecs/ecs.hpp>
namespace phase_shifter::gameplay {
struct Rigid_body_comp : public mirrage::ecs::Component<Rigid_body_comp> {
static constexpr const char* name() { return "Rigid_body"; }
using Component::Component;
float radius;
};
sf2_structDef(Rigid_body_comp, radius);
} // namespace phase_shifter::gameplay
#include "level_system.hpp" #include "level_system.hpp"
#include "../gameplay/movement_comp.hpp"
#include "../gameplay/player_comp.hpp"
#include "../gameplay/rigid_body_comp.hpp"
#include "../gameplay/viewtarget_comp.hpp" #include "../gameplay/viewtarget_comp.hpp"
#include <mirrage/ecs/components/transform_comp.hpp> #include <mirrage/ecs/components/transform_comp.hpp>
...@@ -12,6 +15,8 @@ namespace phase_shifter::level { ...@@ -12,6 +15,8 @@ namespace phase_shifter::level {
Level_system::Level_system(mirrage::ecs::Entity_manager& entities, mirrage::asset::Asset_manager& assets) Level_system::Level_system(mirrage::ecs::Entity_manager& entities, mirrage::asset::Asset_manager& assets)
: _entities(entities), _assets(assets) : _entities(entities), _assets(assets)
{ {
_entities.register_component_type<gameplay::Rigid_body_comp>();
_spawners.emplace("p", [&](const Tile& tile, const glm::vec3& position) { _spawners.emplace("p", [&](const Tile& tile, const glm::vec3& position) {
mirrage::ecs::Entity_facet player = mirrage::ecs::Entity_facet player =
_entities.entity_builder(tile.spawns).position(position).create(); _entities.entity_builder(tile.spawns).position(position).create();
...@@ -36,6 +41,24 @@ namespace phase_shifter::level { ...@@ -36,6 +41,24 @@ namespace phase_shifter::level {
}); });
} }
auto Level_system::update(const mirrage::util::Time& time) -> void
{
for(auto&& [player, movement, transform, rigid_body] : _entities.list<gameplay::Player_comp,
gameplay::Movement_comp,
Transform_comp,
gameplay::Rigid_body_comp>()) {
auto origin = glm::vec2{transform.position.x, transform.position.z};
auto direction = glm::normalize(movement.aim);
auto [hit, contact] = ray_cast(origin, direction);
if(!hit) {
continue;
}
if(contact.distance2 <= rigid_body.radius * rigid_body.radius) {
movement.move = false;
}
}
}
namespace { namespace {
using action = std::function<auto(const Tileset& tileset, using action = std::function<auto(const Tileset& tileset,
const std::string& key, const std::string& key,
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include "../util/collision.hpp" #include "../util/collision.hpp"
#include "level.hpp" #include "level.hpp"
#include <mirrage/utils/units.hpp>
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
...@@ -21,6 +23,7 @@ namespace phase_shifter::level { ...@@ -21,6 +23,7 @@ namespace phase_shifter::level {
public: public:
Level_system(mirrage::ecs::Entity_manager& entities, mirrage::asset::Asset_manager& assets); Level_system(mirrage::ecs::Entity_manager& entities, mirrage::asset::Asset_manager& assets);
auto update(const mirrage::util::Time& time) -> void;
auto load(const std::string& name) -> void; auto load(const std::string& name) -> void;
auto ray_cast(const glm::vec2& origin, const glm::vec2& direction) -> std::pair<bool, util::Contact>; auto ray_cast(const glm::vec2& origin, const glm::vec2& direction) -> std::pair<bool, util::Contact>;
......
...@@ -107,6 +107,7 @@ namespace phase_shifter { ...@@ -107,6 +107,7 @@ namespace phase_shifter {
_beat_system->update(dt); _beat_system->update(dt);
_input_system->update(dt); _input_system->update(dt);
_level_system->update(dt);
_movement_system->update(dt); _movement_system->update(dt);
_combat_system->update(dt); _combat_system->update(dt);
_camera_system->update(dt); _camera_system->update(dt);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment