This commit is contained in:
Bill Rossi 2025-03-01 09:03:41 -05:00
parent 33eb14c24b
commit 61e89820f4
3 changed files with 77 additions and 0 deletions

47
bullet.c Normal file
View File

@ -0,0 +1,47 @@
// Copyright 2025 Bill Rossi
//
// This file is part of Starship Futuretime
//
// Starship Futuretime is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// Starship Futuretime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h>
#include <raylib.h>
#include "bullet.h"
#include "entity.h"
Entity *spawn_bullet(Vector2 spawn_at) {
Entity *e = malloc(sizeof(Entity));
e->name = "Bullet";
e->properties = malloc(sizeof(Entity));
BulletProperties *props = (BulletProperties*)e->properties;
props->position.x = spawn_at.x;
props->position.y = spawn_at.y;
props->position.width = 4;
props->position.height = 10;
props->velocity.x = 0;
props->velocity.y = -10;
e->draw = &draw_bullet;
e->tick = &tick_bullet;
e->free = &free_bullet;
return e;
}
void draw_bullet(Entity *e) {
BulletProperties *props = e->properties;
DrawRectangleRec(props->position, BLACK);
}
void tick_bullet(Entity *e, float dt) {
BulletProperties *props = e->properties;
props->position.y += props->velocity.y;
}
void free_bullet(Entity *e) {
free(e->properties);
free(e);
}

26
bullet.h Normal file
View File

@ -0,0 +1,26 @@
// Copyright 2025 Bill Rossi
//
// This file is part of Starship Futuretime
//
// Starship Futuretime is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// Starship Futuretime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see <https://www.gnu.org/licenses/>.
#ifndef BULLET_H
#define BULLET_H
#include <raylib.h>
#include "entity.h"
typedef struct BulletProperties {
Rectangle position;
Vector2 velocity;
} BulletProperties;
Entity *spawn_bullet(Vector2 spawn_at);
void draw_bullet(Entity *e);
void tick_bullet(Entity *e, float dt);
void free_bullet(Entity *e);
#endif

4
game.c
View File

@ -13,6 +13,7 @@
#include "game.h"
#include "enemy.h"
#include "bullet.h"
#define FOREACH_ENTITY for (size_t i = 0; i < g->entities->count; i++)
#define INIT_ENTITY Entity *e = g->entities->entities[i]
@ -40,6 +41,9 @@ void handle_input(Game *g) {
if (IsKeyPressed(KEY_S)) {
add_entity(g, spawn_enemy());
}
if (IsKeyPressed(KEY_SPACE)) {
add_entity(g, spawn_bullet(g->player->position));
}
}
void run_frame(Game *g) {