@@ -24,6 +24,16 @@ extern "C" void mod_init(Swan::Mod &mod) { | |||
.dropped_item = "grass", | |||
}); | |||
mod.registerItem("stone", new Swan::Item{ | |||
.image = mod.loadImage("assets/tiles/stone.png"), | |||
}); | |||
mod.registerItem("dirt", new Swan::Item{ | |||
.image = mod.loadImage("assets/tiles/stone.png"), | |||
}); | |||
mod.registerItem("grass", new Swan::Item{ | |||
.image = mod.loadImage("assets/tiles/grass.png"), | |||
}); | |||
mod.registerWorldGen("default", new WGDefault::Factory()); | |||
mod.registerEntity("player", new EntPlayer::Factory()); | |||
@@ -31,8 +41,3 @@ extern "C" void mod_init(Swan::Mod &mod) { | |||
mod.registerAsset("player-running", new Swan::Asset("assets/entities/player-running.png")); | |||
mod.registerAsset("player-still", new Swan::Asset("assets/entities/player-still.png")); | |||
} | |||
int main() { | |||
Swan::Mod mod; | |||
mod.init("core"); | |||
} |
@@ -1,8 +1,20 @@ | |||
#pragma once | |||
#include <memory> | |||
#include <string> | |||
#include <SFML/Graphics/Image.hpp> | |||
namespace Swan { | |||
class Item { | |||
public: | |||
std::unique_ptr<sf::Image> image; | |||
std::string name = ""; | |||
static Item INVALID_ITEM; | |||
static Item *createInvalid(); | |||
static void initGlobal(); | |||
}; | |||
} |
@@ -7,6 +7,7 @@ | |||
#include <SFML/Graphics/Image.hpp> | |||
#include "Tile.h" | |||
#include "Item.h" | |||
#include "WorldGen.h" | |||
#include "Entity.h" | |||
#include "Asset.h" | |||
@@ -19,6 +20,7 @@ public: | |||
void init(const std::string &name); | |||
void registerTile(const std::string &name, Tile *tile); | |||
void registerItem(const std::string &name, Item *item); | |||
void registerWorldGen(const std::string &name, WorldGen::Factory *gen); | |||
void registerEntity(const std::string &name, Entity::Factory *ent); | |||
void registerAsset(const std::string &name, Asset *asset); | |||
@@ -28,6 +30,7 @@ public: | |||
std::string name_; | |||
std::string path_; | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
std::vector<std::shared_ptr<Item>> items_; | |||
std::vector<std::shared_ptr<WorldGen::Factory>> worldgens_; | |||
std::vector<std::shared_ptr<Entity::Factory>> entities_; | |||
std::vector<std::shared_ptr<Asset>> assets_; |
@@ -24,26 +24,27 @@ public: | |||
void setWorldGen(const std::string &gen); | |||
void spawnPlayer(); | |||
void registerTile(std::shared_ptr<Tile> t); | |||
void registerItem(std::shared_ptr<Item> i); | |||
void registerWorldGen(std::shared_ptr<WorldGen::Factory> gen); | |||
void registerEntity(std::shared_ptr<Entity::Factory> ent); | |||
void registerAsset(std::shared_ptr<Asset> asset); | |||
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::ID getTileID(const std::string &name); | |||
Tile &getTile(const std::string &name); | |||
Item &getItem(const std::string &name); | |||
Asset &getAsset(const std::string &name); | |||
void draw(Game &game, Win &win); | |||
void update(Game &game, float dt); | |||
void tick(); | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
std::map<std::string, Tile::ID> tiles_map_; | |||
std::map<std::string, std::shared_ptr<Item>> items_; | |||
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_; | |||
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: |
@@ -31,11 +31,12 @@ void Game::loadMod(const std::string &path) { | |||
void Game::createWorld(std::string worldgen) { | |||
world_.reset(new World()); | |||
for (auto &mod: registered_mods_) { | |||
// Register invalids | |||
world_->registerTile(std::shared_ptr<Tile>(Tile::createInvalid())); | |||
for (auto &tile: mod.tiles_) | |||
world_->registerTile(tile); | |||
for (auto &item: mod.items_) | |||
world_->registerItem(item); | |||
for (auto &worldgen: mod.worldgens_) | |||
world_->registerWorldGen(worldgen); | |||
for (auto &entity: mod.entities_) | |||
@@ -74,6 +75,7 @@ void Game::tick() { | |||
void Game::initGlobal() { | |||
Tile::initGlobal(); | |||
Item::initGlobal(); | |||
Asset::initGlobal(); | |||
} | |||
@@ -1,7 +1,24 @@ | |||
#include "Item.h" | |||
#include "common.h" | |||
namespace Swan { | |||
Item Item::INVALID_ITEM; | |||
static void initInvalid(Item *i) { | |||
i->name = "@internal::invalid"; | |||
i->image.reset(new sf::Image()); | |||
i->image->create(TILE_SIZE, TILE_SIZE, sf::Color(254, 66, 242)); | |||
} | |||
Item *Item::createInvalid() { | |||
Item *i = new Item(); | |||
initInvalid(i); | |||
return i; | |||
} | |||
void Item::initGlobal() { | |||
initInvalid(&INVALID_ITEM); | |||
} | |||
} |
@@ -17,13 +17,21 @@ void Mod::registerTile(const std::string &name, Tile *tile) { | |||
tiles_.push_back(std::shared_ptr<Tile>(tile)); | |||
} | |||
void Mod::registerItem(const std::string &name, Item *item) { | |||
item->name = name_ + "::" + name; | |||
fprintf(stderr, "Adding item: %s\n", item->name.c_str()); | |||
items_.push_back(std::shared_ptr<Item>(item)); | |||
} | |||
void Mod::registerWorldGen(const std::string &name, WorldGen::Factory *gen) { | |||
gen->name_ = name_ + "::" + name; | |||
fprintf(stderr, "Adding world gen: %s\n", gen->name_.c_str()); | |||
worldgens_.push_back(std::shared_ptr<WorldGen::Factory>(gen)); | |||
} | |||
void Mod::registerEntity(const std::string &name, Entity::Factory *ent) { | |||
ent->name_ = name_ + "::" + name; | |||
fprintf(stderr, "Adding entity: %s\n",ent->name_.c_str()); | |||
entities_.push_back(std::shared_ptr<Entity::Factory>(ent)); | |||
} | |||
@@ -54,6 +54,10 @@ void World::registerTile(std::shared_ptr<Tile> t) { | |||
tiles_map_[t->name] = id; | |||
} | |||
void World::registerItem(std::shared_ptr<Item> i) { | |||
items_[i->name] = i; | |||
} | |||
void World::registerWorldGen(std::shared_ptr<WorldGen::Factory> gen) { | |||
worldgens_[gen->name_] = gen; | |||
} |