aboutsummaryrefslogtreecommitdiff
path: root/src/pickup.c
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-06-27 23:18:22 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-06-27 23:18:22 +0100
commit399e039d799186aeacebee60e1c2009a46677c0d (patch)
tree6b0d2dc98f86e7aecea073e40c35cd21ee6314f3 /src/pickup.c
parentcb51a0b3973399c4adfb1406b93cd8a25e2f726c (diff)
downloadgold-mine-run-399e039d799186aeacebee60e1c2009a46677c0d.tar.gz
gold-mine-run-399e039d799186aeacebee60e1c2009a46677c0d.zip
Add pikcups
WIP: only time and bonuses for now.
Diffstat (limited to 'src/pickup.c')
-rw-r--r--src/pickup.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/src/pickup.c b/src/pickup.c
new file mode 100644
index 0000000..6b53cda
--- /dev/null
+++ b/src/pickup.c
@@ -0,0 +1,195 @@
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "vga.h"
+#include "entities.h"
+#include "game.h"
+
+#include "player.h"
+#include "effect.h"
+
+#include "pickup.h"
+
+#define MAX_FRAME 3
+#define MAX_BONUS_FRAME 4
+
+#define MAX_TTL ((uint16_t)800)
+
+typedef enum
+{
+ PICKUP_TIME = 0,
+ PICKUP_BONUS,
+ PICKUP_PICKAXE,
+ PICKUP_GOLD_KEY,
+ PICKUP_SILVER_KEY,
+} PickupType;
+
+static const Rect frames_in[2 * 4] =
+{
+ { 96, 16, 144, 144 },
+ { 128, 16, 144, 144 },
+ { 112, 16, 144, 144 },
+
+ /* not used */
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 }
+};
+
+static const Rect frames[2 * 4] =
+{
+ /* time */
+ { 80, 16, 144, 144 },
+
+ /* pickaxe */
+ { 16, 32, 144, 144 },
+
+ /* gold key */
+ { 0, 0, 144, 144 },
+
+ /* siver key */
+ { 0, 0, 144, 144 },
+
+ /* not used */
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 }
+};
+
+static const Rect frames_bonus0[2 * 4] =
+{
+ { 0, 48, 144, 144 },
+ { 16, 48, 144, 144 },
+ { 32, 48, 144, 144 },
+ { 48, 48, 144, 144 },
+
+ /* not used */
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 }
+};
+
+static const Rect frames_bonus1[2 * 4] =
+{
+ { 0, 64, 144, 144 },
+ { 16, 64, 144, 144 },
+ { 32, 64, 144, 144 },
+ { 48, 64, 144, 144 },
+
+ /* not used */
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 }
+};
+
+static const Rect frames_bonus2[2 * 4] =
+{
+ { 64, 64, 144, 144 },
+ { 80, 64, 144, 144 },
+ { 96, 64, 144, 144 },
+ { 112, 64, 144, 144 },
+
+ /* not used */
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 },
+ { 0, 0, 144, 144 }
+};
+
+static const Rect *frames_bonuses[] =
+{
+ frames_bonus0, frames_bonus1, frames_bonus2
+};
+
+void pickup_time_init(Entity *e)
+{
+ e->frames = (const Rect *)frames_in;
+ e->flags = PICKUP_TIME;
+ e->counter = MAX_TTL * 2 + (rand() % MAX_TTL);
+ e->update = pickup_wait_update;
+}
+
+void pickup_bonus_init(Entity *e)
+{
+ e->frames = (const Rect *)frames_in;
+ e->flags = PICKUP_BONUS;
+ e->counter = MAX_TTL + (rand() % MAX_TTL);
+ e->update = pickup_wait_update;
+}
+
+void pickup_wait_update(Entity *e)
+{
+ if (e->counter-- == 0)
+ e->update = pickup_in_update;
+}
+
+void pickup_in_update(Entity *e)
+{
+ if (e->delay++ == WALK_DELAY - 2)
+ {
+ e->delay = 0;
+ e->frame++;
+ if (e->frame == MAX_FRAME)
+ {
+ switch (e->flags)
+ {
+ case PICKUP_TIME:
+ e->frames = (const Rect *)frames;
+ e->frame = 0;
+ e->counter = 0;
+ e->update = pickup_update;
+ break;
+ case PICKUP_BONUS:
+ e->frames = (const Rect *)frames_bonuses[rand() % 3];
+ e->frame = 0;
+ e->counter = 0;
+ e->update = pickup_update;
+ break;
+ }
+
+ }
+ }
+}
+
+void pickup_update(Entity *e)
+{
+ if (e->counter++ == MAX_TTL)
+ {
+ effect_out_init(e);
+ return;
+ }
+
+ /* the bonuses have an animation */
+ if (e->flags == PICKUP_BONUS)
+ {
+ e->delay++;
+ /* first frame lasts longer */
+ if ((e->frame == 0 && e->delay == WALK_DELAY * 4)
+ || (e->frame != 0 && e->delay == WALK_DELAY - 2))
+ {
+ e->delay = 0;
+ e->frame++;
+ if (e->frame == MAX_BONUS_FRAME)
+ e->frame = 0;
+ }
+ }
+
+ if (player_collision(e))
+ {
+ switch (e->flags)
+ {
+ case PICKUP_TIME:
+ reset_time();
+ break;
+ case PICKUP_BONUS:
+ add_score(250);
+ break;
+ }
+ e->used = 0;
+ }
+}