#include "EntItemStack.h" | #include "EntItemStack.h" | ||||
EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms): | EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms): | ||||
body_(SIZE, MASS) { | |||||
PhysicsEntity(SIZE, MASS) { | |||||
readSRF(ctx, params); | readSRF(ctx, params); | ||||
} | } | ||||
win.draw(sprite_); | 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) { | void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | ||||
auto &arr = dynamic_cast<const Swan::SRFArray &>(srf); | auto &arr = dynamic_cast<const Swan::SRFArray &>(srf); | ||||
auto *pos = dynamic_cast<Swan::SRFFloatArray *>(arr.val[0].get()); | auto *pos = dynamic_cast<Swan::SRFFloatArray *>(arr.val[0].get()); |
#include <swan/swan.h> | #include <swan/swan.h> | ||||
class EntItemStack: public Swan::Entity { | |||||
class EntItemStack: public Swan::PhysicsEntity { | |||||
public: | public: | ||||
class Factory: public Swan::Entity::Factory { | class Factory: public Swan::Entity::Factory { | ||||
Swan::Entity *create(const Swan::Context &ctx, const Swan::SRF ¶ms) override { | Swan::Entity *create(const Swan::Context &ctx, const Swan::SRF ¶ms) override { | ||||
EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms); | EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms); | ||||
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 readSRF(const Swan::Context &ctx, const Swan::SRF &srf) override; | void readSRF(const Swan::Context &ctx, const Swan::SRF &srf) override; | ||||
Swan::SRF *writeSRF(const Swan::Context &ctx) override; | Swan::SRF *writeSRF(const Swan::Context &ctx) override; | ||||
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.5, 0.5); | static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.5, 0.5); | ||||
Swan::Item *item_ = &Swan::Item::INVALID_ITEM; | Swan::Item *item_ = &Swan::Item::INVALID_ITEM; | ||||
Swan::Body body_; | |||||
sf::Texture tex_; | sf::Texture tex_; | ||||
sf::Sprite sprite_; | sf::Sprite sprite_; | ||||
}; | }; |
#include "EntPlayer.h" | #include "EntPlayer.h" | ||||
EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms): | EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms): | ||||
body_(SIZE, MASS) { | |||||
PhysicsEntity(SIZE, MASS) { | |||||
readSRF(ctx, params); | readSRF(ctx, params); | ||||
anims_[(int)state_].reset(); | anims_[(int)state_].reset(); | ||||
anims_[(int)state_].tick(dt); | 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) { | void EntPlayer::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { |
#include <swan/swan.h> | #include <swan/swan.h> | ||||
class EntPlayer: public Swan::Entity { | |||||
class EntPlayer: public Swan::PhysicsEntity { | |||||
public: | public: | ||||
class Factory: public Swan::Entity::Factory { | class Factory: public Swan::Entity::Factory { | ||||
public: | public: | ||||
EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms); | EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms); | ||||
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; | ||||
void readSRF(const Swan::Context &ctx, const Swan::SRF &srf) override; | void readSRF(const Swan::Context &ctx, const Swan::SRF &srf) override; | ||||
static constexpr float FORCE = 3000; | static constexpr float FORCE = 3000; | ||||
static constexpr float JUMP_FORCE = 10; | static constexpr float JUMP_FORCE = 10; | ||||
static constexpr float MASS = 80; | 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); | static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9); | ||||
enum class State { | enum class State { | ||||
Swan::Timer jump_timer_; | Swan::Timer jump_timer_; | ||||
Swan::TilePos mouse_tile_; | Swan::TilePos mouse_tile_; | ||||
Swan::Body body_; | |||||
}; | }; |
#include <SFML/Graphics.hpp> | #include <SFML/Graphics.hpp> | ||||
#include "common.h" | #include "common.h" | ||||
#include "WorldPlane.h" | |||||
#include "BoundingBox.h" | #include "BoundingBox.h" | ||||
namespace Swan { | namespace Swan { | ||||
class WorldPlane; | |||||
class Body { | class Body { | ||||
public: | public: | ||||
Body(Vec2 size, float mass, Vec2 pos = Vec2::ZERO): | Body(Vec2 size, float mass, Vec2 pos = Vec2::ZERO): | ||||
size_(size), mass_(mass), pos_(pos) {}; | 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 gravity(Vec2 g = Vec2(0, 20)); | ||||
void collide(WorldPlane &plane); | void collide(WorldPlane &plane); | ||||
#include "common.h" | #include "common.h" | ||||
#include "SRF.h" | #include "SRF.h" | ||||
#include "BoundingBox.h" | #include "BoundingBox.h" | ||||
#include "Body.h" | |||||
namespace Swan { | namespace Swan { | ||||
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) {} | ||||
virtual void tick() {} | virtual void tick() {} | ||||
virtual void despawn() {} | |||||
virtual void moveTo(const Vec2 &pos) {} | |||||
virtual void readSRF(const Swan::Context &ctx, const SRF &srf) {} | virtual void readSRF(const Swan::Context &ctx, const SRF &srf) {} | ||||
virtual SRF *writeSRF(const Swan::Context &ctx) { return new SRFNone(); } | 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_; | |||||
}; | |||||
} | } |
#include <math.h> | #include <math.h> | ||||
#include "WorldPlane.h" | |||||
namespace Swan { | namespace Swan { | ||||
void Body::friction(Vec2 coef) { | void Body::friction(Vec2 coef) { | ||||
pos_.x = endx - size_.x - 0.001; | pos_.x = endx - size_.x - 0.001; | ||||
endx = (int)floor(pos_.x + size_.x); | endx = (int)floor(pos_.x + size_.x); | ||||
} | } | ||||
plane.debugBox(TilePos(x, ry)); | |||||
//plane.debugBox(TilePos(x, ry)); | |||||
} | } | ||||
} | } | ||||
vel_.y = 0; | vel_.y = 0; | ||||
} | } | ||||
plane.debugBox(TilePos(x, y)); | |||||
//plane.debugBox(TilePos(x, y)); | |||||
} | } | ||||
// Collide with floor | // Collide with floor | ||||
vel_.y = 0; | vel_.y = 0; | ||||
on_ground_ = true; | on_ground_ = true; | ||||
} | } | ||||
plane.debugBox(TilePos(x, y)); | |||||
//plane.debugBox(TilePos(x, y)); | |||||
} | } | ||||
} | } | ||||