camera shake + refacto
This commit is contained in:
parent
7488d9e413
commit
b5a3b66030
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
all:
|
||||
gcc -Wall -Wextra main.c -o voleur -lraylib
|
||||
gcc -Wall -Wextra -ggdb main.c -o voleur -lraylib
|
||||
|
||||
release:
|
||||
gcc -Wall -Wextra -O3 -DRELEASE main.c -o voleur -lraylib
|
||||
|
|
56
main.c
56
main.c
|
@ -1,32 +1,37 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define SCREEN_MIDDLE ((Vector2) {window_size.x/2, window_size.y/2})
|
||||
|
||||
typedef struct Player {
|
||||
Vector2 screen;
|
||||
float radius;
|
||||
Vector2 coord;
|
||||
} Player;
|
||||
|
||||
typedef struct Map {
|
||||
const Texture texture;
|
||||
const Vector2 screen;
|
||||
const Rectangle box;
|
||||
} Map;
|
||||
|
||||
Camera2D shake(Camera2D cam, int force)
|
||||
{
|
||||
cam.offset.x += GetRandomValue(-force/2, force/2);
|
||||
cam.offset.y += GetRandomValue(-force/2, force/2);
|
||||
return cam;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Vector2 window_size = { 800.0f, 600.0f };
|
||||
|
||||
Camera2D camera = {
|
||||
.target = {0},
|
||||
.offset = (Vector2) {window_size.x/2, window_size.y/2},
|
||||
.offset = SCREEN_MIDDLE,
|
||||
.rotation = 0.0f,
|
||||
.zoom = 1.0f,
|
||||
};
|
||||
|
||||
Player player = {
|
||||
.screen = (Vector2) {window_size.x/2, window_size.y/2},
|
||||
.radius = 25.0f,
|
||||
};
|
||||
|
||||
|
@ -36,9 +41,8 @@ int main(void)
|
|||
|
||||
Map map = {
|
||||
.texture = LoadTexture("data/map2.png"),
|
||||
.screen = {-map.texture.width/2, -map.texture.height/2},
|
||||
.box = (Rectangle) {
|
||||
.x = map.screen.x, .y = map.screen.y,
|
||||
.x = -map.texture.width/2, .y = -map.texture.height/2,
|
||||
.width = map.texture.width, .height = map.texture.height,
|
||||
},
|
||||
};
|
||||
|
@ -46,8 +50,7 @@ int main(void)
|
|||
while (!WindowShouldClose()) {
|
||||
if (IsWindowResized()) {
|
||||
window_size = (Vector2) {GetScreenWidth(), GetScreenHeight()};
|
||||
camera.offset = (Vector2) {window_size.x/2, window_size.y/2};
|
||||
player.screen = (Vector2) {window_size.x/2, window_size.y/2};
|
||||
camera.offset = SCREEN_MIDDLE;
|
||||
}
|
||||
|
||||
float movement_speed = 10.0f;
|
||||
|
@ -74,29 +77,38 @@ int main(void)
|
|||
if (IsKeyDown(KEY_S))
|
||||
player.radius--;
|
||||
|
||||
player.coord = GetScreenToWorld2D(player.screen, camera);
|
||||
if (IsKeyReleased(KEY_F))
|
||||
camera.target = player.coord;
|
||||
if (!IsKeyDown(KEY_F)) {
|
||||
player.coord = GetScreenToWorld2D(SCREEN_MIDDLE, camera);
|
||||
|
||||
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.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;
|
||||
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;
|
||||
if (IsKeyDown(KEY_SPACE))
|
||||
effect_camera = shake(camera, 12);
|
||||
|
||||
BeginDrawing();
|
||||
{
|
||||
ClearBackground(LIME);
|
||||
|
||||
BeginMode2D(camera);
|
||||
BeginMode2D(effect_camera);
|
||||
{
|
||||
DrawTexture(map.texture, map.screen.x, map.screen.y, WHITE);
|
||||
DrawTexture(map.texture, map.box.x, map.box.y, WHITE);
|
||||
DrawCircle(player.coord.x, player.coord.y, player.radius, RED);
|
||||
}
|
||||
EndMode2D();
|
||||
|
||||
DrawCircle(player.screen.x, player.screen.y, player.radius, RED);
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue