Compare commits
	
		
			No commits in common. "21efc3d6e9496b3d32000ac907515e873e402d16" and "cb9c9d13a429e2f8f7b0c5feb858d08f39ea54f0" have entirely different histories.
		
	
	
		
			21efc3d6e9
			...
			cb9c9d13a4
		
	
		
							
								
								
									
										3
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								game.c
									
									
									
									
									
								
							| @ -96,9 +96,8 @@ void process_bullet_collisions(Game *g, Entity *bullet) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void run_frame(Game *g) { | void run_frame(Game *g) { | ||||||
|   float dt = GetFrameTime(); |  | ||||||
|   handle_input(g); |   handle_input(g); | ||||||
|   tick_player(g->player, dt); |   move_player(g->player); | ||||||
|   FOREACH_ENEMY { |   FOREACH_ENEMY { | ||||||
|     INIT_ENEMY; |     INIT_ENEMY; | ||||||
|     if (entity_expired(e)) { |     if (entity_expired(e)) { | ||||||
|  | |||||||
							
								
								
									
										21
									
								
								level.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								level.c
									
									
									
									
									
								
							| @ -35,35 +35,16 @@ void init_level(Game *g, Level *l, char *filepath) { | |||||||
|   l->game = g; |   l->game = g; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| size_t level_tile_index_from_position(Level *l, Vector2 p) { |  | ||||||
|   size_t x_tile = p.x / 32; |  | ||||||
|   size_t y_tile = p.y / 32; |  | ||||||
|   return ((y_tile * l->width) + x_tile) % l->data_size; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| FourIndices rect_corners(Level *l, Rectangle r) { |  | ||||||
|   return (FourIndices) { |  | ||||||
|     level_tile_index_from_position(l, (Vector2) { r.x, r.y }), |  | ||||||
|     level_tile_index_from_position(l, (Vector2) { r.x + r.width, r.y }), |  | ||||||
|     level_tile_index_from_position(l, (Vector2) { r.x, r.y + r.height }), |  | ||||||
|     level_tile_index_from_position(l, (Vector2) { r.x + r.width, r.y + r.height}), |  | ||||||
|   }; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| Color COLORS[4] = {BLACK, DARKGRAY, GRAY, LIGHTGRAY}; | Color COLORS[4] = {BLACK, DARKGRAY, GRAY, LIGHTGRAY}; | ||||||
| void draw_tile(Level *l, float x, float y) { | void draw_tile(Level *l, float x, float y) { | ||||||
|   size_t index = (size_t)(x + (y * l->width)) % l->data_size; |   size_t index = (size_t)(x + (y * l->width)) % l->data_size; | ||||||
|   size_t data = l->data[index]; |   size_t data = l->data[index]; | ||||||
|   Color c = COLORS[data]; |   Color c = COLORS[data]; | ||||||
|   FourIndices fi = rect_corners(l, l->game->player->bbox); |  | ||||||
|   if (fi.index[0] == index) c = RED; |  | ||||||
|   if (fi.index[1] == index) c = RED; |  | ||||||
|   if (fi.index[2] == index) c = RED; |  | ||||||
|   if (fi.index[3] == index) c = RED; |  | ||||||
|   DrawRectangle(x * 32, y * 32, 32, 32, c); |   DrawRectangle(x * 32, y * 32, 32, 32, c); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void draw_level(Level *l) { | void draw_level(Level *l) { | ||||||
|  |   Camera2D *camera = l->game->camera; | ||||||
|   float y = l->game->player->position.y; |   float y = l->game->player->position.y; | ||||||
|   y /= 32; |   y /= 32; | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								level.h
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								level.h
									
									
									
									
									
								
							| @ -2,9 +2,7 @@ | |||||||
| #define LEVEL_H | #define LEVEL_H | ||||||
| 
 | 
 | ||||||
| typedef struct Level Level; | typedef struct Level Level; | ||||||
| typedef struct FourIndices FourIndices; |  | ||||||
| 
 | 
 | ||||||
| #include <raylib.h> |  | ||||||
| #include "game.h" | #include "game.h" | ||||||
| 
 | 
 | ||||||
| struct Level { | struct Level { | ||||||
| @ -15,13 +13,7 @@ struct Level { | |||||||
|   Game *game; |   Game *game; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct FourIndices { |  | ||||||
|   size_t index[4]; |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| void init_level(Game *g, Level *l, char *filepath); | void init_level(Game *g, Level *l, char *filepath); | ||||||
| void draw_level(Level *l); | void draw_level(Level *l); | ||||||
| FourIndices rect_corners(Level *l, Rectangle r); |  | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										56
									
								
								player.c
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								player.c
									
									
									
									
									
								
							| @ -16,15 +16,12 @@ void initialize_player(Game *g, Player *p) { | |||||||
|   p->position = (Vector2) { 400, 300 }; |   p->position = (Vector2) { 400, 300 }; | ||||||
|   p->velocity = (Vector2) { 0, 0 }; |   p->velocity = (Vector2) { 0, 0 }; | ||||||
|   p->acceleration = (Vector2) { 0, 0 }; |   p->acceleration = (Vector2) { 0, 0 }; | ||||||
|   p->bbox = (Rectangle) { 390, 320, 20, 60 }; |  | ||||||
|   p->pitch = PITCH_NEUTRAL; |   p->pitch = PITCH_NEUTRAL; | ||||||
|   p->roll = ROLL_NEUTRAL; |   p->roll = ROLL_NEUTRAL; | ||||||
|   Image sprite_img = LoadImage("img/player.png"); |   Image sprite_img = LoadImage("img/player.png"); | ||||||
|   p->spritesheet = LoadTextureFromImage(sprite_img); |   p->spritesheet = LoadTextureFromImage(sprite_img); | ||||||
|   UnloadImage(sprite_img); |   UnloadImage(sprite_img); | ||||||
|   p->game = g; |   p->game = g; | ||||||
|   p->timer = 2.0; |  | ||||||
|   p->destroyed = false; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void handle_player_input(Player *p) { | void handle_player_input(Player *p) { | ||||||
| @ -38,17 +35,11 @@ void handle_player_input(Player *p) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void draw_player(Player *p) { | void draw_player(Player *p) { | ||||||
|   if (p->destroyed) return; |  | ||||||
| 
 |  | ||||||
|   if (p->timer < 0 || ((int)(p->timer * 20.)) % 2) { |  | ||||||
|   DrawTextureRec(p->spritesheet, (Rectangle) { 128, 128, 128, 128 }, (Vector2) { p->position.x - 64, p->position.y - 29 }, WHITE); |   DrawTextureRec(p->spritesheet, (Rectangle) { 128, 128, 128, 128 }, (Vector2) { p->position.x - 64, p->position.y - 29 }, WHITE); | ||||||
|   } |   DrawRectangle(p->position.x, p->position.y, 5, 5, BLUE); | ||||||
|   DrawRectangleRec(p->bbox, YELLOW); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void move_player(Player *p) { | void move_player(Player *p) { | ||||||
|   if (p->destroyed) return; |  | ||||||
| 
 |  | ||||||
|   switch (p->roll) { |   switch (p->roll) { | ||||||
|   case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break; |   case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break; | ||||||
|   case ROLL_NEUTRAL: p->acceleration.x = 0; break; |   case ROLL_NEUTRAL: p->acceleration.x = 0; break; | ||||||
| @ -71,13 +62,7 @@ void move_player(Player *p) { | |||||||
|   p->position.x += p->velocity.x; |   p->position.x += p->velocity.x; | ||||||
|   p->position.y += p->velocity.y; |   p->position.y += p->velocity.y; | ||||||
| 
 | 
 | ||||||
|   p->bbox.x = p->position.x - (p->bbox.width / 2); |  | ||||||
|   p->bbox.y = p->position.y + 20; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void update_camera_player(Player *p) { |  | ||||||
|   p->game->camera->target = p->position; |   p->game->camera->target = p->position; | ||||||
|   p->game->camera->offset.y = 400; |  | ||||||
|   int level_width_in_pixels = p->game->level->width * 32; |   int level_width_in_pixels = p->game->level->width * 32; | ||||||
|   bool too_far_left = p->position.x < 400; |   bool too_far_left = p->position.x < 400; | ||||||
|   bool too_far_right = level_width_in_pixels - p->position.x < 400; |   bool too_far_right = level_width_in_pixels - p->position.x < 400; | ||||||
| @ -87,43 +72,6 @@ void update_camera_player(Player *p) { | |||||||
|     p->game->camera->offset.x = p->position.x - level_width_in_pixels + 800; |     p->game->camera->offset.x = p->position.x - level_width_in_pixels + 800; | ||||||
|   } else { |   } else { | ||||||
|     p->game->camera->offset.x = 400; |     p->game->camera->offset.x = 400; | ||||||
|  |     p->game->camera->offset.y = 275; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| void handle_terrain_crash(Player *p) { |  | ||||||
|   printf("Boom!\n"); |  | ||||||
|   p->destroyed = true; |  | ||||||
|   p->timer = 3.; |  | ||||||
|   p->velocity.x = 0; |  | ||||||
|   p->velocity.y = 0; |  | ||||||
|   p->acceleration.x = 0; |  | ||||||
|   p->acceleration.y = 0; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void check_collision_player_with_terrain(Player *p) { |  | ||||||
|   if (p->timer > 0) return; |  | ||||||
|   Level *l = p->game->level; |  | ||||||
|   FourIndices fi = rect_corners(l, p->bbox); |  | ||||||
|   for (int i = 0; i < 4; i++) { |  | ||||||
|     if (l->data[fi.index[0]] == 0) { |  | ||||||
|       handle_terrain_crash(p); |  | ||||||
|       break; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void revive_player(Player *p) { |  | ||||||
|   if (!p->destroyed) return; |  | ||||||
|   if (p->timer > 0.) return; |  | ||||||
| 
 |  | ||||||
|   p->destroyed = false; |  | ||||||
|   p->timer = 2.; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void tick_player(Player *p, float dt) { |  | ||||||
|   if (p->timer > 0) p->timer -= dt; |  | ||||||
|   revive_player(p); |  | ||||||
|   move_player(p); |  | ||||||
|   update_camera_player(p); |  | ||||||
|   check_collision_player_with_terrain(p); |  | ||||||
| } |  | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								player.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								player.h
									
									
									
									
									
								
							| @ -37,24 +37,20 @@ typedef enum PlayerRoll { | |||||||
| typedef struct Player Player; | typedef struct Player Player; | ||||||
| 
 | 
 | ||||||
| #include "game.h" | #include "game.h" | ||||||
| #include "level.h" |  | ||||||
| 
 | 
 | ||||||
| struct Player { | struct Player { | ||||||
|   Vector2 position; |   Vector2 position; | ||||||
|   Vector2 velocity; |   Vector2 velocity; | ||||||
|   Vector2 acceleration; |   Vector2 acceleration; | ||||||
|   Rectangle bbox; |  | ||||||
|   PlayerPitch pitch; |   PlayerPitch pitch; | ||||||
|   PlayerRoll roll; |   PlayerRoll roll; | ||||||
|   Texture2D spritesheet; |   Texture2D spritesheet; | ||||||
|   Game *game; |   Game *game; | ||||||
|   float timer; |  | ||||||
|   bool destroyed; |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| void initialize_player(Game *g, Player *p); | void initialize_player(Game *g, Player *p); | ||||||
| void handle_player_input(Player *p); | void handle_player_input(Player *p); | ||||||
| void draw_player(Player *p); | void draw_player(Player *p); | ||||||
| void tick_player(Player *p, float dt); | void move_player(Player *p); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user