Enemies fire at you
This commit is contained in:
		
							parent
							
								
									5207df1c30
								
							
						
					
					
						commit
						f501cdc0db
					
				
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							| @ -18,9 +18,15 @@ ITCH_CHANNEL_MAC=mac | ||||
| run: $(GAME)_linux | ||||
| 	SKIP_INTRO=1 ./$(GAME)_linux | ||||
| 
 | ||||
| debug: $(GAME)_linux_dbg | ||||
| 	SKIP_INTRO=1 gdb ./$(GAME)_linux | ||||
| 
 | ||||
| $(GAME)_linux: *.c | ||||
| 	$(CC) *.c $(CFLAGS) -o $(GAME)_linux | ||||
| 
 | ||||
| $(GAME)_linux_dbg: *.c | ||||
| 	$(CC) *.c -g $(CFLAGS) -o $(GAME)_linux | ||||
| 
 | ||||
| $(GAME)_windows.exe: *.c | ||||
| 	$(WIN_CC) *.c $(WIN_LIB) $(WIN_CFLAGS) -o $(GAME)_windows.exe | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										9
									
								
								bullet.c
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								bullet.c
									
									
									
									
									
								
							| @ -14,7 +14,7 @@ | ||||
| #include "bullet.h" | ||||
| #include "entity.h" | ||||
| 
 | ||||
