diff --git a/CMakeLists.txt b/CMakeLists.txt index e96b398..cd42a2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,30 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Option to switch real platform vs. SDL implementation... +include(FetchContent) +FetchContent_Declare( + SDL3 + URL https://github.com/libsdl-org/SDL/releases/download/release-3.2.14/SDL3-3.2.14.tar.gz + OVERRIDE_FIND_PACKAGE +) +FetchContent_MakeAvailable(SDL3) + +FetchContent_Declare( + SDL3_ttf + URL https://github.com/libsdl-org/SDL_ttf/releases/download/release-3.2.2/SDL3_ttf-3.2.2.tar.gz + OVERRIDE_FIND_PACKAGE +) +FetchContent_MakeAvailable(SDL3_ttf) + +FetchContent_Declare( + flecs + URL https://github.com/SanderMertens/flecs/archive/refs/tags/v4.0.5.tar.gz + OVERRIDE_FIND_PACKAGE +) +FetchContent_MakeAvailable(flecs) + find_package(SDL3 CONFIG REQUIRED) +find_package(SDL3_ttf CONFIG REQUIRED) find_package(flecs CONFIG REQUIRED) find_package(spdlog CONFIG REQUIRED) @@ -16,6 +39,6 @@ add_executable(HansTheGatherer src/physics.cpp ) -target_link_libraries(HansTheGatherer SDL3::SDL3 flecs::flecs spdlog::spdlog) +target_link_libraries(HansTheGatherer SDL3::SDL3 SDL3_ttf::SDL3_ttf flecs::flecs spdlog::spdlog) set_property(TARGET HansTheGatherer PROPERTY CXX_STANDARD 20) diff --git a/NOTICE b/NOTICE index e7c6951..b50786c 100644 --- a/NOTICE +++ b/NOTICE @@ -6,3 +6,5 @@ Assets used by this project: URL: https://trashboat93.itch.io/jungle-background-parallax - "Jamaican Sunrise - Reggae" by DavidKBD URL: https://davidkbd.itch.io/tropical-dreams-spring-and-summer-music-pack +- "SPIDER ENEMY PIXEL ART" by camacebra + URL: https://camacebra.itch.io/spider-pixel-art-pack-16x16 \ No newline at end of file diff --git a/assets/images/spiders.bmp b/assets/images/spiders.bmp new file mode 100644 index 0000000..5cb2954 Binary files /dev/null and b/assets/images/spiders.bmp differ diff --git a/src/assets.cpp b/src/assets.cpp index 3b71e20..d55f6a6 100644 --- a/src/assets.cpp +++ b/src/assets.cpp @@ -13,6 +13,10 @@ static constexpr uint8_t FRUITS_DATA[] = { #embed "../assets/images/fruits.bmp" }; +static constexpr uint8_t SPIDERS_DATA[] = { +#embed "../assets/images/spiders.bmp" +}; + static constexpr uint8_t BASKET_DATA[] = { #embed "../assets/images/basket.bmp" }; @@ -79,12 +83,16 @@ AssetModule::AssetModule(flecs::world& world) auto* fruits = load_texture(FRUITS_DATA, sizeof(FRUITS_DATA), renderer); TextureAtlasLayout fruits_layout = {.width = 16, .height = 16, .rows = 6, .columns = 38}; + auto* spiders = load_texture(SPIDERS_DATA, sizeof(SPIDERS_DATA), renderer); + TextureAtlasLayout spiders_layout = {.width = 16, .height = 16, .rows = 2, .columns = 4}; + auto* basket = load_texture(BASKET_DATA, sizeof(BASKET_DATA), renderer); TextureAtlasLayout basket_layout = {.width = 16, .height = 16, .rows = 1, .columns = 1}; world.set(TextureAssets{ .background = Texture{.sdl_texture = background, .texture_atlas_layout = background_layout}, .fruits = Texture{.sdl_texture = fruits, .texture_atlas_layout = fruits_layout}, + .spiders = Texture{.sdl_texture = spiders, .texture_atlas_layout = spiders_layout}, .basket = Texture{.sdl_texture = basket, .texture_atlas_layout = basket_layout}}); auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA)); diff --git a/src/assets.hpp b/src/assets.hpp index 9ba327e..99a081c 100644 --- a/src/assets.hpp +++ b/src/assets.hpp @@ -24,6 +24,7 @@ struct TextureAssets { Texture background; Texture fruits; + Texture spiders; Texture basket; }; diff --git a/src/definitions.hpp b/src/definitions.hpp index 939df87..49b5c8d 100644 --- a/src/definitions.hpp +++ b/src/definitions.hpp @@ -16,6 +16,6 @@ struct SdlHandles struct Game { uint32_t ticks; - uint32_t time; - uint32_t score; + int32_t time; + int32_t score; }; diff --git a/src/level.hpp b/src/level.hpp index c76238d..8895c34 100644 --- a/src/level.hpp +++ b/src/level.hpp @@ -12,6 +12,10 @@ struct Fruit { }; +struct Spider +{ +}; + struct Basket { }; @@ -53,23 +57,44 @@ struct LevelModule Game const& game, TextureAssets const& texture_assets) { - if ((game.ticks % 40) == 0) - { - auto fruit = - it.world() - .entity() - .add() - .add() - .set(Position{.x = std::rand() % WINDOW_WIDTH, .y = -16}) - .set(Velocity{.x = 0, .y = 2}) - .set(Sprite{.texture = &texture_assets.fruits, - .texture_atlas_index = - static_cast(game.ticks % 228)}) - .set(Size{.w = 32, .h = 32}); + bool spider = std::rand() % 3 == 0; + int vel = std::rand() % 3 + 1; + if ((game.ticks % 50) == 0) + { + flecs::entity e; + if (!spider) + { + e = it.world() + .entity() + .add() + .add() + .set( + Position{.x = std::rand() % WINDOW_WIDTH, .y = -16}) + .set(Velocity{.x = 0, .y = vel}) + .set( + Sprite{.texture = &texture_assets.fruits, + .texture_atlas_index = + static_cast(std::rand() % 228)}) + .set(Size{.w = 32, .h = 32}); + } + else + { + e = it.world() + .entity() + .add() + .add() + .set( + Position{.x = std::rand() % WINDOW_WIDTH, .y = -16}) + .set(Velocity{.x = 0, .y = vel}) + .set(Sprite{.texture = &texture_assets.spiders, + .texture_atlas_index = + static_cast(std::rand() % 8)}) + .set(Size{.w = 32, .h = 32}); + } it.world() .entity("CollisionBox") - .child_of(fruit) + .child_of(e) .add() .set(Position{.x = 0, .y = 0}) .set(Size{.w = 32, .h = 32}) @@ -86,7 +111,7 @@ struct LevelModule // e.destruct(); // }); - world.system("CollectItem") + world.system("CollectFruit") .term_at(0) .singleton() .each( @@ -96,6 +121,16 @@ struct LevelModule pos.x = 1000; }); + world.system("CollectSpider") + .term_at(0) + .singleton() + .each( + [](flecs::entity e, Game& game, Position& pos, Spider, Collided) + { + game.score -= 50; + pos.x = 1000; + }); + world.system("MoveBasket") .term_at(0) .singleton() @@ -119,4 +154,4 @@ struct LevelModule pos.x = pos.x > WINDOW_WIDTH - size.w ? WINDOW_WIDTH - size.w : pos.x; }); } -}; \ No newline at end of file +};