1 #include "shop_item.h"
2 #include "label.h"
3 #include "i18n.h"
4 #include "resource_manager.h"
5 #include "animation_model.h"
6 #include "sdlx/surface.h"
7 #include "button.h"
8 #include "math/binary.h"
9
ShopItem(const Campaign & campaign,const Campaign::ShopItem & item,const int w)10 ShopItem::ShopItem(const Campaign &campaign, const Campaign::ShopItem &item, const int w) : _active(false), t(0), dir_t(0) {
11 _name = new Label("medium", item.name);
12 int fw, fh;
13 _name->get_size(fw, fh);
14
15 int bw, bh;
16 _b_plus = new Button("medium", "+");
17 _b_plus->get_size(bw, bh);
18 _b_minus = new Button("medium", "-");
19
20 int h = math::max(bh, fh);
21 ybase = h / 2;
22 int yfont = h / 2 - fh / 2;
23
24 add(0, yfont, _name);
25 add(w / 2, yfont, _price = new Label("medium", mrt::format_string("%d", item.price).c_str()));
26
27 int x_am = 3 * w / 4;
28 add(x_am, yfont, _amount = new Label("medium", "0"));
29
30 xbase = 7 * w / 16;
31 dir_speed = item.dir_speed;
32
33 add(x_am - 112 + bw, h / 2 - bh / 2, _b_minus);
34 add(x_am + 32, h / 2 - bh / 2, _b_plus);
35
36 revalidate(campaign, item, false);
37 }
38
revalidate(const Campaign & campaign,const Campaign::ShopItem & item,const bool active)39 void ShopItem::revalidate(const Campaign &campaign, const Campaign::ShopItem &item, const bool active) {
40 _active = active;
41
42 _b_plus->hide(!active);
43 _b_minus->hide(!active);
44
45 int cash = campaign.getCash();
46 std::string font = item.price > cash?"medium_dark":"medium";
47 _name->setFont(font);
48 _price->setFont(font);
49 _amount->setFont(font);
50 _amount->set(mrt::format_string("%d", item.amount));
51
52 if (!item.object.empty() && !item.animation.empty() && !item.pose.empty()) {
53 _animation = ResourceManager.get_const()->getAnimation(item.animation);
54 _surface = ResourceManager->load_surface(_animation->surface);
55 _animation_model = ResourceManager->get_animation_model(_animation->model);
56 _pose = _animation_model->getPose(item.pose);
57 } else {
58 _animation = NULL;
59 _animation_model = NULL;
60 _pose = NULL;
61 }
62 }
63
render(sdlx::Surface & surface,const int x,const int y) const64 void ShopItem::render(sdlx::Surface &surface, const int x, const int y) const {
65 Container::render(surface, x, y);
66 if (_pose == NULL || _animation == NULL || _animation_model == NULL)
67 return;
68 int frame = ((int)(t * _pose->speed)) % _pose->frames.size();
69
70 int dirs = (_surface->get_width() - 1) / _animation->tw + 1;
71 int dir = ((int)(dir_t * dir_speed)) % dirs;
72
73 sdlx::Rect from(dir * _animation->tw, _pose->frames[frame] * _animation->th, _animation->tw, _animation->th);
74 surface.blit(*_surface, from, x + xbase - _animation->tw / 2, y + ybase - _animation->th / 2);
75 }
76
tick(const float dt)77 void ShopItem::tick(const float dt) {
78 Container::tick(dt);
79 if (_b_plus->changed()) {
80 _b_plus->reset();
81 sold = false;
82 invalidate(true);
83 }
84
85 if (_b_minus->changed()) {
86 _b_minus->reset();
87 sold = true;
88 invalidate(true);
89 }
90
91 if (_pose == NULL || _animation == NULL || _surface == NULL || !_active)
92 return;
93 t += dt;
94 dir_t += dt;
95 //LOG_DEBUG(("t = %g", t));
96 if ((t * _pose->speed) > (int)_pose->frames.size())
97 t -= (float)_pose->frames.size() / _pose->speed;
98
99 int dirs = (_surface->get_width() - 1) / _animation->tw + 1;
100 if ((dir_t * dir_speed) > dirs)
101 dir_t -= (float)dirs / dir_speed;
102 }
103