@@ -1,33 +1,33 @@ | |||
#include "EntPlayer.h" | |||
EntPlayer::EntPlayer(Swan::World &world, const Swan::Vec2 &pos): | |||
EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::Vec2 &pos): | |||
body_(pos, SIZE, MASS) { | |||
anims_[(int)State::IDLE].init(32, 64, 0.8, | |||
world.getAsset("core::player-still")); | |||
ctx.world.getAsset("core::player-still")); | |||
anims_[(int)State::RUNNING_R].init(32, 64, 1, | |||
world.getAsset("core::player-running")); | |||
ctx.world.getAsset("core::player-running")); | |||
anims_[(int)State::RUNNING_L].init(32, 64, 1, | |||
world.getAsset("core::player-running"), (int)Swan::Animation::Flags::HFLIP); | |||
ctx.world.getAsset("core::player-running"), (int)Swan::Animation::Flags::HFLIP); | |||
} | |||
void EntPlayer::draw(Swan::Win &win) { | |||
void EntPlayer::draw(const Swan::Context &ctx, Swan::Win &win) { | |||
body_.outline(win); | |||
win.setPos(body_.pos_ - Swan::Vec2(0.2, 0.1)); | |||
anims_[(int)state_].draw(win); | |||
} | |||
void EntPlayer::update(Swan::Game &game, Swan::WorldPlane &plane, float dt) { | |||
void EntPlayer::update(const Swan::Context &ctx, float dt) { | |||
State oldState = state_; | |||
state_ = State::IDLE; | |||
mouse_tile_ = game.getMouseTile(); | |||
plane.debugBox(mouse_tile_); | |||
mouse_tile_ = ctx.game.getMouseTile(); | |||
ctx.plane.debugBox(mouse_tile_); | |||
jump_timer_.tick(dt); | |||
// Break block | |||
if (game.isMousePressed()) | |||
plane.setTile(mouse_tile_, "core::air"); | |||
if (ctx.game.isMousePressed()) | |||
ctx.plane.setTile(mouse_tile_, "core::air"); | |||
// Move left | |||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { | |||
@@ -56,5 +56,5 @@ void EntPlayer::update(Swan::Game &game, Swan::WorldPlane &plane, float dt) { | |||
body_.friction(FRICTION); | |||
body_.gravity(); | |||
body_.update(dt); | |||
body_.collide(plane); | |||
body_.collide(ctx.plane); | |||
} |
@@ -6,17 +6,17 @@ class EntPlayer: public Swan::Entity { | |||
public: | |||
class Factory: public Swan::Entity::Factory { | |||
public: | |||
Swan::Entity *create(Swan::World &world, const Swan::Vec2 &pos) override { | |||
return new EntPlayer(world, pos); | |||
Swan::Entity *create(const Swan::Context &ctx, const Swan::Vec2 &pos) override { | |||
return new EntPlayer(ctx, pos); | |||
} | |||
}; | |||
EntPlayer(Swan::World &world, const Swan::Vec2 &pos); | |||
EntPlayer(const Swan::Context &ctx, const Swan::Vec2 &pos); | |||
const Swan::Vec2 &getPos() override { return body_.pos_; } | |||
void draw(Swan::Win &win) override; | |||
void update(Swan::Game &game, Swan::WorldPlane &plane, float dt) override; | |||
void draw(const Swan::Context &ctx, Swan::Win &win) override; | |||
void update(const Swan::Context &ctx, float dt) override; | |||
private: | |||
static constexpr float FORCE = 3000; |
@@ -32,8 +32,8 @@ public: | |||
void compress(); | |||
void decompress(); | |||
void render(World &world); | |||
void draw(Game &game, Win &win); | |||
void render(const Context &ctx); | |||
void draw(const Context &ctx, Win &win); | |||
ChunkPos pos_; | |||
@@ -15,7 +15,7 @@ public: | |||
class Factory { | |||
public: | |||
virtual ~Factory() = default; | |||
virtual Entity *create(World &world, const Vec2 &pos) = 0; | |||
virtual Entity *create(const Context &ctx, const Vec2 &pos) = 0; | |||
std::string name_; | |||
}; | |||
@@ -23,8 +23,8 @@ public: | |||
virtual const Vec2 &getPos() { return Vec2::ZERO; } | |||
virtual void draw(Win &win) {} | |||
virtual void update(Game &game, WorldPlane &plane, float dt) {} | |||
virtual void draw(const Context &ctx, Win &win) {} | |||
virtual void update(const Context &ctx, float dt) {} | |||
virtual void tick() {} | |||
}; | |||
@@ -18,6 +18,8 @@ class Game; | |||
class World { | |||
public: | |||
World(Game *game): game_(game) {} | |||
WorldPlane &addPlane(std::string gen); | |||
WorldPlane &addPlane() { return addPlane(default_world_gen_); } | |||
void setCurrentPlane(WorldPlane &plane); | |||
@@ -35,8 +37,8 @@ public: | |||
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 draw(Win &win); | |||
void update(float dt); | |||
void tick(); | |||
std::vector<std::shared_ptr<Tile>> tiles_; | |||
@@ -46,6 +48,7 @@ public: | |||
std::map<std::string, std::shared_ptr<Entity::Factory>> ents_; | |||
std::map<std::string, std::shared_ptr<Asset>> assets_; | |||
Entity *player_; | |||
Game *game_; | |||
private: | |||
class ChunkRenderer { |
@@ -24,6 +24,8 @@ public: | |||
Entity &spawnEntity(const std::string &name, const Vec2 &pos); | |||
Context getContext(); | |||
bool hasChunk(ChunkPos pos); | |||
Chunk &getChunk(ChunkPos pos); | |||
void setTileID(TilePos pos, Tile::ID id); | |||
@@ -32,8 +34,8 @@ public: | |||
Entity &spawnPlayer(); | |||
void draw(Game &game, Win &win); | |||
void update(Game &game, float dt); | |||
void draw(Win &win); | |||
void update(float dt); | |||
void tick(); | |||
void debugBox(TilePos pos); |
@@ -15,10 +15,17 @@ static constexpr int CHUNK_WIDTH = 32; | |||
using TilePos = Vec2i; | |||
using ChunkPos = Vec2i; | |||
class Game; | |||
class World; | |||
class WorldPlane; | |||
struct Context { | |||
Game &game; | |||
World &world; | |||
WorldPlane &plane; | |||
}; | |||
struct Win { | |||
public: | |||
sf::RenderWindow *window_; | |||
sf::Transform transform_; | |||
Vec2 cam_; |
@@ -89,7 +89,7 @@ void Chunk::decompress() { | |||
compressed_size_ = -1; | |||
} | |||
void Chunk::render(World &world) { | |||
void Chunk::render(const Context &ctx) { | |||
Tile::ID prevID = Tile::INVALID_ID; | |||
Tile *tile = &Tile::INVALID_TILE; | |||
@@ -98,7 +98,7 @@ void Chunk::render(World &world) { | |||
Tile::ID id = getTileID(RelPos(x, y)); | |||
if (id != prevID) { | |||
prevID = id; | |||
tile = &world.getTileByID(id); | |||
tile = &ctx.world.getTileByID(id); | |||
} | |||
const sf::Uint8 *imgptr = tile->image->getPixelsPtr(); | |||
@@ -117,12 +117,12 @@ void Chunk::render(World &world) { | |||
visuals_->dirty_ = true; | |||
} | |||
void Chunk::draw(Game &game, Win &win) { | |||
void Chunk::draw(const Context &ctx, Win &win) { | |||
if (compressed_size_ != -1) | |||
decompress(); | |||
if (need_render_) { | |||
render(*game.world_); | |||
render(ctx); | |||
need_render_ = false; | |||
} | |||
@@ -29,7 +29,7 @@ void Game::loadMod(const std::string &path) { | |||
} | |||
void Game::createWorld(std::string worldgen) { | |||
world_.reset(new World()); | |||
world_.reset(new World(this)); | |||
for (auto &mod: registered_mods_) { | |||
world_->registerTile(std::shared_ptr<Tile>(Tile::createInvalid())); | |||
@@ -62,11 +62,11 @@ bool Game::isMousePressed() { | |||
} | |||
void Game::draw() { | |||
world_->draw(*this, win_); | |||
world_->draw(win_); | |||
} | |||
void Game::update(float dt) { | |||
world_->update(*this, dt); | |||
world_->update(dt); | |||
} | |||
void Game::tick() { |
@@ -122,14 +122,14 @@ WorldPlane &World::addPlane(std::string gen) { | |||
return planes_[id]; | |||
} | |||
void World::draw(Game &game, Win &win) { | |||
void World::draw(Win &win) { | |||
win.cam_ = player_->getPos() - (win.getSize() / 2) + 0.5; | |||
planes_[current_plane_].draw(game, win); | |||
planes_[current_plane_].draw(win); | |||
} | |||
void World::update(Game &game, float dt) { | |||
void World::update(float dt) { | |||
for (auto &plane: planes_) | |||
plane.update(game, dt); | |||
plane.update(dt); | |||
} | |||
void World::tick() { |
@@ -25,6 +25,10 @@ static Chunk::RelPos relPos(TilePos pos) { | |||
return Chunk::RelPos(rx, ry); | |||
} | |||
Context WorldPlane::getContext() { | |||
return { .game = *world_->game_, .world = *world_, .plane = *this }; | |||
} | |||
Entity &WorldPlane::spawnEntity(const std::string &name, const Vec2 &pos) { | |||
if (world_->ents_.find(name) == world_->ents_.end()) { | |||
fprintf(stderr, "Tried to spawn non-existant entity %s!", | |||
@@ -32,7 +36,7 @@ Entity &WorldPlane::spawnEntity(const std::string &name, const Vec2 &pos) { | |||
abort(); | |||
} | |||
Entity *ent = world_->ents_[name]->create(*world_, pos); | |||
Entity *ent = world_->ents_[name]->create(getContext(), pos); | |||
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; | |||
@@ -48,7 +52,7 @@ Chunk &WorldPlane::getChunk(ChunkPos pos) { | |||
if (iter == chunks_.end()) { | |||
iter = chunks_.emplace(pos, Chunk(pos)).first; | |||
gen_->genChunk(*this, iter->second); | |||
iter->second.render(*world_); | |||
iter->second.render(getContext()); | |||
} | |||
return iter->second; | |||
@@ -75,7 +79,7 @@ Entity &WorldPlane::spawnPlayer() { | |||
return gen_->spawnPlayer(*this); | |||
} | |||
void WorldPlane::draw(Game &game, Win &win) { | |||
void WorldPlane::draw(Win &win) { | |||
const Vec2 &ppos = world_->player_->getPos(); | |||
ChunkPos pcpos = ChunkPos( | |||
(int)floor(ppos.x_ / CHUNK_WIDTH), | |||
@@ -85,12 +89,12 @@ void WorldPlane::draw(Game &game, Win &win) { | |||
for (int y = -1; y <= 1; ++y) { | |||
auto chunk = chunks_.find(pcpos + ChunkPos(x, y)); | |||
if (chunk != chunks_.end()) | |||
chunk->second.draw(game, win); | |||
chunk->second.draw(getContext(), win); | |||
} | |||
} | |||
for (auto &ent: entities_) | |||
ent->draw(win); | |||
ent->draw(getContext(), win); | |||
if (debug_boxes_.size() > 0) { | |||
sf::RectangleShape rect(Vec2(TILE_SIZE, TILE_SIZE)); | |||
@@ -104,10 +108,10 @@ void WorldPlane::draw(Game &game, Win &win) { | |||
} | |||
} | |||
void WorldPlane::update(Game &game, float dt) { | |||
void WorldPlane::update(float dt) { | |||
debug_boxes_.clear(); | |||
for (auto &ent: entities_) | |||
ent->update(game, *this, dt); | |||
ent->update(getContext(), dt); | |||
} | |||
void WorldPlane::tick() { |