diff --git a/.gitignore b/.gitignore index 0112d50..86771fd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/ .wakatime-project voleur voleur.exe +*.zip diff --git a/data/ninja.aseprite b/data/ninja.aseprite new file mode 100644 index 0000000..ace4059 Binary files /dev/null and b/data/ninja.aseprite differ diff --git a/data/ninja.png b/data/ninja.png new file mode 100644 index 0000000..a9446fc Binary files /dev/null and b/data/ninja.png differ diff --git a/main.c b/main.c index 108e0d7..2545a7d 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,13 @@ #include #include #include "raylib.h" +#include "raymath.h" + +#include "ninja.h" #define SCREEN_MIDDLE ((Vector2) {window_size.x/2, window_size.y/2}) -typedef struct Player { - float radius; - Vector2 coord; -} Player; +Vector2 window_size = { 800.0f, 600.0f }; typedef struct Map { const Texture texture; @@ -41,17 +41,31 @@ void change_layout_qwerty(int *keys) keys[KC_RIGHT] = KEY_D; } -Camera2D shake(Camera2D cam, int force) +Vector2 shake(int force) { - cam.offset.x += GetRandomValue(-force/2, force/2); - cam.offset.y += GetRandomValue(-force/2, force/2); - return cam; + 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) { - Vector2 window_size = { 800.0f, 600.0f }; - Camera2D camera = { .target = {0}, .offset = SCREEN_MIDDLE, @@ -59,10 +73,6 @@ int main(void) .zoom = 1.0f, }; - Player player = { - .radius = 25.0f, - }; - #ifdef RELEASE SetTraceLogLevel(LOG_FATAL); #endif @@ -89,6 +99,8 @@ int main(void) keys[KC_FREECAM] = KEY_F; change_layout_azerty(keys); + load_ninja(); + while (!WindowShouldClose()) { float DT = GetFrameTime(); if (IsWindowResized()) { @@ -111,38 +123,24 @@ int main(void) if (IsKeyDown(keys[KC_UP])) camera.target.y += movement_speed ; - if (IsKeyReleased(keys[KC_FREECAM])) - camera.target = player.coord; - if (!IsKeyDown(keys[KC_FREECAM])) { - player.coord = GetScreenToWorld2D(SCREEN_MIDDLE, camera); + camera_bound(&camera, map.box, ninja_radius()); - 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; - - 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; + Vector2 cam_shake = {0}; if (IsMouseButtonDown(keys[KC_SHOOT])) - effect_camera = shake(camera, 12); + cam_shake = shake(12); BeginDrawing(); { ClearBackground(LIME); + Camera2D effect_camera = camera; + effect_camera.offset = Vector2Add(effect_camera.offset, cam_shake); BeginMode2D(effect_camera); { DrawTexture(map.texture, map.box.x, map.box.y, WHITE); - DrawCircle(player.coord.x, player.coord.y, player.radius, RED); } EndMode2D(); + draw_ninja(Vector2Add(SCREEN_MIDDLE, cam_shake)); } EndDrawing(); } diff --git a/ninja.c b/ninja.c new file mode 100644 index 0000000..8173c42 --- /dev/null +++ b/ninja.c @@ -0,0 +1,29 @@ +#include "raylib.h" +#include "raymath.h" +#include "stdio.h" + +Texture ninja_texture; + +void load_ninja() +{ + ninja_texture = LoadTexture("data/ninja.png"); +} + +float ninja_radius() +{ + return fmax(ninja_texture.width, ninja_texture.height)/2; +} + +void draw_ninja(Vector2 pos) +{ + Texture n = ninja_texture; + // DrawTexture(n, pos.x - n.width/2, pos.y - n.height/2, WHITE); + + Rectangle source = {0, 0, n.width, n.height}; + Rectangle dest = {pos.x, pos.y, n.width, n.height}; + Vector2 origin = {n.width/2, n.height/2}; + float angle = -Vector2LineAngle(pos, GetMousePosition()); + angle *= 180/PI; + angle += 90; + DrawTexturePro(n, source, dest, origin, angle, WHITE); +} diff --git a/ninja.h b/ninja.h new file mode 100644 index 0000000..1313bb4 --- /dev/null +++ b/ninja.h @@ -0,0 +1,10 @@ +#ifndef NINJA_H +#define NINJA_H + +void load_ninja(); + +float ninja_radius(); + +void draw_ninja(Vector2 truc); + +#endif // NINJA_H