Browse Source

ints -> TilePos and ChunkPos

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
33493a2083

+ 2
- 2
core.mod/src/WGDefault.cc View File

@@ -1,9 +1,9 @@
#include "WGDefault.h"

void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk, int x, int y) {
void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
if (y == 0 && cy == 3)
if (chunk.pos_.y_ == 0 && cy == 3)
chunk.tiles_[cx][cy] = tGrass_;
else
chunk.tiles_[cx][cy] = tAir_;

+ 1
- 1
core.mod/src/WGDefault.h View File

@@ -14,6 +14,6 @@ public:
WGDefault(Swan::TileMap &tmap):
tGrass_(tmap.getID("core::grass")), tAir_(tmap.getID("core::air")) {}

void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk, int x, int y) override;
void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) override;
Swan::Entity &spawnPlayer(Swan::WorldPlane &plane) override;
};

+ 1
- 2
libswan/include/swan/Chunk.h View File

@@ -11,8 +11,7 @@ namespace Swan {

class Chunk {
public:
using ChunkPos = Vector2<int>;
using RelPos = Vector2<int>;
using RelPos = TilePos;

Chunk(ChunkPos pos): pos_(pos) {
texture_.create(CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE);

+ 1
- 1
libswan/include/swan/WorldGen.h View File

@@ -21,7 +21,7 @@ public:

virtual ~WorldGen() = default;

virtual void genChunk(WorldPlane &plane, Chunk &chunk, int x, int y) = 0;
virtual void genChunk(WorldPlane &plane, Chunk &chunk) = 0;
virtual Entity &spawnPlayer(WorldPlane &plane) = 0;
};


+ 3
- 3
libswan/include/swan/WorldPlane.h View File

@@ -24,9 +24,9 @@ public:

Entity &spawnEntity(const std::string &name, const Vec2 &pos);

Chunk &getChunk(int x, int y);
void setTileID(int x, int y, Tile::ID id);
Tile &getTile(int x, int y);
Chunk &getChunk(ChunkPos pos);
void setTileID(TilePos pos, Tile::ID id);
Tile &getTile(TilePos pos);

Entity &spawnPlayer();


+ 3
- 0
libswan/include/swan/common.h View File

@@ -12,6 +12,9 @@ static constexpr int TICK_RATE = 20;
static constexpr int CHUNK_HEIGHT = 32;
static constexpr int CHUNK_WIDTH = 32;

using TilePos = Vector2<int>;
using ChunkPos = Vector2<int>;

struct Win {
public:
sf::RenderWindow *window_;

+ 1
- 1
libswan/src/Body.cc View File

@@ -16,7 +16,7 @@ void Body::collide(WorldPlane &plane) {

int y = (int)(pos_.y_ + size_.y_);
for (int x = startx; x <= endx; ++x) {
Tile &tile = plane.getTile(x, y);
Tile &tile = plane.getTile(TilePos(x, y));
if (tile.is_solid_) {
pos_.y_ = y - size_.y_;
vel_.y_ = 0;

+ 15
- 16
libswan/src/WorldPlane.cc View File

@@ -4,18 +4,18 @@

namespace Swan {

static Chunk::ChunkPos chunkPos(int x, int y) {
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 ChunkPos chunkPos(TilePos pos) {
int chx = pos.x_ / CHUNK_WIDTH;
if (pos.x_ < 0 && pos.x_ % CHUNK_WIDTH != 0) chx -= 1;
int chy = pos.y_ / CHUNK_HEIGHT;
if (pos.y_ < 0 && pos.y_ % CHUNK_HEIGHT != 0) chy -= 1;
return ChunkPos(chx, chy);
}

static Chunk::RelPos relPos(int x, int y) {
int rx = x % CHUNK_WIDTH;
static Chunk::RelPos relPos(TilePos pos) {
int rx = pos.x_ % CHUNK_WIDTH;
if (rx < 0) rx += CHUNK_WIDTH;
int ry = y % CHUNK_HEIGHT;
int ry = pos.y_ % CHUNK_HEIGHT;
if (ry < 0) ry += CHUNK_HEIGHT;
return Chunk::RelPos(rx, ry);
}
@@ -33,13 +33,12 @@ Entity &WorldPlane::spawnEntity(const std::string &name, const Vec2 &pos) {
return *ent;
}

Chunk &WorldPlane::getChunk(int x, int y) {
Chunk::ChunkPos pos = chunkPos(x, y);
Chunk &WorldPlane::getChunk(ChunkPos pos) {
auto iter = chunks_.find(pos);

if (iter == chunks_.end()) {
iter = chunks_.emplace(pos, new Chunk(pos)).first;
gen_->genChunk(*this, *iter->second, pos.x_, pos.y_);
gen_->genChunk(*this, *iter->second);
iter->second->redraw(world_->tile_map_);
fprintf(stderr, "Generated chunk %i,%i\n", pos.x_, pos.y_);
}
@@ -47,12 +46,12 @@ Chunk &WorldPlane::getChunk(int x, int y) {
return *iter->second;
}

void WorldPlane::setTileID(int x, int y, Tile::ID id) {
getChunk(x, y).setTileID(world_->tile_map_, relPos(x, y), id);
void WorldPlane::setTileID(TilePos pos, Tile::ID id) {
getChunk(chunkPos(pos)).setTileID(world_->tile_map_, relPos(pos), id);
}

Tile &WorldPlane::getTile(int x, int y) {
return getChunk(x, y).getTile(world_->tile_map_, relPos(x, y));
Tile &WorldPlane::getTile(TilePos pos) {
return getChunk(chunkPos(pos)).getTile(world_->tile_map_, relPos(pos));
}

Entity &WorldPlane::spawnPlayer() {

Loading…
Cancel
Save