Compare commits
	
		
			3 Commits
		
	
	
		
			61e89820f4
			...
			5207df1c30
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5207df1c30 | |||
| 62bb35a311 | |||
| 8622d86c2f | 
							
								
								
									
										2
									
								
								bullet.c
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								bullet.c
									
									
									
									
									
								
							| @ -18,6 +18,7 @@ Entity *spawn_bullet(Vector2 spawn_at) { | |||||||
|   Entity *e = malloc(sizeof(Entity)); |   Entity *e = malloc(sizeof(Entity)); | ||||||
|   e->name = "Bullet"; |   e->name = "Bullet"; | ||||||
|   e->properties = malloc(sizeof(Entity)); |   e->properties = malloc(sizeof(Entity)); | ||||||
|  |   e->timer = 0.5; | ||||||
|   BulletProperties *props = (BulletProperties*)e->properties; |   BulletProperties *props = (BulletProperties*)e->properties; | ||||||
|   props->position.x = spawn_at.x; |   props->position.x = spawn_at.x; | ||||||
|   props->position.y = spawn_at.y; |   props->position.y = spawn_at.y; | ||||||
| @ -38,6 +39,7 @@ void draw_bullet(Entity *e) { | |||||||
| 
 | 
 | ||||||
| void tick_bullet(Entity *e, float dt) { | void tick_bullet(Entity *e, float dt) { | ||||||
|   BulletProperties *props = e->properties; |   BulletProperties *props = e->properties; | ||||||
|  |   e->timer -= dt; | ||||||
|   props->position.y += props->velocity.y; |   props->position.y += props->velocity.y; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								enemy.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								enemy.c
									
									
									
									
									
								
							| @ -18,6 +18,7 @@ Entity *spawn_enemy(void) { | |||||||
|   Entity *e = malloc(sizeof(Entity)); |   Entity *e = malloc(sizeof(Entity)); | ||||||
|   e->name = "Enemy"; |   e->name = "Enemy"; | ||||||
|   e->properties = malloc(sizeof(Entity)); |   e->properties = malloc(sizeof(Entity)); | ||||||
|  |   e->timer = 1.0; | ||||||
|   EnemyProperties *props = (EnemyProperties*)e->properties; |   EnemyProperties *props = (EnemyProperties*)e->properties; | ||||||
|   props->position.x = 400; |   props->position.x = 400; | ||||||
|   props->position.y = 50; |   props->position.y = 50; | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								entity.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								entity.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | |||||||
|  | //    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,12 +11,16 @@ | |||||||
| #ifndef ENTITY_H | #ifndef ENTITY_H | ||||||
| #define ENTITY_H | #define ENTITY_H | ||||||
| 
 | 
 | ||||||
|  | #include <stdbool.h> | ||||||
|  | #include <stddef.h> | ||||||
|  | 
 | ||||||
| typedef struct Entity Entity; | typedef struct Entity Entity; | ||||||
| typedef struct Entities Entities; | typedef struct Entities Entities; | ||||||
| 
 | 
 | ||||||
| struct Entity { | struct Entity { | ||||||
|   char *name; |   char *name; | ||||||
|   void *properties; |   void *properties; | ||||||
|  |   float timer; | ||||||
|   void (*draw)(Entity *e); |   void (*draw)(Entity *e); | ||||||
|   void (*tick)(Entity *e, float dt); |   void (*tick)(Entity *e, float dt); | ||||||
|   void (*free)(Entity *e); |   void (*free)(Entity *e); | ||||||
| @ -28,4 +32,6 @@ struct Entities { | |||||||
|   size_t capacity; |   size_t capacity; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | bool entity_expired(Entity *e); | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										85
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										85
									
								
								game.c
									
									
									
									
									
								
							| @ -8,6 +8,7 @@ | |||||||
| //
 | //
 | ||||||
| //    You should have received a copy of the GNU General Public License along with Starship Futuretime. If not, see <https://www.gnu.org/licenses/>. 
 | //    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 <stdlib.h> | ||||||
|  | #include <stdio.h> | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| #include <raylib.h> | #include <raylib.h> | ||||||
| 
 | 
 | ||||||
| @ -15,52 +16,102 @@ | |||||||
| #include "enemy.h" | #include "enemy.h" | ||||||
| #include "bullet.h" | #include "bullet.h" | ||||||
| 
 | 
 | ||||||
| #define FOREACH_ENTITY for (size_t i = 0; i < g->entities->count; i++) | #define FOREACH_ENEMY for (size_t i = 0; i < g->enemies->count; i++) | ||||||
| #define INIT_ENTITY Entity *e = g->entities->entities[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] | ||||||
| 
 | 
 | ||||||
| void initialize_game(Game *g) { | void initialize_game(Game *g) { | ||||||
|   g->should_close = false; |   g->should_close = false; | ||||||
|   g->player = malloc(sizeof(Player)); |   g->player = malloc(sizeof(Player)); | ||||||
|   initialize_player(g->player); |   initialize_player(g->player); | ||||||
|   g->entities = malloc(sizeof(Entities)); |   g->enemies = malloc(sizeof(Entities)); | ||||||
|   g->entities->entities = malloc(10 * sizeof(Entity*)); |   g->enemies->entities = malloc(10 * sizeof(Entity*)); | ||||||
|   g->entities->count = 0; |   g->enemies->count = 0; | ||||||
|   g->entities->capacity = 10; |   g->enemies->capacity = 10; | ||||||
|  |   g->bullets = malloc(sizeof(Entities)); | ||||||
|  |   g->bullets->entities = malloc(10 * sizeof(Entity*)); | ||||||
|  |   g->bullets->count = 0; | ||||||
|  |   g->bullets->capacity = 10; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void add_entity(Game *g, Entity *e) { | void add_entity(Entities *entities, Entity *e) { | ||||||
|   if (g->entities->count >= g->entities->capacity) { |   if (entities->count >= entities->capacity) { | ||||||
|     g->entities->capacity *= 2; |     entities->capacity *= 2; | ||||||
|     g->entities->entities = realloc(g->entities->entities, g->entities->capacity * sizeof(Entity*)); |     entities->entities = realloc(entities->entities, entities->capacity * sizeof(Entity*)); | ||||||
|   } |   } | ||||||
|   g->entities->entities[g->entities->count++] = e; |   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--; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void handle_input(Game *g) { | void handle_input(Game *g) { | ||||||
|   handle_player_input(g->player); |   handle_player_input(g->player); | ||||||
|   if (IsKeyPressed(KEY_S)) { |   if (IsKeyPressed(KEY_S)) { | ||||||
|     add_entity(g, spawn_enemy()); |     add_entity(g->enemies, spawn_enemy()); | ||||||
|   } |   } | ||||||
|   if (IsKeyPressed(KEY_SPACE)) { |   if (IsKeyPressed(KEY_SPACE)) { | ||||||
|     add_entity(g, spawn_bullet(g->player->position)); |     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); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void run_frame(Game *g) { | void run_frame(Game *g) { | ||||||
|   handle_input(g); |   handle_input(g); | ||||||
|   move_player(g->player); |   move_player(g->player); | ||||||
|   FOREACH_ENTITY { |   FOREACH_ENEMY { | ||||||
|     INIT_ENTITY; |     INIT_ENEMY; | ||||||
|  |     if (entity_expired(e)) { | ||||||
|  |       remove_entity(g->enemies, e); | ||||||
|  |       continue; | ||||||
|  |     } | ||||||
|     e->tick(e, GetFrameTime()); |     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) { | void draw_frame(Game *g) { | ||||||
|   BeginDrawing(); |   BeginDrawing(); | ||||||
|   ClearBackground(RAYWHITE); |   ClearBackground(RAYWHITE); | ||||||
|   draw_player(g->player); |   draw_player(g->player); | ||||||
|   FOREACH_ENTITY { |   FOREACH_ENEMY { | ||||||
|     INIT_ENTITY; |     INIT_ENEMY; | ||||||
|  |     e->draw(e); | ||||||
|  |   } | ||||||
|  |   FOREACH_BULLET { | ||||||
|  |     INIT_BULLET; | ||||||
|     e->draw(e); |     e->draw(e); | ||||||
|   } |   } | ||||||
|   EndDrawing(); |   EndDrawing(); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user