void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) { | void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) { | ||||
win.setPos(body_.pos_); | 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) { | void EntItemStack::tick(const Swan::Context &ctx, float dt) { | ||||
body_.pos_.set(pos->val[0], pos->val[1]); | body_.pos_.set(pos->val[0], pos->val[1]); | ||||
item_ = &ctx.world.getItem(name->val); | item_ = &ctx.world.getItem(name->val); | ||||
tex_.loadFromImage(*item_->image); | |||||
sprite_.setTexture(tex_); | |||||
sprite_.setScale(SIZE); | |||||
} | } | ||||
Swan::SRF *EntItemStack::writeSRF(const Swan::Context &ctx) { | Swan::SRF *EntItemStack::writeSRF(const Swan::Context &ctx) { | ||||
return new Swan::SRFArray{ | return new Swan::SRFArray{ | ||||
new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }, | new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }, | ||||
new Swan::SRFString{ item_->name }, | |||||
new Swan::SRFString{ item_->name_ }, | |||||
}; | }; | ||||
} | } |
static constexpr float DESPAWN_TIME = 5 * 60; | static constexpr float DESPAWN_TIME = 5 * 60; | ||||
float despawn_timer_ = DESPAWN_TIME; | float despawn_timer_ = DESPAWN_TIME; | ||||
Swan::Item *item_ = &Swan::Item::INVALID_ITEM; | |||||
sf::Texture tex_; | |||||
sf::Sprite sprite_; | |||||
Swan::Item *item_ = NULL; | |||||
}; | }; |
#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): | ||||
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); | 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) { | void EntPlayer::draw(const Swan::Context &ctx, Swan::Win &win) { |
#pragma once | #pragma once | ||||
#include <swan/swan.h> | #include <swan/swan.h> | ||||
#include <array> | |||||
class EntPlayer: public Swan::PhysicsEntity { | class EntPlayer: public Swan::PhysicsEntity { | ||||
public: | public: | ||||
}; | }; | ||||
State state_ = State::IDLE; | State state_ = State::IDLE; | ||||
Swan::Animation anims_[(int)State::COUNT]; | |||||
std::array<Swan::Animation, (int)State::COUNT> anims_; | |||||
Swan::Timer jump_timer_; | Swan::Timer jump_timer_; | ||||
Swan::TilePos mouse_tile_; | Swan::TilePos mouse_tile_; |
.image = "core::grass", | .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>()); | |||||
} | } |
HFLIP = 1, | 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 tick(float dt); | ||||
void draw(Win &win); | void draw(Win &win); | ||||
ImageResource &resource_; | ImageResource &resource_; | ||||
float interval_; | float interval_; | ||||
float timer_; | float timer_; | ||||
Flags flags_; | |||||
int frame_ = 0; | int frame_ = 0; | ||||
bool dirty_ = true; | bool dirty_ = true; | ||||
}; | }; |