From c9472f1aead922fe54dc6b4da459584523fe74e7 Mon Sep 17 00:00:00 2001 From: Jean Tampon Date: Tue, 5 May 2020 16:53:50 +0200 Subject: [PATCH] Fix reserve bug --- include/ant.hpp | 3 ++- include/display_manager.hpp | 3 ++- include/world.hpp | 18 ++++++++++-------- src/display_manager.cpp | 16 ++++++++++------ src/main.cpp | 4 ++-- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/include/ant.hpp b/include/ant.hpp index db8d71ef..df4c0251 100644 --- a/include/ant.hpp +++ b/include/ant.hpp @@ -87,9 +87,10 @@ struct Ant , last_direction_update(getRandUnder(100.0f) * 0.01f * direction_update_period) , last_marker(getRandUnder(100.0f) * 0.01f * marker_period) , phase(Marker::Type::ToFood) - , reserve(max_reserve) + , reserve(0.0f) , id(id_) { + reserve = max_reserve; } void update(const float dt, World& world) diff --git a/include/display_manager.hpp b/include/display_manager.hpp index c1a48f49..48880e86 100644 --- a/include/display_manager.hpp +++ b/include/display_manager.hpp @@ -35,7 +35,8 @@ class DisplayManager sf::Vector2f displayCoordToWorldCoord(const sf::Vector2f&); bool clic; - bool emit; + bool pause; + bool draw_markers; bool update; float render_time; bool speed_mode; diff --git a/include/world.hpp b/include/world.hpp index ca67f40d..0e084d3a 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -128,14 +128,14 @@ struct World markers_count = 0u; for (std::list& l : grid_markers_home.cells) { for (Marker& m : l) { - markers_count += !m.permanent; + markers_count += m.permanent ? 0 : 1; m.update(dt); } } for (std::list& l : grid_markers_food.cells) { for (Marker& m : l) { - markers_count += !m.permanent; + markers_count += m.permanent ? 0 : 1; m.update(dt); } } @@ -146,13 +146,15 @@ struct World return getGrid(marker.type).add(marker); } - void render(sf::RenderTarget& target, const sf::RenderStates& states) const + void render(sf::RenderTarget& target, const sf::RenderStates& states, bool draw_markers = true) const { - va.resize(4 * markers_count); - generateMarkersVertexArray(va); - sf::RenderStates rs = states; - rs.texture = &(*Conf<>::MARKER_TEXTURE); - target.draw(va, rs); + if (draw_markers) { + va.resize(4 * markers_count); + generateMarkersVertexArray(va); + sf::RenderStates rs = states; + rs.texture = &(*Conf<>::MARKER_TEXTURE); + target.draw(va, rs); + } for (const std::list& l : grid_food.cells) { for (const Food& f : l) { diff --git a/src/display_manager.cpp b/src/display_manager.cpp index 60ff4ad1..a2046dcc 100644 --- a/src/display_manager.cpp +++ b/src/display_manager.cpp @@ -1,5 +1,4 @@ #include "display_manager.hpp" -#include DisplayManager::DisplayManager(sf::RenderTarget& target, sf::RenderWindow& window, World& world, Colony& colony) @@ -16,7 +15,8 @@ DisplayManager::DisplayManager(sf::RenderTarget& target, sf::RenderWindow& windo , m_colony(colony) , clic(false) , m_mouse_button_pressed(false) - , emit(true) + , pause(false) + , draw_markers(true) { m_windowOffsetX = m_window.getSize().x * 0.5f; m_windowOffsetY = m_window.getSize().y * 0.5f; @@ -68,7 +68,7 @@ void DisplayManager::draw() rs.transform.scale(m_zoom, m_zoom); rs.transform.translate(-m_offsetX, -m_offsetY); - m_world.render(m_target, rs); + m_world.render(m_target, rs, draw_markers); m_colony.render(m_target, rs); render_time = clock.getElapsedTime().asMicroseconds() * 0.001f; @@ -89,7 +89,8 @@ void DisplayManager::processEvents() else if ((event.key.code == sf::Keyboard::Subtract)) zoom(0.8f); else if ((event.key.code == sf::Keyboard::Add)) zoom(1.2f); else if ((event.key.code == sf::Keyboard::Space)) update = !update; - else if ((event.key.code == sf::Keyboard::E)) emit = !emit; + else if ((event.key.code == sf::Keyboard::E)) pause = !pause; + else if ((event.key.code == sf::Keyboard::A)) draw_markers = !draw_markers; else if ((event.key.code == sf::Keyboard::D)) debug_mode = !debug_mode; else if ((event.key.code == sf::Keyboard::R)) { @@ -97,10 +98,13 @@ void DisplayManager::processEvents() m_offsetY = 0.0f; m_zoom = 1.0f; } - else if ((event.key.code == sf::Keyboard::E)) + else if ((event.key.code == sf::Keyboard::S)) { speed_mode = !speed_mode; - m_window.setVerticalSyncEnabled(!speed_mode); + if (speed_mode) + m_window.setFramerateLimit(0); + else + m_window.setFramerateLimit(60); } break; case sf::Event::MouseWheelMoved: diff --git a/src/main.cpp b/src/main.cpp index 9829b1b0..3e197137 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ int main() World world(Conf<>::WIN_WIDTH, Conf<>::WIN_HEIGHT); Colony colony(800, 450, 512); - world.addMarker(Marker(colony.position, Marker::ToHome, 100000.0f, true)); + world.addMarker(Marker(colony.position, Marker::ToHome, 10.0f, true)); DisplayManager display_manager(window, window, world, colony); @@ -40,7 +40,7 @@ int main() const float dt = 0.016f; - if (display_manager.emit) { + if (!display_manager.pause) { colony.update(dt, world); world.update(dt); }