This commit is contained in:
huaqo-games
2025-10-26 21:24:31 +01:00
parent f7b84f9016
commit 66b961d7ab
22 changed files with 406 additions and 165 deletions
+85 -4
View File
@@ -1,37 +1,108 @@
#ifndef Floor_H
#define Floor_H
#include <raylib.h>
#include <sprite.h>
typedef struct
{
Sprite sprite;
Rectangle bounds;
Shader shader;
float time;
} Floor;
Floor CreateFloor(Texture2D texture, float textureWidth)
// ----------------------------------------------------
// Create floor (loads shader and sets defaults)
// ----------------------------------------------------
Floor CreateFloor()
{
return (Floor){
Texture2D base = LoadTexture("assets/ocean_base.png");
Texture2D caustic = LoadTexture("assets/CausticTexture.png");
Texture2D causticHighlight = LoadTexture("assets/CausticTextureHighlights.png");
Texture2D noise = LoadTexture("assets/noiseTexture.png");
Texture2D cloud = LoadTexture("assets/cloudnoise_1.png");
Texture2D voronoise = LoadTexture("assets/voronoise_3.png");
float textureWidth = 64.0f;
Floor floor = {
.sprite = {
.texture = texture,
.texture = base,
.frameSize = {textureWidth, textureWidth},
.sourceRec = {0.0f, 0.0f, textureWidth, textureWidth},
.destRec = {0.0f, 0.0f, textureWidth, textureWidth},
.origin = {textureWidth / 2, textureWidth / 2},
.rotation = 0.0f,
.color = WHITE},
.bounds = {0.0f, 0.0f, 0.0f, 0.0f}};
.bounds = {0},
.shader = LoadShader("shaders/water.vs", "shaders/water.fs"),
.time = 0.0f
};
float worldTileSize = 128.0f; // one tile covers 128x128 world units
float textureSize = 32.0f; // texture is 32x32 pixels
floor.sprite.frameSize = (Vector2){worldTileSize, worldTileSize};
floor.sprite.sourceRec = (Rectangle){0, 0, textureSize, textureSize};
floor.sprite.destRec = (Rectangle){0, 0, worldTileSize, worldTileSize};
floor.sprite.origin = (Vector2){0, 0};
// Optional: load your extra water textures here and bind them
// Example:
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticTexture"), caustic);
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticHighlightTexture"), causticHighlight);
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticNoiseTexture"), noise);
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticFadeNoiseTexture"), cloud);
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "specularNoiseTexture"), voronoise);
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "specularMovementLeftNoiseTexture"), voronoise);
SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "specularMovementRightNoiseTexture"), cloud);
// Texture2D caustic = LoadTexture("resources/caustic.png");
// SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticTexture"), caustic);
// Setup aspect ratio and pixelization uniforms once
float aspect = (float)GetScreenHeight() / (float)GetScreenWidth();
float pixelization = 2048.0f;
int aspectLoc = GetShaderLocation(floor.shader, "aspectRatio");
int pixelLoc = GetShaderLocation(floor.shader, "pixelization");
SetShaderValue(floor.shader, aspectLoc, &aspect, SHADER_UNIFORM_FLOAT);
SetShaderValue(floor.shader, pixelLoc, &pixelization, SHADER_UNIFORM_FLOAT);
floor.sprite.texture = base;
return floor;
}
// ----------------------------------------------------
// Update visible area (same logic as before)
// ----------------------------------------------------
void UpdateFloor(Floor *floor, const Camera2D *camera)
{
floor->bounds.x = (int)((camera->target.x - (GetScreenWidth() / 2.0f / camera->zoom)) / floor->sprite.frameSize.x) - 1;
floor->bounds.width = (int)((camera->target.x + (GetScreenWidth() / 2.0f / camera->zoom)) / floor->sprite.frameSize.x) + 1;
floor->bounds.y = (int)((camera->target.y - (GetScreenHeight() / 2.0f / camera->zoom)) / floor->sprite.frameSize.y) - 1;
floor->bounds.height = (int)((camera->target.y + (GetScreenHeight() / 2.0f / camera->zoom)) / floor->sprite.frameSize.y) + 1;
// Update time uniform
floor->time += GetFrameTime();
int timeLoc = GetShaderLocation(floor->shader, "time");
SetShaderValue(floor->shader, timeLoc, &floor->time, SHADER_UNIFORM_FLOAT);
}
// ----------------------------------------------------
// Render floor tiles using the shader
// ----------------------------------------------------
void RenderFloor(Floor *floor)
{
BeginShaderMode(floor->shader);
for (int y = (int)floor->bounds.y; y <= (int)floor->bounds.height; y++)
{
for (int x = (int)floor->bounds.x; x <= (int)floor->bounds.width; x++)
@@ -41,6 +112,16 @@ void RenderFloor(Floor *floor)
RenderSprite(&floor->sprite);
}
}
EndShaderMode();
}
// ----------------------------------------------------
// Cleanup
// ----------------------------------------------------
void UnloadFloor(Floor *floor)
{
UnloadShader(floor->shader);
}
#endif // Floor_H
+17 -37
View File
@@ -1,34 +1,26 @@
#ifndef GAME_H
#define GAME_H
#include <object.h>
#include <camera.h>
#include <input.h>
#include "floor.h"
#include "player.h"
#include "gameassets.h"
#include <postfx.h>
#include <soundtrack.h>
#include <asset.h>
#include <array.h>
#include <stdlib.h>
#include "gameassets.h"
#include "floor.h"
#include "player.h"
#include "bee.h"
#include "hive.h"
#include "flowers.h"
typedef struct
{
Assets assets;
Assets assets;
Texture2D textures[TEX_COUNT];
PostProcessing postFX[SHADER_COUNT];
Soundtrack soundtracks[SOUNDTRACK_COUNT];
RenderTexture2D target;
Floor floor;
Object hive;
Array* flowers;
Player player;
Bee bee1;
Camera2D camera;
Mouse mouse;
Image image;
@@ -57,22 +49,13 @@ void InitGame(GameState *g, Window *window)
g->soundtracks[i] = LoadSoundtrack(g->assets.soundtrackAssets[i].path);
}
// Target texture
g->target = LoadRenderTexture(window->width, window->height);
// Static Entities
g->flowers = CreateFlowers(g->textures[FLOWER], g->assets.textureAssets[FLOWER].frameWidth, g->assets.textureAssets[FLOWER].rotation, 50.0f);
g->floor = CreateFloor(g->textures[GRASS], g->assets.textureAssets[GRASS].frameWidth);
g->hive = CreateHive(g->textures[HIVE], g->assets.textureAssets[HIVE].frameWidth);
// Animated Entities
g->player = CreatePlayer(IDLE_DOWN, HOE, g->textures[PLAYER], g->assets.textureAssets[PLAYER].frameWidth, 4, 2, g->hive.position, g->assets.textureAssets[PLAYER].rotation, 50.0f);
// g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive);
// I/O
g->player = CreatePlayer();
g->floor = CreateFloor();
g->camera = CreateCamera(window->width, window->height, g->player.physics.position, 10.0f);
g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &g->textures[CURSOR]);
Texture2D mouseTexture = LoadTexture("assets/SproutLandsUI/Spritesheets/Mouse sprites/TriangleMouseicon1.png");
g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &mouseTexture);
}
void UpdateGame(GameState *g, State *state, Flags *flags)
@@ -85,15 +68,18 @@ void UpdateGame(GameState *g, State *state, Flags *flags)
{
SetTargetFPS(0);
}
if (IsKeyPressed(KEY_ESCAPE))
{
state->currentView = MENU;
}
if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))
{
state->running = false;
}
for (int i = 0; i < SHADER_COUNT; i++)
for (int i = 0; i < SHADER_COUNT; i++)
{
UpdatePostFX(&g->postFX[i]);
}
@@ -105,18 +91,13 @@ void UpdateGame(GameState *g, State *state, Flags *flags)
UpdateMouse(&g->mouse, &g->camera);
UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse);
UpdateFloor(&g->floor, &g->camera);
UpdatePlayer(&g->player, g->flowers);
UpdateArray(g->flowers, UpdateFlower);
// UpdateBee(&g->bee1, &g->flowers, &g->hive);
UpdatePlayer(&g->player);
}
void RenderComponents(GameState *g)
{
RenderFloor(&g->floor);
RenderSprite(&g->hive.sprite);
RenderArray(g->flowers, RenderFlower);
RenderSprite(&g->player.sprite);
RenderSprite(&g->bee1.sprite);
RenderPlayer(&g->player);
}
void RenderGame(GameState *g, const State *appState, Flags *flags)
@@ -128,13 +109,13 @@ void RenderGame(GameState *g, const State *appState, Flags *flags)
EndMode2D();
DrawTextureRec(g->mouse.cursorTexture, (Rectangle){0, 0, g->mouse.cursorTexture.width, g->mouse.cursorTexture.height}, (Vector2){g->mouse.screenPosition.x, g->mouse.screenPosition.y}, WHITE);
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
for (int i = 0; i < SHADER_COUNT; i++)
{
RenderPostFX(&g->postFX[i], &g->target);
}
DrawText(TextFormat("Flower Seeds: %d", g->player.inventar.flowerSeedCount), 10, 10, 50, WHITE);
if (flags->showFPS)
{
@@ -162,7 +143,6 @@ void CleanupGame(GameState *g)
UnloadMusicStream(g->soundtracks[i].music);
}
UnloadRenderTexture(g->target);
FreeArray(&g->flowers);
}
#endif // GAME_H
+2 -2
View File
@@ -18,7 +18,7 @@ typedef enum
} TextureID;
const TextureAsset texAssets[TEX_COUNT] = {
{"assets/SproutLandsBasic/Characters/character.png", 48.0f, 48.0f, 0.0f},
{"assets/ship.png", 32.0f, 32.0f, 0.0f},
{"assets/SproutLandsBasic/Characters/BasicCharakterActions.png", 48.0f, 48.0f, 0.0f},
{"assets/SproutLandsBasic/Objects/tools.png", 48.0f, 16.0f, 0.0f},
{"assets/carrot_and_tomato.png", 32.0f, 32.0f, 0.0f},
@@ -47,7 +47,7 @@ typedef enum
} SoundtrackID;
const SoundtrackAsset soundtrackAssets[SOUNDTRACK_COUNT] = {
{"assets/beez.mp3"}};
{"assets/sea.mp3"}};
////////////////////////////////////////////////////////////////////////////
+19
View File
@@ -0,0 +1,19 @@
#ifndef GAMEUI
#define GAMEUI
#include "flags.h"
void RenderGameUI(bool showFPS){
BeginDrawing();
ClearBackground(BLACK);
if (showFPS)
{
DrawFPS(10, 10);
}
EndDrawing();
}
#endif //GAMEUI
+109 -106
View File
@@ -5,145 +5,148 @@
#include <animation.h>
#include <physics.h>
#include <input.h>
#include <array.h>
#include <asset.h>
#include "flowers.h"
#include "inventar.h"
typedef enum
{
IDLE,
WALKING,
USING_TOOL
} PlayerState;
START_SHIP,
PLAYER_TEX_COUNT
} playerTextureID;
typedef enum
{
NO_TOOL,
HOE,
AXT,
CAN
} SelectedTool;
const TextureAsset textureAssets[PLAYER_TEX_COUNT] = {
{"assets/start_ship.png", 16.0f, 16.0f, 0.0f}
};
typedef enum {
ANCHOR,
SLOW_AHEAD,
FAST_AHEAD,
SPEED_COUNT
} playerSpeedID;
float playerSpeeds[SPEED_COUNT] = {
0.0f,
5.0f,
10.0f
};
typedef enum
{
IDLE_DOWN,
IDLE_UP,
IDLE_LEFT,
IDLE_RIGHT,
WALKING_DOWN,
WALKING_UP,
WALKING_LEFT,
WALKING_RIGHT,
HOE_DOWN,
HOE_UP,
HOE_RIGHT,
HOE_LEFT,
AXT_DOWN,
AXT_UP,
AXT_RIGHT,
AXT_LEFT,
CAN_DOWN,
CAN_UP,
CAN_RIGHT,
CAN_LEFT,
} PlayerAnimationState;
typedef struct
{
SelectedTool selectedTool;
Sprite sprite;
Animation animation;
Physics physics;
Inventar inventar;
float rotation;
} Player;
Player CreatePlayer(PlayerAnimationState animState, SelectedTool selectedTool, Texture2D texture, float frameWidth, int maxFrame, int framesSpeed, Vector2 startPosition, float rotation, float speed)
Player CreatePlayer(void)
{
return (Player){
.selectedTool = selectedTool,
.sprite = {
TextureAsset startShipAsset = textureAssets[START_SHIP];
Texture2D texture = LoadTexture(startShipAsset.path);
float frameWidth = startShipAsset.frameWidth;
float rotation = startShipAsset.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},
.animation = {.state = animState, .currentFrame = 0, .maxFrame = maxFrame, .framesCounter = 0, .framesSpeed = framesSpeed, .animTimer = 0.0f},
.physics = {.position = startPosition, .direction = (Vector2){0.0f, 0.0f}, .speed = speed},
.inventar = {.flowerSeedCount = 6}};
.color = WHITE
};
Animation playerAnimation = {
.state = 1,
.currentFrame = 0,
.maxFrame = 3,
.framesCounter = 0,
.framesSpeed = 0.0f,
.animTimer = 0.0f
};
Physics playerPhysics = {
.position = (Vector2){0.0f,0.0f},
.direction = (Vector2){0.0f, 0.0f},
.speed = playerSpeeds[ANCHOR]
};
return (Player){
.sprite = playerSprite,
.animation = playerAnimation,
.physics = playerPhysics,
.rotation = rotation
};
}
void UpdatePlayerAnimationState(int *state, SelectedTool *selectedTool, Vector2 *dir)
void UpdatePlayer(Player *player)
{
if (IsKeyPressed(KEY_ZERO))
{
*selectedTool = NO_TOOL;
}
if (IsKeyPressed(KEY_ONE))
{
*selectedTool = HOE;
}
if (IsKeyPressed(KEY_TWO))
{
*selectedTool = AXT;
}
if(IsKeyPressed(KEY_W) || IsKeyPressed(KEY_UP)){
if(player->physics.speed < playerSpeeds[FAST_AHEAD]){
if(player->physics.speed < playerSpeeds[SLOW_AHEAD]){
player->physics.speed = playerSpeeds[SLOW_AHEAD];
player->sprite.sourceRec.x = 16.0f * 1;
}
else {
player->physics.speed = playerSpeeds[FAST_AHEAD];
player->sprite.sourceRec.x = 16.0f * 2;
}
}
}
if (IsKeyPressed(KEY_THREE))
{
*selectedTool = CAN;
}
if(IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)){
if(player->physics.speed > playerSpeeds[ANCHOR]){
if(player->physics.speed > playerSpeeds[SLOW_AHEAD]){
player->physics.speed = playerSpeeds[SLOW_AHEAD];
player->sprite.sourceRec.x = 16.0f * 1;
}
else {
player->physics.speed = playerSpeeds[ANCHOR];
player->sprite.sourceRec.x = 16.0f * 0;
}
}
}
float rotationSpeed = 100.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();
}
}
player->sprite.rotation = player->rotation;
float rad = (player->rotation - 90.0f) * (PI / 180.0f);
Vector2 dir = { cosf(rad), sinf(rad) };
if (dir->x == 0 && dir->y == 0)
{
switch (*state)
{
case WALKING_DOWN:
*state = IDLE_DOWN;
break;
case WALKING_UP:
*state = IDLE_UP;
break;
case WALKING_LEFT:
*state = IDLE_LEFT;
break;
case WALKING_RIGHT:
*state = IDLE_RIGHT;
break;
default:
break;
}
}
else
{
if (dir->y == 1)
*state = WALKING_DOWN;
else if (dir->y == -1)
*state = WALKING_UP;
else if (dir->x == -1)
*state = WALKING_LEFT;
else if (dir->x == 1)
*state = WALKING_RIGHT;
}
}
void UpdatePlayer(Player *player, Array* flowers)
{
if (IsKeyPressed(KEY_SPACE) && player->inventar.flowerSeedCount > 0)
{
AddFlower(flowers, player->sprite.destRec);
player->inventar.flowerSeedCount--;
}
Vector2 dir = GetDirectionVector();
UpdatePlayerAnimationState(&player->animation.state, &player->selectedTool, &dir);
UpdatePhysics(&player->physics, dir);
UpdateSpriteDestRec(&player->sprite, &player->physics.position);
UpdateAnimation(&player->animation, GetFrameTime());
UpdateSpriteSourceRec(&player->sprite, &(Vector2){player->sprite.frameSize.x * player->animation.currentFrame, player->sprite.frameSize.y * player->animation.state});
//UpdateAnimation(&player->animation, GetFrameTime());
//UpdateSpriteSourceRec(&player->sprite, &(Vector2){player->sprite.frameSize.x * player->animation.currentFrame, player->sprite.frameSize.y * player->animation.state});
}
void RenderPlayer(Player *player){
RenderSprite(&player->sprite);
}
#endif // PLAYER_H
View File