Browse Source

PhysicsEntity

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
91d08dd9c7

+ 1
- 7
core.mod/src/entities/EntItemStack.cc View File

@@ -1,7 +1,7 @@
#include "EntItemStack.h"

EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF &params):
body_(SIZE, MASS) {
PhysicsEntity(SIZE, MASS) {

readSRF(ctx, params);
}
@@ -11,12 +11,6 @@ void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) {
win.draw(sprite_);
}

void EntItemStack::update(const Swan::Context &ctx, float dt) {
body_.gravity();
body_.update(dt);
body_.collide(ctx.plane);
}

void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) {
auto &arr = dynamic_cast<const Swan::SRFArray &>(srf);
auto *pos = dynamic_cast<Swan::SRFFloatArray *>(arr.val[0].get());

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

@@ -2,7 +2,7 @@

#include <swan/swan.h>

class EntItemStack: public Swan::Entity {
class EntItemStack: public Swan::PhysicsEntity {
public:
class Factory: public Swan::Entity::Factory {
Swan::Entity *create(const Swan::Context &ctx, const Swan::SRF &params) override {
@@ -13,7 +13,6 @@ public:
EntItemStack(const Swan::Context &ctx, const Swan::SRF &params);

void draw(const Swan::Context &ctx, Swan::Win &win) override;
void update(const Swan::Context &ctx, float dt) override;
void readSRF(const Swan::Context &ctx, const Swan::SRF &srf) override;
Swan::SRF *writeSRF(const Swan::Context &ctx) override;

@@ -22,7 +21,6 @@ private:
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.5, 0.5);

Swan::Item *item_ = &Swan::Item::INVALID_ITEM;
Swan::Body body_;
sf::Texture tex_;
sf::Sprite sprite_;
};

+ 2
- 5
core.mod/src/entities/EntPlayer.cc View File

@@ -1,7 +1,7 @@
#include "EntPlayer.h"

EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF &params):
body_(SIZE, MASS) {
PhysicsEntity(SIZE, MASS) {

readSRF(ctx, params);

@@ -56,10 +56,7 @@ void EntPlayer::update(const Swan::Context &ctx, float dt) {
anims_[(int)state_].reset();
anims_[(int)state_].tick(dt);

body_.friction(FRICTION);
body_.gravity();
body_.update(dt);
body_.collide(ctx.plane);
Swan::PhysicsEntity::update(ctx, dt);
}

void EntPlayer::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) {

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

@@ -2,7 +2,7 @@

#include <swan/swan.h>

class EntPlayer: public Swan::Entity {
class EntPlayer: public Swan::PhysicsEntity {
public:
class Factory: public Swan::Entity::Factory {
public:
@@ -13,8 +13,6 @@ public:

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

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;
void readSRF(const Swan::Context &ctx, const Swan::SRF &srf) override;
@@ -24,7 +22,6 @@ private:
static constexpr float FORCE = 3000;
static constexpr float JUMP_FORCE = 10;
static constexpr float MASS = 80;
static constexpr Swan::Vec2 FRICTION = Swan::Vec2(400, 50);
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9);

enum class State {
@@ -39,6 +36,4 @@ private:

Swan::Timer jump_timer_;
Swan::TilePos mouse_tile_;

Swan::Body body_;
};

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

@@ -3,17 +3,18 @@
#include <SFML/Graphics.hpp>

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

namespace Swan {

class WorldPlane;

class Body {
public:
Body(Vec2 size, float mass, Vec2 pos = Vec2::ZERO):
size_(size), mass_(mass), pos_(pos) {};

void friction(Vec2 coef);
void friction(Vec2 coef = Vec2(400, 50));
void gravity(Vec2 g = Vec2(0, 20));
void collide(WorldPlane &plane);


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

@@ -6,6 +6,7 @@
#include "common.h"
#include "SRF.h"
#include "BoundingBox.h"
#include "Body.h"

namespace Swan {

@@ -29,9 +30,31 @@ public:
virtual void draw(const Context &ctx, Win &win) {}
virtual void update(const Context &ctx, float dt) {}
virtual void tick() {}
virtual void despawn() {}
virtual void moveTo(const Vec2 &pos) {}
virtual void readSRF(const Swan::Context &ctx, const SRF &srf) {}
virtual SRF *writeSRF(const Swan::Context &ctx) { return new SRFNone(); }
};

class PhysicsEntity: public Entity {
public:
PhysicsEntity(Vec2 size, double mass):
body_(size, mass) {}

virtual std::optional<BoundingBox> getBounds() { return body_.getBounds(); }

virtual void update(const Context &ctx, float dt) override {
body_.friction();
body_.gravity();
body_.update(dt);
body_.collide(ctx.plane);
}

virtual void moveTo(const Vec2 &pos) override {
body_.pos_ = pos;
}

protected:
Body body_;
};

}

+ 5
- 3
libswan/src/Body.cc View File

@@ -2,6 +2,8 @@

#include <math.h>

#include "WorldPlane.h"

namespace Swan {

void Body::friction(Vec2 coef) {
@@ -34,7 +36,7 @@ void Body::collide(WorldPlane &plane) {
pos_.x = endx - size_.x - 0.001;
endx = (int)floor(pos_.x + size_.x);
}
plane.debugBox(TilePos(x, ry));
//plane.debugBox(TilePos(x, ry));
}
}

@@ -47,7 +49,7 @@ void Body::collide(WorldPlane &plane) {
vel_.y = 0;
}

plane.debugBox(TilePos(x, y));
//plane.debugBox(TilePos(x, y));
}

// Collide with floor
@@ -60,7 +62,7 @@ void Body::collide(WorldPlane &plane) {
vel_.y = 0;
on_ground_ = true;
}
plane.debugBox(TilePos(x, y));
//plane.debugBox(TilePos(x, y));
}
}


Loading…
Cancel
Save