Browse Source

more changes

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

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

@@ -9,8 +9,8 @@ void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk, int x, int
chunk.tiles_[cx][cy] = tAir_;
}
}
}

if (plane.id_ == 0 && x == 0 && y == 0) {
plane.spawnEntity("core::player", Swan::Vec2(0, 0));
}
Swan::Entity &WGDefault::spawnPlayer(Swan::WorldPlane &plane) {
return plane.spawnEntity("core::player", Swan::Vec2(0, 0));
}

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

@@ -14,5 +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);
void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk, int x, int y) override;
Swan::Entity &spawnPlayer(Swan::WorldPlane &plane) override;
};

+ 2
- 0
core.mod/src/entities/EntPlayer.h View File

@@ -14,6 +14,8 @@ public:
EntPlayer(Swan::Vec2 pos):
body_(pos, SIZE, MASS) {}

const Swan::Vec2 &getPos() override { return body_.pos_; }

void draw(Swan::Win &win) override;
void update(Swan::WorldPlane &plane, float dt) override;


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

@@ -19,6 +19,8 @@ public:

virtual ~Entity() = default;

virtual const Vec2 &getPos() { return Vec2::ZERO; }

virtual void draw(Win &win) {}
virtual void update(WorldPlane &plane, float dt) {}
virtual void tick() {}

+ 4
- 4
libswan/include/swan/Tile.h View File

@@ -14,15 +14,15 @@ public:

Tile *solid(bool b) { is_solid_ = b; return this; }

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_;

static sf::Image INVALID_IMAGE;
static Tile INVALID_TILE;
static void initInvalid();
};

}

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

@@ -14,7 +14,7 @@ public:

Tile &get(Tile::ID id) {
if (id >= tiles_.size())
return Tile::invalid_tile;
return Tile::INVALID_TILE;
return *tiles_[id];
}


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

@@ -78,6 +78,8 @@ public:

T x_;
T y_;

static constexpr Vector2<T> ZERO = Vector2<T>(0, 0);
};

using Vec2 = Vector2<float>;

+ 15
- 34
libswan/include/swan/World.h View File

@@ -14,47 +14,28 @@ namespace Swan {

class World {
public:
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];
}

Tile::ID getTileID(const std::string &name) {
return tile_map_.getID(name);
}

void registerTile(std::shared_ptr<Tile> t) {
tile_map_.registerTile(t);
}

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

void registerEntity(std::shared_ptr<Entity::Factory> ent) {
ents_[ent->name_] = ent;
}
WorldPlane &addPlane(std::string gen);
WorldPlane &addPlane() { return addPlane(default_world_gen_); }
void setCurrentPlane(WorldPlane &plane);
void setWorldGen(const std::string &gen);
void spawnPlayer();
void registerTile(std::shared_ptr<Tile> t);
void registerWorldGen(std::shared_ptr<WorldGen::Factory> gen);
void registerEntity(std::shared_ptr<Entity::Factory> ent);

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_;
TileMap tile_map_;

private:
Entity *player_;
WorldPlane::ID current_plane_;
std::vector<WorldPlane> planes_;
std::string default_world_gen_;
};

}

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

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

#include "Chunk.h"
#include "TileMap.h"
#include "Entity.h"

namespace Swan {

@@ -21,6 +22,7 @@ public:
virtual ~WorldGen() = default;

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

}

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

@@ -20,9 +20,7 @@ public:
using ID = uint16_t;

WorldPlane(ID id, World *world, std::shared_ptr<WorldGen> gen):
id_(id), world_(world), gen_(gen) {
getChunk(0, 0); // Create the initial chunk
}
id_(id), world_(world), gen_(gen) {}

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

@@ -30,6 +28,8 @@ public:
void setTileID(int x, int y, Tile::ID id);
Tile &getTile(int x, int y);

Entity &spawnPlayer();

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

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

@@ -36,7 +36,9 @@ void Game::createWorld(std::string worldgen) {
world_->registerEntity(entity);
}

world_->default_worldgen_ = worldgen;
world_->setWorldGen(worldgen);
world_->setCurrentPlane(world_->addPlane());
world_->spawnPlayer();
}

void Game::draw(Win &win) {

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

@@ -18,7 +18,7 @@ void Mod::registerTile(const std::string &name, Tile *tile) {
std::string asset_path = path_ + "/" + tile->path_;
if (!tile->image_.loadFromFile(asset_path)) {
fprintf(stderr, "Tile %s: Failed to load image %s\n", tile->name_.c_str(), asset_path.c_str());
tile->image_ = Tile::invalid_image;
tile->image_ = Tile::INVALID_IMAGE;
}

tiles_.push_back(std::shared_ptr<Tile>(tile));

+ 6
- 6
libswan/src/Tile.cc View File

@@ -3,14 +3,14 @@

namespace Swan {

sf::Image Tile::invalid_image;
Tile Tile::invalid_tile("");
sf::Image Tile::INVALID_IMAGE;
Tile Tile::INVALID_TILE("");

void Tile::initInvalid() {
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;
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;
}

}

+ 32
- 2
libswan/src/World.cc View File

@@ -2,7 +2,31 @@

namespace Swan {

WorldPlane::ID World::addPlane(std::string gen) {
void World::setCurrentPlane(WorldPlane &plane) {
current_plane_ = plane.id_;
}

void World::setWorldGen(const std::string &gen) {
default_world_gen_ = gen;
}

void World::spawnPlayer() {
player_ = &planes_[current_plane_].spawnPlayer();
}

void World::registerTile(std::shared_ptr<Tile> t) {
tile_map_.registerTile(t);
}

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

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

WorldPlane &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",
@@ -12,10 +36,16 @@ WorldPlane::ID World::addPlane(std::string gen) {

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

void World::draw(Win &win) {
auto size = win.window_->getSize();
const Vec2 &pos = player_->getPos();
win.cam_ = Vec2(
pos.x_ * TILE_SIZE - size.x / 2,
pos.y_ * TILE_SIZE - size.y / 2);

planes_[current_plane_].draw(win);
}


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

@@ -55,6 +55,10 @@ Tile &WorldPlane::getTile(int x, int y) {
return getChunk(x, y).getTile(world_->tile_map_, relPos(x, y));
}

Entity &WorldPlane::spawnPlayer() {
return gen_->spawnPlayer(*this);
}

void WorldPlane::draw(Win &win) {
for (auto &ch: chunks_)
ch.second->draw(win);

+ 0
- 1
src/main.cc View File

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

game.createWorld("core::default");
game.world_->setCurrentPlane(game.world_->addPlane());

double prevtime = getTime();
double fpsAcc = 0;

Loading…
Cancel
Save