瀏覽代碼

bounding box

opengl-renderer-broken
Martin Dørum 4 年之前
父節點
當前提交
1a5988bfd9

+ 1
- 1
core.mod/src/entities/EntPlayer.h 查看文件



EntPlayer(const Swan::Context &ctx, const Swan::SRF &params); EntPlayer(const Swan::Context &ctx, const Swan::SRF &params);


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;

+ 3
- 0
libswan/include/swan/Body.h 查看文件



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

+ 12
- 0
libswan/include/swan/BoundingBox.h 查看文件

#pragma once

#include "Vector2.h"

namespace Swan {

struct BoundingBox {
Vec2 pos;
Vec2 size;
};

}

+ 3
- 1
libswan/include/swan/Entity.h 查看文件

#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) {}

+ 1
- 0
libswan/include/swan/swan.h 查看文件

#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>

+ 4
- 3
libswan/src/World.cc 查看文件

} }


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));
} }


} }

+ 3
- 3
libswan/src/WorldPlane.cc 查看文件

} }


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) {

Loading…
取消
儲存