Skip to content

Commit

Permalink
Edits working
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Dec 7, 2021
1 parent 2fe4b2b commit 12a3f78
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 29 deletions.
12 changes: 10 additions & 2 deletions include/editor/control_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ struct ControlState
using ViewAction = std::function<void(sf::Vector2f)>;
using Action = std::function<void(void)>;
// Actions
Action action = nullptr;
ViewAction view_action = nullptr;
Action action = nullptr;
ViewAction view_action = nullptr;
Action view_action_end = nullptr;
// Special commands
bool focus_requested = false;
trn::Transition<sf::Vector2f> focus;
Expand All @@ -25,6 +26,13 @@ struct ControlState
}
}

void executeViewActionEnd()
{
if (view_action_end) {
view_action_end();
}
}

void executeAction(sf::Vector2f)
{
if (action) {
Expand Down
4 changes: 1 addition & 3 deletions include/editor/editor_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,9 @@ struct EditorScene : public GUI::Scene
global_controls->addItem(time_controls);

watch(edit_toggle, [this, edit_toggle, time_controls](){
this->tool_selector->setEditMode(edit_toggle->state);
if (edit_toggle->state) {
time_controls->select(TimeController::State::Pause);
this->tool_selector->setCallback();
} else {
this->tool_selector->resetCallback();
}
});

Expand Down
93 changes: 82 additions & 11 deletions include/editor/tool_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "GUI/button.hpp"
#include "control_state.hpp"
#include "simulation/simulation.hpp"
#include <future>


namespace edtr
Expand Down Expand Up @@ -51,9 +52,9 @@ struct ToolSelector : public GUI::NamedContainer
{
enum class Tool
{
BrushCreate,
BrushDelete,
BrushColor
BrushWall,
BrushFood,
BrushDelete
};

Tool current_tool;
Expand All @@ -62,6 +63,7 @@ struct ToolSelector : public GUI::NamedContainer
SPtr<ToolOption> tool_food;
SPtr<ToolOption> tool_erase;

bool edit_mode = false;
Simulation& simulation;
ControlState& control_state;

Expand All @@ -70,23 +72,23 @@ struct ToolSelector : public GUI::NamedContainer
explicit
ToolSelector(ControlState& control_state_, Simulation& simulation_)
: GUI::NamedContainer("Brushes", Container::Orientation::Horizontal)
, current_tool(Tool::BrushCreate)
, current_tool(Tool::BrushWall)
, control_state(control_state_)
, simulation(simulation_)
{
padding = 5.0f;

root->setHeight(30.0f, GUI::Size::Fixed);
tool_wall = create<ToolOption>("Wall", [this](){
current_tool = Tool::BrushCreate;
current_tool = Tool::BrushWall;
select(tool_wall);
});
tool_food = create<ToolOption>("Food", [this](){
current_tool = Tool::BrushDelete;
current_tool = Tool::BrushFood;
select(tool_food);
});
tool_erase = create<ToolOption>("Erase", [this](){
current_tool = Tool::BrushColor;
current_tool = Tool::BrushDelete;
select(tool_erase);
});
tool_food->color = sf::Color(200, 255, 200);
Expand All @@ -111,6 +113,7 @@ struct ToolSelector : public GUI::NamedContainer
reset();
if (option) {
option->select();
setCallback();
}
notifyChanged();
}
Expand All @@ -132,16 +135,84 @@ struct ToolSelector : public GUI::NamedContainer
}
}

void addWall(sf::Vector2f mouse_position)
{
const auto x = to<int32_t>(mouse_position.x) / simulation.world.map.cell_size;
const auto y = to<int32_t>(mouse_position.y) / simulation.world.map.cell_size;

const int32_t min_x = std::max(1, x - brush_size);
const int32_t max_x = std::min(to<int32_t>(simulation.world.size.x - 2), x + brush_size + 1);
const int32_t min_y = std::max(1, y - brush_size);
const int32_t max_y = std::min(to<int32_t>(simulation.world.size.y - 2), y + brush_size + 1);

for (int32_t px(min_x); px < max_x; ++px) {
for (int32_t py(min_y); py < max_y; ++py) {
simulation.world.addWall(sf::Vector2i{px, py});
}
}
}

void erase(sf::Vector2f mouse_position)
{
const auto x = to<int32_t>(mouse_position.x) / simulation.world.map.cell_size;
const auto y = to<int32_t>(mouse_position.y) / simulation.world.map.cell_size;

const int32_t min_x = std::max(1, x - brush_size);
const int32_t max_x = std::min(to<int32_t>(simulation.world.size.x - 2), x + brush_size + 1);
const int32_t min_y = std::max(1, y - brush_size);
const int32_t max_y = std::min(to<int32_t>(simulation.world.size.y - 2), y + brush_size + 1);

for (int32_t px(min_x); px < max_x; ++px) {
for (int32_t py(min_y); py < max_y; ++py) {
simulation.world.map.clearCell({px, py});
}
}
}

void setCallback()
{
control_state.view_action = [this](sf::Vector2f mouse_position){
addFood(mouse_position);
};
if (edit_mode) {
switch (current_tool) {
case Tool::BrushWall:
control_state.view_action = [this](sf::Vector2f mouse_position) {
addWall(mouse_position);
};
control_state.view_action_end = [this]() {
simulation.distance_field_builder.requestUpdate();
};
break;
case Tool::BrushFood:
control_state.view_action = [this](sf::Vector2f mouse_position) {
addFood(mouse_position);
};
break;
case Tool::BrushDelete:
control_state.view_action = [this](sf::Vector2f mouse_position) {
erase(mouse_position);
simulation.distance_field_builder.requestUpdate();
};
control_state.view_action_end = [this]() {
simulation.distance_field_builder.requestUpdate();
};
break;
}
}
}

void setEditMode(bool b)
{
edit_mode = b;
if (b) {
setCallback();
} else {
resetCallback();
}
}

void resetCallback()
{
control_state.view_action = nullptr;
control_state.view_action = nullptr;
control_state.view_action_end = nullptr;
}
};

