File: shop.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (136 lines) | stat: -rw-r--r-- 2,953 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "shop.h"
#include "config.h"
#include "box.h"
#include "campaign.h"
#include "shop_item.h"
#include "scroll_list.h"

Shop::Shop(const int w, const int h)  {
	Box * b = new Box("menu/background_box.png", w - 32, h - 32);
	int mx, my, bw, bh;
	b->getMargins(mx, my);
	b->get_size(bw, bh);
	int xbase = (w - bw) / 2, ybase = (h - bh) / 2;
	add(xbase, ybase, b);
	
	int sw, sh;
	_wares = new ScrollList("menu/background_box.png", "medium", w - 4 * mx, h - 4 * my, 20);
	_wares->initBG("menu/background_box.png", w - 4 * mx, h - 4 * my, 36);
	_wares->get_size(sw, sh);
	add(xbase + mx, ybase + my, _wares);
}

void Shop::init(Campaign *campaign) {
	_campaign = campaign;
	if (_campaign == NULL)
		return;
	
	std::string profile;
	Config->get("engine.profile", profile, std::string());
	if (profile.empty())
		throw_ex(("empty profile"));

	_prefix = "campaign." + profile + "." + campaign->name + ".";
	LOG_DEBUG(("selecting campaign %s, cash: %d", campaign->name.c_str(), campaign->getCash()));

	int w, h;
	get_size(w, h);

	_wares->clear();
	for(size_t i = 0; i < campaign->wares.size(); ++i) {
		_wares->append(new ShopItem(*campaign, campaign->wares[i], w));
	}
}

void Shop::revalidate() {
	if (_campaign == NULL)
		return;
	
	size_t n = _campaign->wares.size();
	assert((int)n == _wares->size());
	size_t c = _wares->get();
	for(size_t i = 0; i < n; ++i) {
		Control *ctrl = _wares->getItem(i);
		ShopItem *s = dynamic_cast<ShopItem *>(ctrl);
		if (s != NULL) {
			s->revalidate(*_campaign, _campaign->wares[i], i == c);
		}
	}
}

void Shop::tick(const float dt) {
	bool do_revalidate = false;
	Container::tick(dt);

	int i = _wares->get();
	
	if (_campaign != NULL && i < (int)_campaign->wares.size()) {
		Campaign::ShopItem &item = _campaign->wares[i];
				
		size_t n = _campaign->wares.size();
		assert((int)n == _wares->size());
		for(size_t i = 0; i < n; ++i) {
			Control *ctrl = _wares->getItem(i);
			ShopItem *s = dynamic_cast<ShopItem *>(ctrl);
			if (s == NULL || !s->changed())
				continue;

			s->reset();
		
			if (s->wasSold()) 
				_campaign->sell(item);
			else
				_campaign->buy(item);
			do_revalidate = true;
		}
	}
	
	if (do_revalidate || _wares->changed()) {
		_wares->reset();
		revalidate();
	}
}

bool Shop::onKey(const SDL_keysym sym) {
	if (Container::onKey(sym))
		return true;

	bool buy = true;

	switch(sym.sym) {
	case SDLK_MINUS:
	case SDLK_UNDERSCORE:
	case SDLK_KP_MINUS:
		buy = false;
	
	case SDLK_SPACE: 
	case SDLK_LCTRL: 
	case SDLK_KP_ENTER:
	case SDLK_RETURN: 
	case SDLK_KP_PLUS:
	case SDLK_PLUS:
	case SDLK_EQUALS:
		{
			if (_campaign == NULL)
				return true;
		
			int i = _wares->get();
			if (i >= (int)_campaign->wares.size()) 
				return true;
			Campaign::ShopItem &item = _campaign->wares[i];
			if (buy) 
				_campaign->buy(item);
			else
				_campaign->sell(item);
				
			revalidate();
		} return true;
	
	case SDLK_ESCAPE: 
		hide();
		return true;
	
	default: 
		return true;
	}
}