Compare commits

...

3 Commits

Author SHA1 Message Date
nemo 7124b9b6f8 ninjaaaaa 2024-09-27 12:40:28 +02:00
nemo 523d2fb9b0 makefile dist clean phony 2024-09-27 11:28:31 +02:00
nemo 42250b568e windows support 2024-09-27 11:02:40 +02:00
7 changed files with 91 additions and 36 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
build/
.wakatime-project
voleur
voleur.exe
*.zip

View File

@ -1,8 +1,24 @@
SRC=main.c ninja.c
EXE=voleur
CC=gcc
all:
gcc -Wall -Wextra -ggdb main.c -o voleur /usr/local/lib/libraylib.a -lm
$(CC) -Wall -Wextra -ggdb $(SRC) -o $(EXE) /usr/local/lib/libraylib.a -lm
release:
gcc -Wall -Wextra -O3 -DRELEASE main.c -o voleur /usr/local/lib/libraylib.a -lm
$(CC) -Wall -Wextra -O3 -DRELEASE $(SRC) -o $(EXE) /usr/local/lib/libraylib.a -lm
win:
x86_64-w64-mingw32-gcc -Wall -Wextra -O3 -DRELEASE $(SRC) -o $(EXE) /usr/local/lib/win-libraylib.a -I /usr/local/include -lm -lgdi32 -lwinmm
dist: release win
zip -r $(EXE)-linux data/ $(EXE)
zip -r $(EXE)-windows data/ $(EXE).exe
run: all
./voleur
clean:
rm -f $(EXE) $(EXE).exe *.zip
.PHONY: all release win dist run clean

BIN
data/ninja.aseprite Normal file

Binary file not shown.

BIN
data/ninja.png Normal file

Binary file not shown.

After

(image error) Size: 674 B

66
main.c
View File

@ -1,13 +1,13 @@
#include <stdio.h>
#include <math.h>
#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();
}

29
ninja.c Normal file
View File

@ -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);
}

10
ninja.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef NINJA_H
#define NINJA_H
void load_ninja();
float ninja_radius();
void draw_ninja(Vector2 truc);
#endif // NINJA_H