diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/CMakeLists.txt | 17 | ||||
-rw-r--r-- | game/Config.h | 11 | ||||
-rw-r--r-- | game/GameScene.cpp | 20 | ||||
-rw-r--r-- | game/PlayerScript.cpp | 11 | ||||
-rw-r--r-- | game/PlayerScript.h | 8 | ||||
-rw-r--r-- | game/PlayerSubScene.cpp | 76 | ||||
-rw-r--r-- | game/PlayerSubScene.h | 10 | ||||
-rw-r--r-- | game/background/CMakeLists.txt | 9 | ||||
-rw-r--r-- | game/main.cpp | 3 | ||||
-rw-r--r-- | game/prefab/CMakeLists.txt | 5 | ||||
-rw-r--r-- | game/prefab/PlayerObject.cpp | 76 | ||||
-rw-r--r-- | game/prefab/PlayerObject.h | 30 | ||||
-rw-r--r-- | game/prefab/PlayerScript.cpp | 26 | ||||
-rw-r--r-- | game/prefab/PlayerScript.h | 23 | ||||
-rw-r--r-- | game/prefab/ZapperObject.h | 7 |
15 files changed, 209 insertions, 123 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 937b5e6..a149487 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -8,20 +8,19 @@ set(CMAKE_BUILD_TYPE Debug) project(game C CXX) add_subdirectory(../src crepe) -add_executable(main - background/AquariumSubScene.cpp - background/BackgroundSubScene.cpp - background/ForestParallaxScript.cpp - background/ForestSubScene.cpp + +add_executable(main) + +target_sources(main PUBLIC GameScene.cpp - background/HallwaySubScene.cpp MoveCameraManualyScript.cpp - PlayerScript.cpp - PlayerSubScene.cpp StartGameScript.cpp - background/StartSubScene.cpp main.cpp ) +add_subdirectory(background) +add_subdirectory(prefab) + target_link_libraries(main PUBLIC crepe) +target_include_directories(main PRIVATE .) diff --git a/game/Config.h b/game/Config.h index ec753df..350cd02 100644 --- a/game/Config.h +++ b/game/Config.h @@ -1,5 +1,16 @@ #pragma once +#include <crepe/api/Config.h> + +static const crepe::Config ENGINE_CONFIG { + .log { + .level = crepe::Log::Level::DEBUG, + }, + .window_settings { + .window_title = "Jetpack joyride clone", + }, +}; + static constexpr int SORT_IN_LAY_BACK_BACKGROUND = 3; // For all scenes static constexpr int SORT_IN_LAY_BACKGROUND = 4; // For all scenes static constexpr int SORT_IN_LAY_FORE_BACKGROUND = 5; // For all scenes diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2511567..57c6531 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -1,11 +1,3 @@ -#include "GameScene.h" -#include "Config.h" -#include "MoveCameraManualyScript.h" -#include "PlayerSubScene.h" -#include "StartGameScript.h" - -#include "background/BackgroundSubScene.h" - #include <cmath> #include <crepe/api/Animator.h> #include <crepe/api/Asset.h> @@ -22,10 +14,20 @@ #include <crepe/api/Transform.h> #include <crepe/types.h> +#include "GameScene.h" +#include "Config.h" +#include "MoveCameraManualyScript.h" +#include "StartGameScript.h" + +#include "background/BackgroundSubScene.h" +#include "prefab/PlayerObject.h" + using namespace crepe; using namespace std; void GameScene::load_scene() { + logf(Log::DEBUG, "Loading (main) GameScene..."); + BackgroundSubScene background(*this); GameObject camera = new_object("camera", "camera", vec2(650, 0)); @@ -38,7 +40,7 @@ void GameScene::load_scene() { camera.add_component<BehaviorScript>().set_script<MoveCameraManualyScript>(); camera.add_component<Rigidbody>(Rigidbody::Data {}); - PlayerSubScene player(*this); + PlayerObject player {new_object("player", "player", vec2(-100, 200))}; GameObject floor = new_object("floor", "game_world", vec2(0, 325)); floor.add_component<Rigidbody>(Rigidbody::Data { diff --git a/game/PlayerScript.cpp b/game/PlayerScript.cpp deleted file mode 100644 index 1c388f5..0000000 --- a/game/PlayerScript.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "PlayerScript.h" - -#include <crepe/api/Rigidbody.h> - -using namespace crepe; -using namespace std; - -void PlayerScript::fixed_update(crepe::duration_t dt) { - Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front(); - if (this->get_key_state(Keycode::SPACE)) rb.add_force_linear(vec2(0, -10)); -} diff --git a/game/PlayerScript.h b/game/PlayerScript.h deleted file mode 100644 index 84c4f7f..0000000 --- a/game/PlayerScript.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include <crepe/api/Script.h> - -class PlayerScript : public crepe::Script { -public: - void fixed_update(crepe::duration_t dt); -}; diff --git a/game/PlayerSubScene.cpp b/game/PlayerSubScene.cpp deleted file mode 100644 index 00b7810..0000000 --- a/game/PlayerSubScene.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "PlayerSubScene.h" -#include "Config.h" -#include "PlayerScript.h" - -#include <crepe/api/Animator.h> -#include <crepe/api/GameObject.h> -#include <crepe/api/Scene.h> -#include <crepe/api/Script.h> -#include <crepe/api/Sprite.h> - -using namespace crepe; -using namespace std; - -PlayerSubScene::PlayerSubScene(Scene & scn) { - GameObject player = scn.new_object("player", "player", vec2(-100, 200)); - Asset player_body_asset {"asset/barry/defaultBody.png"}; - Sprite & player_body_sprite = player.add_component<Sprite>( - player_body_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 0, - .size = vec2(0, 50), - } - ); - player.add_component<Animator>( - player_body_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - Asset player_head_asset {"asset/barry/defaultHead.png"}; - Sprite & player_head_sprite = player.add_component<Sprite>( - player_head_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 1, - .size = vec2(0, 50), - .position_offset = vec2(0, -20), - } - ); - player.add_component<Animator>( - player_head_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - Asset player_jetpack_asset {"asset/barry/jetpackDefault.png"}; - Sprite & player_jetpack_sprite = player.add_component<Sprite>( - player_jetpack_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_PLAYER, - .order_in_layer = 2, - .size = vec2(0, 60), - .position_offset = vec2(-20, 0), - } - ); - player_jetpack_sprite.active = false; - player.add_component<Animator>( - player_jetpack_sprite, ivec2(32, 44), uvec2(4, 4), - Animator::Data { - .fps = 5, - .looping = true, - } - ); - player.add_component<Rigidbody>(Rigidbody::Data { - .gravity_scale = 20, - .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = vec2(100, 0), - .collision_layers = {COLL_LAY_BOT_TOP}, - .collision_layer = COLL_LAY_PLAYER, - }); - player.add_component<BoxCollider>(vec2(50, 50)); - player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; -} diff --git a/game/PlayerSubScene.h b/game/PlayerSubScene.h deleted file mode 100644 index bf94c32..0000000 --- a/game/PlayerSubScene.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace crepe { -class Scene; -} - -class PlayerSubScene { -public: - PlayerSubScene(crepe::Scene & scn); -}; diff --git a/game/background/CMakeLists.txt b/game/background/CMakeLists.txt new file mode 100644 index 0000000..1d705f5 --- /dev/null +++ b/game/background/CMakeLists.txt @@ -0,0 +1,9 @@ +target_sources(main PUBLIC + AquariumSubScene.cpp + BackgroundSubScene.cpp + ForestParallaxScript.cpp + ForestSubScene.cpp + HallwaySubScene.cpp + StartSubScene.cpp +) + diff --git a/game/main.cpp b/game/main.cpp index 325b66d..2198f73 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,11 +1,14 @@ #include <crepe/api/Engine.h> #include <crepe/api/Script.h> +#include "Config.h" #include "GameScene.h" using namespace crepe; int main() { + Config::get_instance() = ENGINE_CONFIG; + Engine gameloop; gameloop.add_scene<GameScene>(); diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt new file mode 100644 index 0000000..a588090 --- /dev/null +++ b/game/prefab/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(main PUBLIC + PlayerObject.cpp + PlayerScript.cpp +) + diff --git a/game/prefab/PlayerObject.cpp b/game/prefab/PlayerObject.cpp new file mode 100644 index 0000000..736704a --- /dev/null +++ b/game/prefab/PlayerObject.cpp @@ -0,0 +1,76 @@ +#include <crepe/util/Log.h> + +#include "Config.h" +#include "PlayerObject.h" +#include "PlayerScript.h" + +using namespace crepe; + +PlayerObject::PlayerObject(crepe::GameObject && base) + : GameObject(std::move(base)), + sprite { + .body = add_component<Sprite>( + Asset {"asset/barry/defaultBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 0, + .size = vec2(0, 50), + } + ), + .head = add_component<Sprite>( + Asset {"asset/barry/defaultHead.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 1, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ), + .jetpack = add_component<Sprite>( + Asset {"asset/barry/jetpackDefault.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .order_in_layer = 2, + .size = vec2(0, 60), + .position_offset = vec2(-20, 0), + } + ) + }, + animator { + .body = add_component<Animator>( + sprite.body, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + .head = add_component<Animator>( + sprite.head, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + .jetpack = add_component<Animator>( + sprite.jetpack, ivec2(32, 44), uvec2(4, 4), + Animator::Data { + .fps = 5, + .looping = true, + } + ), + }, + body(add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(100, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + .collision_layer = COLL_LAY_PLAYER, + })), + collider(add_component<BoxCollider>(vec2(50, 50))), + controller(add_component<BehaviorScript>().set_script<PlayerScript>(this)) { + sprite.jetpack.active = false; + // controller.active = false; + + Log::logf(Log::DEBUG, "PlayerObject: ref {}", (void*) &(this->body.game_object_id)); +} + diff --git a/game/prefab/PlayerObject.h b/game/prefab/PlayerObject.h new file mode 100644 index 0000000..a44d367 --- /dev/null +++ b/game/prefab/PlayerObject.h @@ -0,0 +1,30 @@ +#pragma once + +#include <crepe/api/Animator.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/BoxCollider.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> + +class PlayerObject : public crepe::GameObject { +public: + PlayerObject(crepe::GameObject &&); + +public: + struct { + crepe::Sprite & body; + crepe::Sprite & head; + crepe::Sprite & jetpack; + } sprite; + + struct { + crepe::Animator & body; + crepe::Animator & head; + crepe::Animator & jetpack; + } animator; + + crepe::Rigidbody & body; + crepe::BoxCollider & collider; + crepe::BehaviorScript & controller; +}; diff --git a/game/prefab/PlayerScript.cpp b/game/prefab/PlayerScript.cpp new file mode 100644 index 0000000..e4a4951 --- /dev/null +++ b/game/prefab/PlayerScript.cpp @@ -0,0 +1,26 @@ +#include <crepe/api/Rigidbody.h> +#include <cassert> + +#include "PlayerScript.h" + +using namespace crepe; +using namespace std; + +PlayerScript::PlayerScript(PlayerObject * player) : player(player) { + logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); + logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); + logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); +} + +void PlayerScript::init() { + logf(Log::DEBUG, "PlayerScript: [C] player {}", (void*) &(*player)); + logf(Log::DEBUG, "PlayerScript: [C] player.body {}", (void*) &(player->body)); + logf(Log::DEBUG, "PlayerScript: [C] player.body.id {}", (void*) &(player->body.game_object_id)); + player->controller.active = false; +} + +void PlayerScript::fixed_update(crepe::duration_t dt) { + if (this->get_key_state(Keycode::SPACE)) + player->body.add_force_linear({ 0, -10 }); +} + diff --git a/game/prefab/PlayerScript.h b/game/prefab/PlayerScript.h new file mode 100644 index 0000000..bd4a00a --- /dev/null +++ b/game/prefab/PlayerScript.h @@ -0,0 +1,23 @@ +#pragma once + +#include <crepe/api/Script.h> + +#include "PlayerObject.h" + +class PlayerScript : public crepe::Script { +public: + PlayerScript(PlayerObject * player); + + PlayerScript(const PlayerScript &) = delete; + PlayerScript(PlayerScript &&) = delete; + PlayerScript & operator=(const PlayerScript &) = delete; + PlayerScript & operator=(PlayerScript &&) = delete; + +protected: + void fixed_update(crepe::duration_t dt); + void init(); + +protected: + PlayerObject * player = nullptr; +}; + diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h new file mode 100644 index 0000000..1f32cd7 --- /dev/null +++ b/game/prefab/ZapperObject.h @@ -0,0 +1,7 @@ +#pragma once + +#include <crepe/api/GameObject.h> + +class ZapperObject : public crepe::GameObject { + // afsd +}; |