A 2D tile-based sandbox game.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ItemStackEntity.cc 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. item_ = &ctx.world.getItem(item);
  9. body_.pos_ = pos;
  10. body_.pos_.y += 0.5 - body_.size_.y / 2;
  11. body_.vel_ += Swan::Vec2{ vx(ctx.world.random_), vy(ctx.world.random_) };
  12. }
  13. ItemStackEntity::ItemStackEntity(const Swan::Context &ctx, const PackObject &obj):
  14. ItemStackEntity() {
  15. deserialize(ctx, obj);
  16. }
  17. void ItemStackEntity::draw(const Swan::Context &ctx, Swan::Win &win) {
  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. }
  24. void ItemStackEntity::tick(const Swan::Context &ctx, float dt) {
  25. despawn_timer_ -= dt;
  26. if (despawn_timer_ <= 0)
  27. despawn(ctx);
  28. }
  29. void ItemStackEntity::deserialize(const Swan::Context &ctx, const PackObject &obj) {
  30. body_.pos_ = obj.at("pos").as<Swan::Vec2>();
  31. item_ = &ctx.world.getItem(obj.at("item").as<std::string>());
  32. }
  33. Swan::Entity::PackObject ItemStackEntity::serialize(const Swan::Context &ctx, msgpack::zone &zone) {
  34. return {
  35. { "pos", msgpack::object(body_.pos_, zone) },
  36. { "tile", msgpack::object(item_->name_, zone) },
  37. };
  38. }