@@ -17,7 +17,7 @@ EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||
void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) { | |||
win.setPos(body_.pos_); | |||
win.draw(sprite_); | |||
SDL_RenderCopy(win.renderer_, item_->image_.texture_.get(), NULL, NULL); | |||
} | |||
void EntItemStack::tick(const Swan::Context &ctx, float dt) { | |||
@@ -33,14 +33,11 @@ void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||
body_.pos_.set(pos->val[0], pos->val[1]); | |||
item_ = &ctx.world.getItem(name->val); | |||
tex_.loadFromImage(*item_->image); | |||
sprite_.setTexture(tex_); | |||
sprite_.setScale(SIZE); | |||
} | |||
Swan::SRF *EntItemStack::writeSRF(const Swan::Context &ctx) { | |||
return new Swan::SRFArray{ | |||
new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }, | |||
new Swan::SRFString{ item_->name }, | |||
new Swan::SRFString{ item_->name_ }, | |||
}; | |||
} |
@@ -23,7 +23,5 @@ private: | |||
static constexpr float DESPAWN_TIME = 5 * 60; | |||
float despawn_timer_ = DESPAWN_TIME; | |||
Swan::Item *item_ = &Swan::Item::INVALID_ITEM; | |||
sf::Texture tex_; | |||
sf::Sprite sprite_; | |||
Swan::Item *item_ = NULL; | |||
}; |
@@ -1,16 +1,13 @@ | |||
#include "EntPlayer.h" | |||
EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||
PhysicsEntity(SIZE, MASS) { | |||
PhysicsEntity(SIZE, MASS), | |||
anims_{ | |||
Swan::Animation(ctx.world.getImage("core::player-still"), 0.8), | |||
Swan::Animation(ctx.world.getImage("core::player-running"), 1), | |||
Swan::Animation(ctx.world.getImage("core::player-running"), 1, Swan::Animation::Flags::HFLIP) } { | |||
readSRF(ctx, params); | |||
anims_[(int)State::IDLE].init(32, 64, 0.8, | |||
ctx.world.getAsset("core::player-still")); | |||
anims_[(int)State::RUNNING_R].init(32, 64, 1, | |||
ctx.world.getAsset("core::player-running")); | |||
anims_[(int)State::RUNNING_L].init(32, 64, 1, | |||
ctx.world.getAsset("core::player-running"), (int)Swan::Animation::Flags::HFLIP); | |||
} | |||
void EntPlayer::draw(const Swan::Context &ctx, Swan::Win &win) { |
@@ -1,6 +1,7 @@ | |||
#pragma once | |||
#include <swan/swan.h> | |||
#include <array> | |||
class EntPlayer: public Swan::PhysicsEntity { | |||
public: | |||
@@ -32,7 +33,7 @@ private: | |||
}; | |||
State state_ = State::IDLE; | |||
Swan::Animation anims_[(int)State::COUNT]; | |||
std::array<Swan::Animation, (int)State::COUNT> anims_; | |||
Swan::Timer jump_timer_; | |||
Swan::TilePos mouse_tile_; |
@@ -48,8 +48,8 @@ extern "C" void mod_init(Swan::Mod &mod) { | |||
.image = "core::grass", | |||
}); | |||
mod.registerWorldGen("default", new WGDefault::Factory()); | |||
mod.registerWorldGen("default", std::make_unique<WGDefault::Factory>()); | |||
mod.registerEntity("player", new EntPlayer::Factory()); | |||
mod.registerEntity("item-stack", new EntItemStack::Factory()); | |||
mod.registerEntity("player", std::make_unique<EntPlayer::Factory>()); | |||
mod.registerEntity("item-stack", std::make_unique<EntItemStack::Factory>()); | |||
} |
@@ -13,8 +13,8 @@ public: | |||
HFLIP = 1, | |||
}; | |||
Animation(ImageResource &resource, float interval): | |||
resource_(resource), interval_(interval), timer_(interval) {} | |||
Animation(ImageResource &resource, float interval, Flags flags = (Flags)0): | |||
resource_(resource), interval_(interval), timer_(interval), flags_(flags) {} | |||
void tick(float dt); | |||
void draw(Win &win); | |||
@@ -24,6 +24,7 @@ private: | |||
ImageResource &resource_; | |||
float interval_; | |||
float timer_; | |||
Flags flags_; | |||
int frame_ = 0; | |||
bool dirty_ = true; | |||
}; |