Expand Down
1 change: 1 addition & 0 deletions include/editor/world_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct WorldView : GUI::Item
control_state.focus.setValueInstant(simulation.renderer.vp_handler.state.offset);
} else if (button == sf::Mouse::Button::Right) {
action_button_click = false;
control_state.executeViewActionEnd();
}
}

Expand Down
11 changes: 4 additions & 7 deletions include/simulation/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "render/renderer.hpp"
#include "simulation/world/map_loader.hpp"
#include "simulation/ant/fight_system.hpp"
#include "world/async_distance_field_builder.hpp"


struct Simulation
Expand All @@ -16,22 +17,23 @@ struct Simulation
World world;
// Render
Renderer renderer;
sfev::EventManager ev_manager;
EventSate ev_state;
FightSystem fight_system;
sf::Clock clock;
AsyncDistanceFieldBuilder distance_field_builder;

explicit
Simulation(sf::Window& window)
: world(Conf::WORLD_WIDTH, Conf::WORLD_HEIGHT)
, renderer()
, ev_manager(window, true)
, distance_field_builder(world.map)
{
}

void loadMap(const std::string& map_filename)
{
MapLoader::loadMap(world, map_filename);
distance_field_builder.requestUpdate();
}

void selectColony()
Expand All @@ -47,11 +49,6 @@ struct Simulation
world.renderer.selected_colony = -1;
}

void processEvents()
{
ev_manager.processEvents();
}

civ::Ref<Colony> createColony(float colony_x, float colony_y)
{
// Create the colony object
Expand Down
49 changes: 49 additions & 0 deletions include/simulation/world/async_distance_field_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <atomic>
#include <mutex>
#include <thread>
#include "world.hpp"
#include "distance_field_builder.hpp"


struct AsyncDistanceFieldBuilder
{
WorldGrid& map;
std::atomic<bool> update_requested;
bool running;
std::mutex mutex;
std::condition_variable condition;
std::thread worker;

explicit
AsyncDistanceFieldBuilder(WorldGrid& map_)
: map(map_)
, update_requested(false)
, running(true)
{
worker = std::thread([this]{update();});
}

~AsyncDistanceFieldBuilder()
{
update_requested = true;
running = false;
condition.notify_all();
worker.join();
}

void update()
{
std::unique_lock<std::mutex> lock(mutex);
while (running) {
condition.wait(lock, [this](){return update_requested.load();});
update_requested = false;
DistanceFieldBuilder::computeDistance(map);
}
}

void requestUpdate()
{
update_requested = true;
condition.notify_all();
}
};
3 changes: 1 addition & 2 deletions include/simulation/world/distance_field_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ struct DistanceFieldBuilder

static float getMinDist(int32_t x, int32_t y, WorldGrid& grid, bool dist_to_wall, int32_t max_iteration)
{
const sf::Vector2f cell_pos(to<float>(x), to<float>(y));
float min_dist = to<float>(max_iteration);
auto min_dist = to<float>(max_iteration);
for (int32_t dx(-max_iteration); dx <= max_iteration; ++dx) {
for (int32_t dy(-max_iteration); dy <= max_iteration; ++dy) {
const WorldCell* cell = grid.getSafe(sf::Vector2i(x + dx, y + dy));
Expand Down
1 change: 0 additions & 1 deletion include/simulation/world/map_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct MapLoader
}
}
}
DistanceFieldBuilder::computeDistance(world.map);
}
};

11 changes: 8 additions & 3 deletions include/simulation/world/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ struct World

void addWall(const sf::Vector2f& position)
{
if (map.checkCoords(position)) {
map.get(position).wall = 1;
}
addWall(sf::Vector2i{to<int32_t>(position.x) / map.cell_size, to<int32_t>(position.y) / map.cell_size});
}

void addWall(const sf::Vector2i& position)
{
if (map.checkCoords(position)) {
map.get(position).wall = 1;
}
}

void removeWall(const sf::Vector2f& position)
{
if (map.checkCoords(position)) {
Expand Down
7 changes: 7 additions & 0 deletions include/simulation/world/world_grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ struct WorldGrid : public Grid<WorldCell>
return get(pos).pick();
}

void clearCell(sf::Vector2i cell_coord)
{
auto& cell = get(cell_coord);
cell.wall = 0;
cell.food = 0;
}

HitPoint getFirstHit(sf::Vector2f p, sf::Vector2f d, float max_dist)
{
HitPoint intersection;
Expand Down

0 comments on commit 12a3f78

Please sign in to comment.