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.

ItemStackEntity.cc 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "ItemStackEntity.h"
  2. #include <random>
  3. ItemStackEntity::ItemStackEntity(
  4. const Swan::Context &ctx, Swan::Vec2 pos, const std::string &item):
  5. ItemStackEntity() {
  6. static std::uniform_real_distribution vx(-2.3f, 2.3f);
  7. static std::uniform_real_distribution vy(-2.3f, -1.2f);
  8. body_.pos = pos;
  9. item_ = &ctx.world.getItem(item);
  10. physics_.vel += Swan::Vec2{ vx(ctx.world.random_), vy(ctx.world.random_) };
  11. }
  12. ItemStackEntity::ItemStackEntity(const Swan::Context &ctx, const PackObject &obj):
  13. ItemStackEntity() {
  14. deserialize(ctx, obj);
  15. }
  16. void ItemStackEntity::draw(const Swan::Context &ctx, Cygnet::Renderer &rnd) {
  17. /*
  18. SDL_Rect rect = item_->image_.frameRect();
  19. SDL_Texture *tex = item_->image_.texture_.get();
  20. Swan::TexColorMod darken(tex, 220, 220, 220);
  21. win.showTexture(body_.pos, tex, &rect,
  22. { .hscale = 0.5, .vscale = 0.5 });
  23. TODO */
  24. }
  25. void ItemStackEntity::update(const Swan::Context &ctx, float dt) {
  26. physics(ctx, dt, { .mass = MASS, .bounciness = 0.6 });
  27. }
  28. void ItemStackEntity::tick(const Swan::Context &ctx, float dt) {
  29. despawnTimer_ -= dt;
  30. if (despawnTimer_ <= 0)
  31. despawn(ctx);
  32. }
  33. void ItemStackEntity::deserialize(const Swan::Context &ctx, const PackObject &obj) {
  34. body_.pos = obj.at("pos").as<Swan::Vec2>();
  35. item_ = &ctx.world.getItem(obj.at("item").as<std::string>());
  36. }
  37. Swan::Entity::PackObject ItemStackEntity::serialize(const Swan::Context &ctx, msgpack::zone &zone) {
  38. return {
  39. { "pos", msgpack::object(body_.pos, zone) },
  40. { "tile", msgpack::object(item_->name, zone) },
  41. };
  42. }