bullet
This commit is contained in:
parent
2f1fc1e4ae
commit
d4e02f01b5
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
|||
SRC=main.c ninja.c keys.c
|
||||
SRC=main.c ninja.c keys.c bullet.c
|
||||
EXE=voleur
|
||||
CC=gcc
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
#include "bullet.h"
|
||||
|
||||
typedef struct Bullet {
|
||||
BulletType type;
|
||||
Vector2 pos;
|
||||
float time;
|
||||
float angle;
|
||||
} Bullet;
|
||||
|
||||
#define BULLET_MAX 500
|
||||
|
||||
Bullet bullets[BULLET_MAX] = {0};
|
||||
|
||||
Texture textures[BULLET_COUNT];
|
||||
|
||||
void bullet_load()
|
||||
{
|
||||
textures[BULLET_SHURIKEN] = LoadTexture("data/shuriken.png");
|
||||
}
|
||||
|
||||
void bullet_spawn(BulletType type, Vector2 pos, float angle)
|
||||
{
|
||||
for (int i = 0; i < BULLET_MAX; i++) {
|
||||
if (bullets[i].time >= EPSILON)
|
||||
continue;
|
||||
bullets[i].time = 5;
|
||||
bullets[i].angle = angle;
|
||||
bullets[i].pos.x = pos.x - textures[type].width/2;
|
||||
bullets[i].pos.y = pos.y - textures[type].height/2;
|
||||
bullets[i].type = type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void draw_bullets(Camera2D *cam)
|
||||
{
|
||||
for (int i = 0; i < BULLET_MAX; i++) {
|
||||
if (bullets[i].time <= EPSILON)
|
||||
continue;
|
||||
|
||||
float angle = bullets[i].angle * PI/180;
|
||||
Vector2 toward = Vector2Normalize((Vector2) {cos(angle), sin(angle)});
|
||||
bullets[i].pos = Vector2Add(bullets[i].pos, Vector2Scale(toward, 50));
|
||||
bullets[i].time -= GetFrameTime();
|
||||
DrawTexture(textures[bullets[i].type], bullets[i].pos.x, bullets[i].pos.y, WHITE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef BULLET_H
|
||||
#define BULLET_H
|
||||
|
||||
typedef enum BulletType {
|
||||
BULLET_SHURIKEN,
|
||||
BULLET_COUNT,
|
||||
} BulletType;
|
||||
|
||||
void bullet_load();
|
||||
|
||||
void bullet_spawn(BulletType type, Vector2 pos, float angle);
|
||||
|
||||
void draw_bullets(Camera2D *cam);
|
||||
|
||||
#endif // BULLET_H
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 400 B |
Binary file not shown.
After Width: | Height: | Size: 400 B |
3
main.c
3
main.c
|
@ -3,6 +3,7 @@
|
|||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
#include "bullet.h"
|
||||
#include "keys.h"
|
||||
#include "ninja.h"
|
||||
|
||||
|
@ -70,6 +71,7 @@ int main(void)
|
|||
change_layout_azerty();
|
||||
|
||||
load_ninja();
|
||||
bullet_load();
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
float DT = GetFrameTime();
|
||||
|
@ -109,6 +111,7 @@ int main(void)
|
|||
BeginMode2D(effect_camera);
|
||||
{
|
||||
DrawTexture(map.texture, map.box.x, map.box.y, WHITE);
|
||||
draw_bullets(&camera);
|
||||
}
|
||||
EndMode2D();
|
||||
draw_ninja(Vector2Add(SCREEN_MIDDLE, cam_shake), &camera);
|
||||
|
|
5
ninja.c
5
ninja.c
|
@ -3,6 +3,7 @@
|
|||
#include "stdio.h"
|
||||
|
||||
#include "keys.h"
|
||||
#include "bullet.h"
|
||||
|
||||
Texture ninja_texture;
|
||||
Texture ninja_attack_texture;
|
||||
|
@ -91,6 +92,10 @@ void draw_ninja(Vector2 pos, Camera2D *cam)
|
|||
float angle = -Vector2LineAngle(pos, GetMousePosition());
|
||||
angle *= 180/PI;
|
||||
angle += 90;
|
||||
|
||||
if (key_pressed(KC_SPECIAL))
|
||||
bullet_spawn(BULLET_SHURIKEN, GetScreenToWorld2D(pos, *cam), angle - 90);
|
||||
|
||||
DrawTexturePro(n, source, dest, origin, angle, WHITE);
|
||||
|
||||
// hit_hitbox(pos, angle);
|
||||
|
|
Loading…
Reference in New Issue