Commit 3c39a57c authored by Tim Scheiber's avatar Tim Scheiber

screen shake - the game

parent ccb6e796
Pipeline #3421 failed with stage
in 2 minutes and 33 seconds
......@@ -15,5 +15,6 @@
"Spring": {
"spring_constant": 20.0,
"mass": 1.0
}
},
"Shake": {}
}
......@@ -40,6 +40,7 @@ namespace phase_shifter::gameplay {
if(beat) {
_beat_index++;
LOG(plog::debug) << "beat";
_bus.send<Screen_shake_msg>(0.03f, 0.05f);
beats_left = size - 1 - _beat_index + _beat_offset;
if(beats_left <= 0 || _beat_index + 1 >= size) {
......
......@@ -6,17 +6,28 @@ namespace phase_shifter::gameplay {
using mirrage::ecs::components::Transform_comp;
Camera_system::Camera_system(mirrage::ecs::Entity_manager& entity_manager)
: _entity_manager(entity_manager)
Camera_system::Camera_system(mirrage::util::Message_bus& bus, mirrage::ecs::Entity_manager& entity_manager)
: _mailbox(bus), _entity_manager(entity_manager)
{
_entity_manager.register_component_type<Viewtarget_comp>();
_entity_manager.register_component_type<Spring_comp>();
_entity_manager.register_component_type<Shake_comp>();
_mailbox.subscribe_to([&](Screen_shake_msg& e) {
for (auto& shake : _entity_manager.list<Shake_comp>()) {
shake.intesity = e.intensity;
shake.duration = e.duration;
shake.time_remaining = e.duration;
}
});
}
void Camera_system::update(mirrage::util::Time dt)
{
for(auto&& [transform, viewtarget, spring] :
_entity_manager.list<Transform_comp, Viewtarget_comp, Spring_comp>()) {
_mailbox.update_subscriptions();
for(auto&& [transform, viewtarget, spring, shake] :
_entity_manager.list<Transform_comp, Viewtarget_comp, Spring_comp, Shake_comp>()) {
auto target_facet = _entity_manager.get(viewtarget.target);
if(target_facet.is_some()) {
......@@ -35,7 +46,13 @@ namespace phase_shifter::gameplay {
glm::vec3 spring_force = spring.spring_constant * (dest_pos - transform.position);
glm::vec3 acceleration = (spring_force + damping_force) / spring.mass;
spring.velocity += dt.value() * acceleration;
transform.position += dt.value() * spring.velocity;
spring.position += dt.value() * spring.velocity;
transform.position = spring.position;
if (shake.time_remaining > 0) {
glm::vec3 shake_direction = glm::normalize(glm::vec3{std::rand(), 0, std::rand()});
transform.position += shake.intesity * (shake.time_remaining / shake.duration) * shake_direction;
shake.time_remaining -= dt.value();
}
transform.direction(-offset);
}
}
......
......@@ -2,18 +2,23 @@
#include <mirrage/utils/units.hpp>
#include <mirrage/ecs/entity_manager.hpp>
#include <mirrage/utils/messagebus.hpp>
#include "../messages.hpp"
#include "viewtarget_comp.hpp"
#include "spring_comp.hpp"
#include "shake_comp.hpp"
namespace phase_shifter::gameplay {
class Camera_system {
public:
Camera_system(mirrage::ecs::Entity_manager& entity_manager);
Camera_system(mirrage::util::Message_bus& bus, mirrage::ecs::Entity_manager& entity_manager);
void update(mirrage::util::Time dt);
private:
mirrage::ecs::Entity_manager& _entity_manager;
mirrage::util::Mailbox_collection _mailbox;
};
}
\ No newline at end of file
......@@ -115,6 +115,7 @@ namespace phase_shifter::gameplay {
_bus.send<Enemy_killed_msg>(k_transform.position,
glm::vec3(attack_direction.x, 0.f, attack_direction.y));
_bus.send<Screen_shake_msg>(0.5f, 0.3f);
_ecs.erase(entity);
}
}
......
......@@ -221,6 +221,7 @@ namespace phase_shifter::gameplay {
Stadium{last_player_pos, player_pos, player_body.radius})) {
_entity_manager.erase(bullet_handle);
_bus.send<Damaged_msg>(player_handle);
_bus.send<Screen_shake_msg>(0.3f, 0.3f);
}
}
}
......
......@@ -12,6 +12,8 @@
#include "beat_system.hpp"
#include "../messages.hpp"
namespace phase_shifter::gameplay {
struct Stadium {
......
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
namespace phase_shifter::gameplay {
struct Shake_comp : public mirrage::ecs::Component<Shake_comp> {
static constexpr const char* name() { return "Shake"; }
using Component::Component;
float intesity = 0;
float duration = 0; //duration in seconds
float time_remaining = 0; //remaining shake time in seconds
};
} // namespace phase_shifter::gameplay
......@@ -12,6 +12,7 @@ namespace phase_shifter::gameplay {
float spring_constant = 1.f;
float mass = 1.f;
glm::vec3 position = {0.f, 0.f, 0.f};
glm::vec3 velocity = {0.f, 0.f, 0.f};
};
......
......@@ -4,6 +4,7 @@
#include "../gameplay/player_comp.hpp"
#include "../gameplay/rigid_body_comp.hpp"
#include "../gameplay/viewtarget_comp.hpp"
#include "../gameplay/spring_comp.hpp"
#include "../helper/attachment_comp.hpp"
#include <mirrage/ecs/components/transform_comp.hpp>
......@@ -66,6 +67,10 @@ namespace phase_shifter::level {
entity.process([=](gameplay::Viewtarget_comp& viewtarget) {
viewtarget.target = playerhandle;
});
entity.process(
[=](gameplay::Spring_comp& spring, Transform_comp& transform) {
spring.position = transform.position;
});
})
.create();
......
......@@ -29,4 +29,9 @@ namespace phase_shifter {
mirrage::util::Str_id id;
};
struct Screen_shake_msg {
float intensity;
float duration; //duration in seconds
};
} // namespace phase_shifter
......@@ -43,7 +43,7 @@ namespace phase_shifter {
, _combat_system(std::make_unique<gameplay::Combat_system>(engine.bus(), _entities, *_beat_system))
, _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities, *_beat_system))
, _camera_system(std::make_unique<gameplay::Camera_system>(_entities))
, _camera_system(std::make_unique<gameplay::Camera_system>(engine.bus(), _entities))
, _enemy_system(std::make_unique<gameplay::Enemy_system>(engine.bus(), _entities, *_beat_system))
, _dash_system(std::make_unique<gameplay::Dash_system>(engine.bus(), _entities))
, _stationary_attack_system(
......
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