This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
forked from joeldipops/just-add-water
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloth.c
456 lines (381 loc) · 15 KB
/
cloth.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include "cloth.h"
#include "config.h"
#include "text.h"
#include "weather.h"
#include "renderer.h"
#include "animation.h"
#include <stdint.h>
#include <stdlib.h>
#include <libdragon.h>
#include <stdio.h>
static void changeClothState(Cloth* cloth, DryingState newState) {
cloth->oldSize = cloth->size;
// There's no coming back from dirty.
if (cloth->dryingState == DRYING_DIRTY) {
return;
}
if (newState <= DRYING_DRY) {
newState = DRYING_DRY;
}
s32 diff = newState - cloth->dryingState;
switch(cloth->growthType) {
case GROWTH_LINEAR:
// -1: Grow under sun, shrink in rain.
cloth->size += diff * cloth->growthFactor * -1;
break;
case GROWTH_QUADRATIC:
// TODO
//cloth->size = cloth->size * diff * cloth->growthFactor;
break;
case GROWTH_NONE:
break;
default: ;
// throw;
}
if (cloth->size < 1) {
cloth->size = 1;
} else if (cloth->size > LINE_SIZE) {
cloth->size = LINE_SIZE;
}
cloth->dryingState = newState;
}
/**
* TODO: This only handles discrete 'size' chunks which is next to useless, but good to have on the board.
* The next step is to nudge those x values along so stuff is actually growing with each frame...
*/
void setClothAnimationFrames(Cloth* cloth, s32 pixelLength, Animation** animations, s32 frameIndex, s32 x, s32 y) {
#define currentFrame() (&animations[animationIndex]->frames[frameIndex])
SpriteCode spriteId;
s32 animationIndex = 0;
float secsPerFrame = 0.05;
setSimpleFrame(currentFrame(), BASE_CLOTH_SPRITE, x, y, secsPerFrame);
currentFrame()->z = 0;
currentFrame()->scaleX = ((float)pixelLength / (float)TILE_WIDTH);
currentFrame()->scaleY = 2;
animationIndex++;
s32 tilesUsed = pixelLength / TILE_WIDTH;
s32 overHang = pixelLength % TILE_WIDTH;
// Draw border.
s32 drawPriority = 1;
if (cloth->dryingState > DRYING_DRY) {
// Left end
setSimpleFrame(currentFrame(), CURSOR_TOP_LEFT_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), CURSOR_BOTTOM_LEFT_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
if (pixelLength > TILE_WIDTH * 2) {
for (s32 i = TILE_WIDTH; i < pixelLength - TILE_WIDTH; i += TILE_WIDTH) {
s32 xPos = x + i;
setSimpleFrame(currentFrame(), CURSOR_TOP_SPRITE, xPos, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), CURSOR_BOTTOM_SPRITE, xPos, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
}
}
// Right end
s32 xPos = x + TILE_WIDTH * (tilesUsed - 1) + overHang;
setSimpleFrame(currentFrame(), CURSOR_TOP_RIGHT_SPRITE, xPos, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), CURSOR_BOTTOM_RIGHT_SPRITE, xPos, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
} else {
// TODO - how to cover the progression to gilding. Probably doing one side at a time so the gilding gradually
// circles the cloth.
// Left end
setSimpleFrame(currentFrame(), GILDED_TOP_LEFT_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), GILDED_BOTTOM_LEFT_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
if (pixelLength > TILE_WIDTH * 2) {
for (s32 i = TILE_WIDTH; i < pixelLength - TILE_WIDTH; i += TILE_WIDTH) {
s32 xPos = x + i;
setSimpleFrame(currentFrame(), GILDED_TOP_SPRITE, xPos, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), GILDED_BOTTOM_SPRITE, xPos, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
}
}
// Right end
s32 xPos = x + TILE_WIDTH * (tilesUsed - 1) + overHang;
setSimpleFrame(currentFrame(), GILDED_TOP_RIGHT_SPRITE, xPos, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), GILDED_BOTTOM_RIGHT_SPRITE, xPos, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
}
drawPriority = 2;
// Draw water gauge.
switch(cloth->dryingState) {
case DRYING_DRENCHED:
setSimpleFrame(currentFrame(), FULL_WATER_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), FULL_WATER_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
break;
case DRYING_SPUN:
setSimpleFrame(currentFrame(), HALF_WATER_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), FULL_WATER_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
break;
case DRYING_MOIST:
setSimpleFrame(currentFrame(), NO_WATER_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), FULL_WATER_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
break;
case DRYING_DAMP:
setSimpleFrame(currentFrame(), NO_WATER_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), HALF_WATER_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
break;
case DRYING_DRY:
case DRYING_COMPLETE:
setSimpleFrame(currentFrame(), NO_WATER_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), NO_WATER_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
break;
case DRYING_DIRTY:
setSimpleFrame(currentFrame(), DIRTY_WATER_SPRITE, x, y, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(currentFrame(), DIRTY_WATER_SPRITE, x, y + TILE_WIDTH, secsPerFrame);
currentFrame()->z = drawPriority;
animationIndex++;
break;
default: break;
}
s32 xPos = x + TILE_WIDTH * (tilesUsed - 1) + overHang;
if (cloth->growthFactor > 0) {
setSimpleFrame(
currentFrame(), BIG_DRY_SPRITE,
xPos, y, secsPerFrame
);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(
currentFrame(), SMALL_WET_SPRITE,
xPos, y + TILE_WIDTH, secsPerFrame
);
currentFrame()->z = drawPriority;
animationIndex++;
} else if (cloth->growthFactor < 0) {
setSimpleFrame(
currentFrame(), BIG_WET_SPRITE,
xPos, y, secsPerFrame
);
currentFrame()->z = drawPriority;
animationIndex++;
setSimpleFrame(
currentFrame(), SMALL_DRY_SPRITE,
xPos, y + TILE_WIDTH, secsPerFrame
);
currentFrame()->z = drawPriority;
animationIndex++;
}
if (cloth->growthFactor) {
setSimpleFrame(
currentFrame(), GROWTH_1_SPRITE + abs(cloth->growthFactor) - 1,
xPos + 6,
y + 6, secsPerFrame
);
currentFrame()->z = drawPriority;
animationIndex++;
}
#undef currentFrame
}
void prepareClothAnimation(Cloth* cloth, s32 x, s32 y) {
s32 minSize;
s32 maxSize;
if (cloth->oldSize > cloth->size) {
minSize = cloth->size;
maxSize = cloth->oldSize;
} else {
maxSize = cloth->size;
minSize = cloth->oldSize;
}
float diff = (maxSize - minSize) * TILE_WIDTH;
// Some arbitrary number, we should actually calculate this from the cloth properties...
s32 spritesNeeded = 10 + (maxSize * 2);
// We will have 16 animation frames.
const float numberOfFrames = 16;
Animation* animations[spritesNeeded];
for (s32 i = 0; i < spritesNeeded; i++) {
animations[i] = newAnimation(numberOfFrames, 1);
animations[i]->numberOfFrames = numberOfFrames;
}
for (s32 i = 0; i < numberOfFrames; i++) {
s32 pixelWidth;
if (cloth->size > cloth->oldSize) {
// Only assumes growth.
pixelWidth = (cloth->oldSize * TILE_WIDTH) + ((diff / numberOfFrames) * i);
} else {
pixelWidth = (cloth->size * TILE_WIDTH) + ((diff / numberOfFrames) * (numberOfFrames - i));
}
setClothAnimationFrames(cloth, pixelWidth, animations, i, x, y);
}
// Ensure all sprites start and end at the same time.
disable_interrupts();
for (s32 i = 0; i < spritesNeeded; i++) {
startAnimation(animations[i]);
}
enable_interrupts();
}
void drawCloth(Cloth* cloth, s32 x, s32 y) {
SpriteCode spriteId;
drawScaledSprite(BASE_CLOTH_SPRITE, x, y, 0, cloth->size, 2);
// Draw border.
s32 drawPriority = 1;
if (cloth->dryingState > DRYING_DRY) {
// Normal border.
drawSprite(CURSOR_TOP_LEFT_SPRITE, x, y, drawPriority, 1);
drawSprite(CURSOR_BOTTOM_LEFT_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
for (s32 i = 0; i < cloth->size; i++) {
s32 xPos = x + TILE_WIDTH * i;
if (i + 1 == cloth->size) {
drawSprite(CURSOR_TOP_RIGHT_SPRITE, xPos, y, drawPriority, 1);
drawSprite(CURSOR_BOTTOM_RIGHT_SPRITE, xPos, y + TILE_WIDTH, drawPriority, 1);
} else {
drawSprite(CURSOR_TOP_SPRITE, xPos, y, drawPriority, 1);
drawSprite(CURSOR_BOTTOM_SPRITE, xPos, y + TILE_WIDTH, drawPriority, 1);
}
}
} else {
// Gilded border to show we're done.
drawSprite(GILDED_TOP_LEFT_SPRITE, x, y, drawPriority, 1);
drawSprite(GILDED_BOTTOM_LEFT_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
for (s32 i = 0; i < cloth->size; i++) {
s32 xPos = x + TILE_WIDTH * i;
if (i + 1 == cloth->size) {
drawSprite(GILDED_TOP_RIGHT_SPRITE, xPos, y, drawPriority, 1);
drawSprite(GILDED_BOTTOM_RIGHT_SPRITE, xPos, y + TILE_WIDTH, drawPriority, 1);
} else {
drawSprite(GILDED_TOP_SPRITE, xPos, y, drawPriority, 1);
drawSprite(GILDED_BOTTOM_SPRITE, xPos, y + TILE_WIDTH, drawPriority, 1);
}
}
}
drawPriority = 2;
// Draw water gauge.
switch(cloth->dryingState) {
case DRYING_DRENCHED:
drawSprite(FULL_WATER_SPRITE, x, y, drawPriority, 1);
drawSprite(FULL_WATER_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
break;
case DRYING_SPUN:
drawSprite(HALF_WATER_SPRITE, x, y, drawPriority, 1);
drawSprite(FULL_WATER_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
break;
case DRYING_MOIST:
drawSprite(NO_WATER_SPRITE, x, y, drawPriority, 1);
drawSprite(FULL_WATER_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
break;
case DRYING_DAMP:
drawSprite(NO_WATER_SPRITE, x, y, drawPriority, 1);
drawSprite(HALF_WATER_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
break;
case DRYING_DRY:
case DRYING_COMPLETE:
drawSprite(NO_WATER_SPRITE, x, y, drawPriority, 1);
drawSprite(NO_WATER_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
break;
case DRYING_DIRTY:
drawSprite(DIRTY_WATER_SPRITE, x, y, drawPriority, 1);
drawSprite(DIRTY_WATER_SPRITE, x, y + TILE_WIDTH, drawPriority, 1);
break;
default: break;
}
if (cloth->growthFactor > 0) {
drawSprite(BIG_DRY_SPRITE, x + TILE_WIDTH * (cloth->size - 1), y, drawPriority, 1);
drawSprite(SMALL_WET_SPRITE, x + TILE_WIDTH * (cloth->size - 1), y + TILE_WIDTH, drawPriority, 1);
} else if (cloth->growthFactor < 0) {
drawSprite(BIG_WET_SPRITE, x + TILE_WIDTH * (cloth->size - 1), y, drawPriority, 1);
drawSprite(SMALL_DRY_SPRITE, x + TILE_WIDTH * (cloth->size - 1), y + TILE_WIDTH, drawPriority, 1);
}
if (cloth->growthFactor) {
drawSprite(GROWTH_1_SPRITE + abs(cloth->growthFactor) - 1, x + TILE_WIDTH * (cloth->size - 1) + 6, y + 6, drawPriority, 1);
}
}
s32 calculateScore(Cloth* cloth) {
s32 result = cloth->size;
// A multipliable bonus if your cloth started of bigger than normal.
result += cloth->initialSize - 1;
// Higher than normal growth factors get lots of points.
if (cloth->growthFactor > 1) {
result *= cloth->growthFactor;
}
// A non-multipliable bonus for a higher than normal initial drying state.
if (cloth->initialDryingState == DRYING_DRENCHED) {
result += 2;
}
// Easier than baseline cloths don't have any specific negative
return result;
}
bool isClothDry(Cloth* cloth) {
return cloth->dryingState <= DRYING_DRY;
}
void updateCloth(Cloth* cloth, Weather weather) {
switch(weather) {
case WEATHER_STORM:
cloth->dryingState = DRYING_DIRTY;
// Stop the animation bouncing around.
cloth->oldSize = cloth->size;
return;
case WEATHER_RAIN:
changeClothState(cloth, DRYING_DRENCHED);
break;
case WEATHER_CLOUDY:
changeClothState(cloth, cloth->dryingState - 1);
break;
case WEATHER_SUNNY:
changeClothState(cloth, cloth->dryingState - 2);
break;
}
}
/**
* Non-allocating constructor.
*/
void initCloth(Cloth* cloth, s32 size, DryingState dryingState) {
cloth->grabPoint = 0;
cloth->size = size;
cloth->initialSize = size;
cloth->oldSize = size;
cloth->dryingState = dryingState;
cloth->initialDryingState = dryingState;
}
/**
* Constructor
*/
Cloth* newCloth(s32 size, DryingState dryingState) {
Cloth* result = calloc(sizeof(Cloth), 1);
result->grabPoint = 0;
result->size = size;
result->initialSize = size;
result->oldSize = size;
result->dryingState = dryingState;
result->initialDryingState = dryingState;
return result;
}