player now a human with animation depending on direction and movement

This commit is contained in:
huaqo-games
2025-03-19 19:38:21 +01:00
parent 59b815fedf
commit 5d048a5269
30 changed files with 54 additions and 15 deletions
+13 -7
View File
@@ -14,6 +14,7 @@
#include "flowers.h"
typedef enum {
PLAYER,
FLOWER,
BEE,
GRASS,
@@ -21,11 +22,12 @@ typedef enum {
TEX_COUNT
} TextureID;
typedef struct {
PostProcessing postFX;
MyMusic ambient;
RenderTexture2D target;
Texture2D flowerTexture, beeTexture, floorTexture, hiveTexture;
Floor floor;
Object hive;
ObjectArray flowers;
@@ -33,11 +35,12 @@ typedef struct {
Bee bee1;
Camera2D camera;
Mouse mouse;
Image image;
TextureAsset textureAssets[TEX_COUNT];
Texture2D textures[TEX_COUNT];
} GameState;
void InitGame(GameState* g){
void InitGame(GameState* g, Display *display){
InitAudioDevice();
@@ -45,29 +48,32 @@ void InitGame(GameState* g){
g->ambient = CreateMusic("assets/beez.mp3");
g->textureAssets[PLAYER] = (TextureAsset){"assets/character/character.png", 64.0f, 64.0f, 180.0f};
g->textureAssets[FLOWER] = (TextureAsset){"assets/flower.png", 16.0f, 16.0f, 45.0f};
g->textureAssets[BEE] = (TextureAsset){"assets/character_bee.png", 16.0f, 16.0f, 0.0f};
g->textureAssets[GRASS] = (TextureAsset){"assets/grass.png", 16.0f, 16.0f, 0.0f};
g->textureAssets[HIVE] = (TextureAsset){"assets/hive.png", 32.0f, 32.0f, 0.0f};
g->textures[PLAYER] = LoadTexture(g->textureAssets[PLAYER].path);
g->textures[FLOWER] = LoadTexture(g->textureAssets[FLOWER].path);
g->textures[BEE] = LoadTexture(g->textureAssets[BEE].path);
g->textures[GRASS] = LoadTexture(g->textureAssets[GRASS].path);
g->textures[HIVE] = LoadTexture(g->textureAssets[HIVE].path);
g->player = CreatePlayer(g->textures[PLAYER], g->textureAssets[PLAYER].frameWidth, 4, 5, g->hive.position, g->textureAssets[PLAYER].rotation, 50.0f);
g->flowers = CreateFlowers(g->textures[FLOWER], g->textureAssets[FLOWER].frameWidth, g->textureAssets[FLOWER].rotation, 50.0f);
g->player = CreatePlayer(g->textures[BEE], g->textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 50.0f);
g->floor = CreateFloor(g->textures[GRASS], g->textureAssets[GRASS].frameWidth);
g->hive = CreateHive(g->textures[HIVE], g->textureAssets[HIVE].frameWidth);
g->bee1 = CreateBee(g->textures[BEE], g->textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive);
g->camera = CreateCamera(GetScreenWidth(), GetScreenHeight(), g->player.physics.position, 10.0f);
g->camera = CreateCamera(display->width, display->height, g->player.physics.position, 5.0f);
g->mouse = CreateMouse(0.10f, 5.0f, 10.0f);
g->target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
g->target = LoadRenderTexture(display->width, display->height);
}
void UpdateGame(GameState* g, State *state){
SetTargetFPS(0);
UpdatePostFX(&g->postFX);
UpdateMusic(&g->ambient);
UpdateMouse(&g->mouse, &g->camera);
-1
View File
@@ -77,7 +77,6 @@ void UpdateLogo(LogoState *l, State *state) {
}
}
else if (l->state == 4) { // State 4: Transition to GAMEPLAY
SetTargetFPS(0);
state->currentScreen = MENU;
}
}
+39 -5
View File
@@ -5,21 +5,33 @@
#include "../engine/physics.h"
#include "../engine/input.h"
typedef enum {
IDLE_UP,
IDLE_LEFT,
IDLE_DOWN,
IDLE_RIGHT,
WALKING_UP,
WALKING_LEFT,
WALKING_DOWN,
WALKING_RIGHT
} PlayerState;
typedef struct {
PlayerState state;
Sprite sprite;
Animation animation;
Physics physics;
} Player;
Player CreatePlayer(Texture2D texture, float frameWidth, int maxFrame, int framesSpeed, Vector2 startPosition, float speed){
Player CreatePlayer(Texture2D texture, float frameWidth, int maxFrame, int framesSpeed, Vector2 startPosition, float rotation, float speed){
return (Player){
.sprite = {
.texture = texture,
.frameSize = {frameWidth, frameWidth},
.sourceRec = {0.0f, 0.0f, frameWidth, frameWidth},
.sourceRec = {0.0f,0.0f, frameWidth, frameWidth},
.destRec = {0.0f, 0.0f, frameWidth, frameWidth},
.origin = {frameWidth / 2, frameWidth / 2},
.rotation = 0.0f,
.rotation = rotation,
.color = WHITE
},
.animation = {
@@ -37,12 +49,34 @@ Player CreatePlayer(Texture2D texture, float frameWidth, int maxFrame, int frame
};
}
void UpdateState(PlayerState *state) {
Vector2 dir = GetDirectionVector();
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_RIGHT: *state = IDLE_RIGHT; break;
case WALKING_LEFT: *state = IDLE_LEFT; 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_RIGHT;
else if (dir.x == -1) *state = WALKING_LEFT;
}
}
void UpdatePlayer(Player* player){
UpdatePhysics(&player->physics, GetDirectionVector());
UpdateSpriteRotation(&player->sprite, &player->physics.direction);
UpdateState(&player->state);
// UpdateSpriteRotation(&player->sprite, &player->physics.direction);
UpdateSpriteDestRec(&player->sprite, &player->physics.position);
UpdateAnimation(&player->animation, GetFrameTime());
UpdateSpriteSourceRec(&player->sprite, &(Vector2){player->sprite.frameSize.x, player->sprite.frameSize.x * player->animation.currentFrame});
UpdateSpriteSourceRec(&player->sprite, &(Vector2){player->sprite.frameSize.x * player->state, player->sprite.frameSize.y * player->animation.currentFrame});
}
#endif // PLAYER_H
+2 -2
View File
@@ -20,7 +20,7 @@ typedef struct {
int main(void)
{
Application app;
app.state.currentScreen = LOGO;
app.state.currentScreen = GAMEPLAY;
app.state.running = true;
app.state.gameStarted = false;
app.display.name = "The Last Hive";
@@ -31,7 +31,7 @@ int main(void)
InitLogo(&app.logo);
InitMenu(&app.menu);
InitGame(&app.game);
InitGame(&app.game,&app.display);
while (app.state.running)
{