voleur/main.c

148 lines
3.9 KiB
C
Raw Permalink 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-27 12:40:28 +02:00
#include "raymath.h"
2024-09-30 14:24:11 +02:00
#include "bullet.h"
2024-09-27 14:03:07 +02:00
#include "keys.h"
2024-09-27 12:40:28 +02:00
#include "ninja.h"
2024-10-08 10:41:21 +02:00
#include "minigun.h"
typedef enum PlayerType {
PLAYER_NINJA,
PLAYER_MINIGUN,
PLAYER_COUNT,
} PlayerType;
2024-03-19 19:56:47 +01:00
2024-09-25 09:50:25 +02:00
#define SCREEN_MIDDLE ((Vector2) {window_size.x/2, window_size.y/2})
2024-09-27 12:40:28 +02:00
Vector2 window_size = { 800.0f, 600.0f };
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-27 12:40:28 +02:00
Vector2 shake(int force)
2024-09-25 09:50:25 +02:00
{
2024-09-27 12:40:28 +02:00
Vector2 s;
s.x = GetRandomValue(-force/2, force/2);
s.y = GetRandomValue(-force/2, force/2);
return s;
2024-09-25 09:50:25 +02:00
}
2024-09-27 12:40:28 +02:00
void camera_bound(Camera2D *cam, Rectangle bound, float player_radius)
2024-03-22 16:29:37 +01:00
{
2024-09-27 12:40:28 +02:00
Vector2 coord = GetScreenToWorld2D(SCREEN_MIDDLE, *cam);
2024-03-22 16:29:37 +01:00
2024-09-27 12:40:28 +02:00
if (coord.x - player_radius < bound.x)
cam->target.x = bound.x + player_radius;
else if (coord.x + player_radius > bound.x + bound.width)
cam->target.x = bound.x + bound.width - player_radius;
if (coord.y - player_radius < bound.y)
cam->target.y = bound.y + player_radius;
else if (coord.y + player_radius > bound.y + bound.height)
cam->target.y = bound.y + bound.height - player_radius;
}
int main(void)
{
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-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-27 14:03:07 +02:00
change_layout_azerty();
2024-09-25 10:23:51 +02:00
2024-10-08 10:41:21 +02:00
PlayerType player_type = PLAYER_MINIGUN;
2024-10-08 10:48:20 +02:00
load_ninja();
load_minigun();
2024-10-08 10:41:21 +02:00
2024-09-30 14:24:11 +02:00
bullet_load();
2024-09-27 12:40:28 +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-10-08 10:48:20 +02:00
#ifndef RELEASE
if (IsKeyPressed(KEY_SPACE)) {
player_type = (player_type+1)%PLAYER_COUNT;
}
#endif
2024-09-27 15:16:05 +02:00
if (ninja_dash_time() <= EPSILON) {
float movement_speed = 10.0f * DT * 50;
if ((key_down(KC_UP) || key_down(KC_DOWN)) && (key_down(KC_LEFT) || key_down(KC_RIGHT)))
movement_speed = sqrtf(movement_speed) * 2;
if (key_down(KC_CROUCH))
movement_speed /= 2.0f;
if (key_down(KC_LEFT))
camera.target.x -= movement_speed ;
if (key_down(KC_RIGHT))
camera.target.x += movement_speed ;
if (key_down(KC_DOWN))
camera.target.y -= movement_speed ;
if (key_down(KC_UP))
camera.target.y += movement_speed ;
}
2024-08-15 16:39:48 +02:00
2024-09-27 12:40:28 +02:00
camera_bound(&camera, map.box, ninja_radius());
2024-09-25 09:50:25 +02:00
2024-09-27 14:03:07 +02:00
// NOTE: si on doit faire un screen shake, l'appliquer a cet variable
2024-09-27 12:40:28 +02:00
Vector2 cam_shake = {0};
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-27 12:40:28 +02:00
Camera2D effect_camera = camera;
effect_camera.offset = Vector2Add(effect_camera.offset, cam_shake);
2024-10-11 11:41:38 +02:00
if (player_type == PLAYER_MINIGUN)
process_minigun(Vector2Add(SCREEN_MIDDLE, cam_shake), &camera, &effect_camera);
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);
2024-09-30 14:43:39 +02:00
draw_bullets();
2024-03-23 12:04:55 +01:00
}
2024-08-15 16:39:48 +02:00
EndMode2D();
2024-10-08 10:41:21 +02:00
if (player_type == PLAYER_NINJA)
draw_ninja(Vector2Add(SCREEN_MIDDLE, cam_shake), &camera);
else if (player_type == PLAYER_MINIGUN)
draw_minigun(Vector2Add(SCREEN_MIDDLE, cam_shake), &camera);
2024-03-19 19:56:47 +01:00
}
EndDrawing();
}
CloseWindow();
2024-09-27 14:03:07 +02:00
2024-03-19 19:56:47 +01:00
return 0;
}