Skip to content

Commit

Permalink
Fix reserve bug
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed May 5, 2020
1 parent d1e6de6 commit c9472f1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/ant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion include/display_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions include/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ struct World
markers_count = 0u;
for (std::list<Marker>& 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<Marker>& l : grid_markers_food.cells) {
for (Marker& m : l) {
markers_count += !m.permanent;
markers_count += m.permanent ? 0 : 1;
m.update(dt);
}
}
Expand All @@ -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<Food>& l : grid_food.cells) {
for (const Food& f : l) {
Expand Down
16 changes: 10 additions & 6 deletions src/display_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "display_manager.hpp"
#include <iostream>


DisplayManager::DisplayManager(sf::RenderTarget& target, sf::RenderWindow& window, World& world, Colony& colony)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -89,18 +89,22 @@ 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))
{
m_offsetX = 0.0f;
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:
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down

0 comments on commit c9472f1

Please sign in to comment.