EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms); | EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms); | ||||
const Swan::Vec2 &getPos() override { return body_.pos_; } | |||||
std::optional<Swan::BoundingBox> getBounds() override { return body_.getBounds(); } | |||||
void draw(const Swan::Context &ctx, Swan::Win &win) override; | void draw(const Swan::Context &ctx, Swan::Win &win) override; | ||||
void update(const Swan::Context &ctx, float dt) override; | void update(const Swan::Context &ctx, float dt) override; |
#include "common.h" | #include "common.h" | ||||
#include "WorldPlane.h" | #include "WorldPlane.h" | ||||
#include "BoundingBox.h" | |||||
namespace Swan { | namespace Swan { | ||||
void outline(Win &win); | void outline(Win &win); | ||||
void update(float dt); | void update(float dt); | ||||
BoundingBox getBounds() { return { pos_, size_ }; } | |||||
Vec2 force_ = { 0, 0 }; | Vec2 force_ = { 0, 0 }; | ||||
Vec2 vel_ = { 0, 0 }; | Vec2 vel_ = { 0, 0 }; | ||||
Vec2 size_; | Vec2 size_; |
#pragma once | |||||
#include "Vector2.h" | |||||
namespace Swan { | |||||
struct BoundingBox { | |||||
Vec2 pos; | |||||
Vec2 size; | |||||
}; | |||||
} |
#pragma once | #pragma once | ||||
#include <memory> | #include <memory> | ||||
#include <optional> | |||||
#include "common.h" | #include "common.h" | ||||
#include "SRF.h" | #include "SRF.h" | ||||
#include "BoundingBox.h" | |||||
namespace Swan { | namespace Swan { | ||||
virtual ~Entity() = default; | virtual ~Entity() = default; | ||||
virtual const Vec2 &getPos() { return Vec2::ZERO; } | |||||
virtual std::optional<BoundingBox> getBounds() { return std::nullopt; } | |||||
virtual void draw(const Context &ctx, Win &win) {} | virtual void draw(const Context &ctx, Win &win) {} | ||||
virtual void update(const Context &ctx, float dt) {} | virtual void update(const Context &ctx, float dt) {} |
#include <swan/Animation.h> | #include <swan/Animation.h> | ||||
#include <swan/Asset.h> | #include <swan/Asset.h> | ||||
#include <swan/Body.h> | #include <swan/Body.h> | ||||
#include <swan/BoundingBox.h> | |||||
#include <swan/Chunk.h> | #include <swan/Chunk.h> | ||||
#include <swan/Entity.h> | #include <swan/Entity.h> | ||||
#include <swan/Game.h> | #include <swan/Game.h> |
} | } | ||||
void World::draw(Win &win) { | void World::draw(Win &win) { | ||||
win.cam_ = player_->getPos() - (win.getSize() / 2) + 0.5; | |||||
auto bounds = *player_->getBounds(); | |||||
win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size.x_ / 2); | |||||
planes_[current_plane_].draw(win); | planes_[current_plane_].draw(win); | ||||
} | } | ||||
for (auto &plane: planes_) | for (auto &plane: planes_) | ||||
plane.tick(); | plane.tick(); | ||||
const Vec2 &abspos = player_->getPos(); | |||||
auto bounds = *player_->getBounds(); | |||||
chunk_renderer_.tick( | chunk_renderer_.tick( | ||||
planes_[current_plane_], | planes_[current_plane_], | ||||
ChunkPos((int)abspos.x_ / CHUNK_WIDTH, (int)abspos.y_ / CHUNK_HEIGHT)); | |||||
ChunkPos((int)bounds.pos.x_ / CHUNK_WIDTH, (int)bounds.pos.y_ / CHUNK_HEIGHT)); | |||||
} | } | ||||
} | } |
} | } | ||||
void WorldPlane::draw(Win &win) { | void WorldPlane::draw(Win &win) { | ||||
const Vec2 &ppos = world_->player_->getPos(); | |||||
auto pbounds = *world_->player_->getBounds(); | |||||
ChunkPos pcpos = ChunkPos( | ChunkPos pcpos = ChunkPos( | ||||
(int)floor(ppos.x_ / CHUNK_WIDTH), | |||||
(int)floor(ppos.y_ / CHUNK_HEIGHT)); | |||||
(int)floor(pbounds.pos.x_ / CHUNK_WIDTH), | |||||
(int)floor(pbounds.pos.y_ / CHUNK_HEIGHT)); | |||||
for (int x = -1; x <= 1; ++x) { | for (int x = -1; x <= 1; ++x) { | ||||
for (int y = -1; y <= 1; ++y) { | for (int y = -1; y <= 1; ++y) { |