Browse Source

chunk stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
aac335db3b
2 changed files with 17 additions and 8 deletions
  1. 5
    0
      libswan/include/swan/Vector2.h
  2. 12
    8
      libswan/src/WorldPlane.cc

+ 5
- 0
libswan/include/swan/Vector2.h View File

@@ -1,6 +1,7 @@
#pragma once

#include <SFML/System/Vector2.hpp>
#include <utility>

namespace Swan {

@@ -16,6 +17,10 @@ public:
return sf::Vector2<T>(x_, y_);
}

operator std::pair<T, T>() const {
return std::pair<T, T>(x_, y_);
}

Vector2<T> operator-() const {
return Vector2<T>(-x_, -y_);
}

+ 12
- 8
libswan/src/WorldPlane.cc View File

@@ -5,23 +5,27 @@
namespace Swan {

static Chunk::ChunkPos chunkPos(int x, int y) {
return Chunk::ChunkPos(
(x >= 0 ? x : x - CHUNK_WIDTH) / CHUNK_WIDTH,
(y >= 0 ? y : y - CHUNK_HEIGHT) / CHUNK_HEIGHT);
int chx = x / CHUNK_WIDTH;
if (x < 0 && x % CHUNK_WIDTH != 0) chx -= 1;
int chy = y / CHUNK_HEIGHT;
if (y < 0 && y % CHUNK_HEIGHT != 0) chy -= 1;
return Chunk::ChunkPos(chx, chy);
}

static Chunk::RelPos relPos(int x, int y) {
return Chunk::ChunkPos(
(x >= 0 ? x : x + CHUNK_WIDTH) % CHUNK_WIDTH,
(y >= 0 ? y : y + CHUNK_HEIGHT) % CHUNK_HEIGHT);
int rx = x % CHUNK_WIDTH;
if (rx < 0) rx += CHUNK_WIDTH;
int ry = y % CHUNK_HEIGHT;
if (ry < 0) ry += CHUNK_HEIGHT;
return Chunk::RelPos(rx, ry);
}

Chunk &WorldPlane::getChunk(int x, int y) {
Chunk::ChunkPos pos = chunkPos(x, y);
auto it = chunks_.find(std::pair<int, int>(pos.x_, pos.y_));
auto it = chunks_.find(pos);

if (it == chunks_.end()) {
it = chunks_.emplace(std::pair<int, int>(pos.x_, pos.y_), Chunk(pos)).first;
it = chunks_.emplace(pos, Chunk(pos)).first;
gen_->genChunk(it->second, pos.x_, pos.y_);
it->second.redraw(world_->tile_map_);
}

Loading…
Cancel
Save