Skip to content

Commit

Permalink
refactor(utils): added utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ms0g committed Apr 19, 2024
1 parent 322596f commit f4e79f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions src/utils/key.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>
#include "glm/glm.hpp"

namespace key {
std::string createFromPosition(const glm::vec3 pos) {
return std::string(std::to_string(pos.x) + (char)pos.y + (char)pos.z);
}
}
12 changes: 4 additions & 8 deletions src/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "glm/gtc/matrix_transform.hpp"
#include "cell.h"
#include "mesh/cellMesh.h"
#include "../utils/key.hpp"
#include "../shader/shader.h"

#define ADJ 1
Expand All @@ -18,8 +19,7 @@ World::World(const std::vector<glm::vec3>& initialState) {
mShader->setInt(mTextures[0].name, 0);

for (auto& pos: initialState) {
auto key = std::to_string(pos.x) + std::to_string(pos.y) + std::to_string(pos.z);
mAliveCells.emplace(key, Cell{pos});
mAliveCells.emplace(key::createFromPosition(pos), Cell{pos});
}
}

Expand Down Expand Up @@ -50,10 +50,7 @@ void World::update() {
for (auto& neighboringDeadCell: mNeighboringDeadCells) {
if (neighboringDeadCell.aliveNeighborsCount() == 4) {
neighboringDeadCell.resetAliveNeighbors();
auto key = std::to_string(neighboringDeadCell.pos().x) +
std::to_string(neighboringDeadCell.pos().y) +
std::to_string(neighboringDeadCell.pos().z);
mAliveCells.emplace(key, std::move(neighboringDeadCell));
mAliveCells.emplace(key::createFromPosition(neighboringDeadCell.pos()), std::move(neighboringDeadCell));
}
}

Expand Down Expand Up @@ -139,8 +136,7 @@ void World::checkNeighbor(Cell& currentAlive, glm::vec3 neighPos) {
if (currentAlive == candidateCell)
return;

auto key = std::to_string(neighPos.x) + std::to_string(neighPos.y) + std::to_string(neighPos.z);
if (auto it = mAliveCells.find(key); it != mAliveCells.end()) {
if (auto it = mAliveCells.find(key::createFromPosition(neighPos)); it != mAliveCells.end()) {
currentAlive.incAliveNeighbors();
found = true;
}
Expand Down

0 comments on commit f4e79f9

Please sign in to comment.