voleur/main.c

148 lines
3.9 KiB
C

#include <stdio.h>
#include <math.h>
#include "raylib.h"
#include "raymath.h"
#include "bullet.h"
#include "keys.h"
#include "ninja.h"
#include "minigun.h"
typedef enum PlayerType {
PLAYER_NINJA,
PLAYER_MINIGUN,
PLAYER_COUNT,
} PlayerType;
#define SCREEN_MIDDLE ((Vector2) {window_size.x/2, window_size.y/2})
Vector2 window_size = { 800.0f, 600.0f };
typedef struct Map {
const Texture texture;
const Rectangle box;
} Map;
Vector2 shake(int force)
{
Vector2 s;
s.x = GetRandomValue(-force/2, force/2);
s.y = GetRandomValue(-force/2, force/2);
return s;
}
void camera_bound(Camera2D *cam, Rectangle bound, float player_radius)
{
Vector2 coord = GetScreenToWorld2D(SCREEN_MIDDLE, *cam);
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)
{
Camera2D camera = {
.target = {0},
.offset = SCREEN_MIDDLE,
.rotation = 0.0f,
.zoom = 1.0f,
};
#ifdef RELEASE
SetTraceLogLevel(LOG_FATAL);
#endif
InitWindow(window_size.x, window_size.y, "voleur");
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
#ifdef RELEASE
SetExitKey(KEY_NULL);
#endif
Map map = {
.texture = LoadTexture("data/map2.png"),
.box = (Rectangle) {
.x = -map.texture.width/2, .y = -map.texture.height/2,
.width = map.texture.width, .height = map.texture.height,
},
};
change_layout_azerty();
PlayerType player_type = PLAYER_MINIGUN;
load_ninja();
load_minigun();
bullet_load();
while (!WindowShouldClose()) {
float DT = GetFrameTime();
if (IsWindowResized()) {
window_size = (Vector2) {GetScreenWidth(), GetScreenHeight()};
camera.offset = SCREEN_MIDDLE;
}
#ifndef RELEASE
if (IsKeyPressed(KEY_SPACE)) {
player_type = (player_type+1)%PLAYER_COUNT;
}
#endif
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 ;
}
camera_bound(&camera, map.box, ninja_radius());
// NOTE: si on doit faire un screen shake, l'appliquer a cet variable
Vector2 cam_shake = {0};
BeginDrawing();
{
ClearBackground(LIME);
Camera2D effect_camera = camera;
effect_camera.offset = Vector2Add(effect_camera.offset, cam_shake);
if (player_type == PLAYER_MINIGUN)
process_minigun(Vector2Add(SCREEN_MIDDLE, cam_shake), &camera, &effect_camera);
BeginMode2D(effect_camera);
{
DrawTexture(map.texture, map.box.x, map.box.y, WHITE);
draw_bullets();
}
EndMode2D();
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);
}
EndDrawing();
}
CloseWindow();
return 0;
}