zkwul/src/main.c

97 lines
2.2 KiB
C
Raw Normal View History

2021-03-07 16:10:33 +01:00
#include <gint/display.h>
#include <gint/keyboard.h>
2021-03-08 16:51:00 +01:00
#include "main.h"
struct Pos search(int x, int level[16][16]) {
// Search for x in a given matrix.
// If x is found, return it coordinates
struct Pos coordinates = {0, 0};
for(int m = 0; m < LEVEL_SIZE; ++m) {
for(int n = 0; n < LEVEL_SIZE; ++n) {
if(level[m][n] == x) {
// idk why m and n are inversed but it works kek
coordinates.x = n;
coordinates.y = m;
return coordinates;
}
}
}
return coordinates;
}
int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
2021-03-08 18:49:51 +01:00
// Check if there's something in (x, y)
if(obj == level[x][y]) {
return 1;
}
else {
return 0;
}
2021-03-08 18:49:51 +01:00
}
int collide(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
2021-03-08 18:49:51 +01:00
// Check if there's something in
// the square (x + 1, y + 1, x + 11, y + 11)
if( collide_pixel(x / TILE_SIZE, y / TILE_SIZE, obj, level) ||
collide_pixel(x / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, obj, level) ||
collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, y / TILE_SIZE, obj, level) ||
collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, obj, level)
2021-03-08 18:49:51 +01:00
) {
return 1;
2021-03-08 16:51:00 +01:00
}
2021-03-08 18:49:51 +01:00
return 0;
2021-03-08 16:51:00 +01:00
}
2021-03-07 16:10:33 +01:00
int main(void)
{
2021-03-07 16:32:24 +01:00
extern bopti_image_t img_player;
2021-03-08 08:16:10 +01:00
extern bopti_image_t img_wall;
2021-03-08 18:49:51 +01:00
extern int level[LEVEL_SIZE][LEVEL_SIZE];
2021-03-07 16:32:24 +01:00
2021-03-07 16:10:33 +01:00
int running = 1;
// player
struct Player player = {0, 0};
struct Pos spawn = {0, 0};
spawn = search(2, level);
player.x = spawn.x * 12;
player.y = spawn.y * 12;
2021-03-07 16:10:33 +01:00
// main loop
while(running) {
2021-03-07 16:32:24 +01:00
dclear(C_BLACK);
2021-03-08 08:16:10 +01:00
2021-03-07 16:10:33 +01:00
// drawing the player
dimage(player.x, player.y, &img_player);
2021-03-08 08:16:10 +01:00
// drawing the level
2021-03-08 18:49:51 +01:00
for(int m = 0; m < LEVEL_SIZE; ++m) {
for(int n = 0; n < LEVEL_SIZE; ++n) {
if(level[m][n] == 1) {
2021-03-08 18:49:51 +01:00
dimage(m * TILE_SIZE, n * TILE_SIZE, &img_wall);
2021-03-08 08:16:10 +01:00
}
}
}
2021-03-07 16:10:33 +01:00
dupdate();
2021-03-08 16:51:00 +01:00
int mov_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT);
int mov_y = keydown(KEY_DOWN) - keydown(KEY_UP);
clearevents();
2021-03-08 16:51:00 +01:00
2021-03-07 16:10:33 +01:00
// trying to move the player >w<
if(!collide(player.x + mov_x, player.y, 1, level)) {
player.x += mov_x;
2021-03-08 16:51:00 +01:00
}
2021-03-07 16:10:33 +01:00
if(!collide(player.x, player.y + mov_y, 1, level)) {
player.y += mov_y;
2021-03-08 16:51:00 +01:00
}
2021-03-08 17:08:05 +01:00
2021-03-07 16:10:33 +01:00
if(keydown(KEY_EXIT)) {running = 0;}
}
return 1;
}