diff options
-rw-r--r-- | game/CMakeLists.txt | 2 | ||||
-rw-r--r-- | game/Config.h | 7 | ||||
-rw-r--r-- | game/Random.cpp | 28 | ||||
-rw-r--r-- | game/Random.h | 11 | ||||
-rw-r--r-- | game/main.cpp | 7 | ||||
-rw-r--r-- | game/prefab/ZapperPoolScript.cpp | 9 |
6 files changed, 57 insertions, 7 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 48844b7..5c7bb6b 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -4,7 +4,6 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_BUILD_TYPE Debug) - project(game C CXX) add_subdirectory(../src crepe) @@ -45,6 +44,7 @@ target_sources(main PUBLIC hud/HudSubScene.cpp hud/HudScript.cpp hud/SpeedScript.cpp + Random.cpp ) add_subdirectory(background) diff --git a/game/Config.h b/game/Config.h index 8c27226..95846d2 100644 --- a/game/Config.h +++ b/game/Config.h @@ -31,11 +31,12 @@ static constexpr int COLL_LAY_ZAPPER = 6; // Only for GameScene static constexpr int COLL_LAY_LASER = 7; // Only for GameScene static constexpr int COLL_LAY_MISSILE = 8; // Only for GameScene -static constexpr int GAME_HEIGHT = 800; // In game units +static constexpr float GAME_HEIGHT = 800; // In game units +static constexpr float HALLWAY_HEIGHT = 475; // In game units -static constexpr int VIEWPORT_X = 1100; // In game units +static constexpr float VIEWPORT_X = 1100; // In game units // 'GAME_HEIGHT' (below) should be replaced by '500' when game development is finished -static constexpr int VIEWPORT_Y = 500; // In game units +static constexpr float VIEWPORT_Y = 500; // In game units // Font settings static constexpr const char* FONT = "Jetpackia"; diff --git a/game/Random.cpp b/game/Random.cpp new file mode 100644 index 0000000..59be3c5 --- /dev/null +++ b/game/Random.cpp @@ -0,0 +1,28 @@ +#include <cstdlib> + +#include "Random.h" + +float Random::f(float upper, float lower) { + float range = upper - lower; + float x = ((float) rand() / (float) (RAND_MAX)) * range; + return x + lower; +} + +double Random::d(double upper, double lower) { + double range = upper - lower; + double x = ((double) rand() / (double) (RAND_MAX)) * range; + return x + lower; +} + +int Random::i(int upper, int lower) { + int range = upper - lower; + int x = rand() % range; + return x + lower; +} + +unsigned Random::u(unsigned upper, unsigned lower) { + unsigned range = upper - lower; + unsigned x = rand() % range; + return x + lower; +} + diff --git a/game/Random.h b/game/Random.h new file mode 100644 index 0000000..cf05e87 --- /dev/null +++ b/game/Random.h @@ -0,0 +1,11 @@ +#pragma once + +class Random { +public: + static float f(float upper = 1.0, float lower = 0.0); + static double d(double upper = 1.0, double lower = 0.0); + static int i(int upper, int lower = 0); + static unsigned u(unsigned upper, unsigned lower = 0); + +}; + diff --git a/game/main.cpp b/game/main.cpp index bd5ca93..b6458b8 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,3 +1,5 @@ +#include <cstdlib> + #include <crepe/api/Engine.h> #include <crepe/api/Script.h> @@ -6,16 +8,17 @@ #include "menus/mainmenu/MainMenuScene.h" #include "menus/shop/ShopMenuScene.h" - using namespace crepe; int main() { + srand(time(NULL)); + Config::get_instance() = ENGINE_CONFIG; Engine gameloop; + gameloop.add_scene<GameScene>(); gameloop.add_scene<MainMenuScene>(); gameloop.add_scene<ShopMenuScene>(); - gameloop.add_scene<GameScene>(); return gameloop.main(); } diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index e42adc9..c6ce1e7 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -1,9 +1,11 @@ #include <crepe/api/Camera.h> #include "../Config.h" +#include "../Random.h" #include "ZapperPoolScript.h" #include "ZapperPoolSubScene.h" +#include "util/OptionalRef.h" using namespace crepe; using namespace std; @@ -24,9 +26,14 @@ void ZapperPoolScript::fixed_update(crepe::duration_t) { } void ZapperPoolScript::spawn_random() { + OptionalRef<ZapperObject> zapper = pool.get_next_zapper(); + if (!zapper) return; // pool exhausted + vec2 pos = this->get_camera_pos(); - logf(Log::DEBUG, "Spawning random zapper at {}", pos); + pos.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT; + zapper->place(pos, 0, Random::f(400, 200)); + logf(Log::DEBUG, "Spawning random zapper at {}", pos); } vec2 ZapperPoolScript::get_camera_pos() { |