Compare commits
10 Commits
270f330b8a
...
eb91450733
Author | SHA1 | Date |
---|---|---|
|
eb91450733 | |
|
630d204b0c | |
|
c129a617b4 | |
|
e61e66f52d | |
|
29f554b88b | |
|
2f80a81ceb | |
|
3c076f85ae | |
|
92be7db967 | |
|
98bad910cf | |
|
3b1e7aa850 |
Binary file not shown.
After Width: | Height: | Size: 215 B |
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
196
main.c
196
main.c
|
@ -3,8 +3,14 @@
|
|||
|
||||
#include "raylib.h"
|
||||
|
||||
#define MIN(a, b) (a) < (b) ? (a) : (b)
|
||||
#define MAX(a, b) (a) > (b) ? (a) : (b)
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define ABS(a) ((a) < 0 ? -(a) : (a))
|
||||
|
||||
#define RAD_TO_DEG(rad) (rad) * (180 / PI)
|
||||
#define DEG_TO_RAD(deg) (deg) / (180 / PI)
|
||||
|
||||
#define NB_MAX_BULLET 5000
|
||||
|
||||
typedef struct Vec2i {
|
||||
union {
|
||||
|
@ -17,6 +23,12 @@ typedef struct Vec2i {
|
|||
};
|
||||
} Vec2i;
|
||||
|
||||
typedef struct Bullet {
|
||||
Vector2 coord;
|
||||
float angle;
|
||||
float lifetime;
|
||||
} Bullet;
|
||||
|
||||
typedef enum Cardinal {
|
||||
CARDINAL_BEGIN_NOT_AT_ZERO = 0,
|
||||
NORD_OUEST, NORD, NORD_EST,
|
||||
|
@ -40,28 +52,31 @@ char *cardinal_text[CARDINAL_SIZE] = {
|
|||
|
||||
Vec2i screen = {.width = 1344, .height = 756};
|
||||
|
||||
const int trap_len = 300;
|
||||
Rectangle trap = {
|
||||
.width = trap_len,
|
||||
.height = trap_len,
|
||||
};
|
||||
Rectangle trap;
|
||||
|
||||
Texture player_t;
|
||||
Vec2i player;
|
||||
const int player_radius = 25;
|
||||
float player_radius;
|
||||
const int player_speed = 5;
|
||||
|
||||
Image map_collision;
|
||||
Texture map;
|
||||
Vector2 map_coord;
|
||||
const float map_factor = 2.0f;
|
||||
|
||||
Texture bullet;
|
||||
Bullet bullets[NB_MAX_BULLET];
|
||||
int bullet_index = 0;
|
||||
|
||||
void handle_resize_window(void)
|
||||
{
|
||||
Vec2i old_screen = screen;
|
||||
screen.width = GetScreenWidth();
|
||||
screen.height = GetScreenHeight();
|
||||
trap.x = screen.width/2 - trap_len/2;
|
||||
trap.y = screen.height/2 - trap_len/2;
|
||||
trap.width = screen.width/5;
|
||||
trap.height = screen.height/5;
|
||||
trap.x = (int) (screen.width/2 - trap.width/2);
|
||||
trap.y = (int) (screen.height/2 - trap.height/2);
|
||||
player.x -= (old_screen.width - screen.width) / 2;
|
||||
player.y -= (old_screen.height - screen.height) / 2;
|
||||
map_coord.x -= (old_screen.width - screen.width) / 2;
|
||||
|
@ -75,6 +90,14 @@ int point_rec_collision(Vec2i point, Rectangle rec)
|
|||
(point.y >= rec.y && point.y <= rec.y + rec.height);
|
||||
}
|
||||
|
||||
Vector2 move_forward_angle(Vector2 origin, float angle, float step)
|
||||
{
|
||||
float rad = DEG_TO_RAD(angle);
|
||||
origin.x += sin(rad) * step;
|
||||
origin.y += cos(rad) * step;
|
||||
return origin;
|
||||
}
|
||||
|
||||
int move_player_inside_trap(Vec2i *player, Rectangle trap,
|
||||
const int speed, const float delta_time)
|
||||
{
|
||||
|
@ -177,6 +200,69 @@ void move_map(Vector2 *map, Cardinal direction, const int speed, const float DT)
|
|||
}
|
||||
}
|
||||
|
||||
float vector_angle(Vector2 base, Vector2 point)
|
||||
{
|
||||
float x = point.x - base.x;
|
||||
float y = point.y - base.y;
|
||||
int pad = 0;
|
||||
if (x < 0 && y <= 0) {
|
||||
pad = 180;
|
||||
} else if (x < 0 && y >= 0) {
|
||||
pad = 180;
|
||||
} else if (x >= 0 && y >= 0) {
|
||||
pad = 360;
|
||||
}
|
||||
return ABS(pad - RAD_TO_DEG(atan(y / x)));
|
||||
}
|
||||
|
||||
Vector2 map_to_screen_coord(Vector2 coord)
|
||||
{
|
||||
Vector2 r = {.x = map_coord.x + coord.x, .y = map_coord.y + coord.y};
|
||||
return r;
|
||||
}
|
||||
|
||||
Vec2i screen_to_map_coord(Vec2i coord)
|
||||
{
|
||||
Vec2i r = {.x = -map_coord.x + coord.x, .y = -map_coord.y + coord.y};
|
||||
return r;
|
||||
}
|
||||
|
||||
Vec2i Vec2i_cast(Vector2 vec)
|
||||
{
|
||||
Vec2i v = {
|
||||
.x = vec.x,
|
||||
.y = vec.y
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector2 Vector2_cast(Vec2i vec)
|
||||
{
|
||||
Vector2 v = {
|
||||
.x = vec.x,
|
||||
.y = vec.y
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
int circle_collision(Image map, Vec2i coord, float radius)
|
||||
{
|
||||
int count = 0;
|
||||
for (
|
||||
int x=MAX(0, MIN((coord.x-radius)/map_factor, map.width-1));
|
||||
x<MAX(0, MIN((coord.x+radius)/map_factor, map.width-1));
|
||||
x++) {
|
||||
for (
|
||||
int y=MAX(0, MIN((coord.y-radius)/map_factor, map.height-1));
|
||||
y<MAX(0, MIN((coord.y+radius)/map_factor, map.height-1));
|
||||
y++) {
|
||||
Color c = *(Color*)(map.data + sizeof(c)*map.width*y + sizeof(c)*x);
|
||||
count += c.r == 255 && c.b == 255 && c.g == 255 && c.a == 255;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
SetTraceLogLevel(LOG_ERROR);
|
||||
|
@ -186,37 +272,105 @@ int main(void)
|
|||
|
||||
player = (Vec2i) {.x = screen.width/2, .y = screen.height/2};
|
||||
player_t = LoadTexture("data/player.png");
|
||||
map = LoadTexture("data/map2.png");
|
||||
player_radius = player_t.width/2;
|
||||
map_collision = LoadImage("data/map_collision.png");
|
||||
map = LoadTexture("data/map_collision.png");
|
||||
ImageFormat(&map_collision, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||
map_coord = (Vector2){
|
||||
screen.width/2 - map.width*map_factor/2,
|
||||
screen.height/2 - map.height*map_factor/2,
|
||||
};
|
||||
|
||||
bullet = LoadTexture("data/bullet.png");
|
||||
|
||||
handle_resize_window();
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
BeginDrawing();
|
||||
{
|
||||
ClearBackground(BLACK);
|
||||
ClearBackground(LIME);
|
||||
const float DT = GetFrameTime();
|
||||
|
||||
if (IsWindowResized())
|
||||
handle_resize_window();
|
||||
|
||||
// map
|
||||
DrawTextureEx(map, map_coord, 0.0f, map_factor, WHITE);
|
||||
|
||||
Vector2 mouse = GetMousePosition();
|
||||
float angle = vector_angle((Vector2){player.x, player.y}, mouse);
|
||||
|
||||
Vec2i old_player = player;
|
||||
Vector2 old_map_coord = map_coord;
|
||||
if (!move_player_inside_trap(&player, trap, player_speed, DT)) {
|
||||
Cardinal direction = snap_player_inside_trap(&player, trap);
|
||||
move_map(&map_coord, direction, player_speed, DT);
|
||||
}
|
||||
|
||||
DrawTextureEx(map, map_coord, 0.0f, map_factor, WHITE);
|
||||
DrawTexture(
|
||||
player_t,
|
||||
player.x-player_t.width/2,
|
||||
player.y-player_t.height/2,
|
||||
WHITE
|
||||
);
|
||||
|
||||
// DrawRectangleLinesEx(trap, 1, RED);
|
||||
Vec2i player_map = screen_to_map_coord(player);
|
||||
|
||||
if (circle_collision(map_collision, player_map, player_radius)) {
|
||||
player = old_player;
|
||||
map_coord = old_map_coord;
|
||||
}
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||
bullets[bullet_index] = (Bullet) {
|
||||
.coord = move_forward_angle(
|
||||
Vector2_cast(player_map),
|
||||
angle + 90, player_radius
|
||||
),
|
||||
.angle = -angle,
|
||||
.lifetime = 5.0f,
|
||||
};
|
||||
|
||||
bullet_index = (bullet_index + 1) % NB_MAX_BULLET;
|
||||
}
|
||||
|
||||
for (int i=0; i<NB_MAX_BULLET; i++) {
|
||||
Bullet *b = &bullets[i];
|
||||
if (b->lifetime > 0.0f) {
|
||||
b->lifetime -= DT;
|
||||
Vector2 co = map_to_screen_coord(b->coord);
|
||||
DrawTexturePro(
|
||||
bullet,
|
||||
(Rectangle) {0, 0, bullet.width, bullet.height},
|
||||
(Rectangle) {co.x, co.y, bullet.width, bullet.height},
|
||||
(Vector2) {bullet.width/2, bullet.height/2},
|
||||
b->angle, WHITE
|
||||
);
|
||||
b->coord = move_forward_angle(b->coord, 90-b->angle, 27.0f);
|
||||
|
||||
int collision = circle_collision(
|
||||
map_collision, Vec2i_cast(b->coord), bullet.height/2
|
||||
);
|
||||
if (collision) {
|
||||
b->lifetime = -1.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
|
||||
DrawLineEx(
|
||||
(Vector2){.x = player.x, .y = player.y},
|
||||
mouse, 5.0f, GOLD
|
||||
);
|
||||
}
|
||||
|
||||
DrawCircleV(Vector2_cast(player), player_radius, BLUE);
|
||||
|
||||
// player
|
||||
DrawTexturePro(
|
||||
player_t,
|
||||
(Rectangle) {
|
||||
0, 0, player_t.width, player_t.height
|
||||
},
|
||||
(Rectangle) {
|
||||
player.x, player.y, player_t.width, player_t.height
|
||||
},
|
||||
(Vector2) {player_t.width/2, player_t.height/2},
|
||||
450 - angle, WHITE
|
||||
);
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue