Compare commits

...

2 Commits

Author SHA1 Message Date
98ed7634d4 Time to really fly 2025-03-08 10:34:37 -05:00
e2c82a5bcd Camera follows the player 2025-03-08 10:25:24 -05:00
8 changed files with 36 additions and 18 deletions

View File

@ -14,7 +14,7 @@
#include "bullet.h" #include "bullet.h"
#include "entity.h" #include "entity.h"
Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy) { Entity *spawn_bullet(Vector2 spawn_at, Vector2 velocity, bool from_enemy) {
Entity *e = malloc(sizeof(Entity)); Entity *e = malloc(sizeof(Entity));
e->name = "Bullet"; e->name = "Bullet";
e->properties = malloc(sizeof(Entity)); e->properties = malloc(sizeof(Entity));
@ -25,7 +25,7 @@ Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy) {
props->position.width = 4; props->position.width = 4;
props->position.height = 10; props->position.height = 10;
props->velocity.x = 0; props->velocity.x = 0;
props->velocity.y = from_enemy ? 10 : -10; props->velocity.y = velocity.y + (from_enemy ? 10 : -10);
props->from_enemy = from_enemy; props->from_enemy = from_enemy;
e->draw = &draw_bullet; e->draw = &draw_bullet;
e->tick = &tick_bullet; e->tick = &tick_bullet;
@ -35,7 +35,7 @@ Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy) {
void draw_bullet(Entity *e) { void draw_bullet(Entity *e) {
BulletProperties *props = e->properties; BulletProperties *props = e->properties;
DrawRectangleRec(props->position, props->from_enemy ? RED : BLACK); DrawRectangleRec(props->position, props->from_enemy ? RED : ORANGE);
} }
void tick_bullet(Game *g, Entity *e, float dt) { void tick_bullet(Game *g, Entity *e, float dt) {

View File

@ -20,7 +20,7 @@ typedef struct BulletProperties {
bool from_enemy; bool from_enemy;
} BulletProperties; } BulletProperties;
Entity *spawn_bullet(Vector2 spawn_at, bool from_enemy); Entity *spawn_bullet(Vector2 spawn_at, Vector2 velocity, bool from_enemy);
void draw_bullet(Entity *e); void draw_bullet(Entity *e);
void tick_bullet(Game *g, Entity *e, float dt); void tick_bullet(Game *g, Entity *e, float dt);
void free_bullet(Entity *e); void free_bullet(Entity *e);

View File

@ -62,7 +62,7 @@ void tick_enemy(Game *g, Entity *e, float dt) {
if (props->bullet_timer < 0.) { if (props->bullet_timer < 0.) {
props->bullet_timer = (float)(rand() % 4); props->bullet_timer = (float)(rand() % 4);
add_entity(g->bullets, spawn_bullet((Vector2) { props->position.x, props->position.y }, true)); add_entity(g->bullets, spawn_bullet((Vector2) { props->position.x, props->position.y }, props->velocity, true));
} }
} }

10
game.c
View File

@ -24,7 +24,7 @@
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, g->player);
g->enemies = malloc(sizeof(Entities)); g->enemies = malloc(sizeof(Entities));
g->enemies->entities = malloc(10 * sizeof(Entity*)); g->enemies->entities = malloc(10 * sizeof(Entity*));
g->enemies->count = 0; g->enemies->count = 0;
@ -34,6 +34,10 @@ void initialize_game(Game *g) {
g->bullets->count = 0; g->bullets->count = 0;
g->bullets->capacity = 10; g->bullets->capacity = 10;
g->camera = malloc(sizeof(Camera));
g->camera->zoom = 1.;
g->camera->rotation = 0.;
g->level = malloc(sizeof(Level)); g->level = malloc(sizeof(Level));
init_level(g, g->level); init_level(g, g->level);
} }
@ -66,7 +70,7 @@ void handle_input(Game *g) {
add_entity(g->enemies, spawn_enemy()); add_entity(g->enemies, spawn_enemy());
} }
if (IsKeyPressed(KEY_SPACE)) { if (IsKeyPressed(KEY_SPACE)) {
add_entity(g->bullets, spawn_bullet(g->player->position, false)); add_entity(g->bullets, spawn_bullet(g->player->position, g->player->velocity, false));
} }
} }
@ -116,6 +120,7 @@ void run_frame(Game *g) {
void draw_frame(Game *g) { void draw_frame(Game *g) {
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
BeginMode2D(*g->camera);
draw_level(g->level); draw_level(g->level);
draw_player(g->player); draw_player(g->player);
FOREACH_ENEMY { FOREACH_ENEMY {
@ -126,6 +131,7 @@ void draw_frame(Game *g) {
INIT_BULLET; INIT_BULLET;
e->draw(e); e->draw(e);
} }
EndMode2D();
EndDrawing(); EndDrawing();
} }

1
game.h
View File

@ -23,6 +23,7 @@ struct Game {
Entities *enemies; Entities *enemies;
Entities *bullets; Entities *bullets;
Level *level; Level *level;
Camera2D *camera;
/* /*
Levels levels; Levels levels;
*/ */

View File

@ -2,11 +2,11 @@
void init_level(Game *g, Level *l) { void init_level(Game *g, Level *l) {
l->width = 50; l->width = 50;
l->length = 30; l->length = 300;
l->data_size = l->width * l->length; l->data_size = l->width * l->length;
l->data = malloc(l->data_size * sizeof(char)); l->data = malloc(l->data_size * sizeof(char));
for (int i = 0; i < l->data_size; i++) { for (int i = 0; i < l->data_size; i++) {
l->data[i] = 3; l->data[i] = rand() % 3;
} }
l->game = g; l->game = g;
} }
@ -16,7 +16,7 @@ void draw_tile(Level *l, int x, int y) {
size_t index = x + (y * l->width); size_t index = x + (y * l->width);
size_t data = l->data[index] & 3; size_t data = l->data[index] & 3;
Color c = COLORS[data]; Color c = COLORS[data];
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) {

View File

@ -12,7 +12,7 @@
#include "player.h" #include "player.h"
void initialize_player(Player *p) { 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 };
@ -21,6 +21,7 @@ void initialize_player(Player *p) {
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;
} }
void handle_player_input(Player *p) { void handle_player_input(Player *p) {
@ -45,9 +46,9 @@ void move_player(Player *p) {
} }
switch (p->pitch) { switch (p->pitch) {
case PITCH_DESCENDING: p->acceleration.y = 4; break; case PITCH_DESCENDING: p->acceleration.y = .4; break;
case PITCH_NEUTRAL: p->acceleration.y = 0; break; case PITCH_NEUTRAL: p->acceleration.y = 0; break;
case PITCH_ASCENDING: p->acceleration.y = -4; break; case PITCH_ASCENDING: p->acceleration.y = -.4; break;
} }
p->velocity.x += p->acceleration.x; p->velocity.x += p->acceleration.x;
@ -55,8 +56,12 @@ void move_player(Player *p) {
p->velocity.x *= DAMPING_X; p->velocity.x *= DAMPING_X;
p->velocity.y += p->acceleration.y; p->velocity.y += p->acceleration.y;
p->velocity.y = CLAMP(p->velocity.y, MAX_VEL_Y); p->velocity.y = MAX(MIN(p->velocity.y, -MIN_VEL_Y), -MAX_VEL_Y);
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->game->camera->target = p->position;
p->game->camera->offset.x = 400;
p->game->camera->offset.y = 275;
} }

View File

@ -15,7 +15,8 @@
#define ACCEL_X 0.7 #define ACCEL_X 0.7
#define MAX_VEL_X 5 #define MAX_VEL_X 5
#define DAMPING_X 0.85 #define DAMPING_X 0.85
#define MAX_VEL_Y 2 #define MAX_VEL_Y 20
#define MIN_VEL_Y 1
#define MAX(a, b) ((a > b) ? a : b) #define MAX(a, b) ((a > b) ? a : b)
#define MIN(a, b) ((a < b) ? a : b) #define MIN(a, b) ((a < b) ? a : b)
@ -33,16 +34,21 @@ typedef enum PlayerRoll {
ROLL_RIGHT, ROLL_RIGHT,
} PlayerRoll; } PlayerRoll;
typedef struct Player { typedef struct Player Player;
#include "game.h"
struct Player {
Vector2 position; Vector2 position;
Vector2 velocity; Vector2 velocity;
Vector2 acceleration; Vector2 acceleration;
PlayerPitch pitch; PlayerPitch pitch;
PlayerRoll roll; PlayerRoll roll;
Texture2D spritesheet; Texture2D spritesheet;
} Player; Game *game;
};
void initialize_player(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 move_player(Player *p); void move_player(Player *p);