Compare commits
	
		
			No commits in common. "5207df1c3091004e1fbff40fa0671845b09ae282" and "61e89820f4ce329fbba27b44aab0cb617eb0e098" have entirely different histories.
		
	
	
		
			5207df1c30
			...
			61e89820f4
		
	
		
							
								
								
									
										2
									
								
								bullet.c
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								bullet.c
									
									
									
									
									
								
							| @ -18,7 +18,6 @@ Entity *spawn_bullet(Vector2 spawn_at) { | ||||
|   Entity *e = malloc(sizeof(Entity)); | ||||
|   e->name = "Bullet"; | ||||
|   e->properties = malloc(sizeof(Entity)); | ||||
|   e->timer = 0.5; | ||||
|   BulletProperties *props = (BulletProperties*)e->properties; | ||||
|   props->position.x = spawn_at.x; | ||||
|   props->position.y = spawn_at.y; | ||||
| @ -39,7 +38,6 @@ void draw_bullet(Entity *e) { | ||||
| 
 | ||||
| void tick_bullet(Entity *e, float dt) { | ||||
|   BulletProperties *props = e->properties; | ||||
|   e->timer -= dt; | ||||
|   props->position.y += props->velocity.y; | ||||
| } | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										1
									
								
								enemy.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								enemy.c
									
									
									
									
									
								
							| @ -18,7 +18,6 @@ Entity *spawn_enemy(void) { | ||||
|   Entity *e = malloc(sizeof(Entity)); | ||||
|   e->name = "Enemy"; | ||||
|   e->properties = malloc(sizeof(Entity)); | ||||
|   e->timer = 1.0; | ||||
|   EnemyProperties *props = (EnemyProperties*)e->properties; | ||||
|   props->position.x = 400; | ||||
|   props->position.y = 50; | ||||
|  | ||||
							
								
								
									
										15
									
								
								entity.c
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								entity.c
									
									
									
									
									
								
							| @ -1,15 +0,0 @@ | ||||
| //    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 "entity.h" | ||||
| 
 | ||||
| bool entity_expired(Entity *e) { | ||||
|   return e->timer < 0; | ||||
| } | ||||
							
								
								
									
										6
									
								
								entity.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								entity.h
									
									
									
									
									
								
							| @ -11,16 +11,12 @@ | ||||
| #ifndef ENTITY_H | ||||
| #define ENTITY_H | ||||
| 
 | ||||
| #include <stdbool.h> | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| typedef struct Entity Entity; | ||||
| typedef struct Entities Entities; | ||||
| 
 | ||||
| struct Entity { | ||||
|   char *name; | ||||
|   void *properties; | ||||
|   float timer; | ||||
|   void (*draw)(Entity *e); | ||||
|   void (*tick)(Entity *e, float dt); | ||||
|   void (*free)(Entity *e); | ||||
| @ -32,6 +28,4 @@ struct Entities { | ||||
|   size_t capacity; | ||||
| }; | ||||
| 
 | ||||
| bool entity_expired(Entity *e); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
							
								
								
									
										85
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										85
									
								
								game.c
									
									
									
									
									
								
							| @ -8,7 +8,6 @@ | ||||
| //
 | ||||
| //    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 <stdio.h> | ||||
| #include <stdbool.h> | ||||
| #include <raylib.h> | ||||
| 
 | ||||
| @ -16,102 +15,52 @@ | ||||
| #include "enemy.h" | ||||
| #include "bullet.h" | ||||
| 
 | ||||
| #define FOREACH_ENEMY for (size_t i = 0; i < g->enemies->count; i++) | ||||
| #define INIT_ENEMY Entity *e = g->enemies->entities[i] | ||||
| #define FOREACH_BULLET for (size_t i = 0; i < g->bullets->count; i++) | ||||
| #define INIT_BULLET Entity *e = g->bullets->entities[i] | ||||
| #define FOREACH_ENTITY for (size_t i = 0; i < g->entities->count; i++) | ||||
| #define INIT_ENTITY Entity *e = g->entities->entities[i] | ||||
| 
 | ||||
| void initialize_game(Game *g) { | ||||
|   g->should_close = false; | ||||
|   g->player = malloc(sizeof(Player)); | ||||
|   initialize_player(g->player); | ||||
|   g->enemies = malloc(sizeof(Entities)); | ||||
|   g->enemies->entities = malloc(10 * sizeof(Entity*)); | ||||
|   g->enemies->count = 0; | ||||
|   g->enemies->capacity = 10; | ||||
|   g->bullets = malloc(sizeof(Entities)); | ||||
|   g->bullets->entities = malloc(10 * sizeof(Entity*)); | ||||
|   g->bullets->count = 0; | ||||
|   g->bullets->capacity = 10; | ||||
|   g->entities = malloc(sizeof(Entities)); | ||||
|   g->entities->entities = malloc(10 * sizeof(Entity*)); | ||||
|   g->entities->count = 0; | ||||
|   g->entities->capacity = 10; | ||||
| } | ||||
| 
 | ||||
| void add_entity(Entities *entities, Entity *e) { | ||||
|   if (entities->count >= entities->capacity) { | ||||
|     entities->capacity *= 2; | ||||
|     entities->entities = realloc(entities->entities, entities->capacity * sizeof(Entity*)); | ||||
| void add_entity(Game *g, Entity *e) { | ||||
|   if (g->entities->count >= g->entities->capacity) { | ||||
|     g->entities->capacity *= 2; | ||||
|     g->entities->entities = realloc(g->entities->entities, g->entities->capacity * sizeof(Entity*)); | ||||
|   } | ||||
|   entities->entities[entities->count++] = e; | ||||
| } | ||||
| 
 | ||||
| void remove_entity(Entities *entities, Entity *e) { | ||||
|   size_t i; | ||||
|   for (i = 0; entities->entities[i] != e; i++) { | ||||
|     if (i > entities->count) { | ||||
|       printf("Couldn't find entity %s\n", e->name); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
|   for (; i < entities->count - 1; i++) { | ||||
|     entities->entities[i] = entities->entities[i + 1]; | ||||
|   } | ||||
|   entities->count--; | ||||
|   g->entities->entities[g->entities->count++] = e; | ||||
| } | ||||
| 
 | ||||
| void handle_input(Game *g) { | ||||
|   handle_player_input(g->player); | ||||
|   if (IsKeyPressed(KEY_S)) { | ||||
|     add_entity(g->enemies, spawn_enemy()); | ||||
|     add_entity(g, spawn_enemy()); | ||||
|   } | ||||
|   if (IsKeyPressed(KEY_SPACE)) { | ||||
|     add_entity(g->bullets, spawn_bullet(g->player->position)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void process_bullet_collisions(Game *g, Entity *bullet) { | ||||
|   FOREACH_ENEMY { | ||||
|     INIT_ENEMY; | ||||
|     Rectangle bullet_rec = ((BulletProperties*)bullet->properties)->position; | ||||
|     Rectangle enemy_rec = ((EnemyProperties*)e->properties)->position; | ||||
|     if (CheckCollisionRecs(bullet_rec, enemy_rec)) { | ||||
|       remove_entity(g->bullets, bullet); | ||||
|       remove_entity(g->enemies, e); | ||||
|     } | ||||
|     add_entity(g, spawn_bullet(g->player->position)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void run_frame(Game *g) { | ||||
|   handle_input(g); | ||||
|   move_player(g->player); | ||||
|   FOREACH_ENEMY { | ||||
|     INIT_ENEMY; | ||||
|     if (entity_expired(e)) { | ||||
|       remove_entity(g->enemies, e); | ||||
|       continue; | ||||
|     } | ||||
|   FOREACH_ENTITY { | ||||
|     INIT_ENTITY; | ||||
|     e->tick(e, GetFrameTime()); | ||||
|   } | ||||
| 
 | ||||
|   FOREACH_BULLET { | ||||
|     INIT_BULLET; | ||||
|     if (entity_expired(e)) { | ||||
|       remove_entity(g->bullets, e); | ||||
|       continue; | ||||
|     } | ||||
|     e->tick(e, GetFrameTime()); | ||||
|     process_bullet_collisions(g, e); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void draw_frame(Game *g) { | ||||
|   BeginDrawing(); | ||||
|   ClearBackground(RAYWHITE); | ||||
|   draw_player(g->player); | ||||
|   FOREACH_ENEMY { | ||||
|     INIT_ENEMY; | ||||
|     e->draw(e); | ||||
|   } | ||||
|   FOREACH_BULLET { | ||||
|     INIT_BULLET; | ||||
|   FOREACH_ENTITY { | ||||
|     INIT_ENTITY; | ||||
|     e->draw(e); | ||||
|   } | ||||
|   EndDrawing(); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user