add_library(core.mod SHARED | add_library(core.mod SHARED | ||||
src/main.cc | src/main.cc | ||||
src/DefaultWorldGen.cc | src/DefaultWorldGen.cc | ||||
src/entities/EntPlayer.cc | |||||
src/entities/EntItemStack.cc) | |||||
src/entities/PlayerEntity.cc | |||||
src/entities/ItemStackEntity.cc) | |||||
set_target_properties(core.mod PROPERTIES OUTPUT_NAME mod PREFIX "") | set_target_properties(core.mod PROPERTIES OUTPUT_NAME mod PREFIX "") | ||||
target_link_libraries(core.mod libswan ${libraries}) | target_link_libraries(core.mod libswan ${libraries}) | ||||
#include "EntItemStack.h" | |||||
#include "ItemStackEntity.h" | |||||
#include <random> | #include <random> | ||||
EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||||
ItemStackEntity::ItemStackEntity(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||||
PhysicsEntity(SIZE, MASS) { | PhysicsEntity(SIZE, MASS) { | ||||
PhysicsEntity::body_.bounciness_ = 0.6; | PhysicsEntity::body_.bounciness_ = 0.6; | ||||
body_.vel_ += Swan::Vec2{ vx(ctx.world.random_), vy(ctx.world.random_) }; | body_.vel_ += Swan::Vec2{ vx(ctx.world.random_), vy(ctx.world.random_) }; | ||||
} | } | ||||
void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) { | |||||
void ItemStackEntity::draw(const Swan::Context &ctx, Swan::Win &win) { | |||||
SDL_Rect rect = item_->image_.frameRect(); | SDL_Rect rect = item_->image_.frameRect(); | ||||
SDL_Texture *tex = item_->image_.texture_.get(); | SDL_Texture *tex = item_->image_.texture_.get(); | ||||
{ .hscale = 0.5, .vscale = 0.5 }); | { .hscale = 0.5, .vscale = 0.5 }); | ||||
} | } | ||||
void EntItemStack::tick(const Swan::Context &ctx, float dt) { | |||||
void ItemStackEntity::tick(const Swan::Context &ctx, float dt) { | |||||
despawn_timer_ -= dt; | despawn_timer_ -= dt; | ||||
if (despawn_timer_ <= 0) | if (despawn_timer_ <= 0) | ||||
ctx.plane.despawnEntity(*this); | ctx.plane.despawnEntity(*this); | ||||
} | } | ||||
void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||||
void ItemStackEntity::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()); | ||||
auto *name = dynamic_cast<Swan::SRFString *>(arr.val[1].get()); | auto *name = dynamic_cast<Swan::SRFString *>(arr.val[1].get()); | ||||
item_ = &ctx.world.getItem(name->val); | item_ = &ctx.world.getItem(name->val); | ||||
} | } | ||||
Swan::SRF *EntItemStack::writeSRF(const Swan::Context &ctx) { | |||||
Swan::SRF *ItemStackEntity::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_ }, |
#include <swan/swan.h> | #include <swan/swan.h> | ||||
class EntItemStack: public Swan::PhysicsEntity { | |||||
class ItemStackEntity: 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 { | ||||
return new EntItemStack(ctx, params); | |||||
return new ItemStackEntity(ctx, params); | |||||
} | } | ||||
}; | }; | ||||
EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms); | |||||
ItemStackEntity(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 tick(const Swan::Context &ctx, float dt) override; | void tick(const Swan::Context &ctx, float dt) override; |
#include "EntPlayer.h" | |||||
#include "PlayerEntity.h" | |||||
#include <cmath> | #include <cmath> | ||||
#include "EntItemStack.h" | |||||
#include "ItemStackEntity.h" | |||||
EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||||
PlayerEntity::PlayerEntity(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||||
PhysicsEntity(SIZE, MASS), inventory_(INVENTORY_SIZE), | PhysicsEntity(SIZE, MASS), inventory_(INVENTORY_SIZE), | ||||
anims_{ | anims_{ | ||||
Swan::Animation(ctx.resources.getImage("core::player-still"), 0.8), | Swan::Animation(ctx.resources.getImage("core::player-still"), 0.8), | ||||
readSRF(ctx, params); | readSRF(ctx, params); | ||||
} | } | ||||
void EntPlayer::draw(const Swan::Context &ctx, Swan::Win &win) { | |||||
void PlayerEntity::draw(const Swan::Context &ctx, Swan::Win &win) { | |||||
body_.outline(win); | body_.outline(win); | ||||
anims_[(int)state_].draw(body_.pos_ - Swan::Vec2(0.2, 0.1), win); | anims_[(int)state_].draw(body_.pos_ - Swan::Vec2(0.2, 0.1), win); | ||||
} | } | ||||
void EntPlayer::update(const Swan::Context &ctx, float dt) { | |||||
void PlayerEntity::update(const Swan::Context &ctx, float dt) { | |||||
State oldState = state_; | State oldState = state_; | ||||
state_ = State::IDLE; | state_ = State::IDLE; | ||||
PhysicsEntity::update(ctx, dt); | PhysicsEntity::update(ctx, dt); | ||||
} | } | ||||
void EntPlayer::tick(const Swan::Context &ctx, float dt) { | |||||
for (EntItemStack *ent: ctx.plane.getEntsOfType<EntItemStack>()) { | |||||
void PlayerEntity::tick(const Swan::Context &ctx, float dt) { | |||||
for (ItemStackEntity *ent: ctx.plane.getEntsOfType<ItemStackEntity>()) { | |||||
float squared_dist = | float squared_dist = | ||||
(getBody().getBounds().bottomMid() - ent->getBody().getBounds().center()) | (getBody().getBounds().bottomMid() - ent->getBody().getBounds().center()) | ||||
.squareLength(); | .squareLength(); | ||||
} | } | ||||
} | } | ||||
void EntPlayer::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||||
void PlayerEntity::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||||
auto pos = dynamic_cast<const Swan::SRFFloatArray &>(srf); | auto pos = dynamic_cast<const Swan::SRFFloatArray &>(srf); | ||||
body_.pos_.set(pos.val[0], pos.val[1]); | body_.pos_.set(pos.val[0], pos.val[1]); | ||||
} | } | ||||
Swan::SRF *EntPlayer::writeSRF(const Swan::Context &ctx) { | |||||
Swan::SRF *PlayerEntity::writeSRF(const Swan::Context &ctx) { | |||||
return new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }; | return new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }; | ||||
} | } |
#include <swan/swan.h> | #include <swan/swan.h> | ||||
#include <array> | #include <array> | ||||
class EntPlayer: public Swan::PhysicsEntity, public Swan::InventoryTrait::HasInventory { | |||||
class PlayerEntity: public Swan::PhysicsEntity, public Swan::InventoryTrait::HasInventory { | |||||
public: | public: | ||||
class Factory: public Swan::Entity::Factory { | class Factory: public Swan::Entity::Factory { | ||||
public: | public: | ||||
Swan::Entity *create(const Swan::Context &ctx, const Swan::SRF ¶ms) override { | Swan::Entity *create(const Swan::Context &ctx, const Swan::SRF ¶ms) override { | ||||
return new EntPlayer(ctx, params); | |||||
return new PlayerEntity(ctx, params); | |||||
} | } | ||||
}; | }; | ||||
EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms); | |||||
PlayerEntity(const Swan::Context &ctx, const Swan::SRF ¶ms); | |||||
Swan::InventoryTrait::Inventory &getInventory() override { return inventory_; } | Swan::InventoryTrait::Inventory &getInventory() override { return inventory_; } | ||||
#include <swan/swan.h> | #include <swan/swan.h> | ||||
#include "DefaultWorldGen.h" | #include "DefaultWorldGen.h" | ||||
#include "entities/EntPlayer.h" | |||||
#include "entities/EntItemStack.h" | |||||
#include "entities/PlayerEntity.h" | |||||
#include "entities/ItemStackEntity.h" | |||||
extern "C" void mod_init(Swan::Mod &mod) { | extern "C" void mod_init(Swan::Mod &mod) { | ||||
mod.init("core"); | mod.init("core"); | ||||
mod.registerWorldGen("default", std::make_unique<DefaultWorldGen::Factory>()); | mod.registerWorldGen("default", std::make_unique<DefaultWorldGen::Factory>()); | ||||
mod.registerEntity("player", std::make_unique<EntPlayer::Factory>()); | |||||
mod.registerEntity("item-stack", std::make_unique<EntItemStack::Factory>()); | |||||
mod.registerEntity("player", std::make_unique<PlayerEntity::Factory>()); | |||||
mod.registerEntity("item-stack", std::make_unique<ItemStackEntity::Factory>()); | |||||
} | } |