171 lines
5.3 KiB
C
171 lines
5.3 KiB
C
#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] = {
|
|
{"assets/ship_start.png", 16.0f, 16.0f, 0.0f}};
|
|
|
|
float playerSpeeds[SPEED_COUNT] = {0.0f, 5.0f, 10.0f};
|
|
|
|
#define PLAYER_TERRAIN_CLEARANCE 40.0f
|
|
#define PLAYER_SPAWN_MAX_RADIUS 400.0f
|
|
|
|
PlayerSpeedID GetPlayerSpeedID(float speed) {
|
|
for (int i = 0; i < SPEED_COUNT; i++) {
|
|
if (playerSpeeds[i] == speed) {
|
|
return (PlayerSpeedID)i;
|
|
}
|
|
}
|
|
return ANCHOR;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
Player CreatePlayer(Terrain *terrain) {
|
|
Player player = {0};
|
|
InitConfig(&player.config, "config/player.ini");
|
|
|
|
int PLAYER_HEALTH_MAX = GetConfigInt(&player.config, "HEALTH_MAX");
|
|
int START_GOLD = GetConfigInt(&player.config, "START_GOLD");
|
|
|
|
TextureAsset startShipAsset = playerTextureAssets[START_SHIP];
|
|
Texture2D texture = LoadTextureCached(startShipAsset.path);
|
|
float frameWidth = startShipAsset.frameWidth;
|
|
|
|
PlayerSaveData saveData = {0};
|
|
LoadStorageData(STORAGE_DATA_FILE, PLAYER_SAVE_VERSION, &saveData, sizeof(saveData));
|
|
|
|
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};
|
|
|
|
player.animation = (Animation){.state = 1,
|
|
.currentFrame = 0,
|
|
.maxFrame = 3,
|
|
.framesCounter = 0,
|
|
.framesSpeed = 0.0f,
|
|
.animTimer = 0.0f};
|
|
|
|
player.physics = (Physics){.position = {0.0f, 0.0f},
|
|
.direction = {0.0f, 0.0f},
|
|
.speed = playerSpeeds[ANCHOR],
|
|
.weight = 10.0f};
|
|
|
|
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;
|
|
}
|
|
|
|
RandomizeSpawn(&player, terrain);
|
|
|
|
return player;
|
|
}
|
|
|
|
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];
|
|
} else {
|
|
player->physics.speed = playerSpeeds[FAST_AHEAD];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (IsGameActionPressed(ACTION_BACKWARD)) {
|
|
if (player->physics.speed > playerSpeeds[ANCHOR]) {
|
|
if (player->physics.speed > playerSpeeds[SLOW_AHEAD]) {
|
|
player->physics.speed = playerSpeeds[SLOW_AHEAD];
|
|
} else {
|
|
player->physics.speed = playerSpeeds[ANCHOR];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
UpdatePlayerRotationFromInput(player);
|
|
|
|
player->sprite.rotation = player->rotation;
|
|
Vector2 dir = RotationToVector2(player->rotation - 90.0f);
|
|
UpdatePhysics(&player->physics, dir);
|
|
UpdateSpriteDestRec(&player->sprite, &player->physics.position);
|
|
UpdateWakeTrail(&player->wakeTrail, player->physics.position);
|
|
SavePlayer(player);
|
|
}
|
|
|
|
void RenderPlayer(Player *player) {
|
|
RenderSprite(&player->sprite);
|
|
}
|