Compare commits
	
		
			2 Commits
		
	
	
		
			238ab68322
			...
			33eb14c24b
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 33eb14c24b | |||
| cec3337c7a | 
							
								
								
									
										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 | ||||
							
								
								
									
										29
									
								
								game.c
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								game.c
									
									
									
									
									
								
							| @ -10,26 +10,55 @@ | ||||
| #include <stdlib.h> | ||||
| #include <stdbool.h> | ||||
| #include <raylib.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) { | ||||
|   g->should_close = false; | ||||
|   g->player = malloc(sizeof(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) { | ||||
|   handle_player_input(g->player); | ||||
|   if (IsKeyPressed(KEY_S)) { | ||||
|     add_entity(g, spawn_enemy()); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void run_frame(Game *g) { | ||||
|   handle_input(g); | ||||
|   move_player(g->player); | ||||
|   FOREACH_ENTITY { | ||||
|     INIT_ENTITY; | ||||
|     e->tick(e, GetFrameTime()); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void draw_frame(Game *g) { | ||||
|   BeginDrawing(); | ||||
|   ClearBackground(RAYWHITE); | ||||
|   draw_player(g->player); | ||||
|   FOREACH_ENTITY { | ||||
|     INIT_ENTITY; | ||||
|     e->draw(e); | ||||
|   } | ||||
|   EndDrawing(); | ||||
| } | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										3
									
								
								game.h
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								game.h
									
									
									
									
									
								
							| @ -13,12 +13,13 @@ | ||||
| 
 | ||||
| #include <stdlib.h> | ||||
| #include "player.h" | ||||
| #include "entity.h" | ||||
| 
 | ||||
| typedef struct Game { | ||||
|   Player *player; | ||||
|   Entities *entities; | ||||
|   /*
 | ||||
|   Levels levels; | ||||
|   Entities entities; | ||||
|   */ | ||||
|   bool should_close; | ||||
| } Game; | ||||
|  | ||||
							
								
								
									
										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