-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathitem_builder.c
More file actions
346 lines (305 loc) · 7.46 KB
/
item_builder.c
File metadata and controls
346 lines (305 loc) · 7.46 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2025 Linus Probert <linus.probert@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SDL3/SDL.h>
#include <stdlib.h>
#include <assert.h>
#include "item_builder.h"
#include "texture.h"
#include "util.h"
#include "gui.h"
#include "mixer.h"
#include "random.h"
#include "texturecache.h"
#include "sprite.h"
#include "sprite_util.h"
#include "map.h"
static ItemBuilder *builder = NULL;
void
item_builder_init(SDL_Renderer *renderer)
{
builder = ec_malloc(sizeof(ItemBuilder));
builder->renderer = renderer;
}
static void
check_builder(void)
{
if (!builder)
fatal("item_builder_init() not run");
}
static void
eat_flesh(Item *item, Player *player)
{
int original_hp = player->stats.hp;
player->stats.hp += (int) item->value;
if (player->stats.hp > player->stats.maxhp)
player->stats.hp = player->stats.maxhp;
mixer_play_effect(EAT);
gui_log("You eat some foul meat and gain %d health",
player->stats.hp - original_hp);
}
static void
drink_health(Item *item, Player *player)
{
player->potion_sips += (int) item->value;
mixer_play_effect(BOTTLE);
gui_log("You collect %u sips of health", (unsigned int) item->value);
}
static void
pickup_dagger(Item *item, Player *player)
{
player->daggers += (Uint32) item->value;
mixer_play_effect(DAGGER_PICKUP);
double count = player->class == ROGUE ? item->value * 2 : item->value;
if (count > 1)
gui_log("You collect %u daggers", (Uint32) count);
else
gui_log("You collect a dagger");
}
static Item *
create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item*, Player*))
{
Item *item;
item = item_create();
Texture *t0 = texturecache_add(path0);
Texture *t1 = NULL;
if (path1)
t1 = texturecache_add(path1);
item->sprite = sprite_create();
sprite_set_texture(item->sprite, t0, 0);
sprite_set_texture(item->sprite, t1, 1);
item->sprite->dim = GAME_DIMENSION;
item->sprite->clip = clip;
item->effect = cb;
return item;
}
static void
pickup_gold(Item *item, Player *player)
{
player->gold += item->value;
mixer_play_effect(COIN);
gui_log("You pick up %s", item->label);
}
Item *
item_builder_build_treasure(Treasure type, double goldAmt)
{
double amt = goldAmt;
char label[50] = "";
SDL_Rect clip = CLIP16(0, 0);
switch (type) {
case COPPER:
m_sprintf(label, 50, "%.0f copper", amt);
amt /= 100;
break;
case SILVER:
m_sprintf(label, 50, "%.0f silver", amt);
clip.x = 48;
amt /= 10;
break;
case GOLD:
m_sprintf(label, 50, "%.0f gold", amt);
clip.y = 16;
break;
case PLATINUM:
m_sprintf(label, 50, "%.0f platinum", amt);
clip.x = 48;
clip.y = 16;
amt *= 10;
break;
default:
break;
}
if (amt > 15 && amt < 30)
clip.x += 16;
else if (amt <= 15)
clip.x += 32;
Item *item = create_item("Items/Money.png", NULL, clip, &pickup_gold);
m_strcpy(item->label, 50, label);
item->value = amt;
return item;
}
static void
pickup_silver_key(Item *item, Player *player)
{
gui_log("You pickup %s", item->label);
mixer_play_effect(KEY_PICKUP);
player->equipment.keys |= LOCK_SILVER;
}
static void
pickup_gold_key(Item *item, Player *player)
{
gui_log("You pickup %s", item->label);
mixer_play_effect(KEY_PICKUP);
player->equipment.keys |= LOCK_GOLD;
}
Item *
item_builder_build_key(unsigned int type)
{
char label[20];
Item *item;
switch (type) {
case 1:
m_sprintf(label, 20, "a silver key");
item = create_item("Extras/Keys.png",
NULL,
CLIP16(0, 0),
&pickup_silver_key);
break;
case 2:
m_sprintf(label, 20, "a gold key");
item = create_item("Extras/Keys.png",
NULL,
CLIP16(16, 0),
&pickup_gold_key);
break;
default:
fatal("Bad keytype provided");
}
m_strcpy(item->label, 20, label);
return item;
}
static void
pickup_bloodlust(Item *item, Player *player)
{
gui_log("You drink a bloodlust potion. Rage pulses through your veins.");
player->effects.effect = POTION_BLOODLUST;
player->effects.damage_multiplier = 4;
sprite_set_color_mod(player->sprite, 255, 25, 45);
}
static void
pickup_frost(Item *item, Player *player)
{
gui_log("You drink a frost potion. Your skin is ice.");
player->effects.effect = POTION_FROST;
player->effects.damage_reduction = 2 * player->stats.lvl;
sprite_set_color_mod(player->sprite, 94, 156, 255);
}
Item *
item_builder_build_potion(PotionEffect effect)
{
Item *item;
assert(effect != POTION_NONE);
switch (effect) {
case POTION_BLOODLUST:
item = create_item("Items/Potion.png",
NULL,
CLIP16(48, 32),
pickup_bloodlust);
break;
case POTION_FROST:
item = create_item("Items/Potion.png",
NULL,
CLIP16(96, 0),
pickup_frost);
break;
case POTION_NONE:
default:
break;
}
return item;
}
static Item *
create_treasure(int current_level)
{
double amt;
unsigned int highest_treasure;
unsigned int value;
amt = (unsigned int) 1 + get_random(5*current_level) % 40;
if (current_level > 9) {
highest_treasure = PLATINUM;
} else if (current_level > 3) {
highest_treasure = GOLD;
} else {
highest_treasure = SILVER;
}
value = get_random(highest_treasure);
return item_builder_build_treasure((Treasure) value, amt);
}
Item *
item_builder_build_item(ItemKey key, int level)
{
static const char *path_flesh = "Items/Flesh.png";
static const char *path_potion = "Items/Potion.png";
static const char *path_short_wep = "Items/ShortWep.png";
check_builder();
Item *item = NULL;
switch (key) {
case TREASURE:
item = create_treasure(level*2);
break;
case FLESH:
item = create_item(path_flesh,
NULL,
CLIP16(get_random(7) * 16, get_random(1) * 16),
&eat_flesh);
item->value = 1 + get_random(level);
break;
case HEALTH:
item = create_item(path_potion,
NULL,
CLIP16(0, 0),
&drink_health);
item->value = 1 + get_random(level);
break;
case DAGGER:
item = create_item(path_short_wep,
NULL,
CLIP16(0, 0),
&pickup_dagger);
item->value = 1;
break;
default:
fatal("in item_builder_build() : Unhandled item key %d", key);
break;
}
if (item->value != 1) {
Sprite *valueSprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf",
8,
C_BLUE,
C_BLACK,
"%g",
item->value);
valueSprite->offset.x = item->sprite->dim.width - valueSprite->dim.width;
valueSprite->offset.y = item->sprite->dim.height - valueSprite->dim.height;
linkedlist_append(&item->subsprites, valueSprite);
}
return item;
}
Item *
item_builder_build_sack(void)
{
return create_item("Items/Chest0.png",
NULL,
CLIP16(0, 32),
NULL);
}
Item *
item_builder_build_container(const char *path0, const char *path1, SDL_Rect clip)
{
Item *chest = create_item(path0,
path1,
clip,
NULL);
chest->openable = true;
chest->sprite->animate = false;
return chest;
}
void
item_builder_close(void)
{
free(builder);
}