Fumofumotris/source/fumotris/fumotris.c

103 lines
2.2 KiB
C
Raw Normal View History

2024-05-07 22:10:15 +00:00
#include "fumotris.h"
2024-05-15 05:44:13 +00:00
#include <stdlib.h>
2024-05-02 22:17:37 +00:00
2024-05-08 12:47:46 +00:00
struct Fumotris {
2024-05-14 04:20:20 +00:00
struct Tetra board;
struct Tetra piece;
2024-05-15 05:44:13 +00:00
2024-05-14 04:20:20 +00:00
bool is_ground;
2024-05-08 12:47:46 +00:00
};
2024-05-15 05:44:13 +00:00
struct TetraTemplate *bag[7] = { &I, &O, &T, &S, &Z, &J, &L };
usize bag_i = 0;
void shuffle()
{
for (usize i = 6; i >= 0; i--) {
usize swap = rand() % i;
struct TetraTemplate *tmp = bag[swap];
bag[swap] = bag[i];
bag[i] = tmp;
}
}
void place_piece(struct Fumotris *fumo)
{
TetraOverlay(&fumo->piece, &fumo->board);
SetTetra(&fumo->piece, bag[bag_i++], 0, 0);
if (bag_i == 7) {
shuffle();
bag_i = 0;
}
}
i16 get_horizontal(struct Controller *ctrl)
{
return (-(i16)ctrl->axes[LEFT].is_down) + ctrl->axes[RIGHT].is_down;
}
2024-05-14 04:20:20 +00:00
void FumotrisStart(struct FumoInstance *inst, struct Fumotris *fumo)
2024-05-02 22:17:37 +00:00
{
2024-05-10 07:38:08 +00:00
ControllerBindMulti(&inst->ctrl, BINDS_N, controls_g, codes_g, types_g);
2024-05-14 04:20:20 +00:00
CreateTetra(&fumo->board, 10, 10);
2024-05-15 05:44:13 +00:00
2024-05-14 04:20:20 +00:00
fumo->is_ground = false;
2024-05-15 05:44:13 +00:00
SetTetra(&fumo->piece, bag[bag_i++], 0, 0);
2024-05-07 22:10:15 +00:00
}
2024-05-07 03:29:10 +00:00
2024-05-14 04:20:20 +00:00
void FumotrisUpdate(struct FumoInstance *inst, struct Fumotris *fumo)
2024-05-07 22:10:15 +00:00
{
2024-05-15 05:44:13 +00:00
TetraMove(&fumo->piece, &fumo->board, get_horizontal(&inst->ctrl), 0);
2024-05-14 04:20:20 +00:00
2024-05-15 05:44:13 +00:00
if (inst->ctrl.axes[SOFT_DROP].is_down) {
2024-05-14 04:20:20 +00:00
TetraMove(&fumo->piece, &fumo->board, 0, 1);
2024-05-15 05:44:13 +00:00
}
2024-05-14 04:20:20 +00:00
if (inst->ctrl.axes[HARD_DROP].is_down) {
while (TetraMove(&fumo->piece, &fumo->board, 0, 1));
2024-05-15 05:44:13 +00:00
place_piece(fumo);
2024-05-14 04:20:20 +00:00
}
2024-05-15 05:44:13 +00:00
}
2024-05-14 04:20:20 +00:00
2024-05-15 05:44:13 +00:00
void FumotrisOnFall(struct FumoInstance *inst, struct Fumotris *fumo)
{
if (!TetraMove(&fumo->piece, &fumo->board, 0, 1)) {
if (!fumo->is_ground)
fumo->is_ground = true;
else
place_piece(fumo);
2024-05-14 04:20:20 +00:00
}
}
void FumotrisDraw(struct FumoInstance *inst, struct Fumotris *fumo)
{
TetraTerminalClear(&fumo->board, &inst->term);
TetraTerminalDraw(&fumo->board, &inst->term);
TetraTerminalDraw(&fumo->piece, &inst->term);
2024-05-02 22:17:37 +00:00
}
int main()
{
2024-05-08 12:47:46 +00:00
struct FumoInstance inst;
CreateFumoInstance(&inst);
2024-05-02 22:17:37 +00:00
2024-05-07 22:10:15 +00:00
struct Fumotris game;
2024-05-15 05:44:13 +00:00
2024-05-08 12:47:46 +00:00
EventAdd(&inst.on_start, FumotrisStart, &game);
EventAdd(&inst.on_update, FumotrisUpdate, &game);
2024-05-15 05:44:13 +00:00
EventAdd(&inst.on_draw, FumotrisDraw, &game);
VectorAdd(FUMOCO_T, &inst.coroutines, );
2024-05-02 22:17:37 +00:00
2024-05-08 12:47:46 +00:00
FumoInstanceRun(&inst);
2024-05-02 22:17:37 +00:00
2024-05-07 22:10:15 +00:00
return 0;
}