Browse Source

move item and tile to struct

fix/style
Martin Dørum 3 years ago
parent
commit
2348fe9f64

+ 7
- 8
libswan/include/swan/Item.h View File

@@ -6,7 +6,6 @@

namespace Swan {

// TODO: Switch to struct
class Item {
public:
struct Builder {
@@ -16,12 +15,12 @@ public:
};

Item(const ResourceManager &resources, const Builder &builder):
name_(builder.name), image_(resources.getImage(builder.image)),
maxStack_(builder.maxStack) {}
name(builder.name), image(resources.getImage(builder.image)),
maxStack(builder.maxStack) {}

const std::string name_;
const ImageResource &image_;
const int maxStack_;
const std::string name;
const ImageResource ℑ
const int maxStack;

static std::unique_ptr<Item> createInvalid(Context &ctx);

@@ -32,8 +31,8 @@ public:
// a complete ImageResource for a headless server, but for now, this will suffice.
protected:
Item(const Builder &builder):
name_(builder.name), image_(*(ImageResource *)this),
maxStack_(builder.maxStack) {}
name(builder.name), image(*(ImageResource *)this),
maxStack(builder.maxStack) {}
};

}

+ 10
- 11
libswan/include/swan/Tile.h View File

@@ -10,8 +10,7 @@

