117 lines
2.7 KiB
C
117 lines
2.7 KiB
C
#include <gint/display.h>
|
|
#include <gint/keyboard.h>
|
|
#include "main.h"
|
|
|
|
struct TwoValues search(int x, int level[16][16]) {
|
|
// Search for x in a given matrix.
|
|
// If x is found, return it coordinates
|
|
struct TwoValues 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 * 12;
|
|
coordinates.y = m * 12;
|
|
return coordinates;
|
|
}
|
|
}
|
|
}
|
|
return coordinates;
|
|
}
|
|
|
|
int collide_pixel(int x, int y, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
|
|
// Check if there's something in (x, y)
|
|
if(obj == level[x][y]) {
|
|
return 1;
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int collide(int x, int y, int h, int obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
|
|
// Check if there's something in
|
|
// the square (x + 1, y + 1, x + 11, y + 11)
|
|
// The size of the hitbox changes with h
|
|
if( collide_pixel((x + h) / TILE_SIZE, (y + h) / TILE_SIZE, obj, level) ||
|
|
collide_pixel((x + h) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE, obj, level) ||
|
|
collide_pixel((x + TILE_SIZE - h - 1) / TILE_SIZE, (y + h) / TILE_SIZE, obj, level) ||
|
|
collide_pixel((x + TILE_SIZE - h - 1) / TILE_SIZE, (y + TILE_SIZE - h - 1) / TILE_SIZE, obj, level)
|
|
) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct Player level_reset(struct Player player) {
|
|
player.pos = player.spawn;
|
|
return player;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
extern bopti_image_t img_player;
|
|
extern bopti_image_t img_wall;
|
|
extern bopti_image_t img_spike;
|
|
|
|
extern int level[LEVEL_SIZE][LEVEL_SIZE];
|
|
|
|
int running = 1;
|
|
|
|
// player
|
|
struct Player player = {
|
|
.pos = {0, 0},
|
|
.spawn = {0, 0}
|
|
};
|
|
player.spawn = search(2, level);
|
|
player = level_reset(player);
|
|
|
|
// main loop
|
|
while(running) {
|
|
|
|
dclear(C_BLACK);
|
|
|
|
// drawing the level
|
|
for(int m = 0; m < LEVEL_SIZE; ++m) {
|
|
for(int n = 0; n < LEVEL_SIZE; ++n) {
|
|
switch(level[n][m]) {
|
|
case 1:
|
|
// walls
|
|
dimage(m * TILE_SIZE, n * TILE_SIZE, &img_wall);
|
|
break;
|
|
case 3:
|
|
// spikes
|
|
dimage(m * TILE_SIZE, n * TILE_SIZE, &img_spike);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// drawing the player
|
|
dimage(player.pos.x, player.pos.y, &img_player);
|
|
|
|
dupdate();
|
|
int mov_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT);
|
|
int mov_y = keydown(KEY_DOWN) - keydown(KEY_UP);
|
|
clearevents();
|
|
|
|
// trying to move the player >w<
|
|
if(!collide(player.pos.x + mov_x, player.pos.y, 0, 1, level)) {
|
|
player.pos.x += mov_x;
|
|
}
|
|
|
|
if(!collide(player.pos.x, player.pos.y + mov_y, 0, 1, level)) {
|
|
player.pos.y += mov_y;
|
|
}
|
|
|
|
// d i e
|
|
if(collide(player.pos.x + mov_x, player.pos.y + mov_y, 2, 3, level)) {
|
|
player = level_reset(player);
|
|
}
|
|
|
|
if(keydown(KEY_EXIT)) {running = 0;}
|
|
}
|
|
|
|
return 1;
|
|
}
|