diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index dfe0770..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto diff --git a/Makefile b/Makefile index 0d1490c..491de50 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,15 @@ -CXX = clang -EMCC = emcc -FLAGS = -std=c99 -g -Wall -Werror -Wpedantic -Wconversion -Wshadow -Wstrict-prototypes -fsanitize=address,undefined -O0 $(shell pkg-config --libs --cflags raylib) +COMPILER = clang +C_FLAGS = -std=c99 -g -Wall -Werror -Wpedantic -Wconversion -Wshadow -Wstrict-prototypes -fsanitize=address,undefined -O0 +RAYLIB_FLAGS = $(shell pkg-config --libs --cflags raylib) SRC = scripts/main.c +TARGET = bin/game -# ARM Build -ARM_TARGET = bin/game -arm: $(ARM_TARGET) -$(ARM_TARGET): $(SRC) - $(CXX) $(FLAGS) $(SRC) -o $(ARM_TARGET) +build: $(TARGET) +$(TARGET): $(SRC) + $(COMPILER) $(FLAGS) $(RAYLIB_FLAGS) $(SRC) -o $(TARGET) -# Web Build -WEB_TARGET = bin/game.js -EMFLAGS = -s USE_GLFW=3 -s ASYNCIFY -s TOTAL_MEMORY=67108864 \ - --preload-file assets -DPLATFORM_WEB -gsource-map -s ASSERTIONS=2 -RAYLIB_WEB_PATH = web-includes -web: $(WEB_TARGET) -$(WEB_TARGET): $(SRC) - $(EMCC) -Wall -std=c++17 -I/opt/homebrew/include -L$(RAYLIB_WEB_PATH) \ - -lraylib.web $(SRC) -o $(WEB_TARGET) $(EMFLAGS) - -# Clean clean: - rm -f $(ARM_TARGET) $(WEB_TARGET) + rm -f $(TARGET) -# Run ARM Build -run: clean arm - ./$(ARM_TARGET) - -# Serve Web Build -serve: web - python3 -m http.server +run: clean build + ./$(TARGET) \ No newline at end of file diff --git a/assets/character.png.import b/assets/character.png.import deleted file mode 100644 index 1f391bb..0000000 --- a/assets/character.png.import +++ /dev/null @@ -1,34 +0,0 @@ -[remap] - -importer="texture" -type="CompressedTexture2D" -uid="uid://n72g6l4saupl" -path="res://.godot/imported/character.png-7d161e056639ffa8fae932e906cc0673.ctex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://assets/character.png" -dest_files=["res://.godot/imported/character.png-7d161e056639ffa8fae932e906cc0673.ctex"] - -[params] - -compress/mode=0 -compress/high_quality=false -compress/lossy_quality=0.7 -compress/hdr_compression=1 -compress/normal_map=0 -compress/channel_pack=0 -mipmaps/generate=false -mipmaps/limit=-1 -roughness/mode=0 -roughness/src_normal="" -process/fix_alpha_border=true -process/premult_alpha=false -process/normal_map_invert_y=false -process/hdr_as_srgb=false -process/hdr_clamp_exposure=false -process/size_limit=0 -detect_3d/compress_to=1 diff --git a/assets/map.png.import b/assets/map.png.import deleted file mode 100644 index f85f152..0000000 --- a/assets/map.png.import +++ /dev/null @@ -1,34 +0,0 @@ -[remap] - -importer="texture" -type="CompressedTexture2D" -uid="uid://clxsj5chse86r" -path="res://.godot/imported/map.png-dfba05084e3346587240e6093ee94fb2.ctex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://assets/map.png" -dest_files=["res://.godot/imported/map.png-dfba05084e3346587240e6093ee94fb2.ctex"] - -[params] - -compress/mode=0 -compress/high_quality=false -compress/lossy_quality=0.7 -compress/hdr_compression=1 -compress/normal_map=0 -compress/channel_pack=0 -mipmaps/generate=false -mipmaps/limit=-1 -roughness/mode=0 -roughness/src_normal="" -process/fix_alpha_border=true -process/premult_alpha=false -process/normal_map_invert_y=false -process/hdr_as_srgb=false -process/hdr_clamp_exposure=false -process/size_limit=0 -detect_3d/compress_to=1 diff --git a/scripts/animsprite.h b/scripts/animsprite.h deleted file mode 100644 index f66f1a3..0000000 --- a/scripts/animsprite.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef ANIMSPRITE_H -#define ANIMSPRITE_H -#include -#include "sprite.h" - -typedef struct { - Sprite sprite; - int currentFrame; - int maxFrame; - int framesCounter; - int framesSpeed; - float animTimer; - bool isVertical; -} AnimSprite; - -void UpdateAnimSprite(AnimSprite *animSprite) { - animSprite->animTimer += GetFrameTime(); - float frameTime = 1.0f / animSprite->framesSpeed; - - if (animSprite->animTimer < frameTime) { - return; - } - - animSprite->animTimer -= frameTime; - animSprite->currentFrame++; - - if (animSprite->currentFrame >= animSprite->maxFrame) { - animSprite->currentFrame = 0; - } - - Vector2 newFramePos = { - .x = animSprite->isVertical ? animSprite->sprite.sourceRec.x - : animSprite->currentFrame * animSprite->sprite.frameSize.x, - .y = animSprite->isVertical ? animSprite->currentFrame * animSprite->sprite.frameSize.y - : animSprite->sprite.sourceRec.y - }; - - UpdateSpriteSourceRec(&animSprite->sprite, &newFramePos); -} - -#endif // ANIMSPRITE_H diff --git a/scripts/main.c b/scripts/main.c index 614d79f..227ff3f 100644 --- a/scripts/main.c +++ b/scripts/main.c @@ -1,79 +1,47 @@ #include -// WHITE, Color, -// KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP, IsKeyDown(), -// InitWindow(), SetTargetFPS(), WindowShouldClose(), GetFrameTime(), CloseWindow() -// Music, InitAudioDevice(), LoadMusicStream(), PlayMusicStream(), UpdateMusicStream(), UnloadMusicStream(), CloseAudioDevice(), -// Texture2D, Rectangle, Vector2, LoadTexture(), DrawTexturePro(), UnloadTexture(), -// BeginDrawing(), EndDrawing(), ClearBackground(), -// Camera2D, BeginMode2D(), EndMode2D() -#include // Vector2Length(), Vector2Normalize(), Vector2Add(), Vector2Scale(), Lerp() -#include // rand(), srand(), malloc() -#include // time() -#include // memcpy() - #include "sprite.h" -#include "animsprite.h" +#include "animation.h" #include "tilesprite.h" #include "vector.h" #include "object.h" +#include "physics.h" +#include "camera.h" +#include "input.h" + // Structs typedef struct { - AnimSprite animSprite; - Vector2 position; - Vector2 direction; - float velocity; + Sprite sprite; + Animation animation; + Physics physics; } Player; typedef struct { - AnimSprite animSprite; - Vector2 position; - Vector2 direction; - float velocity; - Object targetFlower; + Sprite sprite; + Animation animation; + Physics physics; + Object target; Object hive; + int flowerCount; } Bee; -// Pure functions - - -void Move(Vector2 *position, Vector2 *direction, float *velocity, float *rotation) { - if(Vector2Length(*direction) <= 0.0f){ - return; - } - float targetRotation = (float)atan2(direction->y, direction->x) * (180.0f / PI) + 90.0f; - *rotation = Lerp(*rotation, targetRotation, 0.1f); - *position = Vector2Add(*position, Vector2Scale(*direction, *velocity * GetFrameTime())); -} - -// Mutator Functions - -void GetDirectionFromInput(Vector2 *direction){ - *direction = (Vector2){0.0f, 0.0f}; - if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)){ direction->x += 1.0f; } - if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)){ direction->x -= 1.0f; } - if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)){ direction->y += 1.0f; } - if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)){ direction->y -= 1.0f; } - if (Vector2Length(*direction) > 0.0f) { *direction = Vector2Normalize(*direction); } -} - -void UpdateCamera2DTarget(Camera2D *camera, Vector2 *position, const float smoothness){ - camera->target = Vector2Lerp(camera->target, *position, smoothness * GetFrameTime()); -} - -// Main - int main(void) { // Load - // SetConfigFlags(FLAG_VSYNC_HINT); InitWindow(1280, 720, "The Last Hive"); + InitAudioDevice(); Music music = LoadMusicStream("assets/beez.mp3"); PlayMusicStream(music); + + Texture2D flowerTexture = LoadTexture("assets/flower.png"); + Texture2D beeTexture = LoadTexture("assets/character_bee.png"); + Texture2D floorTexture = LoadTexture("assets/grass.png"); + Texture2D hiveTexture = LoadTexture("assets/hive.png"); + TileSprite floor = { .sprite = { - .texture = LoadTexture("assets/grass.png"), + .texture = floorTexture, .frameSize = {16.0f,16.0f}, .sourceRec = { 0.0f, 0.0f, 16.0f, 16.0f}, .destRec = {0.0f, 0.0f, 16.0f, 16.0f}, @@ -83,9 +51,10 @@ int main(void) }, .bounds = {0.0f,0.0f,0.0f,0.0f} }; + Object hive = { .sprite = { - .texture = LoadTexture("assets/hive.png"), + .texture = hiveTexture, .frameSize = {32.0f,32.0f}, .sourceRec = {0.0f, 0.0f, 32.0f, 32.0f}, .destRec = {0.0f, 0.0f, 32.0f, 32.0f}, @@ -95,10 +64,10 @@ int main(void) }, .position = (Vector2){0.0f,0.0f} }; - Texture2D flower = LoadTexture("assets/flower.png"); + ObjectArray flowers = CreateObjectArray(&(Object){ .sprite = { - .texture = flower, + .texture = flowerTexture, .frameSize = {16.0f, 16.0f}, .sourceRec = {0.0f, 0.0f, 16.0f, 16.0f}, .destRec = {0.0f, 0.0f, 16.0f, 16.0f}, @@ -107,106 +76,131 @@ int main(void) .color = WHITE }, .position = {0.0f, 0.0f} - }, 300); + }, 10); if (!flowers.data) { printf("Memory allocation failed.\n"); exit(1); } Rectangle flowerField = {-50.0f, -50.0f, 100.0f, 100.0f}; SetRandomPositionForObjectArray(&flowers, flowerField); + Player player = { - .animSprite = { - .sprite = { - .texture = LoadTexture("assets/character_bee.png"), - .frameSize = {16.0f, 16.0f}, - .sourceRec = {0.0f, 0.0f, 16.0f, 16.0f}, - .destRec = {0.0f, 0.0f, 16.0f, 16.0f}, - .origin = {8.0f, 8.0f}, - .rotation = 0.0f, - .color = WHITE - }, + .sprite = { + .texture = beeTexture, + .frameSize = {16.0f, 16.0f}, + .sourceRec = {0.0f, 0.0f, 16.0f, 16.0f}, + .destRec = {0.0f, 0.0f, 16.0f, 16.0f}, + .origin = {8.0f, 8.0f}, + .rotation = 0.0f, + .color = WHITE + }, + .animation = { .currentFrame = 0, .maxFrame = 4, .framesCounter = 0, .framesSpeed = 10, - .isVertical = true, .animTimer = 0.0f }, - .position = hive.position, - .direction = (Vector2){0.0f,0.0f}, - .velocity = 50.0f + .physics = { + .position = hive.position, + .direction = (Vector2){0.0f,0.0f}, + .speed = 50.0f + } }; + Bee bee1 = { - .animSprite = { - .sprite = { - .texture = LoadTexture("assets/character_bee.png"), - .frameSize = {16.0f, 16.0f}, - .sourceRec = {0.0f, 0.0f, 16.0f, 16.0f}, - .destRec = {0.0f, 0.0f, 16.0f, 16.0f}, - .origin = {8.0f, 8.0f}, - .rotation = 0.0f, - .color = WHITE - }, + .sprite = { + .texture = beeTexture, + .frameSize = {16.0f, 16.0f}, + .sourceRec = {0.0f, 0.0f, 16.0f, 16.0f}, + .destRec = {0.0f, 0.0f, 16.0f, 16.0f}, + .origin = {8.0f, 8.0f}, + .rotation = 0.0f, + .color = WHITE + }, + .animation = { .currentFrame = 0, .maxFrame = 4, .framesCounter = 0, .framesSpeed = 10, - .isVertical = true, .animTimer = 0.0f }, - .position = hive.position, - .direction = (Vector2){0.0f,0.0f}, - .velocity = 5.0f, - .targetFlower = *GetRandomObject(&flowers), + .physics = { + .position = hive.position, + .direction = (Vector2){0.0f,0.0f}, + .speed = 10.0f + }, + .target = *GetRandomObject(&flowers), .hive = hive }; + Camera2D camera = { .offset = (Vector2){GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}, - .target = player.position, + .target = player.physics.position, .rotation = 0.0f, .zoom = 10.0f }; + Mouse mouse = { + .zoomSpeed = 0.10f, + .minZoom = 5.0f, + .maxZoom = 10.0f + }; + // Update & Render while (!WindowShouldClose()) { // Update - if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { - Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera); - AddObjectToArray(&flowers, flowers.prototype, mouseWorldPos); - } - UpdateMusicStream(music); + UpdateTileSpriteBounds(&floor, &camera); - - GetDirectionFromInput(&player.direction); - Move(&player.position, &player.direction, &player.velocity, &player.animSprite.sprite.rotation); - UpdateSpriteDestRec(&player.animSprite.sprite, &player.position); - UpdateAnimSprite(&player.animSprite); - const float tolerance = 1.0f; // Adjust as needed + UpdateMouse(&mouse, &camera); - if (fabs(bee1.position.x - bee1.targetFlower.position.x) < tolerance && - fabs(bee1.position.y - bee1.targetFlower.position.y) < tolerance) { - bee1.velocity = 0.0f; - } else { - Move(&bee1.position, &bee1.direction, &bee1.velocity, &bee1.animSprite.sprite.rotation); - bee1.direction = GetDirectionToObject(&bee1.targetFlower, bee1.position); + if (mouse.leftButton) { + AddObjectToArray(&flowers, flowers.prototype, mouse.worldPosition); } - UpdateSpriteDestRec(&bee1.animSprite.sprite, &bee1.position); - UpdateAnimSprite(&bee1.animSprite); + UpdateVectorOnInput(&player.physics.direction); + Move(&player.physics.position, &player.physics.direction, &player.physics.speed); + 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}); - UpdateCamera2DTarget(&camera, &player.position, 5.0f); + const float tolerance = 1.0f; - float wheel = GetMouseWheelMove(); - if (wheel != 0) + if (bee1.flowerCount < 4){ + if (fabs(bee1.physics.position.x - bee1.target.position.x) < tolerance && + fabs(bee1.physics.position.y - bee1.target.position.y) < tolerance) { + bee1.target = *GetRandomObject(&flowers); + bee1.flowerCount++; + } else { + Move(&bee1.physics.position, &bee1.physics.direction, &bee1.physics.speed); + UpdateSpriteRotation(&bee1.sprite, &bee1.physics.direction); + bee1.physics.direction = GetDirectionToObject(&bee1.target, bee1.physics.position); + } + } else { + bee1.target = hive; + if (fabs(bee1.physics.position.x - bee1.target.position.x) < tolerance && + fabs(bee1.physics.position.y - bee1.target.position.y) < tolerance) { + bee1.physics.speed = 0.0f; + } else { + Move(&bee1.physics.position, &bee1.physics.direction, &bee1.physics.speed); + UpdateSpriteRotation(&bee1.sprite, &bee1.physics.direction); + bee1.physics.direction = GetDirectionToObject(&bee1.target, bee1.physics.position); + } + } + + UpdateSpriteDestRec(&bee1.sprite, &bee1.physics.position); + UpdateAnimation(&bee1.animation, GetFrameTime()); + UpdateSpriteSourceRec(&bee1.sprite, &(Vector2){bee1.sprite.frameSize.x, bee1.sprite.frameSize.x * bee1.animation.currentFrame}); + + UpdateCamera2DTarget(&camera, &player.physics.position, 5.0f); + + if (mouse.wheelMove != 0) { - float zoomSpeed = 0.10f; - float scaleFactor = 1.0f + (zoomSpeed*fabsf(wheel)); - if (wheel < 0) scaleFactor = 1.0f/scaleFactor; - float targetZoom = Clamp(camera.zoom*scaleFactor, 5.0f, 10.0f); - camera.zoom = Lerp(camera.zoom, targetZoom, 0.1f); + UpdateCamera2DZoom(&camera, &mouse.scaleFactor, &mouse.minZoom, &mouse.maxZoom); } // Render @@ -216,18 +210,17 @@ int main(void) RenderTileSprite(&floor); RenderSprite(&hive.sprite); RenderObjectArray(&flowers); - RenderSprite(&bee1.animSprite.sprite); - RenderSprite(&player.animSprite.sprite); + RenderSprite(&bee1.sprite); + RenderSprite(&player.sprite); EndMode2D(); DrawFPS(10,10); EndDrawing(); } // Unload - UnloadTexture(floor.sprite.texture); - UnloadTexture(hive.sprite.texture); - UnloadTexture(flower); - UnloadTexture(bee1.animSprite.sprite.texture); - UnloadTexture(player.animSprite.sprite.texture); + UnloadTexture(floorTexture); + UnloadTexture(hiveTexture); + UnloadTexture(flowerTexture); + UnloadTexture(beeTexture); UnloadMusicStream(music); free(flowers.data); // Close diff --git a/scripts/physics.h b/scripts/physics.h index bc5707e..18e9db5 100644 --- a/scripts/physics.h +++ b/scripts/physics.h @@ -3,7 +3,6 @@ #include #include - typedef struct { Vector2 position; Vector2 direction;