namespace Swan {

// TODO: Switch to struct
class Tile {
struct Tile {
public:
using ID = uint16_t;

@@ -24,15 +23,15 @@ public:
};

Tile(const ResourceManager &resources, const Builder &builder):
name_(builder.name), image_(resources.getImage(builder.image)),
isSolid_(builder.isSolid), lightLevel_(builder.lightLevel),
droppedItem_(builder.droppedItem) {}
const std::string name_;
const ImageResource &image_;
const bool isSolid_;
const float lightLevel_;
const std::optional<std::string> droppedItem_;
name(builder.name), image(resources.getImage(builder.image)),
isSolid(builder.isSolid), lightLevel(builder.lightLevel),
droppedItem(builder.droppedItem) {}
const std::string name;
const ImageResource &image;
const bool isSolid;
const float lightLevel;
const std::optional<std::string> droppedItem;

static std::unique_ptr<Tile> createInvalid(const ResourceManager &ctx);
static std::unique_ptr<Tile> createAir(const ResourceManager &ctx);

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

@@ -137,7 +137,7 @@ void Chunk::render(const Context &ctx, SDL_Renderer *rnd) {
}

SDL_Rect dest{x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
SDL_RenderCopy(rnd, tile->image_.texture_.get(), nullptr, &dest);
SDL_RenderCopy(rnd, tile->image.texture_.get(), nullptr, &dest);
}
}


+ 3
- 3
libswan/src/ItemStack.cc View File

@@ -21,9 +21,9 @@ ItemStack ItemStack::insert(ItemStack st) {

// Merge
count_ += st.count_;
if (count_ > item_->maxStack_) {
st.count_ = count_ - item_->maxStack_;
count_ = item_->maxStack_;
if (count_ > item_->maxStack) {
st.count_ = count_ - item_->maxStack;
count_ = item_->maxStack;
} else {
st.count_ = 0;
st.item_ = nullptr;

+ 3
- 3
libswan/src/World.cc View File

@@ -20,7 +20,7 @@ World::World(Game *game, unsigned long rand_seed):
game_(game), random_(rand_seed), resources_(game->win_) {

std::unique_ptr<Tile> invalidTile = Tile::createInvalid(resources_);
tilesMap_[invalidTile->name_] = 0;
tilesMap_[invalidTile->name] = 0;

// tiles_ is empty, so pushing back now will ensure invalid_tile
// ends up at location 0
@@ -55,12 +55,12 @@ void World::addMod(ModWrapper &&mod) {

for (auto t: mod.buildTiles(resources_)) {
Tile::ID id = tiles_.size();
tilesMap_[t->name_] = id;
tilesMap_[t->name] = id;
tiles_.push_back(std::move(t));
}

for (auto i: mod.buildItems(resources_)) {
items_[i->name_] = std::move(i);
items_[i->name] = std::move(i);
}

for (auto fact: mod.getWorldGens()) {

+ 11
- 11
libswan/src/WorldPlane.cc View File

@@ -95,11 +95,11 @@ Chunk &WorldPlane::slowGetChunk(ChunkPos pos) {
for (int x = 0; x < CHUNK_WIDTH; ++x) {
Tile::ID id = chunk.getTileID({ x, y });
Tile &tile = world_->getTileByID(id);
if (tile.isSolid_) {
if (tile.isSolid) {
lc.blocks[y * CHUNK_HEIGHT + x] = true;
}
if (tile.lightLevel_ > 0) {
lc.light_sources[{ x, y }] = tile.lightLevel_;
if (tile.lightLevel > 0) {
lc.lightSources[{ x, y }] = tile.lightLevel;
}
}
}
@@ -124,22 +124,22 @@ void WorldPlane::setTileID(TilePos pos, Tile::ID id) {
if (id != old) {
Tile &newTile = world_->getTileByID(id);
Tile &oldTile = world_->getTileByID(old);
chunk.setTileID(rp, id, newTile.image_.texture_.get());
chunk.setTileID(rp, id, newTile.image.texture_.get());
chunk.markModified();

if (!oldTile.isSolid_ && newTile.isSolid_) {
if (!oldTile.isSolid && newTile.isSolid) {
lighting_->onSolidBlockAdded(pos);
} else if (oldTile.isSolid_ && !newTile.isSolid_) {
} else if (oldTile.isSolid && !newTile.isSolid) {
lighting_->onSolidBlockRemoved(pos);
}

if (newTile.lightLevel_ != oldTile.lightLevel_) {
if (oldTile.lightLevel_ > 0) {
removeLight(pos, oldTile.lightLevel_);
if (newTile.lightLevel != oldTile.lightLevel) {
if (oldTile.lightLevel > 0) {
removeLight(pos, oldTile.lightLevel);
}

if (newTile.lightLevel_ > 0) {
addLight(pos, newTile.lightLevel_);
if (newTile.lightLevel > 0) {
addLight(pos, newTile.lightLevel);
}
}
}

+ 4
- 4
libswan/src/traits/PhysicsTrait.cc View File

@@ -15,7 +15,7 @@ static void collideX(
for (int y = (int)floor(body.top() + epsilon); y <= (int)floor(body.bottom() - epsilon); ++y) {
int lx = (int)floor(body.left() + epsilon);
Tile &left = plane.getTile({ lx, y });
if (left.isSolid_) {
if (left.isSolid) {
body.pos.x = (float)lx + 1.0;
collided = true;
break;
@@ -23,7 +23,7 @@ static void collideX(

int rx = (int)floor(body.right() - epsilon);
Tile &right = plane.getTile({ rx, y });
if (right.isSolid_) {
if (right.isSolid) {
body.pos.x = (float)rx - body.size.x;
collided = true;
break;
@@ -46,7 +46,7 @@ static void collideY(
for (int x = (int)floor(body.left() + epsilon); x <= (int)floor(body.right() - epsilon); ++x) {
int ty = (int)floor(body.top() + epsilon);
Tile &top = plane.getTile({ x, ty });
if (top.isSolid_) {
if (top.isSolid) {
body.pos.y = (float)ty + 1.0;
collided = true;
break;
@@ -54,7 +54,7 @@ static void collideY(

int by = (int)floor(body.bottom() - epsilon);
Tile &bottom = plane.getTile({ x, by });
if (bottom.isSolid_) {
if (bottom.isSolid) {
body.pos.y = (float)by - body.size.y;
collided = true;
phys.onGround = true;

Loading…
Cancel
Save