@@ -1,8 +1,8 @@ | |||
add_library(core.mod SHARED | |||
src/main.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 "") | |||
target_link_libraries(core.mod libswan ${libraries}) | |||
@@ -1,8 +1,8 @@ | |||
#include "EntItemStack.h" | |||
#include "ItemStackEntity.h" | |||
#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::body_.bounciness_ = 0.6; | |||
@@ -15,7 +15,7 @@ EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||
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_Texture *tex = item_->image_.texture_.get(); | |||
@@ -25,13 +25,13 @@ void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) { | |||
{ .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; | |||
if (despawn_timer_ <= 0) | |||
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 *pos = dynamic_cast<Swan::SRFFloatArray *>(arr.val[0].get()); | |||
auto *name = dynamic_cast<Swan::SRFString *>(arr.val[1].get()); | |||
@@ -40,7 +40,7 @@ void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||
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{ | |||
new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }, | |||
new Swan::SRFString{ item_->name_ }, |
@@ -2,15 +2,15 @@ | |||
#include <swan/swan.h> | |||
class EntItemStack: public Swan::PhysicsEntity { | |||
class ItemStackEntity: public Swan::PhysicsEntity { | |||
public: | |||
class Factory: public Swan::Entity::Factory { | |||
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 tick(const Swan::Context &ctx, float dt) override; |
@@ -1,10 +1,10 @@ | |||
#include "EntPlayer.h" | |||
#include "PlayerEntity.h" | |||
#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), | |||
anims_{ | |||
Swan::Animation(ctx.resources.getImage("core::player-still"), 0.8), | |||
@@ -14,12 +14,12 @@ EntPlayer::EntPlayer(const Swan::Context &ctx, const Swan::SRF ¶ms): | |||
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); | |||
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_ = State::IDLE; | |||
@@ -64,8 +64,8 @@ void EntPlayer::update(const Swan::Context &ctx, float 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 = | |||
(getBody().getBounds().bottomMid() - ent->getBody().getBounds().center()) | |||
.squareLength(); | |||
@@ -76,11 +76,11 @@ void EntPlayer::tick(const Swan::Context &ctx, float dt) { | |||
} | |||
} | |||
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); | |||
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 }; | |||
} |
@@ -3,16 +3,16 @@ | |||
#include <swan/swan.h> | |||
#include <array> | |||
class EntPlayer: public Swan::PhysicsEntity, public Swan::InventoryTrait::HasInventory { | |||
class PlayerEntity: public Swan::PhysicsEntity, public Swan::InventoryTrait::HasInventory { | |||
public: | |||
class Factory: public Swan::Entity::Factory { | |||
public: | |||
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_; } | |||
@@ -1,8 +1,8 @@ | |||
#include <swan/swan.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) { | |||
mod.init("core"); | |||
@@ -67,6 +67,6 @@ extern "C" void mod_init(Swan::Mod &mod) { | |||
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>()); | |||
} |