Skip to content

Commit

Permalink
Move timer logic to ui::player
Browse files Browse the repository at this point in the history
  • Loading branch information
Smertig committed Oct 12, 2020
1 parent 5fe0fec commit 0b9b61b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_executable(${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/source/scene/camera.cpp
${PROJECT_SOURCE_DIR}/source/resources/config.cpp
${PROJECT_SOURCE_DIR}/source/resources/textured_sprite.cpp
${PROJECT_SOURCE_DIR}/source/ui/player.cpp
${PROJECT_SOURCE_DIR}/source/replay/replay.cpp
${PROJECT_SOURCE_DIR}/source/util/platform.cpp
)
Expand Down
23 changes: 13 additions & 10 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <scene/map.hpp>
#include <scene/player.hpp>
#include <scene/camera.hpp>
#include <ui/player.hpp>
#include <replay/replay.hpp>
#include <util/platform.hpp>

Expand Down Expand Up @@ -37,13 +38,16 @@ int main(int argc, char** argv) {
r.parse(ifs);

// Try fix paths
if (!std::filesystem::exists("res/config.json"))
if (!std::filesystem::exists("res/config.json")) {
platform::fix_working_directory();
}

// Make window
sf::RenderWindow window(sf::VideoMode::getFullscreenModes().front(), platform::get_app_fullname());
window.setVerticalSyncEnabled(true);

// Prepare UI
ui::player player(window);
ImGui::SFML::Init(window);

// Prepare map and camera
Expand All @@ -58,13 +62,9 @@ int main(int argc, char** argv) {
camera.zoom(m.get_default_zoom());

sf::Clock deltaClock;
int current_time = 0;

while (window.isOpen()) {
const auto dt = deltaClock.restart();

current_time += dt.asMilliseconds() * 3;

// Process events
sf::Event event{};
while (window.pollEvent(event)) {
Expand All @@ -81,23 +81,25 @@ int main(int argc, char** argv) {

// Update
ImGui::SFML::Update(window, dt);
player.update(dt.asMilliseconds());

if (r.is_meeting(current_time)) {
if (r.is_meeting(player.get_time())) {
for (const auto& [id, info] : r.get_players()) {
m.set_meeting(id);
}
current_time += 1000;

player.update(1000);
}
else {
for (const auto& [id, info] : r.get_players()) {
const auto state = info.get_interpolated(current_time);
const auto state = info.get_interpolated(player.get_time());
m.set_player_state(id, state.position, state.velocity, state.is_dead);
}
}

if (current_time >= r.get_duration()) {
if (player.get_time() >= r.get_duration()) {
if (platform::msgbox_ask("End of round. Restart?")) {
current_time = 0;
player.set_time(0);
deltaClock.restart();
}
else {
Expand All @@ -109,6 +111,7 @@ int main(int argc, char** argv) {
window.clear();
window.setView(camera.view());
m.draw(window, sf::Transform::Identity);
player.render();
ImGui::SFML::Render(window);
window.display();
}
Expand Down
25 changes: 25 additions & 0 deletions source/ui/player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "player.hpp"

namespace ui {

player::player(sf::RenderWindow& window) : m_time(0), m_window(window) {
// nothing
}

void player::update(int dt) {
m_time += dt * 3;
}

void player::render() {
// nothing
}

int player::get_time() const {
return m_time;
}

void player::set_time(int t) {
m_time = t;
}

} // namespace ui
25 changes: 25 additions & 0 deletions source/ui/player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <chrono>

#include <SFML/Graphics/RenderWindow.hpp>

namespace ui {

class player {
int m_time = 0; // milliseconds
sf::RenderWindow& m_window;

public:
explicit player(sf::RenderWindow& window);

void update(int dt);

void render();

int get_time() const;

void set_time(int t);
};

} // namespace ui

0 comments on commit 0b9b61b

Please sign in to comment.