interference/src/main.c

226 lines
8.7 KiB
C

#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/std/string.h>
#include <stdlib.h>
#include "input.h"
#include "main.h"
#include "player.h"
int
main(void)
{
extern bopti_image_t img_tileset;
Level level = {
.width = 50,
.height = 28,
};
/* dynamic level size */
level.data =
(tile_t *)malloc(level.width * level.height * sizeof(tile_t));
tile_t data[] = {
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 50, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 49, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 14, 14, 14, 14, 26, 26, 26,
26, 26, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 25, 26, 26, 26, 26, 26, 40, 41, 64, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 39, 40, 14, 18, 65, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 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14, 14, 14, 14, 14, 14, 14, 14, 18, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 13,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 18, 13, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 18, 13, 14,
14, 14, 14, 14, 14, 14};
memcpy(level.data, data, sizeof(data));
input_init();
Player player = {.pos = {50, 50}, .spawn = {0, 0}, .spd = {0, 0}};
Vec2 camera = {0, 0};
int running = 1;
int jumping = 1;
int airbreak = 1;
int coyote = 0;
while (running) {
/* camera thing */
dclear(C_WHITE);
camera.x = player.pos.x - 190;
if (camera.x < 2) {
camera.x = 2;
}
if (camera.x > level.width * TILE_S - 398) {
camera.x = level.width * TILE_S - 398;
}
camera.y = player.pos.y - 104;
if (camera.y < 0) {
camera.y = 0;
}
if (camera.y > level.height * TILE_S - 224) {
camera.y = level.height * TILE_S - 224;
}
/* tiles */
for (int m = 0; m < level.height; ++m) {
for (int n = 0; n < level.width; ++n) {
tile_t tile = level.data[n + m * level.width];
dsubimage(n * TILE_S - camera.x,
m * TILE_S - camera.y, &img_tileset,
((tile - 1) % TILESET_W) * TILE_S,
((tile - 1) / TILESET_W) * TILE_S,
TILE_S, TILE_S, 0);
/* dprint(n * TILE_S - camera.x, m * TILE_S -
camera.y, C_BLACK, "%d", tile); */
}
}
drect(player.pos.x - camera.x, player.pos.y - camera.y,
player.pos.x + PLAYER_SIZE - camera.x - 1,
player.pos.y + PLAYER_SIZE - camera.y - 1, C_BLACK);
dprint(10, 10, C_BLACK, "%d", jumping);
/* input */
input_step();
Vec2 mov = {
.x = input_down(K_RIGHT) - input_down(K_LEFT),
.y = 0,
};
/* on the ground */
if (collide((Vec2){player.pos.x, player.pos.y + 1}, 0, 1,
level)) {
jumping = 0;
airbreak = 0;
coyote = COYOTE_TIME;
} else {
--coyote;
}
/* JUMP */
if (input_pressed(K_A) && coyote > 0) {
player.spd.y = -JUMP_H;
jumping = 2;
coyote = 0;
airbreak = 0;
}
/* airbreak */
if (!airbreak && (!input_down(K_A) || player.spd.y > 0)) {
airbreak = 1;
}
/* moving */
player.spd.x *= (1 - BASE_FRIC); /* friction */
player.spd.x += mov.x * PLAYER_ACC; /* idem */
player.spd.y += GRAVITY + (GRAVITY * airbreak); /* gravity */
if (player.spd.x > -0.2 && player.spd.x < 0.2) {
player.spd.x = 0;
}
if (player.spd.y > -0.2 && player.spd.y < 0.2) {
player.spd.y = 0;
}
Res_Mov position = move(player, level); /* i'm crying rn */
player = position.player;
/* if the player collide with a wall */
if (position.moved.x) {
player.spd.x = 0;
}
if (position.moved.y) {
player.spd.y = 0;
}
/* exit */
if (input_pressed(K_EXIT)) {
break;
}
dupdate();
}
return 1;
}