voleur/main.c

227 lines
5.2 KiB
C
Raw Normal View History

2024-03-20 10:03:13 +01:00
#include <stdio.h>
2024-03-20 11:48:17 +01:00
#include <math.h>
2024-03-20 10:03:13 +01:00
2024-03-19 19:56:47 +01:00
#include "raylib.h"
#define MIN(a, b) (a) < (b) ? (a) : (b)
#define MAX(a, b) (a) > (b) ? (a) : (b)
2024-03-20 10:03:13 +01:00
typedef struct Vec2i {
2024-03-19 19:56:47 +01:00
union {
int x;
int width;
};
union {
int y;
int height;
};
} Vec2i;
2024-03-20 10:03:13 +01:00
typedef enum Cardinal {
CARDINAL_BEGIN_NOT_AT_ZERO = 0,
NORD_OUEST, NORD, NORD_EST,
OUEST, EST,
SUD_OUEST, SUD, SUD_EST,
CARDINAL_SIZE,
} Cardinal;
#ifndef RELEASE
char *cardinal_text[CARDINAL_SIZE] = {
[NORD_OUEST] = "NORD OUEST",
[NORD] = "NORD",
[NORD_EST] = "NORD EST",
[OUEST] = "OUEST",
[EST] = "EST",
[SUD_OUEST] = "SUD OUEST",
[SUD] = "SUD",
[SUD_EST] = "SUD EST",
};
#endif
2024-03-19 19:56:47 +01:00
Vec2i screen = {.width = 1344, .height = 756};
2024-03-20 12:10:05 +01:00
const int trap_len = 300;
2024-03-19 19:56:47 +01:00
Rectangle trap = {
.width = trap_len,
.height = trap_len,
};
2024-03-20 12:21:21 +01:00
Texture player_t;
2024-03-19 19:56:47 +01:00
Vec2i player;
const int player_radius = 25;
const int player_speed = 5;
2024-03-20 11:57:31 +01:00
Texture map;
Vector2 map_coord;
2024-03-20 12:10:05 +01:00
const float map_factor = 2.0f;
2024-03-20 11:57:31 +01:00
2024-03-19 19:56:47 +01:00
void handle_resize_window(void)
{
Vec2i old_screen = screen;
screen.width = GetScreenWidth();
screen.height = GetScreenHeight();
trap.x = screen.width/2 - trap_len/2;
trap.y = screen.height/2 - trap_len/2;
player.x -= (old_screen.width - screen.width) / 2;
player.y -= (old_screen.height - screen.height) / 2;
2024-03-20 11:57:31 +01:00
map_coord.x -= (old_screen.width - screen.width) / 2;
map_coord.y -= (old_screen.height - screen.height) / 2;
2024-03-19 19:56:47 +01:00
}
int point_rec_collision(Vec2i point, Rectangle rec)
{
return
(point.x >= rec.x && point.x <= rec.x + rec.width) &&
(point.y >= rec.y && point.y <= rec.y + rec.height);
}
2024-03-20 10:03:13 +01:00
int move_player_inside_trap(Vec2i *player, Rectangle trap,
const int speed, const float delta_time)
2024-03-19 19:56:47 +01:00
{
int step = speed * delta_time * 100;
2024-03-20 11:48:17 +01:00
int w = IsKeyDown(KEY_W);
int a = IsKeyDown(KEY_A);
int s = IsKeyDown(KEY_S);
int d = IsKeyDown(KEY_D);
2024-03-19 19:56:47 +01:00
2024-03-20 11:48:17 +01:00
if (w + a + s + d >= 2)
step *= sin(45);
player->x -= step * a;
player->x += step * d;
player->y -= step * w;
player->y += step * s;
2024-03-19 19:56:47 +01:00
return point_rec_collision(*player, trap);
}
2024-03-20 10:03:13 +01:00
Cardinal snap_player_inside_trap(Vec2i *player, const Rectangle trap)
2024-03-19 19:56:47 +01:00
{
2024-03-20 10:03:13 +01:00
Cardinal pushing = 0;
int bound_n = player->y < trap.y;
int bound_s = player->y > trap.y + trap.height;
int bound_e = player->x > trap.x + trap.width;
int bound_o = player->x < trap.x;
int bound_ne = bound_n && bound_e;
int bound_no = bound_n && bound_o;
int bound_se = bound_s && bound_e;
int bound_so = bound_s && bound_o;
if (bound_n) {
player->y = trap.y;
pushing = NORD;
} else if (bound_s) {
player->y = trap.y + trap.height;
pushing = SUD;
}
if (bound_e) {
player->x = trap.x + trap.width;
pushing = EST;
} else if (bound_o) {
player->x = trap.x;
pushing = OUEST;
}
if (bound_ne)
pushing = NORD_EST;
else if (bound_no)
pushing = NORD_OUEST;
else if (bound_se)
pushing = SUD_EST;
else if (bound_so)
pushing = SUD_OUEST;
return pushing;
2024-03-19 19:56:47 +01:00
}
2024-03-20 11:57:31 +01:00
void move_map(Vector2 *map, Cardinal direction, const int speed, const float DT)
{
float step = speed * DT * 100;
float diag_step = sin(45) * step;
switch (direction) {
case NORD:
map->y += step;
break;
case SUD:
map->y -= step;
break;
case EST:
map->x -= step;
break;
case OUEST:
map->x += step;
break;
case NORD_EST:
map->y += diag_step;
map->x -= diag_step;
break;
case NORD_OUEST:
map->y += diag_step;
map->x += diag_step;
break;
case SUD_EST:
map->y -= diag_step;
map->x -= diag_step;
break;
case SUD_OUEST:
map->y -= diag_step;
map->x += diag_step;
break;
2024-03-20 12:21:21 +01:00
default:
break;
2024-03-20 11:57:31 +01:00
}
}
2024-03-19 19:56:47 +01:00
int main(void)
{
SetTraceLogLevel(LOG_ERROR);
InitWindow(screen.width, screen.height, "voleur");
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
player = (Vec2i) {.x = screen.width/2, .y = screen.height/2};
2024-03-20 12:21:21 +01:00
player_t = LoadTexture("data/player.png");
2024-03-20 12:10:05 +01:00
map = LoadTexture("data/map2.png");
2024-03-20 11:57:31 +01:00
map_coord = (Vector2){
screen.width/2 - map.width*map_factor/2,
screen.height/2 - map.height*map_factor/2,
};
handle_resize_window();
2024-03-19 19:56:47 +01:00
while (!WindowShouldClose()) {
BeginDrawing();
{
ClearBackground(BLACK);
2024-03-20 10:03:13 +01:00
const float DT = GetFrameTime();
2024-03-19 19:56:47 +01:00
if (IsWindowResized())
handle_resize_window();
2024-03-20 10:03:13 +01:00
if (!move_player_inside_trap(&player, trap, player_speed, DT)) {
Cardinal direction = snap_player_inside_trap(&player, trap);
2024-03-20 11:57:31 +01:00
move_map(&map_coord, direction, player_speed, DT);
2024-03-19 19:56:47 +01:00
}
2024-03-20 11:57:31 +01:00
DrawTextureEx(map, map_coord, 0.0f, map_factor, WHITE);
2024-03-20 12:21:21 +01:00
DrawTexture(
player_t,
player.x-player_t.width/2,
player.y-player_t.height/2,
WHITE
);
2024-03-20 11:57:31 +01:00
// DrawRectangleLinesEx(trap, 1, RED);
2024-03-19 19:56:47 +01:00
}
EndDrawing();
}
CloseWindow();
return 0;
}