Move the ship about
This commit is contained in:
		
							parent
							
								
									238ab68322
								
							
						
					
					
						commit
						cec3337c7a
					
				
							
								
								
									
										1
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								game.c
									
									
									
									
									
								
							| @ -24,6 +24,7 @@ void handle_input(Game *g) { | ||||
| 
 | ||||
| void run_frame(Game *g) { | ||||
|   handle_input(g); | ||||
|   move_player(g->player); | ||||
| } | ||||
| 
 | ||||
| void draw_frame(Game *g) { | ||||
|  | ||||
							
								
								
									
										28
									
								
								player.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								player.c
									
									
									
									
									
								
							| @ -7,13 +7,15 @@ | ||||
| //    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 <stdio.h> | ||||
| #include <raylib.h> | ||||
| 
 | ||||
| #include "player.h" | ||||
| 
 | ||||
| void initialize_player(Player *p) { | ||||
| 
 | ||||
|   p->position = (Vector2) { 400, 300 }; | ||||
|   p->velocity = (Vector2) { 0, 0 }; | ||||
|   p->acceleration = (Vector2) { 0, 0 }; | ||||
|   p->pitch = PITCH_NEUTRAL; | ||||
|   p->roll = ROLL_NEUTRAL; | ||||
| } | ||||
| @ -41,3 +43,27 @@ void draw_player(Player *p) { | ||||
| 
 | ||||
|   DrawTriangle(nose, left_wing, right_wing, DARKGRAY); | ||||
| } | ||||
| 
 | ||||
| void move_player(Player *p) { | ||||
|   switch (p->roll) { | ||||
|   case ROLL_LEFT: p->acceleration.x = -ACCEL_X; break; | ||||
|   case ROLL_NEUTRAL: p->acceleration.x = 0; break; | ||||
|   case ROLL_RIGHT: p->acceleration.x = ACCEL_X; break; | ||||
|   } | ||||
| 
 | ||||
|   switch (p->pitch) { | ||||
|   case PITCH_DESCENDING: p->acceleration.y = 4; break; | ||||
|   case PITCH_NEUTRAL: p->acceleration.y = 0; break; | ||||
|   case PITCH_ASCENDING: p->acceleration.y = -4; break; | ||||
|   } | ||||
| 
 | ||||
|   p->velocity.x += p->acceleration.x; | ||||
|   p->velocity.x = CLAMP(p->velocity.x, MAX_VEL_X); | ||||
|   p->velocity.x *= DAMPING_X; | ||||
|    | ||||
|   p->velocity.y += p->acceleration.y; | ||||
|   p->velocity.y = CLAMP(p->velocity.y, MAX_VEL_Y); | ||||
| 
 | ||||
|   p->position.x += p->velocity.x; | ||||
|   p->position.y += p->velocity.y; | ||||
| } | ||||
|  | ||||
							
								
								
									
										13
									
								
								player.h
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								player.h
									
									
									
									
									
								
							| @ -10,6 +10,15 @@ | ||||
| #ifndef PLAYER_H | ||||
| #define PLAYER_H | ||||
| 
 | ||||
| #define ACCEL_X 0.7 | ||||
| #define MAX_VEL_X 5 | ||||
| #define DAMPING_X 0.85 | ||||
| #define MAX_VEL_Y 2 | ||||
| 
 | ||||
| #define MAX(a, b) ((a > b) ? a : b) | ||||
| #define MIN(a, b) ((a < b) ? a : b) | ||||
| #define CLAMP(a, b) (MIN(MAX(a, -b), b)) | ||||
| 
 | ||||
| typedef enum PlayerPitch { | ||||
|   PITCH_DESCENDING, | ||||
|   PITCH_NEUTRAL, | ||||
| @ -24,11 +33,15 @@ typedef enum PlayerRoll { | ||||
| 
 | ||||
| typedef struct Player { | ||||
|   Vector2 position; | ||||
|   Vector2 velocity; | ||||
|   Vector2 acceleration; | ||||
|   PlayerPitch pitch; | ||||
|   PlayerRoll roll; | ||||
| } Player; | ||||
| 
 | ||||
| void initialize_player(Player *p); | ||||
| void handle_player_input(Player *p); | ||||
| void draw_player(Player *p); | ||||
| void move_player(Player *p); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user