Browse Source

bounding box

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

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

@@ -13,7 +13,7 @@ public:

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 update(const Swan::Context &ctx, float dt) override;

+ 3
- 0
libswan/include/swan/Body.h View File

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

#include "common.h"
#include "WorldPlane.h"
#include "BoundingBox.h"

namespace Swan {

@@ -19,6 +20,8 @@ public:
void outline(Win &win);
void update(float dt);

BoundingBox getBounds() { return { pos_, size_ }; }

Vec2 force_ = { 0, 0 };
Vec2 vel_ = { 0, 0 };
Vec2 size_;

+ 12
- 0
libswan/include/swan/BoundingBox.h View File

@@ -0,0 +1,12 @@
#pragma once

#include "Vector2.h"

namespace Swan {

struct BoundingBox {
Vec2 pos;
Vec2 size;
};

}

+ 3
- 1
libswan/include/swan/Entity.h View File

@@ -1,9 +1,11 @@
#pragma once

#include <memory>
#include <optional>

#include "common.h"
#include "SRF.h"
#include "BoundingBox.h"

namespace Swan {

@@ -22,7 +24,7 @@ public:

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 update(const Context &ctx, float dt) {}

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

@@ -3,6 +3,7 @@
#include <swan/Animation.h>
#include <swan/Asset.h>
#include <swan/Body.h>
#include <swan/BoundingBox.h>
#include <swan/Chunk.h>
#include <swan/Entity.h>
#include <swan/Game.h>

+ 4
- 3
libswan/src/World.cc View File

@@ -121,7 +121,8 @@ WorldPlane &World::addPlane(std::string gen) {
}

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

@@ -134,10 +135,10 @@ void World::tick() {
for (auto &plane: planes_)
plane.tick();

const Vec2 &abspos = player_->getPos();
auto bounds = *player_->getBounds();
chunk_renderer_.tick(
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 View File

@@ -107,10 +107,10 @@ void WorldPlane::breakBlock(TilePos pos) {
}

void WorldPlane::draw(Win &win) {
const Vec2 &ppos = world_->player_->getPos();
auto pbounds = *world_->player_->getBounds();
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 y = -1; y <= 1; ++y) {

Loading…
Cancel
Save