a lil' spawn for the player (wtf it's working?!)

This commit is contained in:
Masséna Fezard | Nounouille 2021-03-13 18:39:09 +01:00
parent 1cc7ebad05
commit 8e048b5d34
4 changed files with 56 additions and 22 deletions

BIN
assets-cg/img/spike.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

View File

@ -3,5 +3,14 @@
#define LEVEL_SIZE 16
#define TILE_SIZE 12
int collide_pixel(int x, int y, int level[16][16]);
int collide(int x, int y, int level[16][16]);
struct Pos {
int x, y;
};
struct Player {
int x, y;
};
struct Pos search(int x, int level[16][16]);
int collide_pixel(int x, int y, int obj, int level[16][16]);
int collide(int x, int y, int obj, int level[16][16]);

View File

@ -1,6 +1,7 @@
const int level[16][16] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
@ -10,9 +11,8 @@ const int level[16][16] = {
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

View File

@ -2,18 +2,40 @@
#include <gint/keyboard.h>
#include "main.h"
int collide_pixel(int x, int y, int level[LEVEL_SIZE][LEVEL_SIZE]) {
// Check if there's something in (x, y)
return level[x][y];
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(int x, int y, int level[LEVEL_SIZE][LEVEL_SIZE]) {
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 obj, int level[LEVEL_SIZE][LEVEL_SIZE]) {
// 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, level) ||
collide_pixel(x / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, level) ||
collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, y / TILE_SIZE, level) ||
collide_pixel((x + TILE_SIZE - 1) / TILE_SIZE, (y + TILE_SIZE - 1) / TILE_SIZE, level)
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)
) {
return 1;
}
@ -29,9 +51,12 @@ int main(void)
int running = 1;
// player (i don't know how to use struct sorry)
int player_x = 50;
int player_y = 50;
// 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;
// main loop
while(running) {
@ -39,12 +64,12 @@ int main(void)
dclear(C_BLACK);
// drawing the player
dimage(player_x, player_y, &img_player);
dimage(player.x, player.y, &img_player);
// drawing the level
for(int m = 0; m < LEVEL_SIZE; ++m) {
for(int n = 0; n < LEVEL_SIZE; ++n) {
if(level[m][n]) {
if(level[m][n] == 1) {
dimage(m * TILE_SIZE, n * TILE_SIZE, &img_wall);
}
}
@ -56,12 +81,12 @@ int main(void)
clearevents();
// trying to move the player >w<
if(!collide(player_x + mov_x, player_y, level)) {
player_x += mov_x;
if(!collide(player.x + mov_x, player.y, 1, level)) {
player.x += mov_x;
}
if(!collide(player_x, player_y + mov_y, level)) {
player_y += mov_y;
if(!collide(player.x, player.y + mov_y, 1, level)) {
player.y += mov_y;
}
if(keydown(KEY_EXIT)) {running = 0;}