@@ -8,12 +8,12 @@ class WGDefault: public Swan::WorldGen { | |||
public: | |||
class Factory: public Swan::WorldGen::Factory { | |||
public: | |||
WorldGen *create(Swan::TileMap &tmap) { return new WGDefault(tmap); } | |||
WorldGen *create(Swan::World &world) { return new WGDefault(world); } | |||
}; | |||
WGDefault(Swan::TileMap &tmap): | |||
tGrass_(tmap.getID("core::grass")), tDirt_(tmap.getID("core::dirt")), | |||
tStone_(tmap.getID("core::stone")), tAir_(tmap.getID("core::air")) {} | |||
WGDefault(Swan::World &world): | |||
tGrass_(world.getTileID("core::grass")), tDirt_(world.getTileID("core::dirt")), | |||
tStone_(world.getTileID("core::stone")), tAir_(world.getTileID("core::air")) {} | |||
void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) override; | |||
Swan::Entity &spawnPlayer(Swan::WorldPlane &plane) override; |
@@ -7,9 +7,9 @@ extern "C" void mod_init(Swan::Mod &mod) { | |||
mod.init("core"); | |||
mod.registerTile("air", (new Swan::Tile("assets/tiles/air.png"))->solid(false)); | |||
mod.registerTile("stone", (new Swan::Tile("assets/tiles/stone.png"))); | |||
mod.registerTile("dirt", (new Swan::Tile("assets/tiles/dirt.png"))); | |||
mod.registerTile("grass", (new Swan::Tile("assets/tiles/grass.png"))); | |||
mod.registerTile("stone", (new Swan::Tile("assets/tiles/stone.png"))->drops("core::stone")); | |||
mod.registerTile("dirt", (new Swan::Tile("assets/tiles/dirt.png"))->drops("core::dirt")); | |||
mod.registerTile("grass", (new Swan::Tile("assets/tiles/grass.png"))->drops("core::dirt")); | |||
mod.registerWorldGen("default", new WGDefault::Factory()); | |||
@@ -14,11 +14,11 @@ public: | |||
}; | |||
Animation() = default; | |||
Animation(int w, int h, double interval, const Asset &asset, int flags = 0) { | |||
Animation(int w, int h, double interval, const Asset *asset, int flags = 0) { | |||
init(w, h, interval, asset, flags); | |||
} | |||
void init(int w, int h, double interval, const Asset &asset, int flags = 0); | |||
void init(int w, int h, double interval, const Asset *asset, int flags = 0); | |||
void tick(double dt); | |||
void draw(Win &win); |
@@ -5,10 +5,11 @@ | |||
#include "common.h" | |||
#include "Tile.h" | |||
#include "TileMap.h" | |||
namespace Swan { | |||
class World; | |||
class Chunk { | |||
public: | |||
using RelPos = TilePos; | |||
@@ -18,17 +19,17 @@ public: | |||
sprite_ = sf::Sprite(texture_); | |||
} | |||
void setTileID(TileMap &tmap, RelPos pos, Tile::ID id); | |||
Tile &getTile(TileMap &tmap, RelPos pos); | |||
void drawBlock(RelPos pos, const Tile &t); | |||
void drawBlock(TileMap &tmap, RelPos pos, Tile::ID id); | |||
void redraw(TileMap &tmap); | |||
void setTileID(World &world, RelPos pos, Tile::ID id); | |||
Tile *getTile(World &world, RelPos pos); | |||
void redraw(World &world); | |||
void draw(Win &win); | |||
ChunkPos pos_; | |||
Tile::ID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT]; | |||
private: | |||
void drawBlock(RelPos pos, const Tile *t); | |||
void drawBlock(World &world, RelPos pos, Tile::ID id); | |||
bool dirty_ = false; | |||
sf::Texture texture_; | |||
sf::Sprite sprite_; |
@@ -0,0 +1,7 @@ | |||
namespace Swan { | |||
class Item { | |||
}; | |||
} |
@@ -13,8 +13,10 @@ public: | |||
Tile(std::string path): path_(path) {} | |||
Tile *solid(bool b) { is_solid_ = b; return this; } | |||
Tile *drops(std::string item) { dropped_item_ = item; return this; } | |||
bool is_solid_ = true; | |||
std::string dropped_item_ = ""; | |||
std::string path_; | |||
std::string name_; | |||
@@ -22,6 +24,7 @@ public: | |||
static sf::Image INVALID_IMAGE; | |||
static Tile INVALID_TILE; | |||
static ID INVALID_ID; | |||
static void initInvalid(); | |||
}; | |||
@@ -1,32 +0,0 @@ | |||
#pragma once | |||
#include <memory> | |||
#include "Tile.h" | |||
namespace Swan { | |||
class TileMap { | |||
public: | |||
Tile::ID getID(const std::string &name) { | |||
return id_map_[name]; | |||
} | |||
Tile &get(Tile::ID id) { | |||
if (id >= tiles_.size()) | |||
return Tile::INVALID_TILE; | |||
return *tiles_[id]; | |||
} | |||
void registerTile(std::shared_ptr<Tile> t) { | |||
Tile::ID id = tiles_.size(); | |||
tiles_.push_back(t); | |||
id_map_[t->name_] = id; | |||
} | |||
private: | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
std::map<std::string, Tile::ID> id_map_; | |||
}; | |||
} |
@@ -5,11 +5,12 @@ | |||
#include <string> | |||
#include "common.h" | |||
#include "Asset.h" | |||
#include "Item.h" | |||
#include "Tile.h" | |||
#include "WorldPlane.h" | |||
#include "WorldGen.h" | |||
#include "Entity.h" | |||
#include "Asset.h" | |||
namespace Swan { | |||
@@ -25,7 +26,11 @@ public: | |||
void registerEntity(std::shared_ptr<Entity::Factory> ent); | |||
void registerAsset(std::shared_ptr<Asset> asset); | |||
Asset &getAsset(const std::string &name); | |||
Asset *getAsset(const std::string &name); | |||
Item *getItem(const std::string &name); | |||
Tile::ID getTileID(const std::string &name); | |||
Tile *getTileByID(Tile::ID id); | |||
Tile *getTile(const std::string &name); | |||
void draw(Win &win); | |||
void update(float dt); | |||
@@ -34,7 +39,9 @@ public: | |||
std::map<std::string, std::shared_ptr<WorldGen::Factory>> worldgens_; | |||
std::map<std::string, std::shared_ptr<Entity::Factory>> ents_; | |||
std::map<std::string, std::shared_ptr<Asset>> assets_; | |||
TileMap tile_map_; | |||
std::map<std::string, std::shared_ptr<Item>> items_; | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
std::map<std::string, Tile::ID> tiles_map_; | |||
Entity *player_; | |||
private: |
@@ -3,11 +3,11 @@ | |||
#include <memory> | |||
#include "Chunk.h" | |||
#include "TileMap.h" | |||
#include "Entity.h" | |||
namespace Swan { | |||
class World; | |||
class WorldPlane; | |||
class WorldGen { | |||
@@ -15,7 +15,7 @@ public: | |||
class Factory { | |||
public: | |||
virtual ~Factory() = default; | |||
virtual WorldGen *create(TileMap &tmap) = 0; | |||
virtual WorldGen *create(World &tmap) = 0; | |||
std::string name_; | |||
}; | |||
@@ -7,7 +7,6 @@ | |||
#include "common.h" | |||
#include "Chunk.h" | |||
#include "Tile.h" | |||
#include "TileMap.h" | |||
#include "WorldGen.h" | |||
#include "Entity.h" | |||
@@ -8,7 +8,6 @@ | |||
#include <swan/Game.h> | |||
#include <swan/Mod.h> | |||
#include <swan/Tile.h> | |||
#include <swan/TileMap.h> | |||
#include <swan/Timer.h> | |||
#include <swan/Vector2.h> | |||
#include <swan/World.h> |
@@ -2,11 +2,11 @@ | |||
namespace Swan { | |||
void Animation::init(int w, int h, double interval, const Asset &asset, int flags) { | |||
void Animation::init(int w, int h, double interval, const Asset *asset, int flags) { | |||
width_ = w; | |||
height_ = h; | |||
interval_ = interval; | |||
asset_ = &asset; | |||
asset_ = asset; | |||
fcount_ = asset_->img_.getSize().y / height_; | |||
sprite_.setTexture(asset_->tex_); | |||
sprite_.setTextureRect(sf::IntRect(0, 0, width_, height_)); |
@@ -1,29 +1,31 @@ | |||
#include "Chunk.h" | |||
#include "World.h" | |||
namespace Swan { | |||
void Chunk::setTileID(TileMap &tmap, RelPos pos, Tile::ID id) { | |||
void Chunk::setTileID(World &world, RelPos pos, Tile::ID id) { | |||
tiles_[pos.x_][pos.y_] = id; | |||
drawBlock(tmap, pos, id); | |||
drawBlock(world, pos, id); | |||
} | |||
Tile &Chunk::getTile(TileMap &tmap, RelPos pos) { | |||
return tmap.get(tiles_[pos.x_][pos.y_]); | |||
Tile *Chunk::getTile(World &world, RelPos pos) { | |||
return world.getTileByID(tiles_[pos.x_][pos.y_]); | |||
} | |||
void Chunk::drawBlock(RelPos pos, const Tile &t) { | |||
texture_.update(t.image_, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE); | |||
void Chunk::drawBlock(RelPos pos, const Tile *t) { | |||
texture_.update(t->image_, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE); | |||
dirty_ = true; | |||
} | |||
void Chunk::drawBlock(TileMap &tmap, RelPos pos, Tile::ID id) { | |||
drawBlock(pos, tmap.get(id)); | |||
void Chunk::drawBlock(World &world, RelPos pos, Tile::ID id) { | |||
drawBlock(pos, world.getTileByID(id)); | |||
} | |||
void Chunk::redraw(TileMap &tmap) { | |||
void Chunk::redraw(World &world) { | |||
for (int x = 0; x < CHUNK_WIDTH; ++x) { | |||
for (int y = 0; y < CHUNK_HEIGHT; ++y) { | |||
drawBlock(tmap, ChunkPos(x, y), tiles_[x][y]); | |||
drawBlock(world, ChunkPos(x, y), tiles_[x][y]); | |||
} | |||
} | |||
} |
@@ -5,6 +5,7 @@ namespace Swan { | |||
sf::Image Tile::INVALID_IMAGE; | |||
Tile Tile::INVALID_TILE(""); | |||
Tile::ID Tile::INVALID_ID = 0xffff; | |||
void Tile::initInvalid() { | |||
INVALID_IMAGE.create(TILE_SIZE, TILE_SIZE, sf::Color(245, 66, 242)); |
@@ -42,7 +42,9 @@ void World::spawnPlayer() { | |||
} | |||
void World::registerTile(std::shared_ptr<Tile> t) { | |||
tile_map_.registerTile(t); | |||
Tile::ID id = tiles_.size(); | |||
tiles_.push_back(t); | |||
tiles_map_[t->name_] = id; | |||
} | |||
void World::registerWorldGen(std::shared_ptr<WorldGen::Factory> gen) { | |||
@@ -57,14 +59,45 @@ void World::registerAsset(std::shared_ptr<Asset> asset) { | |||
assets_[asset->name_] = asset; | |||
} | |||
Asset &World::getAsset(const std::string &name) { | |||
Asset *World::getAsset(const std::string &name) { | |||
auto iter = assets_.find(name); | |||
if (iter == assets_.end()) { | |||
fprintf(stderr, "Tried to get non-existant asset ''%s'!\n", name.c_str()); | |||
abort(); | |||
return NULL; | |||
} | |||
return *iter->second; | |||
return iter->second.get(); | |||
} | |||
Item *World::getItem(const std::string &name) { | |||
auto iter = items_.find(name); | |||
if (iter == items_.end()) { | |||
fprintf(stderr, "Tried to get non-existant item ''%s'!\n", name.c_str()); | |||
return NULL; | |||
} | |||
return iter->second.get(); | |||
} | |||
Tile::ID World::getTileID(const std::string &name) { | |||
auto iter = tiles_map_.find(name); | |||
if (iter == tiles_map_.end()) { | |||
fprintf(stderr, "Tried to get non-existant tile ''%s'!\n", name.c_str()); | |||
return Tile::INVALID_ID; | |||
} | |||
return iter->second; | |||
} | |||
Tile *World::getTileByID(Tile::ID id) { | |||
return tiles_[id].get(); | |||
} | |||
Tile *World::getTile(const std::string &name) { | |||
Tile::ID id = getTileID(name); | |||
if (id == Tile::INVALID_ID) | |||
return &Tile::INVALID_TILE; | |||
return getTileByID(id); | |||
} | |||
WorldPlane &World::addPlane(std::string gen) { | |||
@@ -75,7 +108,7 @@ WorldPlane &World::addPlane(std::string gen) { | |||
abort(); | |||
} | |||
WorldGen *g = worldgens_[gen]->create(tile_map_); | |||
WorldGen *g = worldgens_[gen]->create(*this); | |||
planes_.push_back(WorldPlane(id, this, std::shared_ptr<WorldGen>(g))); | |||
return planes_[id]; | |||
} |
@@ -44,18 +44,18 @@ Chunk &WorldPlane::getChunk(ChunkPos pos) { | |||
if (iter == chunks_.end()) { | |||
iter = chunks_.emplace(pos, new Chunk(pos)).first; | |||
gen_->genChunk(*this, *iter->second); | |||
iter->second->redraw(world_->tile_map_); | |||
iter->second->redraw(*world_); | |||
} | |||
return *iter->second; | |||
} | |||
void WorldPlane::setTileID(TilePos pos, Tile::ID id) { | |||
getChunk(chunkPos(pos)).setTileID(world_->tile_map_, relPos(pos), id); | |||
getChunk(chunkPos(pos)).setTileID(*world_, relPos(pos), id); | |||
} | |||
Tile &WorldPlane::getTile(TilePos pos) { | |||
return getChunk(chunkPos(pos)).getTile(world_->tile_map_, relPos(pos)); | |||
return *getChunk(chunkPos(pos)).getTile(*world_, relPos(pos)); | |||
} | |||
Entity &WorldPlane::spawnPlayer() { |