diff options
Diffstat (limited to 'game/prefab')
-rw-r--r-- | game/prefab/CMakeLists.txt | 5 | ||||
-rw-r--r-- | game/prefab/PlayerObject.cpp | 72 | ||||
-rw-r--r-- | game/prefab/PlayerObject.h | 30 | ||||
-rw-r--r-- | game/prefab/PlayerScript.cpp | 15 | ||||
-rw-r--r-- | game/prefab/PlayerScript.h | 17 | ||||
-rw-r--r-- | game/prefab/ZapperObject.cpp | 128 | ||||
-rw-r--r-- | game/prefab/ZapperObject.h | 15 | ||||
-rw-r--r-- | game/prefab/ZapperPoolScript.cpp | 70 | ||||
-rw-r--r-- | game/prefab/ZapperPoolScript.h | 33 | ||||
-rw-r--r-- | game/prefab/ZapperPoolSubScene.cpp | 17 | ||||
-rw-r--r-- | game/prefab/ZapperPoolSubScene.h | 19 | ||||
-rw-r--r-- | game/prefab/ZapperScript.cpp | 23 | ||||
-rw-r--r-- | game/prefab/ZapperScript.h | 18 |
13 files changed, 231 insertions, 231 deletions
diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt index 03084e4..6c36ef2 100644 --- a/game/prefab/CMakeLists.txt +++ b/game/prefab/CMakeLists.txt @@ -1,7 +1,6 @@ target_sources(main PUBLIC - PlayerObject.cpp - PlayerScript.cpp ZapperObject.cpp - ZapperScript.cpp + ZapperPoolSubScene.cpp + ZapperPoolScript.cpp ) diff --git a/game/prefab/PlayerObject.cpp b/game/prefab/PlayerObject.cpp deleted file mode 100644 index ef7b6cc..0000000 --- a/game/prefab/PlayerObject.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#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; -} - diff --git a/game/prefab/PlayerObject.h b/game/prefab/PlayerObject.h deleted file mode 100644 index a44d367..0000000 --- a/game/prefab/PlayerObject.h +++ /dev/null @@ -1,30 +0,0 @@ -#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 deleted file mode 100644 index 67aadb6..0000000 --- a/game/prefab/PlayerScript.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include <crepe/api/Rigidbody.h> -#include <cassert> - -#include "PlayerScript.h" - -using namespace crepe; -using namespace std; - -PlayerScript::PlayerScript(const PlayerObject & player) : player(player) { } - -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 deleted file mode 100644 index 131c73f..0000000 --- a/game/prefab/PlayerScript.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include <crepe/api/Script.h> - -#include "PlayerObject.h" - -class PlayerScript : public crepe::Script { -public: - PlayerScript(const PlayerObject & player); - -protected: - void fixed_update(crepe::duration_t dt); - -protected: - PlayerObject player; -}; - diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 2791162..24bbbd2 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -1,6 +1,7 @@ +#include <crepe/api/Transform.h> + #include "Config.h" #include "ZapperObject.h" -#include "ZapperScript.h" using namespace crepe; @@ -11,74 +12,107 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Asset {"asset/obstacles/zapper/orbAnim.png"}, Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 1, - .size = vec2{0, 1} * SCALE, - .position_offset = {0, 100}, + .order_in_layer = 1, + .size = vec2(0, 50) * SCALE, } ), .orb_end = add_component<Sprite>( - sprite.orb_start.source, - Sprite::Data { - .flip = {true, true}, - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 1, - .size = vec2{0, 1} * SCALE, - .position_offset = {0, -100}, - } + sprite.orb_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 1, + .size = vec2(0, 50) * SCALE, + } ), .glow_start = add_component<Sprite>( Asset {"asset/obstacles/zapper/regular_zappers/glow.png"}, Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = -1, - .size = vec2{2, 2} * SCALE, - .position_offset = {0, 100}, + .order_in_layer = -1, + .size = vec2(128, 128) * SCALE, } ), .glow_end = add_component<Sprite>( - sprite.glow_start.source, - Sprite::Data { - .flip = {true, true}, - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = -1, - .size = vec2{2, 2} * SCALE, - .position_offset = {0, -100}, - } + sprite.glow_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = -1, + .size = vec2(128, 128) * SCALE, + } ), - .beam = add_component<Sprite>( + .beam = add_component<Sprite>( Asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}, Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2{200, 50}, - .angle_offset = 90, + .order_in_layer = 0, + .size = vec2(0, 40 * SCALE), + .angle_offset = 90, } - ), + ), }, animator { - .orb_start = add_component<Animator>( + .orb_start = add_component<Animator>( sprite.orb_start, ivec2(62, 42), uvec2(4, 1), Animator::Data { .fps = 10, .looping = true, } - ), - .orb_end = add_component<Animator>( - sprite.orb_end, ivec2(62, 42), uvec2(4, 1), - animator.orb_start.data - ), - .glow_start = add_component<Animator>( - sprite.glow_start, ivec2(128, 128), uvec2(4, 4), - Animator::Data { - .fps = 30, - .looping = true, - } - ), - .glow_end = add_component<Animator>( - sprite.glow_end, ivec2(128, 128), uvec2(4, 4), - animator.glow_start.data - ), + ), + .orb_end = add_component<Animator>( + sprite.orb_end, ivec2(62, 42), uvec2(4, 1), animator.orb_start.data + ), + .glow_start = add_component<Animator>( + sprite.glow_start, ivec2(128, 128), uvec2(16, 1), + Animator::Data { + .fps = 30, + .looping = true, + } + ), + .glow_end = add_component<Animator>( + sprite.glow_end, ivec2(128, 128), uvec2(16, 1), animator.glow_start.data + ), }, - controller(add_component<BehaviorScript>().set_script<ZapperScript>(*this)) -{ } + body {add_component<Rigidbody>(Rigidbody::Data { + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, + .collision_layer = COLL_LAY_ZAPPER, + })}, + collider {add_component<BoxCollider>(vec2(0, 0))} { + this->set_active(false); +} + +void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { + this->transform.position = position; + this->transform.rotation = rotation; + + vec2 offset = vec2(0, 1) * length / 2; + + this->sprite.orb_start.data.position_offset = offset; + this->sprite.glow_start.data.position_offset = offset; + this->sprite.orb_end.data.position_offset = -offset; + this->sprite.glow_end.data.position_offset = -offset; + + this->sprite.beam.data.size.x = length; + + this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; +} + +void ZapperObject::set_active(bool active) { + this->sprite.orb_start.active = active; + this->sprite.orb_end.active = active; + this->sprite.glow_start.active = active; + this->sprite.glow_end.active = active; + this->sprite.beam.active = active; + + this->animator.orb_start.active = active; + this->animator.orb_end.active = active; + this->animator.glow_start.active = active; + this->animator.glow_end.active = active; + + this->body.active = active; + this->collider.active = active; + this->active = active; +} diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index 6b68146..42edc51 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -1,7 +1,6 @@ #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> @@ -12,6 +11,8 @@ public: ZapperObject(crepe::GameObject &&); public: + bool active = true; + struct { crepe::Sprite & orb_start; crepe::Sprite & orb_end; @@ -27,11 +28,13 @@ public: crepe::Animator & glow_end; } animator; - // crepe::Rigidbody & body; - // crepe::BoxCollider & collider; - crepe::BehaviorScript & controller; + crepe::Rigidbody & body; + crepe::BoxCollider & collider; private: - static constexpr int SCALE = 60; -}; + static constexpr float SCALE = 0.8; +public: + void place(const crepe::vec2 & position, float rotation, float length); + void set_active(bool active); +}; diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp new file mode 100644 index 0000000..b832ddd --- /dev/null +++ b/game/prefab/ZapperPoolScript.cpp @@ -0,0 +1,70 @@ +#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; + +ZapperPoolScript::ZapperPoolScript(std::vector<ZapperObject> && pool) : pool(pool) {} + +void ZapperPoolScript::init() { + subscribe<CreateZapperEvent>([this](const CreateZapperEvent &) { + this->spawn_random(); + return true; + }); + + camera_transform = get_components_by_name<Transform>(CAMERA_NAME).back(); + camera_camera = get_components_by_name<Camera>(CAMERA_NAME).back(); +} + +void ZapperPoolScript::fixed_update(crepe::duration_t) { + float threshold + = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; + for (ZapperObject & zapper : this->pool) { + if (!zapper.active) continue; + + if (zapper.transform.position.x < threshold) zapper.set_active(false); + } +} + +void ZapperPoolScript::spawn_random() { + OptionalRef<ZapperObject> zapper = this->get_next_zapper(); + if (!zapper) return; // pool exhausted + + bool horizontal = Random::b(); + vec2 pos; + float rotation, length; + pos.x + = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN; + + if (horizontal) { + rotation = 90; + length = Random::f(400, 200); + pos.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT; + // align zapper to right edge of camera + pos.x -= MAX_LENGTH - (length / 2); + } else { + rotation = 0; + length = Random::f(200, 75); + // ensure zapper doesn't crash into ceiling or floor + pos.y = Random::f(0.5, -0.5) * (HALLWAY_HEIGHT - length); + // align zapper to right edge of camera + pos.x -= MAX_LENGTH; + } + + zapper->place(pos, rotation, length); + zapper->set_active(true); +} + +OptionalRef<ZapperObject> ZapperPoolScript::get_next_zapper() { + for (ZapperObject & zapper : this->pool) { + if (zapper.active) continue; + return zapper; + } + return {}; +} diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h new file mode 100644 index 0000000..2208c80 --- /dev/null +++ b/game/prefab/ZapperPoolScript.h @@ -0,0 +1,33 @@ +#pragma once + +#include <crepe/api/Script.h> + +#include "ZapperObject.h" +#include "util/OptionalRef.h" + +class ZapperPoolSubScene; + +class ZapperPoolScript : public crepe::Script { +public: + ZapperPoolScript(std::vector<ZapperObject> && pool); + + void init(); + void fixed_update(crepe::duration_t); + + unsigned i = 0; + +private: + std::vector<ZapperObject> pool; + +private: + crepe::OptionalRef<crepe::Transform> camera_transform; + crepe::OptionalRef<crepe::Camera> camera_camera; + crepe::OptionalRef<ZapperObject> get_next_zapper(); + +private: + void spawn_random(); + +private: + static constexpr float MAX_LENGTH = 400; + static constexpr float OFFSCREEN_MARGIN = 50 + MAX_LENGTH; +}; diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp new file mode 100644 index 0000000..a52aa75 --- /dev/null +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -0,0 +1,17 @@ +#include <crepe/api/BehaviorScript.h> + +#include "ZapperPoolScript.h" +#include "ZapperPoolSubScene.h" + +using namespace crepe; +using namespace std; + +ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) + : controller {scene.new_object("controller")} { + + vector<ZapperObject> pool; + for (size_t i = 0; i < this->POOL_SIZE; i++) + pool.emplace_back(scene.new_object("zapper", "zapper")); + BehaviorScript & behavior = this->controller.add_component<BehaviorScript>(); + behavior.set_script<ZapperPoolScript>(std::move(pool)); +} diff --git a/game/prefab/ZapperPoolSubScene.h b/game/prefab/ZapperPoolSubScene.h new file mode 100644 index 0000000..6f6e297 --- /dev/null +++ b/game/prefab/ZapperPoolSubScene.h @@ -0,0 +1,19 @@ +#pragma once + +#include <crepe/api/Event.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Scene.h> +#include <crepe/util/OptionalRef.h> + +class CreateZapperEvent : public crepe::Event {}; + +class ZapperPoolSubScene { +public: + ZapperPoolSubScene(crepe::Scene & scene); + +private: + crepe::GameObject controller; + +private: + static constexpr size_t POOL_SIZE = 4; +}; diff --git a/game/prefab/ZapperScript.cpp b/game/prefab/ZapperScript.cpp deleted file mode 100644 index b9b24be..0000000 --- a/game/prefab/ZapperScript.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include <crepe/api/Rigidbody.h> -#include <cassert> - -#include "ZapperScript.h" - -using namespace crepe; -using namespace std; - -ZapperScript::ZapperScript(const ZapperObject & zapper) : zapper(zapper) { } - -void ZapperScript::init() { - zapper.sprite.beam.mask = { - .w = 350, - .h = 117, - .x = 0, - .y = 0, - }; -} - -void ZapperScript::frame_update(duration_t delta_time) { - zapper.sprite.beam.mask.x += 4; -} - diff --git a/game/prefab/ZapperScript.h b/game/prefab/ZapperScript.h deleted file mode 100644 index 5a960df..0000000 --- a/game/prefab/ZapperScript.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include <crepe/api/Script.h> - -#include "ZapperObject.h" - -class ZapperScript : public crepe::Script { -public: - ZapperScript(const ZapperObject & zapper); - -protected: - void init(); - void frame_update(crepe::duration_t delta_time); - -protected: - ZapperObject zapper; -}; - |