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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include "medals.h"
#include "box.h"
#include "game_monitor.h"
#include "resource_manager.h"
#include "campaign.h"
#include "label.h"
#include "tooltip.h"
#include "image.h"
#include <assert.h>
#include "config.h"
#define _USE_MATH_DEFINES
#include <math.h>
#define EFFECT_LEN (0.5f)
Medals::Medals(int w, int h) : _w(w), _h(h), campaign(NULL), active(0), length(0), dir_x(0) {
_modal = true;
add(0, 0, background = new Box("menu/background_box_dark.png", w, h));
title = new Label("big", std::string());
add(0, 0, title);
numbers = new Label("big", "?/?");
add(0, 0, numbers);
int bw, bh;
b_left = new Image(ResourceManager->load_surface("medals/arrow-left.png"));
b_left->get_size(bw, bh);
add(0, h / 2 - bh / 2, b_left);
b_right = new Image(ResourceManager->load_surface("medals/arrow-right.png"));
b_right->get_size(bw, bh);
add(w - bw, h / 2 - bh / 2, b_right);
hint = NULL;
}
void Medals::render(sdlx::Surface &surface, const int x, const int y) const {
Container::render(surface, x, y);
}
void Medals::hide(const bool hide) {
Container::hide(hide);
if (hide) {
if (campaign != NULL) {
LOG_DEBUG(("unloading resources"));
for(size_t i = 0; i < campaign->medals.size(); ++i) {
ResourceManager->unload_surface(campaign->medals[i].tile);
}
for(size_t i = 0; i < tiles.size(); ++i) {
remove(tiles[i]);
}
tiles.clear();
}
return;
}
if (campaign == NULL)
throw_ex(("campaign == NULL"));
tiles.resize(campaign->medals.size());
for(size_t i = 0; i < tiles.size(); ++i) {
tiles[i] = new Image;
tiles[i]->set(ResourceManager->load_surface(campaign->medals[i].tile));
add(0, 0, tiles[i], background);
}
update();
}
void Medals::set(const Campaign * c) {
if (campaign == c)
return;
campaign = c;
update();
}
void Medals::get_medals(const std::string &id, int &now, int &total) const {
now = 0; total = 0;
if (id == "elimination") {
for(size_t i = 0; i < campaign->maps.size(); ++i) {
const Campaign::Map &map = campaign->maps[i];
if (map.no_medals || map.score <= 0)
continue;
++total;
std::string profile;
Config->get("engine.profile", profile, std::string());
if (profile.empty())
throw_ex(("empty profile"));
std::string mname = "campaign." + profile + "." + campaign->name + ".maps." + map.id + ".maximum-score";
if (!Config->has(mname))
continue;
int bs;
Config->get(mname, bs, 0);
if (bs >= map.score)
++now;
}
} else if (id == "speedrun") {
for(size_t i = 0; i < campaign->maps.size(); ++i) {
const Campaign::Map &map = campaign->maps[i];
if (map.no_medals || map.time <= 0)
continue;
++total;
std::string mname = "campaign." + campaign->name + ".maps." + map.id + ".best-time";
if (!Config->has(mname))
continue;
float bt;
Config->get(mname, bt, 3600);
if (bt <= map.time)
++now;
}
} else if (id == "secrets") {
for(size_t i = 0; i < campaign->maps.size(); ++i) {
const Campaign::Map &map = campaign->maps[i];
if (!map.secret)
continue;
++total;
if (campaign->visible(map))
++now;
}
}
}
void Medals::update() {
if (tiles.empty())
return;
assert(campaign != NULL);
int idx = active;
int n = tiles.size();
idx %= n;
if (idx < 0)
idx += n;
const Campaign::Medal &medal = campaign->medals[idx];
title->set("campaign/medals", medal.id);
int iw, ih;
for(int i = 0; i < n; ++i) {
tiles[i]->hide();
}
for(int j = -1; j <= 1; ++j) {
int i = (idx + j + n) % n;
int now, total;
get_medals(campaign->medals[i].id, now, total);
Image * image = tiles[i];
image->hide(false);
image->get_size(iw, ih);
iw /= 2;
sdlx::Rect src(now > 0? 0: iw, 0, iw, ih);
image->set_source(src);
//LOG_DEBUG(("%d: %d", d, _w / 2 + d * _w / 2));
image->set_base(_w / 2 + j * _w / 2 - iw / 2, _h / 2 - ih / 2);
}
int bw, bh;
title->get_size(bw, bh);
title->set_base((_w - bw) / 2, _h / 2 - ih / 2 - bh);
int now, total;
get_medals(medal.id, now, total);
numbers->set(mrt::format_string("%d/%d", now, total));
numbers->get_size(bw, bh);
numbers->set_base((_w - bw) / 2, _h / 2 + ih / 2 - bh);
if (hint != NULL) {
remove(hint);
}
hint = new Tooltip("campaign/medals", medal.id + "-hint", true, 320);
hint->get_size(bw, bh);
add((_w - bw) / 2, _h / 2 + ih / 2 + 32, hint);
invalidate(true);
}
void Medals::tick(const float dt) {
Container::tick(dt);
if (tiles.empty() || length <= 0)
return;
length -= dt;
if (length <= 0) {
length = 0;
dir_x = 0;
update();
return;
}
int n = tiles.size();
int pos_x = (int)(dir_x * sin(M_PI_2 * length / EFFECT_LEN));
for(int j = -2; j <= 2; ++j) {
int i = (active + j + n) % n;
Image * image = tiles[i];
int iw, ih;
image->get_size(iw, ih);
iw /= 2;
int x = (int)pos_x + _w / 2 + j * _w / 2 - iw / 2;
if (x < -iw || x >= _w)
continue;
image->hide(false);
image->set_base(x, _h / 2 - ih / 2);
}
}
void Medals::left() {
if (length > 0)
update();
--active;
dir_x = -_w / 2;
validate();
}
void Medals::right() {
if (length > 0)
update();
++active;
dir_x = _w / 2;
validate();
}
void Medals::validate() {
length = EFFECT_LEN;
if (active < 0)
active += tiles.size();
if (active >= (int)tiles.size())
active -= tiles.size();
}
bool Medals::onKey(const SDL_keysym sym) {
if (Container::onKey(sym))
return true;
switch(sym.sym) {
case SDLK_ESCAPE:
case SDLK_RETURN:
hide();
return true;
case SDLK_RIGHT:
right();
return true;
case SDLK_LEFT:
left();
return true;
default:
return true;
}
}
bool Medals::onMouse(const int button, const bool pressed, const int x, const int y) {
int lx, ly, lw, lh;
b_left->get_base(lx, ly);
b_left->get_size(lw, lh);
sdlx::Rect rleft(lx, ly, lw, lh);
if (rleft.in(x, y)) {
if (!pressed)
left();
return true;
}
int rx, ry, rw, rh;
b_right->get_base(rx, ry);
b_right->get_size(rw, rh);
sdlx::Rect rright(rx, ry, rw, rh);
if (rright.in(x, y)) {
if (!pressed)
right();
return true;
}
return Container::onMouse(button, pressed, x, y);
}
|