Browse Source

entities, and changes to worldgen

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

+ 1
- 1
core.mod/src/mod.cc View File

@@ -30,7 +30,7 @@ extern "C" void mod_init(Swan::Mod &mod) {
mod.registerTile("dirt", (new Swan::Tile("assets/tiles/dirt.png")));
mod.registerTile("grass", (new Swan::Tile("assets/tiles/grass.png")));

mod.registerWorldGen("Default", new DefaultWorldGen::Factory());
mod.registerWorldGen("default", new DefaultWorldGen::Factory());
}

int main() {

+ 22
- 0
libswan/include/swan/Entity.h View File

@@ -0,0 +1,22 @@
#pragma once

#include <memory>

#include "common.h"

namespace Swan {

class Entity {
public:
class Factory {
public:
std::string name_;
virtual Entity *create(const Vec2 &pos) = 0;
};

virtual void draw(Win &win) {}
virtual void update(float dt) {}
virtual void tick() {}
};

}

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

@@ -17,7 +17,7 @@ public:
std::unique_ptr<World> world_ = NULL;

void loadMod(const std::string &path);
void createWorld();
void createWorld(std::string worldgen);

void draw(Win &win);
void update(float dt);

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

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

#include "Tile.h"
#include "WorldGen.h"
#include "Entity.h"

namespace Swan {

@@ -18,11 +19,13 @@ public:
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);
};

}

+ 11
- 6
libswan/include/swan/World.h View File

@@ -8,8 +8,8 @@
#include "Player.h"
#include "Tile.h"
#include "WorldPlane.h"
#include "WorldPlane.h"
#include "Tile.h"
#include "WorldGen.h"
#include "Entity.h"

namespace Swan {

@@ -19,12 +19,13 @@ public:

WorldPlane::ID current_plane_;
std::vector<WorldPlane> planes_;
std::string default_worldgen_;

WorldGen::ID default_worldgen_;
std::vector<std::shared_ptr<WorldGen::Factory>> worldgens_;
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(WorldGen::ID gen);
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]; }
@@ -38,7 +39,11 @@ public:
}

void registerWorldGen(std::shared_ptr<WorldGen::Factory> gen) {
worldgens_.push_back(gen);
worldgens_[gen->name_] = gen;
}

void registerEntity(std::shared_ptr<Entity::Factory> ent) {
ents_[ent->name_] = ent;
}

void draw(Win &win);

+ 0
- 2
libswan/include/swan/WorldGen.h View File

@@ -9,8 +9,6 @@ namespace Swan {

class WorldGen {
public:
using ID = int;

class Factory {
public:
std::string name_;

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

@@ -9,6 +9,7 @@
#include "Tile.h"
#include "TileMap.h"
#include "WorldGen.h"
#include "Entity.h"

namespace Swan {

@@ -18,10 +19,11 @@ class WorldPlane {
public:
using ID = uint16_t;

std::map<std::pair<int, int>, Chunk> chunks_;
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) {}

+ 5
- 1
libswan/src/Game.cc View File

@@ -25,14 +25,18 @@ void Game::loadMod(const std::string &path) {
mod_init(mod);
}

void Game::createWorld() {
void Game::createWorld(std::string worldgen) {
world_.reset(new World());
for (auto &mod: registered_mods_) {
for (auto &tile: mod.tiles_)
world_->registerTile(tile);
for (auto &worldgen: mod.worldgens_)
world_->registerWorldGen(worldgen);
for (auto &entity: mod.entities_)
world_->registerEntity(entity);
}

world_->default_worldgen_ = worldgen;
}

void Game::draw(Win &win) {

+ 6
- 1
libswan/src/Mod.cc View File

@@ -25,8 +25,13 @@ void Mod::registerTile(const std::string &name, Tile *tile) {
}

void Mod::registerWorldGen(const std::string &name, WorldGen::Factory *gen) {
gen->name_ = name;
gen->name_ = name_ + "::" + name;
worldgens_.push_back(std::shared_ptr<WorldGen::Factory>(gen));
}

void Mod::registerEntity(const std::string &name, Entity::Factory *ent) {
ent->name_ = name_ + "::" + name;
entities_.push_back(std::shared_ptr<Entity::Factory>(ent));
}

}

+ 7
- 1
libswan/src/World.cc View File

@@ -2,8 +2,14 @@

namespace Swan {

WorldPlane::ID World::addPlane(WorldGen::ID gen) {
WorldPlane::ID World::addPlane(std::string gen) {
WorldPlane::ID id = planes_.size();
if (worldgens_.find(gen) == worldgens_.end()) {
fprintf(stderr, "Tried to add plane with non-existant world gen '%s'!\n",
gen.c_str());
abort();
}

WorldGen *g = worldgens_[gen]->create(tile_map_);
planes_.push_back(WorldPlane(id, this, std::shared_ptr<WorldGen>(g)));
return id;

+ 6
- 0
libswan/src/WorldPlane.cc View File

@@ -44,13 +44,19 @@ Tile &WorldPlane::getTile(int x, int y) {
void WorldPlane::draw(Win &win) {
for (auto &p: chunks_) {
p.second.draw(win);
for (auto &ent: entities_)
ent->draw(win);
}
}

void WorldPlane::update(float dt) {
for (auto &ent: entities_)
ent->update(dt);
}

void WorldPlane::tick() {
for (auto &ent: entities_)
ent->tick();
}

}

+ 1
- 1
src/main.cc View File

@@ -25,7 +25,7 @@ int main() {
Game game;
game.loadMod("core.mod");

game.createWorld();
game.createWorld("core::default");
game.world_->setCurrentPlane(game.world_->addPlane());
game.world_->player_ = new Player(Vec2(1, 1));


Loading…
Cancel
Save