| Entity *spawn_bullet(Vector2 spawn_at) { | ||||
| Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy) { | ||||
|   Entity *e = malloc(sizeof(Entity)); | ||||
|   e->name = "Bullet"; | ||||
|   e->properties = malloc(sizeof(Entity)); | ||||
| @ -25,7 +25,8 @@ Entity *spawn_bullet(Vector2 spawn_at) { | ||||
|   props->position.width = 4; | ||||
|   props->position.height = 10; | ||||
|   props->velocity.x = 0; | ||||
|   props->velocity.y = -10; | ||||
|   props->velocity.y = from_enemy ? 10 : -10; | ||||
|   props->from_enemy = from_enemy; | ||||
|   e->draw = &draw_bullet; | ||||
|   e->tick = &tick_bullet; | ||||
|   e->free = &free_bullet; | ||||
| @ -34,10 +35,10 @@ Entity *spawn_bullet(Vector2 spawn_at) { | ||||
| 
 | ||||
| void draw_bullet(Entity *e) { | ||||
|   BulletProperties *props = e->properties; | ||||
|   DrawRectangleRec(props->position, BLACK); | ||||
|   DrawRectangleRec(props->position, props->from_enemy ? RED : BLACK); | ||||
| } | ||||
| 
 | ||||
| void tick_bullet(Entity *e, float dt) { | ||||
| void tick_bullet(Game *g, Entity *e, float dt) { | ||||
|   BulletProperties *props = e->properties; | ||||
|   e->timer -= dt; | ||||
|   props->position.y += props->velocity.y; | ||||
|  | ||||
							
								
								
									
										6
									
								
								bullet.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								bullet.h
									
									
									
									
									
								
							| @ -12,15 +12,17 @@ | ||||
| #include <raylib.h> | ||||
| 
 | ||||
| #include "entity.h" | ||||
| #include "game.h" | ||||
| 
 | ||||
| typedef struct BulletProperties { | ||||
|   Rectangle position; | ||||
|   Vector2 velocity; | ||||
|   bool from_enemy; | ||||
| } BulletProperties; | ||||
| 
 | ||||
| Entity *spawn_bullet(Vector2 spawn_at); | ||||
| Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy); | ||||
| void draw_bullet(Entity *e); | ||||
| void tick_bullet(Entity *e, float dt); | ||||
| void tick_bullet(Game *g, Entity *e, float dt); | ||||
| void free_bullet(Entity *e); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
							
								
								
									
										14
									
								
								enemy.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								enemy.c
									
									
									
									
									
								
							| @ -26,6 +26,7 @@ Entity *spawn_enemy(void) { | ||||
|   props->position.height = 25; | ||||
|   props->velocity.x = 3; | ||||
|   props->velocity.y = 0; | ||||
|   props->bullet_timer = rand() % 4; | ||||
|   e->draw = &draw_enemy; | ||||
|   e->tick = &tick_enemy; | ||||
|   e->free = &free_enemy; | ||||
| @ -37,7 +38,7 @@ void draw_enemy(Entity *e) { | ||||
|   DrawRectangleRec(props->position, RED); | ||||
| } | ||||
| 
 | ||||
| void tick_enemy(Entity *e, float dt) { | ||||
| void update_position_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; | ||||
| @ -45,6 +46,17 @@ void tick_enemy(Entity *e, float dt) { | ||||
|   props->position.x += props->velocity.x; | ||||
| } | ||||
| 
 | ||||
| void tick_enemy(Game *g, Entity *e, float dt) { | ||||
|   EnemyProperties *props = e->properties; | ||||
|   update_position_enemy(e, dt); | ||||
|   props->bullet_timer -= dt; | ||||
|   if (props->bullet_timer < 0.) { | ||||
|     props->bullet_timer = (float)(rand() % 4); | ||||
| 
 | ||||
|     add_entity(g->bullets, spawn_bullet((Vector2) { props->position.x, props->position.y }, true)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void free_enemy(Entity *e) { | ||||
|   free(e->properties); | ||||
|   free(e); | ||||
|  | ||||
							
								
								
									
										5
									
								
								enemy.h
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								enemy.h
									
									
									
									
									
								
							| @ -12,15 +12,18 @@ | ||||
| #include <raylib.h> | ||||
| 
 | ||||
| #include "entity.h" | ||||
| #include "bullet.h" | ||||
| #include "game.h" | ||||
| 
 | ||||
| typedef struct EnemyProperties { | ||||
|   Rectangle position; | ||||
|   Vector2 velocity; | ||||
|   float bullet_timer; | ||||
| } EnemyProperties; | ||||
| 
 | ||||
| Entity *spawn_enemy(void); | ||||
| void draw_enemy(Entity *e); | ||||
| void tick_enemy(Entity *e, float dt); | ||||
| void tick_enemy(Game *g, Entity *e, float dt); | ||||
| void free_enemy(Entity *e); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
							
								
								
									
										4
									
								
								entity.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								entity.h
									
									
									
									
									
								
							| @ -17,12 +17,14 @@ | ||||
| typedef struct Entity Entity; | ||||
| typedef struct Entities Entities; | ||||
| 
 | ||||
| #include "game.h" | ||||
| 
 | ||||
| struct Entity { | ||||
|   char *name; | ||||
|   void *properties; | ||||
|   float timer; | ||||
|   void (*draw)(Entity *e); | ||||
|   void (*tick)(Entity *e, float dt); | ||||
|   void (*tick)(Game *g, Entity *e, float dt); | ||||
|   void (*free)(Entity *e); | ||||
| }; | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										26
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								game.c
									
									
									
									
									
								
							| @ -63,18 +63,25 @@ void handle_input(Game *g) { | ||||
|     add_entity(g->enemies, spawn_enemy()); | ||||
|   } | ||||
|   if (IsKeyPressed(KEY_SPACE)) { | ||||
|     add_entity(g->bullets, spawn_bullet(g->player->position)); | ||||
|     add_entity(g->bullets, spawn_bullet(g->player->position, false)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| 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)) { | ||||
|   BulletProperties *bullet_props = bullet->properties; | ||||
|   Rectangle bullet_rec = bullet_props->position; | ||||
|   if (bullet_props->from_enemy) { | ||||
|     if (CheckCollisionPointRec(g->player->position, bullet_rec)) { | ||||
|       remove_entity(g->bullets, bullet); | ||||
|       remove_entity(g->enemies, e); | ||||
|     } | ||||
|   } else { | ||||
|     FOREACH_ENEMY { | ||||
|       INIT_ENEMY; | ||||
|       Rectangle enemy_rec = ((EnemyProperties*)e->properties)->position; | ||||
|       if (CheckCollisionRecs(bullet_rec, enemy_rec)) { | ||||
| 	remove_entity(g->bullets, bullet); | ||||
| 	remove_entity(g->enemies, e); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -88,16 +95,17 @@ void run_frame(Game *g) { | ||||
|       remove_entity(g->enemies, e); | ||||
|       continue; | ||||
|     } | ||||
|     e->tick(e, GetFrameTime()); | ||||
|     e->tick(g, e, GetFrameTime()); | ||||
|   } | ||||
| 
 | ||||
|   FOREACH_BULLET { | ||||
|     INIT_BULLET; | ||||
| 
 | ||||
|     if (entity_expired(e)) { | ||||
|       remove_entity(g->bullets, e); | ||||
|       continue; | ||||
|     } | ||||
|     e->tick(e, GetFrameTime()); | ||||
|     e->tick(g, e, GetFrameTime()); | ||||
|     process_bullet_collisions(g, e); | ||||
|   } | ||||
| } | ||||
|  | ||||
							
								
								
									
										7
									
								
								game.h
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								game.h
									
									
									
									
									
								
							| @ -11,11 +11,13 @@ | ||||
| #ifndef GAME_H | ||||
| #define GAME_H | ||||
| 
 | ||||
| typedef struct Game Game; | ||||
| 
 | ||||
| #include <stdlib.h> | ||||
| #include "player.h" | ||||
| #include "entity.h" | ||||
| 
 | ||||
| typedef struct Game { | ||||
| struct Game { | ||||
|   Player *player; | ||||
|   Entities *enemies; | ||||
|   Entities *bullets; | ||||
| @ -23,9 +25,10 @@ typedef struct Game { | ||||
|   Levels levels; | ||||
|   */ | ||||
|   bool should_close; | ||||
| } Game; | ||||
| }; | ||||
| 
 | ||||
| void initialize_game(Game *g); | ||||
| void run_until_closing(Game *g); | ||||
| void add_entity(Entities *entities, Entity *e); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user