voleur/main.c

153 lines
4.0 KiB
C
Raw Normal View History

2024-03-20 10:03:13 +01:00
#include <stdio.h>
2024-09-25 21:06:01 +02:00
#include <math.h>
2024-03-19 19:56:47 +01:00
#include "raylib.h"
2024-09-25 09:50:25 +02:00
#define SCREEN_MIDDLE ((Vector2) {window_size.x/2, window_size.y/2})
2024-08-15 16:39:48 +02:00
typedef struct Player {
float radius;
2024-03-23 12:26:43 +01:00
Vector2 coord;
2024-08-15 16:39:48 +02:00
} Player;
2024-03-20 11:57:31 +01:00
2024-08-15 16:39:48 +02:00
typedef struct Map {
const Texture texture;
const Rectangle box;
} Map;
2024-03-20 11:57:31 +01:00
2024-09-25 21:06:01 +02:00
typedef enum KeyControl {
KC_UP = 0,
KC_DOWN,
KC_LEFT,
KC_RIGHT,
KC_SHOOT,
KC_FREECAM,
KC_CROUCH,
KC_COUNT,
} KeyControl;
void change_layout_azerty(int *keys)
{
keys[KC_UP] = KEY_S;
keys[KC_DOWN] = KEY_W;
keys[KC_LEFT] = KEY_A;
keys[KC_RIGHT] = KEY_D;
}
void change_layout_qwerty(int *keys)
{
keys[KC_UP] = KEY_W;
keys[KC_DOWN] = KEY_S;
keys[KC_LEFT] = KEY_A;
keys[KC_RIGHT] = KEY_D;
}
2024-09-25 09:50:25 +02:00
Camera2D shake(Camera2D cam, int force)
{
cam.offset.x += GetRandomValue(-force/2, force/2);
cam.offset.y += GetRandomValue(-force/2, force/2);
return cam;
}
2024-08-15 16:39:48 +02:00
int main(void)
2024-03-22 16:29:37 +01:00
{
2024-08-15 16:39:48 +02:00
Vector2 window_size = { 800.0f, 600.0f };
2024-03-22 16:29:37 +01:00
2024-08-15 16:39:48 +02:00
Camera2D camera = {
.target = {0},
2024-09-25 09:50:25 +02:00
.offset = SCREEN_MIDDLE,
2024-08-15 16:39:48 +02:00
.rotation = 0.0f,
.zoom = 1.0f,
};
2024-08-15 16:39:48 +02:00
Player player = {
.radius = 25.0f,
};
2024-03-28 11:28:49 +01:00
2024-09-25 10:38:55 +02:00
#ifdef RELEASE
SetTraceLogLevel(LOG_FATAL);
#endif
2024-08-15 16:39:48 +02:00
InitWindow(window_size.x, window_size.y, "voleur");
2024-03-19 19:56:47 +01:00
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
2024-09-25 15:10:42 +02:00
#ifdef RELEASE
SetExitKey(KEY_NULL);
#endif
2024-08-15 16:39:48 +02:00
Map map = {
.texture = LoadTexture("data/map2.png"),
.box = (Rectangle) {
2024-09-25 09:50:25 +02:00
.x = -map.texture.width/2, .y = -map.texture.height/2,
2024-08-15 16:39:48 +02:00
.width = map.texture.width, .height = map.texture.height,
},
2024-03-20 11:57:31 +01:00
};
2024-09-25 21:06:01 +02:00
int keys[KC_COUNT] = {0};
keys[KC_SHOOT] = MOUSE_BUTTON_LEFT;
keys[KC_CROUCH] = KEY_LEFT_SHIFT;
keys[KC_FREECAM] = KEY_F;
change_layout_azerty(keys);
2024-09-25 10:23:51 +02:00
2024-08-15 16:39:48 +02:00
while (!WindowShouldClose()) {
2024-09-25 10:15:27 +02:00
float DT = GetFrameTime();
2024-08-15 16:39:48 +02:00
if (IsWindowResized()) {
window_size = (Vector2) {GetScreenWidth(), GetScreenHeight()};
2024-09-25 09:50:25 +02:00
camera.offset = SCREEN_MIDDLE;
2024-08-15 16:39:48 +02:00
}
2024-03-23 12:04:55 +01:00
2024-09-25 10:15:27 +02:00
float movement_speed = 10.0f * DT * 50;
2024-09-25 21:06:01 +02:00
if ((IsKeyDown(keys[KC_UP]) || IsKeyDown(keys[KC_DOWN])) && (IsKeyDown(keys[KC_LEFT]) || IsKeyDown(keys[KC_RIGHT])))
movement_speed = sqrtf(movement_speed) * 2;
if (IsKeyDown(keys[KC_CROUCH]))
2024-08-15 16:39:48 +02:00
movement_speed /= 2.0f;
2024-09-25 21:06:01 +02:00
if (IsKeyDown(keys[KC_LEFT]))
camera.target.x -= movement_speed ;
if (IsKeyDown(keys[KC_RIGHT]))
camera.target.x += movement_speed ;
if (IsKeyDown(keys[KC_DOWN]))
camera.target.y -= movement_speed ;
if (IsKeyDown(keys[KC_UP]))
camera.target.y += movement_speed ;
2024-08-15 16:39:48 +02:00
2024-09-25 21:06:01 +02:00
if (IsKeyReleased(keys[KC_FREECAM]))
2024-09-25 09:50:25 +02:00
camera.target = player.coord;
2024-09-25 21:06:01 +02:00
if (!IsKeyDown(keys[KC_FREECAM])) {
2024-09-25 09:50:25 +02:00
player.coord = GetScreenToWorld2D(SCREEN_MIDDLE, camera);
2024-08-15 16:39:48 +02:00
2024-09-25 09:50:25 +02:00
if (player.coord.x - player.radius < map.box.x)
camera.target.x = map.box.x + player.radius;
else if (player.coord.x + player.radius > map.box.x + map.box.width)
camera.target.x = map.box.x + map.box.width - player.radius;
2024-08-15 16:39:48 +02:00
2024-09-25 09:50:25 +02:00
if (player.coord.y - player.radius < map.box.y)
camera.target.y = map.box.y + player.radius;
else if (player.coord.y + player.radius > map.box.y + map.box.height)
camera.target.y = map.box.y + map.box.height - player.radius;
player.coord = GetScreenToWorld2D(SCREEN_MIDDLE, camera);
}
Camera2D effect_camera = camera;
2024-09-25 21:06:01 +02:00
if (IsMouseButtonDown(keys[KC_SHOOT]))
2024-09-25 09:50:25 +02:00
effect_camera = shake(camera, 12);
2024-03-19 19:56:47 +01:00
BeginDrawing();
{
2024-03-21 21:06:30 +01:00
ClearBackground(LIME);
2024-03-28 11:28:49 +01:00
2024-09-25 09:50:25 +02:00
BeginMode2D(effect_camera);
2024-08-15 16:39:48 +02:00
{
2024-09-25 09:50:25 +02:00
DrawTexture(map.texture, map.box.x, map.box.y, WHITE);
DrawCircle(player.coord.x, player.coord.y, player.radius, RED);
2024-03-23 12:04:55 +01:00
}
2024-08-15 16:39:48 +02:00
EndMode2D();
2024-03-19 19:56:47 +01:00
}
EndDrawing();
}
CloseWindow();
return 0;
}