Commit 6bba32a4 authored by Florian Oetke's avatar Florian Oetke
Browse files

Merge branch 'develop' into feature/9-highlevel-gameplay

parents f1002aa7 b544ba3c
Pipeline #3238 failed with stage
in 4 minutes and 8 seconds
......@@ -5,5 +5,16 @@
"fov": 50.0,
"near": 0.05,
"far": 80.0
},
"Viewtarget": {
"offset": {
"x": 0.0,
"y": 10.0,
"z": 1.0
}
},
"Spring": {
"spring_constant": 20.0,
"mass": 1.0
}
}
Subproject commit f2ac702cb0a4c321afee8251ed544114f5c29a22
Subproject commit a75972722af8eef63c531085703c680d535e1a2d
#include "camera_system.hpp"
#include <mirrage/ecs/components/transform_comp.hpp>
namespace phase_shifter::gameplay {
Camera_system::Camera_system(mirrage::ecs::Entity_manager& entity_manager)
: _entity_manager(entity_manager)
{
_entity_manager.register_component_type<Viewtarget_comp>();
_entity_manager.register_component_type<Spring_comp>();
}
void Camera_system::update(mirrage::util::Time dt)
{
for(auto&& [camera, viewtarget, spring] :
_entity_manager.list<mirrage::ecs::Entity_facet, Viewtarget_comp, Spring_comp>()) {
auto target_facet = _entity_manager.get(viewtarget.target);
if(target_facet.is_some()) {
auto target_transform =
target_facet.get_or_throw().get<mirrage::ecs::components::Transform_comp>();
if(target_transform.is_some()) {
auto target_position = target_transform.get_or_throw().position;
glm::vec3 offset = viewtarget.offset;
glm::vec3 dest_pos = {target_position.x + offset.x,
target_position.y + offset.y,
target_position.z + offset.z};
float damping_constant = 2 * sqrtf(spring.spring_constant);
glm::vec3 damping_force = -damping_constant * spring.velocity;
camera.process([&](mirrage::ecs::components::Transform_comp& transform) {
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;
transform.direction(-offset);
});
}
}
}
}
} // namespace phase_shifter::gameplay
#pragma once
#include <mirrage/utils/units.hpp>
#include <mirrage/ecs/entity_manager.hpp>
#include "viewtarget_comp.hpp"
#include "spring_comp.hpp"
namespace phase_shifter::gameplay {
class Camera_system {
public:
Camera_system(mirrage::ecs::Entity_manager& entity_manager);
void update(mirrage::util::Time dt);
private:
mirrage::ecs::Entity_manager& _entity_manager;
};
}
\ No newline at end of file
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
namespace phase_shifter::gameplay {
struct Spring_comp : public mirrage::ecs::Component<Spring_comp> {
static constexpr const char* name() { return "Spring"; }
using Component::Component;
glm::vec3 velocity = {0.f, 0.f, 0.f};
float spring_constant = 1.f;
float mass = 1.f;
};
sf2_structDef(Spring_comp, spring_constant);
}
\ No newline at end of file
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
namespace phase_shifter::gameplay {
struct Viewtarget_comp : public mirrage::ecs::Component<Viewtarget_comp> {
static constexpr const char* name() { return "Viewtarget"; }
using Component::Component;
mirrage::ecs::Entity_handle target;
glm::vec3 offset = {0.f, 10.f, 1.f};
};
sf2_structDef(Viewtarget_comp, offset);
}
\ No newline at end of file
......@@ -3,6 +3,7 @@
#include "audio/audio_asset.hpp"
#include "game_engine.hpp"
#include "gameplay/beat_system.hpp"
#include "gameplay/camera_system.hpp"
#include "gameplay/movement_system.hpp"
#include "helper/attachment_system.hpp"
#include "input/input_system.hpp"
......@@ -30,7 +31,8 @@ namespace phase_shifter {
, _movement_system(std::make_unique<gameplay::Movement_system>(_entities, *_beat_system))
, _input_system(std::make_unique<input::Input_system>(engine.bus(), _entities))
, _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities, *_beat_system))
, _camera_system(std::make_unique<gameplay::Camera_system>(_entities))
{
_entities.register_component_type<ecs::components::Transform_comp>();
......@@ -79,17 +81,6 @@ namespace phase_shifter {
auto cam_azimuth = 0.0f;
auto cam_dir = glm::quat(glm::vec3(
(cam_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * cam_azimuth, 0.f));
entities()
.entity_builder("camera")
.rotation(cam_dir)
.position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f)
.post_create([=](ecs::Entity_facet entity) {
entity.process<Transform_comp>([&](auto& transform) {
transform.position = transform.direction() * -10.f;
transform.look_at({0, 0, 0});
});
})
.create();
auto sun_elevation = 0.05f * glm::pi<float>();
auto sun_azimuth = 0.7f;
......@@ -101,7 +92,21 @@ namespace phase_shifter {
.position(glm::rotate(sun_dir, glm::vec3(0, 0, 1)) * 40.f)
.create();
entities().entity_builder("player").position({0, 1, 0}).create();
mirrage::ecs::Entity_facet player = entities().entity_builder("player").position({0, 1, 0}).create();
auto playerhandle = player.handle();
entities()
.entity_builder("camera")
.rotation(cam_dir)
.position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f)
.post_create([=](ecs::Entity_facet entity) {
entity.process<Transform_comp>([&](auto& transform) {
transform.position = transform.direction() * -10.f;
transform.look_at({0, 0, 0});
});
entity.process(
[=](gameplay::Viewtarget_comp& viewtarget) { viewtarget.target = playerhandle; });
})
.create();
entities()
.entity_builder("cube")
......@@ -127,6 +132,7 @@ namespace phase_shifter {
_beat_system->update(dt);
_input_system->update(dt);
_movement_system->update(dt);
_camera_system->update(dt);
_attachment_system->update(dt);
......
......@@ -22,6 +22,7 @@ namespace phase_shifter {
namespace gameplay {
class Beat_system;
class Movement_system;
class Camera_system;
} // namespace gameplay
namespace helper {
class Attachment_system;
......@@ -60,6 +61,7 @@ namespace phase_shifter {
std::unique_ptr<input::Input_system> _input_system;
std::unique_ptr<helper::Attachment_system> _attachment_system;
std::unique_ptr<ui::Hud_system> _hud_system;
std::unique_ptr<gameplay::Camera_system> _camera_system;
// TODO: add systems here
mirrage::util::Console_command_container _commands;
......
......@@ -13,16 +13,28 @@ namespace phase_shifter::ui {
namespace {
constexpr auto hud_height = 100;
}
constexpr auto count_down = 300;
constexpr auto bar_speed = 100;
} // namespace
Hud_system::Hud_system(mirrage::gui::Gui& gui, mirrage::ecs::Entity_manager& ecs)
: _gui(gui)
, _ecs(ecs)
Hud_system::Hud_system(mirrage::gui::Gui& gui,
mirrage::ecs::Entity_manager& ecs,
const phase_shifter::gameplay::Beat_system& beat_system)
: _gui(gui), _ecs(ecs), _timeLeft(count_down), _beat_system(beat_system)
{
//_circle_texture = _gui.load_texture("tex:circle"_aid);
_max_distance = gui.viewport().z / 2;
}
void Hud_system::update(mirrage::util::Time dt)
{
_timeLeft -= dt;
auto state = _beat_system.beat_state();
int max_bars = static_cast<int>(_max_distance / (bar_speed * state.avg_beat_time) + 1);
if(state.beat && _beats < max_bars) {
_beats++;
}
_offset = state.time_to_beat * bar_speed;
}
void Hud_system::draw()
......@@ -33,20 +45,56 @@ namespace phase_shifter::ui {
if(font.is_some())
ImGui::PushFont(font.get_or_throw());
ImGui::PushStyleColor(ImGuiCol_WindowBg, "#333333FF"_imcolor.Value);
ImGui::PositionNextWindow(glm::vec2(viewport.z, hud_height),
ImGui::WindowPosition_X::center,
ImGui::WindowPosition_Y::bottom);
ImGui::WindowPosition_X::left,
ImGui::WindowPosition_Y::top);
if(ImGui::Begin("hud",
nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize)) {
ImGui::Text("TODO");
nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoBackground)) {
ImGui::Text("%.2f", _timeLeft.value());
ImGui::End();
}
ImGui::PositionNextWindow(
glm::vec2(viewport.z, 150), ImGui::WindowPosition_X::center, ImGui::WindowPosition_Y::bottom);
if(ImGui::Begin("beat",
nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoBackground)) {
ImGui::PushStyleColor(ImGuiCol_Border, "#00000000"_imcolor.Value); // Hide Border
ImVec2 cursor = ImGui::GetCursorScreenPos();
auto beat_state = _beat_system.beat_state();
int max_bars = static_cast<int>(_max_distance / (bar_speed * beat_state.avg_beat_time) + 1);
auto line_offset = bar_speed * beat_state.avg_beat_time;
auto start_offset = (max_bars - _beats) * line_offset;
ImGui::BeginChild("Line", {viewport.z, 90}, true);
ImDrawList* drawList = ImGui::GetWindowDrawList();
for(int i = 0; i < _beats; i++) {
ImVec2 p(cursor.x + _max_distance - _offset - start_offset - i * line_offset, cursor.y + 10);
drawList->AddLine(p, {p.x, p.y + 50}, 0xFFFFFFFF, 1);
}
for(int i = 0; i < _beats; i++) {
ImVec2 p(cursor.x + viewport.z / 2 + _offset + start_offset + i * line_offset, cursor.y + 10);
drawList->AddLine(p, {p.x, p.y + 50}, 0xFFFFFFFF, 1);
}
ImVec2 middle(cursor.x + viewport.z / 2, cursor.y);
drawList->AddLine(middle, {middle.x, middle.y + 70}, 0xFFFF0000, 3);
ImGui::EndChild();
ImGui::PopStyleColor();
ImGui::End();
if(beat_state.beat || beat_state.avg_beat_time - beat_state.time_since_beat < 0.1f
|| beat_state.time_to_beat < 0.1f) {
//TODO: Flash image to beat
//ImGui::Image(_circle_texture.get(), {10, 10});
}
}
ImGui::PopStyleColor();
if(font.is_some())
ImGui::PopFont();
......
......@@ -6,13 +6,16 @@
#include <unordered_map>
#include <memory>
#include "../gameplay/beat_system.hpp"
namespace phase_shifter::ui {
class Hud_system {
public:
Hud_system(mirrage::gui::Gui& gui, mirrage::ecs::Entity_manager& ecs);
Hud_system(mirrage::gui::Gui& gui,
mirrage::ecs::Entity_manager& ecs,
const phase_shifter::gameplay::Beat_system& beat_system);
void update(mirrage::util::Time dt);
void draw();
......@@ -20,6 +23,11 @@ namespace phase_shifter::ui {
private:
mirrage::gui::Gui& _gui;
mirrage::ecs::Entity_manager& _ecs;
mirrage::util::Time _timeLeft;
int _beats = 0;
float _offset = 0;
float _max_distance;
const phase_shifter::gameplay::Beat_system& _beat_system;
};
} // namespace phase_shifter::ui
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