A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntItemStack.cc 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "EntItemStack.h"
  2. #include <random>
  3. EntItemStack::EntItemStack(const Swan::Context &ctx, const Swan::SRF &params):
  4. PhysicsEntity(SIZE, MASS) {
  5. readSRF(ctx, params);
  6. static std::uniform_real_distribution dis(-0.2f, 0.2f);
  7. body_.pos_ += Swan::Vec2{ dis(ctx.world.random_), dis(ctx.world.random_) };
  8. }
  9. void EntItemStack::draw(const Swan::Context &ctx, Swan::Win &win) {
  10. win.setPos(body_.pos_);
  11. win.draw(sprite_);
  12. }
  13. void EntItemStack::tick(const Swan::Context &ctx, float dt) {
  14. despawn_timer_ -= dt;
  15. if (despawn_timer_ <= 0) {
  16. ctx.plane.despawnEntity(*this);
  17. }
  18. }
  19. void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) {
  20. auto &arr = dynamic_cast<const Swan::SRFArray &>(srf);
  21. auto *pos = dynamic_cast<Swan::SRFFloatArray *>(arr.val[0].get());
  22. auto *name = dynamic_cast<Swan::SRFString *>(arr.val[1].get());
  23. body_.pos_.set(pos->val[0], pos->val[1]);
  24. item_ = &ctx.world.getItem(name->val);
  25. tex_.loadFromImage(*item_->image);
  26. sprite_.setTexture(tex_);
  27. sprite_.setScale(SIZE);
  28. }
  29. Swan::SRF *EntItemStack::writeSRF(const Swan::Context &ctx) {
  30. return new Swan::SRFArray{
  31. new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y },
  32. new Swan::SRFString{ item_->name },
  33. };
  34. }