Add entities and enemies
This commit is contained in:
		
							parent
							
								
									cec3337c7a
								
							
						
					
					
						commit
						33eb14c24b
					
				
							
								
								
									
										50
									
								
								enemy.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								enemy.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,50 @@ | |||||||
|  | //    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 "enemy.h" | ||||||
|  | #include "entity.h" | ||||||
|  | 
 | ||||||
|  | Entity *spawn_enemy(void) { | ||||||
|  |   Entity *e = malloc(sizeof(Entity)); | ||||||
|  |   e->name = "Enemy"; | ||||||
|  |   e->properties = malloc(sizeof(Entity)); | ||||||
|  |   EnemyProperties *props = (EnemyProperties*)e->properties; | ||||||
|  |   props->position.x = 400; | ||||||
|  |   props->position.y = 50; | ||||||
|  |   props->position.width = 25; | ||||||
|  |   props->position.height = 25; | ||||||
|  |   props->velocity.x = 3; | ||||||
|  |   props->velocity.y = 0; | ||||||
|  |   e->draw = &draw_enemy; | ||||||
|  |   e->tick = &tick_enemy; | ||||||
|  |   e->free = &free_enemy; | ||||||
|  |   return e; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void draw_enemy(Entity *e) { | ||||||
|  |   EnemyProperties *props = e->properties; | ||||||
|  |   DrawRectangleRec(props->position, RED); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void tick_enemy(Entity *e, float dt) { | ||||||
|  |   EnemyProperties *props = e->properties; | ||||||
|  |   if (props->position.x < 200) props->velocity.x = 3; | ||||||
|  |   if (props->position.x > 600) props->velocity.x = -3; | ||||||
|  | 
 | ||||||
|  |   props->position.x += props->velocity.x; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void free_enemy(Entity *e) { | ||||||
|  |   free(e->properties); | ||||||
|  |   free(e); | ||||||
|  | } | ||||||
							
								
								
									
										26
									
								
								enemy.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								enemy.h
									
									
									
									
									
										Normal 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 ENEMY_H | ||||||
|  | #define ENEMY_H | ||||||
|  | #include <raylib.h> | ||||||
|  | 
 | ||||||
|  | #include "entity.h" | ||||||
|  | 
 | ||||||
|  | typedef struct EnemyProperties { | ||||||
|  |   Rectangle position; | ||||||
|  |   Vector2 velocity; | ||||||
|  | } EnemyProperties; | ||||||
|  | 
 | ||||||
|  | Entity *spawn_enemy(void); | ||||||
|  | void draw_enemy(Entity *e); | ||||||
|  | void tick_enemy(Entity *e, float dt); | ||||||
|  | void free_enemy(Entity *e); | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
							
								
								
									
										31
									
								
								entity.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								entity.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,31 @@ | |||||||
|  | //    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 ENTITY_H | ||||||
|  | #define ENTITY_H | ||||||
|  | 
 | ||||||
|  | typedef struct Entity Entity; | ||||||
|  | typedef struct Entities Entities; | ||||||
|  | 
 | ||||||
|  | struct Entity { | ||||||
|  |   char *name; | ||||||
|  |   void *properties; | ||||||
|  |   void (*draw)(Entity *e); | ||||||
|  |   void (*tick)(Entity *e, float dt); | ||||||
|  |   void (*free)(Entity *e); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | struct Entities { | ||||||
|  |   Entity **entities; | ||||||
|  |   size_t count; | ||||||
|  |   size_t capacity; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
							
								
								
									
										28
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								game.c
									
									
									
									
									
								
							| @ -10,27 +10,55 @@ | |||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| #include <raylib.h> | #include <raylib.h> | ||||||
|  | 
 | ||||||
| #include "game.h" | #include "game.h" | ||||||
|  | #include "enemy.h" | ||||||
|  | 
 | ||||||
|  | #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) { | 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->entities->entities = malloc(10 * sizeof(Entity*)); | ||||||
|  |   g->entities->count = 0; | ||||||
|  |   g->entities->capacity = 10; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 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*)); | ||||||
|  |   } | ||||||
|  |   g->entities->entities[g->entities->count++] = e; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void handle_input(Game *g) { | void handle_input(Game *g) { | ||||||
|   handle_player_input(g->player); |   handle_player_input(g->player); | ||||||
|  |   if (IsKeyPressed(KEY_S)) { | ||||||
|  |     add_entity(g, spawn_enemy()); | ||||||
|  |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 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 { | ||||||
|  |     INIT_ENTITY; | ||||||
|  |     e->tick(e, GetFrameTime()); | ||||||
|  |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 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 { | ||||||
|  |     INIT_ENTITY; | ||||||
|  |     e->draw(e); | ||||||
|  |   } | ||||||
|   EndDrawing(); |   EndDrawing(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										3
									
								
								game.h
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								game.h
									
									
									
									
									
								
							| @ -13,12 +13,13 @@ | |||||||
| 
 | 
 | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include "player.h" | #include "player.h" | ||||||
|  | #include "entity.h" | ||||||
| 
 | 
 | ||||||
| typedef struct Game { | typedef struct Game { | ||||||
|   Player *player; |   Player *player; | ||||||
|  |   Entities *entities; | ||||||
|   /*
 |   /*
 | ||||||
|   Levels levels; |   Levels levels; | ||||||
|   Entities entities; |  | ||||||
|   */ |   */ | ||||||
|   bool should_close; |   bool should_close; | ||||||
| } Game; | } Game; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user