@@ -9,12 +9,6 @@ namespace Swan { | |||
class Body { | |||
public: | |||
Vec2 force_ = { 0, 0 }; | |||
Vec2 vel_ = { 0, 0 }; | |||
Vec2 pos_; | |||
Vec2 size_; | |||
float mass_; | |||
Body(Vec2 pos, Vec2 size, float mass): | |||
pos_(pos), size_(size), mass_(mass) {}; | |||
@@ -24,6 +18,12 @@ public: | |||
void outline(Win &win); | |||
void update(float dt); | |||
Vec2 force_ = { 0, 0 }; | |||
Vec2 vel_ = { 0, 0 }; | |||
Vec2 pos_; | |||
Vec2 size_; | |||
float mass_; | |||
}; | |||
} |
@@ -14,12 +14,6 @@ public: | |||
using ChunkPos = Vector2<int>; | |||
using RelPos = Vector2<int>; | |||
ChunkPos pos_; | |||
bool dirty_ = false; | |||
Tile::ID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT]; | |||
sf::Texture texture_; | |||
sf::Sprite sprite_; | |||
Chunk(ChunkPos pos): pos_(pos) { | |||
texture_.create(CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE); | |||
sprite_ = sf::Sprite(texture_); | |||
@@ -31,6 +25,14 @@ public: | |||
void drawBlock(TileMap &tmap, RelPos pos, Tile::ID id); | |||
void redraw(TileMap &tmap); | |||
void draw(Win &win); | |||
ChunkPos pos_; | |||
Tile::ID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT]; | |||
private: | |||
bool dirty_ = false; | |||
sf::Texture texture_; | |||
sf::Sprite sprite_; | |||
}; | |||
} |
@@ -3,17 +3,18 @@ | |||
#include <memory> | |||
#include "common.h" | |||
#include "WorldPlane.h" | |||
namespace Swan { | |||
class WorldPlane; | |||
class Entity { | |||
public: | |||
class Factory { | |||
public: | |||
std::string name_; | |||
virtual Entity *create(const Vec2 &pos) = 0; | |||
virtual ~Factory() = default; | |||
virtual Entity *create(const Vec2 &pos) = 0; | |||
std::string name_; | |||
}; | |||
virtual ~Entity() = default; |
@@ -12,10 +12,6 @@ namespace Swan { | |||
class Game { | |||
public: | |||
std::vector<Mod> registered_mods_; | |||
std::unique_ptr<World> world_ = NULL; | |||
void loadMod(const std::string &path); | |||
void createWorld(std::string worldgen); | |||
@@ -24,6 +20,11 @@ public: | |||
void tick(); | |||
static void initGlobal(); | |||
std::unique_ptr<World> world_ = NULL; | |||
private: | |||
std::vector<Mod> registered_mods_; | |||
}; | |||
} |
@@ -15,17 +15,17 @@ class Mod { | |||
public: | |||
using ModID = uint32_t; | |||
void init(const std::string &name); | |||
void registerTile(const std::string &name, Tile *tile); | |||
void registerWorldGen(const std::string &name, WorldGen::Factory *gen); | |||
void registerEntity(const std::string &name, Entity::Factory *ent); | |||
std::string name_; | |||
std::string path_; | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
std::vector<std::shared_ptr<WorldGen::Factory>> worldgens_; | |||
std::vector<std::shared_ptr<Entity::Factory>> entities_; | |||
bool inited_ = false; | |||
void init(const std::string &name); | |||
void registerTile(const std::string &name, Tile *tile); | |||
void registerWorldGen(const std::string &name, WorldGen::Factory *gen); | |||
void registerEntity(const std::string &name, Entity::Factory *ent); | |||
}; | |||
} |
@@ -10,12 +10,6 @@ class Tile { | |||
public: | |||
using ID = uint16_t; | |||
std::string path_; | |||
std::string name_; | |||
sf::Image image_; | |||
bool is_solid_ = true; | |||
Tile(std::string path): path_(path) {} | |||
Tile *solid(bool b) { is_solid_ = b; return this; } | |||
@@ -23,6 +17,12 @@ public: | |||
static sf::Image invalid_image; | |||
static Tile invalid_tile; | |||
static void initInvalid(); | |||
bool is_solid_ = true; | |||
std::string path_; | |||
std::string name_; | |||
sf::Image image_; | |||
}; | |||
} |
@@ -8,9 +8,6 @@ namespace Swan { | |||
class TileMap { | |||
public: | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
std::map<std::string, Tile::ID> id_map_; | |||
Tile::ID getID(const std::string &name) { | |||
return id_map_[name]; | |||
} | |||
@@ -26,6 +23,10 @@ public: | |||
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_; | |||
}; | |||
} |
@@ -8,9 +8,6 @@ namespace Swan { | |||
template<typename T> | |||
class Vector2 { | |||
public: | |||
T x_; | |||
T y_; | |||
constexpr Vector2(T x = 0, T y = 0): x_(x), y_(y) {} | |||
operator sf::Vector2<T>() const { | |||
@@ -78,6 +75,9 @@ public: | |||
y_ /= num; | |||
return *this; | |||
} | |||
T x_; | |||
T y_; | |||
}; | |||
using Vec2 = Vector2<float>; |
@@ -14,18 +14,19 @@ namespace Swan { | |||
class World { | |||
public: | |||
WorldPlane::ID current_plane_; | |||
std::vector<WorldPlane> planes_; | |||
std::string default_worldgen_; | |||
WorldPlane::ID addPlane(std::string gen); | |||
TileMap tile_map_; | |||
std::map<std::string, std::shared_ptr<WorldGen::Factory>> worldgens_; | |||
std::map<std::string, std::shared_ptr<Entity::Factory>> ents_; | |||
WorldPlane::ID addPlane() { | |||
return addPlane(default_worldgen_); | |||
} | |||
WorldPlane::ID addPlane(std::string gen); | |||
WorldPlane::ID addPlane() { return addPlane(default_worldgen_); } | |||
void setCurrentPlane(WorldPlane::ID id) { current_plane_ = id; } | |||
WorldPlane &getPlane(WorldPlane::ID id) { return planes_[id]; } | |||
void setCurrentPlane(WorldPlane::ID id) { | |||
current_plane_ = id; | |||
} | |||
WorldPlane &getPlane(WorldPlane::ID id) { | |||
return planes_[id]; | |||
} | |||
Tile::ID getTileID(const std::string &name) { | |||
return tile_map_.getID(name); | |||
@@ -46,6 +47,14 @@ public: | |||
void draw(Win &win); | |||
void update(float dt); | |||
void tick(); | |||
WorldPlane::ID current_plane_; | |||
std::vector<WorldPlane> planes_; | |||
std::string default_worldgen_; | |||
TileMap tile_map_; | |||
std::map<std::string, std::shared_ptr<WorldGen::Factory>> worldgens_; | |||
std::map<std::string, std::shared_ptr<Entity::Factory>> ents_; | |||
}; | |||
} |
@@ -4,17 +4,18 @@ | |||
#include "Chunk.h" | |||
#include "TileMap.h" | |||
#include "WorldPlane.h" | |||
namespace Swan { | |||
class WorldPlane; | |||
class WorldGen { | |||
public: | |||
class Factory { | |||
public: | |||
std::string name_; | |||
virtual WorldGen *create(TileMap &tmap) = 0; | |||
virtual ~Factory() = default; | |||
virtual WorldGen *create(TileMap &tmap) = 0; | |||
std::string name_; | |||
}; | |||
virtual ~WorldGen() = default; |
@@ -8,23 +8,17 @@ | |||
#include "Chunk.h" | |||
#include "Tile.h" | |||
#include "TileMap.h" | |||
#include "WorldGen.h" | |||
#include "Entity.h" | |||
namespace Swan { | |||
class World; | |||
class WorldGen; | |||
class Entity; | |||
class WorldPlane { | |||
public: | |||
using ID = uint16_t; | |||
ID id_; | |||
World *world_; | |||
std::shared_ptr<WorldGen> gen_; | |||
std::map<std::pair<int, int>, Chunk> chunks_; | |||
std::vector<std::shared_ptr<Entity>> entities_; | |||
WorldPlane(ID id, World *world, std::shared_ptr<WorldGen> gen): | |||
id_(id), world_(world), gen_(gen) { | |||
getChunk(0, 0); // Create the initial chunk | |||
@@ -39,6 +33,14 @@ public: | |||
void draw(Win &win); | |||
void update(float dt); | |||
void tick(); | |||
ID id_; | |||
World *world_; | |||
std::shared_ptr<WorldGen> gen_; | |||
private: | |||
std::map<std::pair<int, int>, std::unique_ptr<Chunk>> chunks_; | |||
std::vector<std::unique_ptr<Entity>> entities_; | |||
}; | |||
} |
@@ -7,7 +7,7 @@ | |||
namespace Swan { | |||
static constexpr float TILE_SIZE = 32; | |||
static constexpr int TILE_SIZE = 32; | |||
static constexpr int TICK_RATE = 20; | |||
static constexpr int CHUNK_HEIGHT = 32; | |||
static constexpr int CHUNK_WIDTH = 32; |
@@ -7,7 +7,7 @@ sf::Image Tile::invalid_image; | |||
Tile Tile::invalid_tile(""); | |||
void Tile::initInvalid() { | |||
invalid_image.create((int)TILE_SIZE, (int)TILE_SIZE, sf::Color(245, 66, 242)); | |||
invalid_image.create(TILE_SIZE, TILE_SIZE, sf::Color(245, 66, 242)); | |||
invalid_tile.name_ = "INVALID"; | |||
invalid_tile.image_ = invalid_image; | |||
invalid_tile.is_solid_ = false; |
@@ -28,23 +28,23 @@ Entity &WorldPlane::spawnEntity(const std::string &name, const Vec2 &pos) { | |||
} | |||
Entity *ent = world_->ents_[name]->create(pos); | |||
entities_.push_back(std::shared_ptr<Entity>(ent)); | |||
entities_.push_back(std::unique_ptr<Entity>(ent)); | |||
fprintf(stderr, "Spawned %s at %f,%f.\n", name.c_str(), pos.x_, pos.y_); | |||
return *ent; | |||
} | |||
Chunk &WorldPlane::getChunk(int x, int y) { | |||
Chunk::ChunkPos pos = chunkPos(x, y); | |||
auto it = chunks_.find(pos); | |||
auto iter = chunks_.find(pos); | |||
if (it == chunks_.end()) { | |||
it = chunks_.emplace(pos, Chunk(pos)).first; | |||
gen_->genChunk(*this, it->second, pos.x_, pos.y_); | |||
it->second.redraw(world_->tile_map_); | |||
if (iter == chunks_.end()) { | |||
iter = chunks_.emplace(pos, new Chunk(pos)).first; | |||
gen_->genChunk(*this, *iter->second, pos.x_, pos.y_); | |||
iter->second->redraw(world_->tile_map_); | |||
fprintf(stderr, "Generated chunk %i,%i\n", pos.x_, pos.y_); | |||
} | |||
return it->second; | |||
return *iter->second; | |||
} | |||
void WorldPlane::setTileID(int x, int y, Tile::ID id) { | |||
@@ -56,8 +56,8 @@ Tile &WorldPlane::getTile(int x, int y) { | |||
} | |||
void WorldPlane::draw(Win &win) { | |||
for (auto &p: chunks_) | |||
p.second.draw(win); | |||
for (auto &ch: chunks_) | |||
ch.second->draw(win); | |||
for (auto &ent: entities_) | |||
ent->draw(win); | |||
} |