Camera follows the player
This commit is contained in:
parent
3d23849838
commit
e2c82a5bcd
8
game.c
8
game.c
@ -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);
|
||||||
}
|
}
|
||||||
@ -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
1
game.h
@ -23,6 +23,7 @@ struct Game {
|
|||||||
Entities *enemies;
|
Entities *enemies;
|
||||||
Entities *bullets;
|
Entities *bullets;
|
||||||
Level *level;
|
Level *level;
|
||||||
|
Camera2D *camera;
|
||||||
/*
|
/*
|
||||||
Levels levels;
|
Levels levels;
|
||||||
*/
|
*/
|
||||||
|
2
level.c
2
level.c
@ -6,7 +6,7 @@ void init_level(Game *g, Level *l) {
|
|||||||
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] = 3;
|
||||||
}
|
}
|
||||||
l->game = g;
|
l->game = g;
|
||||||
}
|
}
|
||||||
|
7
player.c
7
player.c
@ -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) {
|
||||||
@ -59,4 +60,8 @@ 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->game->camera->target = p->position;
|
||||||
|
p->game->camera->offset.x = 400;
|
||||||
|
p->game->camera->offset.y = 275;
|
||||||
}
|
}
|
||||||
|
11
player.h
11
player.h
@ -33,16 +33,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);
|
||||||
|
Loading…
Reference in New Issue
Block a user