minigun player

This commit is contained in:
nemo 2024-10-08 10:41:21 +02:00
parent 4ae6c3fdfb
commit 57eba3c089
10 changed files with 78 additions and 4 deletions

View File

@ -1,4 +1,4 @@
SRC=main.c ninja.c keys.c bullet.c
SRC=main.c ninja.c keys.c bullet.c minigun.c
EXE=voleur
CC=gcc

View File

@ -20,6 +20,7 @@ Texture textures[BULLET_COUNT];
void bullet_load()
{
textures[BULLET_SHURIKEN] = LoadTexture("data/shuriken.png");
textures[BULLET_MINIGUN] = LoadTexture("data/bullet.png");
}
void bullet_spawn(BulletType type, Vector2 pos, float angle)
@ -47,6 +48,12 @@ void draw_bullets()
Vector2 toward = Vector2Normalize((Vector2) {cos(angle), sin(angle)});
bullets[i].pos = Vector2Add(bullets[i].pos, Vector2Scale(toward, speed));
bullets[i].time -= GetFrameTime();
DrawTexture(textures[bullets[i].type], bullets[i].pos.x, bullets[i].pos.y, WHITE);
Texture t = textures[bullets[i].type];
Rectangle source = {0, 0, t.width, t.height};
Rectangle dest = {bullets[i].pos.x, bullets[i].pos.y, t.width, t.height};
Vector2 origin = {t.width/2, t.height/2};
DrawTexturePro(t, source, dest, origin, bullets[i].angle + 90, WHITE);
}
}

View File

@ -3,6 +3,7 @@
typedef enum BulletType {
BULLET_SHURIKEN,
BULLET_MINIGUN,
BULLET_COUNT,
} BulletType;

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 215 B

After

Width:  |  Height:  |  Size: 204 B

BIN
data/minigun.aseprite Normal file

Binary file not shown.

BIN
data/minigun.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 880 B

20
main.c
View File

@ -6,6 +6,13 @@
#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})
@ -70,7 +77,13 @@ int main(void)
change_layout_azerty();
load_ninja();
PlayerType player_type = PLAYER_MINIGUN;
if (player_type == PLAYER_NINJA)
load_ninja();
else if (player_type == PLAYER_MINIGUN)
load_minigun();
bullet_load();
while (!WindowShouldClose()) {
@ -114,7 +127,10 @@ int main(void)
draw_bullets();
}
EndMode2D();
draw_ninja(Vector2Add(SCREEN_MIDDLE, cam_shake), &camera);
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();
}

38
minigun.c Normal file
View File

@ -0,0 +1,38 @@
#include "raylib.h"
#include "raymath.h"
#include "keys.h"
#include "bullet.h"
Texture minigun_texture;
void load_minigun()
{
minigun_texture = LoadTexture("data/minigun.png");
}
float minigun_radius()
{
return fmax(minigun_texture.width, minigun_texture.height)/2;
}
void draw_minigun(Vector2 pos, Camera2D *cam)
{
Texture m = minigun_texture;
Rectangle source = {0, 0, m.width, m.height};
Rectangle dest = {pos.x, pos.y, m.width, m.height};
Vector2 origin = {m.width/2, m.height/2};
float angle = -Vector2LineAngle(pos, GetMousePosition());
angle *= 180/PI;
if (key_down(KC_SHOOT)) {
bullet_spawn(BULLET_MINIGUN, GetScreenToWorld2D(pos, *cam), angle);
int speed = 5;
Vector2 toward = Vector2Normalize((Vector2) {cos(angle * PI/180), sin(angle * PI/180)});
cam->target = Vector2Subtract(GetScreenToWorld2D(pos, *cam), Vector2Scale(toward, speed));
}
DrawTexturePro(m, source, dest, origin, angle + 90, WHITE);
}

12
minigun.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef MINIGUN_H
#define MINIGUN_H
void load_minigun();
float minigun_radius();
void draw_minigun(Vector2 pos, Camera2D *cam);
float minigun_dash_time();
#endif // MINIGUN_H