feat: add wake, rose, terrain, pathfinding, gameover, clouds, enemy shooting, variable bird speed refactor: some things
This commit is contained in:
+108
-72
@@ -1,8 +1,27 @@
|
||||
#include "app.h"
|
||||
#include "engine.h"
|
||||
#include <raylib.h>
|
||||
|
||||
#include "appCore.h"
|
||||
#include "engineAssets.h"
|
||||
#include "engineConfig.h"
|
||||
#include "enginePhysics.h"
|
||||
#include "engineSprite.h"
|
||||
#include "engineStorage.h"
|
||||
#include "engineVector.h"
|
||||
#include "pathfinding.h"
|
||||
#include "player.h"
|
||||
|
||||
typedef enum { FLAPPING, SOUND_COUNT } SoundID;
|
||||
|
||||
#define PLAYER_SAVE_VERSION 1
|
||||
|
||||
typedef struct {
|
||||
int version;
|
||||
int gold;
|
||||
float posX;
|
||||
float posY;
|
||||
float rotation;
|
||||
} PlayerSaveData;
|
||||
|
||||
const SoundAsset playerSoundAssets[SOUND_COUNT] = {{"assets/flapping.wav"}};
|
||||
|
||||
const TextureAsset playerTextureAssets[PLAYER_TEX_COUNT] = {
|
||||
@@ -10,78 +29,94 @@ const TextureAsset playerTextureAssets[PLAYER_TEX_COUNT] = {
|
||||
|
||||
float playerSpeeds[SPEED_COUNT] = {0.0f, 5.0f, 10.0f};
|
||||
|
||||
Config config;
|
||||
#define PLAYER_TERRAIN_CLEARANCE 40.0f
|
||||
#define PLAYER_SPAWN_MAX_RADIUS 400.0f
|
||||
|
||||
playerSpeedID GetPlayerSpeedID(float speed) {
|
||||
PlayerSpeedID GetPlayerSpeedID(float speed) {
|
||||
for (int i = 0; i < SPEED_COUNT; i++) {
|
||||
if (playerSpeeds[i] == speed) {
|
||||
return (playerSpeedID)i;
|
||||
return (PlayerSpeedID)i;
|
||||
}
|
||||
}
|
||||
return ANCHOR;
|
||||
}
|
||||
|
||||
Player *GetPlayer(void) {
|
||||
static Player player;
|
||||
return &player;
|
||||
static void RandomizeSpawn(Player *player, Terrain *terrain) {
|
||||
player->physics.position = GetRandomWaterPositionNear(
|
||||
terrain, (Vector2){0.0f, 0.0f}, PLAYER_SPAWN_MAX_RADIUS, PLAYER_TERRAIN_CLEARANCE);
|
||||
player->physics.direction = (Vector2){0.0f, 0.0f};
|
||||
player->physics.speed = playerSpeeds[ANCHOR];
|
||||
player->rotation = (float)GetRandomValue(0, 359);
|
||||
player->sprite.rotation = player->rotation;
|
||||
}
|
||||
|
||||
void CreatePlayer(void) {
|
||||
InitConfig(&config, "config/player.ini");
|
||||
Player CreatePlayer(Terrain *terrain) {
|
||||
Player player = {0};
|
||||
InitConfig(&player.config, "config/player.ini");
|
||||
|
||||
int PLAYER_HEALTH_MAX = GetConfigInt(&config, "HEALTH_MAX");
|
||||
int START_GOLD = GetConfigInt(&config, "START_GOLD");
|
||||
int PLAYER_HEALTH_MAX = GetConfigInt(&player.config, "HEALTH_MAX");
|
||||
int START_GOLD = GetConfigInt(&player.config, "START_GOLD");
|
||||
|
||||
TextureAsset startShipAsset = playerTextureAssets[START_SHIP];
|
||||
|
||||
Texture2D texture = LoadTexture(startShipAsset.path);
|
||||
Texture2D texture = LoadTextureCached(startShipAsset.path);
|
||||
float frameWidth = startShipAsset.frameWidth;
|
||||
float rotation =
|
||||
(float)LoadStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_ROTATION);
|
||||
|
||||
Sprite playerSprite = {.texture = texture,
|
||||
.frameSize = {frameWidth, frameWidth},
|
||||
.sourceRec = {0.0f, 0.0f, frameWidth, frameWidth},
|
||||
.destRec = {0.0f, 0.0f, frameWidth, frameWidth},
|
||||
.origin = {frameWidth / 2, frameWidth / 2},
|
||||
.rotation = rotation,
|
||||
.color = WHITE};
|
||||
PlayerSaveData saveData = {0};
|
||||
LoadStorageData(STORAGE_DATA_FILE, PLAYER_SAVE_VERSION, &saveData, sizeof(saveData));
|
||||
|
||||
Animation playerAnimation = {.state = 1,
|
||||
.currentFrame = 0,
|
||||
.maxFrame = 3,
|
||||
.framesCounter = 0,
|
||||
.framesSpeed = 0.0f,
|
||||
.animTimer = 0.0f};
|
||||
player.sprite = (Sprite){.texture = texture,
|
||||
.frameSize = {frameWidth, frameWidth},
|
||||
.sourceRec = {0.0f, 0.0f, frameWidth, frameWidth},
|
||||
.destRec = {0.0f, 0.0f, frameWidth, frameWidth},
|
||||
.origin = {frameWidth / 2, frameWidth / 2},
|
||||
.rotation = 0.0f,
|
||||
.color = WHITE};
|
||||
|
||||
Physics playerPhysics = {
|
||||
.position = {(float)LoadStorageValue(STORAGE_DATA_FILE,
|
||||
STORAGE_POSITION_POSX),
|
||||
(float)LoadStorageValue(STORAGE_DATA_FILE,
|
||||
STORAGE_POSITION_POSY)},
|
||||
.direction = {0.0f, 0.0f},
|
||||
.speed = playerSpeeds[ANCHOR],
|
||||
.weight = 10.0f};
|
||||
player.animation = (Animation){.state = 1,
|
||||
.currentFrame = 0,
|
||||
.maxFrame = 3,
|
||||
.framesCounter = 0,
|
||||
.framesSpeed = 0.0f,
|
||||
.animTimer = 0.0f};
|
||||
|
||||
Player *player = GetPlayer();
|
||||
player.physics = (Physics){.position = {0.0f, 0.0f},
|
||||
.direction = {0.0f, 0.0f},
|
||||
.speed = playerSpeeds[ANCHOR],
|
||||
.weight = 10.0f};
|
||||
|
||||
*player = (Player){
|
||||
.sprite = playerSprite,
|
||||
.animation = playerAnimation,
|
||||
.physics = playerPhysics,
|
||||
.rotation = rotation,
|
||||
.soundFlapping = LoadSound(playerSoundAssets[FLAPPING].path),
|
||||
.health = PLAYER_HEALTH_MAX,
|
||||
.gold = LoadStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_GOLD)};
|
||||
player.rotation = 0.0f;
|
||||
player.soundFlapping = LoadSoundCached(playerSoundAssets[FLAPPING].path);
|
||||
player.health = PLAYER_HEALTH_MAX;
|
||||
player.gold = saveData.gold;
|
||||
|
||||
if (player->gold == 0) {
|
||||
player->gold = START_GOLD;
|
||||
if (player.gold == 0) {
|
||||
player.gold = START_GOLD;
|
||||
}
|
||||
|
||||
RandomizeSpawn(&player, terrain);
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
void UpdatePlayer(void) {
|
||||
Player *player = GetPlayer();
|
||||
if (IsKeyPressed(KEY_W) || IsKeyPressed(KEY_UP)) {
|
||||
void ResetPlayer(Player *player, Terrain *terrain) {
|
||||
int PLAYER_HEALTH_MAX = GetConfigInt(&player->config, "HEALTH_MAX");
|
||||
player->health = PLAYER_HEALTH_MAX;
|
||||
RandomizeSpawn(player, terrain);
|
||||
ResetWakeTrail(&player->wakeTrail);
|
||||
SavePlayer(player);
|
||||
}
|
||||
|
||||
void SavePlayer(Player *player) {
|
||||
PlayerSaveData saveData = {.version = PLAYER_SAVE_VERSION,
|
||||
.gold = player->gold,
|
||||
.posX = player->physics.position.x,
|
||||
.posY = player->physics.position.y,
|
||||
.rotation = player->rotation};
|
||||
SaveStorageData(STORAGE_DATA_FILE, PLAYER_SAVE_VERSION, &saveData, sizeof(saveData));
|
||||
}
|
||||
|
||||
static void UpdatePlayerSpeedFromInput(Player *player) {
|
||||
if (IsGameActionPressed(ACTION_FORWARD)) {
|
||||
if (player->physics.speed < playerSpeeds[FAST_AHEAD]) {
|
||||
if (player->physics.speed < playerSpeeds[SLOW_AHEAD]) {
|
||||
player->physics.speed = playerSpeeds[SLOW_AHEAD];
|
||||
@@ -91,7 +126,7 @@ void UpdatePlayer(void) {
|
||||
}
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)) {
|
||||
if (IsGameActionPressed(ACTION_BACKWARD)) {
|
||||
if (player->physics.speed > playerSpeeds[ANCHOR]) {
|
||||
if (player->physics.speed > playerSpeeds[SLOW_AHEAD]) {
|
||||
player->physics.speed = playerSpeeds[SLOW_AHEAD];
|
||||
@@ -100,35 +135,36 @@ void UpdatePlayer(void) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void UpdatePlayerRotationFromInput(Player *player) {
|
||||
float rotationSpeed = 50.0f;
|
||||
if (player->physics.speed <= playerSpeeds[ANCHOR])
|
||||
return;
|
||||
|
||||
if (IsGameActionDown(ACTION_TURN_LEFT)) {
|
||||
player->rotation -= rotationSpeed * GetFrameTime();
|
||||
}
|
||||
|
||||
if (IsGameActionDown(ACTION_TURN_RIGHT)) {
|
||||
player->rotation += rotationSpeed * GetFrameTime();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePlayer(Player *player) {
|
||||
UpdatePlayerSpeedFromInput(player);
|
||||
player->sprite.sourceRec.x = 16.0f * GetPlayerSpeedID(player->physics.speed);
|
||||
|
||||
float rotationSpeed = 50.0f;
|
||||
|
||||
if (player->physics.speed > playerSpeeds[ANCHOR]) {
|
||||
|
||||
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {
|
||||
player->rotation -= rotationSpeed * GetFrameTime();
|
||||
}
|
||||
|
||||
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {
|
||||
player->rotation += rotationSpeed * GetFrameTime();
|
||||
}
|
||||
}
|
||||
UpdatePlayerRotationFromInput(player);
|
||||
|
||||
player->sprite.rotation = player->rotation;
|
||||
Vector2 dir = RotationToVector2(player->rotation - 90.0f);
|
||||
UpdatePhysics(&player->physics, dir);
|
||||
UpdateSpriteDestRec(&player->sprite, &player->physics.position);
|
||||
SaveStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_POSX,
|
||||
(int)player->physics.position.x);
|
||||
SaveStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_POSY,
|
||||
(int)player->physics.position.y);
|
||||
SaveStorageValue(STORAGE_DATA_FILE, STORAGE_POSITION_ROTATION,
|
||||
(int)player->rotation);
|
||||
UpdateWakeTrail(&player->wakeTrail, player->physics.position);
|
||||
SavePlayer(player);
|
||||
}
|
||||
|
||||
void RenderPlayer(void) {
|
||||
Player *player = GetPlayer();
|
||||
void RenderPlayer(Player *player) {
|
||||
RenderSprite(&player->sprite);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user