diff options
Diffstat (limited to 'game/preview')
-rw-r--r-- | game/preview/NpcScript.cpp | 32 | ||||
-rw-r--r-- | game/preview/NpcScript.h | 11 | ||||
-rw-r--r-- | game/preview/NpcSubScene.cpp | 69 | ||||
-rw-r--r-- | game/preview/NpcSubScene.h | 10 | ||||
-rw-r--r-- | game/preview/PrevPlayerScript.cpp | 132 | ||||
-rw-r--r-- | game/preview/PrevPlayerScript.h | 23 | ||||
-rw-r--r-- | game/preview/PrevPlayerSubScene.cpp | 86 | ||||
-rw-r--r-- | game/preview/PrevPlayerSubScene.h | 10 | ||||
-rw-r--r-- | game/preview/SmokeSubScene.cpp | 37 | ||||
-rw-r--r-- | game/preview/SmokeSubScene.h | 10 |
10 files changed, 420 insertions, 0 deletions
diff --git a/game/preview/NpcScript.cpp b/game/preview/NpcScript.cpp new file mode 100644 index 0000000..c4148f2 --- /dev/null +++ b/game/preview/NpcScript.cpp @@ -0,0 +1,32 @@ +#include "NpcScript.h" + +#include <crepe/api/Sprite.h> +#include <crepe/api/Transform.h> +#include <crepe/manager/SaveManager.h> + +using namespace std; +using namespace crepe; + +void NpcScript::init() {} +void NpcScript::fixed_update(duration_t dt) { + auto & rb = this->get_component<Rigidbody>(); + auto & npc = this->get_component<Sprite>(); + auto & transform = this->get_component<Transform>(); + + if (transform.position.x < -990) { + rb.data.linear_velocity.x *= -1; + } + if (transform.position.x > 990) { + rb.data.linear_velocity.x *= -1; + } + + if (rb.data.linear_velocity.x < 0) { + npc.data.flip = {true, false}; + } else { + npc.data.flip = {false, false}; + } + + auto & savemgr = this->get_save_manager(); + savemgr.set("npc_x", transform.position.x); + savemgr.set("npc_y", transform.position.y); +} diff --git a/game/preview/NpcScript.h b/game/preview/NpcScript.h new file mode 100644 index 0000000..8d856fd --- /dev/null +++ b/game/preview/NpcScript.h @@ -0,0 +1,11 @@ + +#include <crepe/api/Script.h> + +class NpcScript : public crepe::Script { + +private: + +public: + void init(); + void fixed_update(crepe::duration_t dt); +}; diff --git a/game/preview/NpcSubScene.cpp b/game/preview/NpcSubScene.cpp new file mode 100644 index 0000000..bd6cfb2 --- /dev/null +++ b/game/preview/NpcSubScene.cpp @@ -0,0 +1,69 @@ + + +#include "NpcSubScene.h" + +#include "../Config.h" +#include "NpcScript.h" + +#include <crepe/ValueBroker.h> +#include <crepe/api/Animator.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> +#include <crepe/manager/SaveManager.h> + +using namespace crepe; + +NpcSubScene::NpcSubScene(Scene & scn) { + auto & savemgr = scn.get_save_manager(); + ValueBroker npc_x = savemgr.get<float>("npc_x", 500); + ValueBroker npc_y = savemgr.get<float>("npc_y", 0); + + GameObject npc = scn.new_object("npc", "npc_tag", vec2 {npc_x.get(), npc_y.get()}, 0, 1); + Asset npc_body {"asset/workers/worker1Body.png"}; + Asset npc_head {"asset/workers/worker1Head.png"}; + + auto & npc_body_sprite = npc.add_component<Sprite>( + npc_body, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .size = {0, 50}, + } + ); + auto & npc_head_sprite = npc.add_component<Sprite>( + npc_head, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PLAYER, + .size = {0, 50}, + .position_offset = {0, -20}, + } + ); + + npc.add_component<Animator>( + npc_body_sprite, ivec2 {32, 32}, uvec2 {4, 8}, + Animator::Data { + .fps = 5, + .looping = true, + } + ); + npc.add_component<Animator>( + npc_head_sprite, ivec2 {32, 32}, uvec2 {4, 8}, + Animator::Data { + .fps = 5, + .looping = true, + } + ); + npc.add_component<BoxCollider>(vec2 {50, 50}); + + npc.add_component<Rigidbody>(Rigidbody::Data { + .mass = 10, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {-50, 0}, + //.max_linear_velocity = 40, + .collision_layers = {COLL_LAY_BOT_TOP, COLL_LAY_PLAYER}, + .collision_layer = COLL_LAY_PLAYER, + }); + + npc.add_component<BehaviorScript>().set_script<NpcScript>(); +} diff --git a/game/preview/NpcSubScene.h b/game/preview/NpcSubScene.h new file mode 100644 index 0000000..a226195 --- /dev/null +++ b/game/preview/NpcSubScene.h @@ -0,0 +1,10 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class NpcSubScene { +public: + NpcSubScene(crepe::Scene & scn); +}; diff --git a/game/preview/PrevPlayerScript.cpp b/game/preview/PrevPlayerScript.cpp new file mode 100644 index 0000000..2657b8d --- /dev/null +++ b/game/preview/PrevPlayerScript.cpp @@ -0,0 +1,132 @@ +#include "PrevPlayerScript.h" + +#include "../missile/SpawnEvent.h" +#include "api/Transform.h" +#include <crepe/api/AudioSource.h> +#include <crepe/api/Camera.h> +#include <crepe/manager/SaveManager.h> +#include <iostream> +#include <ostream> + +using namespace crepe; + +bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) { + switch (ev.key) { + case Keycode::A: + this->get_component<Rigidbody>().data.linear_velocity.x = -move_speed; + this->body->data.flip = {true, false}; + this->head->data.flip = {true, false}; + break; + case Keycode::D: + this->get_component<Rigidbody>().data.linear_velocity.x = move_speed; + this->body->data.flip = {false, false}; + this->head->data.flip = {false, false}; + break; + + case Keycode::SPACE: + this->get_component<Rigidbody>().data.linear_velocity.y = -move_speed; + break; + case Keycode::D0: + this->body_anim->set_anim(0); + this->head_anim->set_anim(0); + break; + case Keycode::D1: + this->body_anim->set_anim(1); + this->head_anim->set_anim(1); + break; + case Keycode::D2: + this->body_anim->set_anim(2); + this->head_anim->set_anim(2); + break; + case Keycode::D3: + this->body_anim->set_anim(3); + this->head_anim->set_anim(3); + break; + case Keycode::D4: + this->body_anim->set_anim(4); + this->head_anim->set_anim(4); + break; + case Keycode::D5: + this->body_anim->set_anim(5); + this->head_anim->set_anim(5); + break; + case Keycode::D6: + this->body_anim->set_anim(6); + this->head_anim->set_anim(6); + break; + case Keycode::D7: + this->body_anim->set_anim(7); + this->head_anim->set_anim(7); + break; + case Keycode::LEFT: + this->head->data.angle_offset -= 1; + break; + case Keycode::RIGHT: + this->head->data.angle_offset += 1; + break; + case Keycode::UP: + this->head->data.scale_offset += 0.1; + break; + case Keycode::DOWN: + this->head->data.scale_offset -= 0.1; + break; + case Keycode::P: + this->get_component<AudioSource>().play(); + break; + case Keycode::Q: + this->get_components_by_name<Camera>("camera").front().get().data.zoom -= 0.01; + break; + case Keycode::E: + this->get_components_by_name<Camera>("camera").front().get().data.zoom += 0.01; + break; + case Keycode::J: + this->get_components_by_name<Transform>("camera").front().get().position.x + -= move_speed; + break; + case Keycode::K: + this->get_components_by_name<Transform>("camera").front().get().position.y + -= move_speed; + break; + case Keycode::L: + this->get_components_by_name<Transform>("camera").front().get().position.x + += move_speed; + break; + case Keycode::I: + this->get_components_by_name<Transform>("camera").front().get().position.y + += move_speed; + break; + case Keycode::M: + trigger_event<MissileSpawnEvent>(MissileSpawnEvent {}); + break; + //todo + case Keycode::PAGE_UP: + case Keycode::PAGE_DOWN: + case Keycode::HOME: + break; + default: + break; + } + return false; +} + +void PrevPlayerScript::init() { + auto animations = this->get_components<Animator>(); + body_anim = animations[0]; + head_anim = animations[1]; + + auto sprites = this->get_components<Sprite>(); + body = sprites[0]; + head = sprites[1]; + + subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { + return this->key_pressed(ev); + }); +}; + +void PrevPlayerScript::fixed_update(crepe::duration_t dt) { + auto & savemgr = this->get_save_manager(); + const auto & pos = this->get_component<Transform>().position; + + savemgr.set("player_x", pos.x); + savemgr.set("player_y", pos.y); +}; diff --git a/game/preview/PrevPlayerScript.h b/game/preview/PrevPlayerScript.h new file mode 100644 index 0000000..cc3184e --- /dev/null +++ b/game/preview/PrevPlayerScript.h @@ -0,0 +1,23 @@ + +#include <crepe/api/Event.h> +#include <crepe/api/Script.h> +#include <crepe/util/OptionalRef.h> + +#include <crepe/api/Animator.h> +#include <crepe/api/Sprite.h> + +class PrevPlayerScript : public crepe::Script { +private: + crepe::OptionalRef<crepe::Animator> head_anim; + crepe::OptionalRef<crepe::Animator> body_anim; + crepe::OptionalRef<crepe::Sprite> head; + crepe::OptionalRef<crepe::Sprite> body; + +private: + float move_speed = 100; + +private: + void init(); + void fixed_update(crepe::duration_t dt); + bool key_pressed(const crepe::KeyPressEvent & ev); +}; diff --git a/game/preview/PrevPlayerSubScene.cpp b/game/preview/PrevPlayerSubScene.cpp new file mode 100644 index 0000000..b59a0af --- /dev/null +++ b/game/preview/PrevPlayerSubScene.cpp @@ -0,0 +1,86 @@ + +#include "PrevPlayerSubScene.h" + +#include "../Config.h" +#include "PrevPlayerScript.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/AudioSource.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> + +#include <crepe/ValueBroker.h> +#include <crepe/manager/SaveManager.h> + +using namespace crepe; + +PrevPlayerSubScene::PrevPlayerSubScene(Scene & scn) { + auto & savemgr = scn.get_save_manager(); + + ValueBroker player_x = savemgr.get<float>("player_x", 500); + ValueBroker player_y = savemgr.get<float>("player_y", -100); + + GameObject player + = scn.new_object("player", "TAG", vec2 {player_x.get(), player_y.get()}, 0, 1); + 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<PrevPlayerScript>(); +} diff --git a/game/preview/PrevPlayerSubScene.h b/game/preview/PrevPlayerSubScene.h new file mode 100644 index 0000000..a61f341 --- /dev/null +++ b/game/preview/PrevPlayerSubScene.h @@ -0,0 +1,10 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class PrevPlayerSubScene { +public: + PrevPlayerSubScene(crepe::Scene & scn); +}; diff --git a/game/preview/SmokeSubScene.cpp b/game/preview/SmokeSubScene.cpp new file mode 100644 index 0000000..e363f95 --- /dev/null +++ b/game/preview/SmokeSubScene.cpp @@ -0,0 +1,37 @@ + +#include "SmokeSubScene.h" + +#include "../Config.h" + +#include <crepe/api/ParticleEmitter.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> + +using namespace crepe; + +SmokeSubScene::SmokeSubScene(Scene & scn) { + GameObject smoke = scn.new_object("smoke_particle", "TAG", vec2 {500, -210}, 0, 1); + + Asset smoke_ss {"asset/particles/smoke.png"}; + + auto & smoke_sprite = smoke.add_component<Sprite>( + smoke_ss, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_PARTICLES_FOREGROUND, + .size = {0, 30}, + } + ); + + smoke.add_component<ParticleEmitter>( + smoke_sprite, + ParticleEmitter::Data { + .offset = {0, -60}, + .max_particles = 10, + .emission_rate = 25, + .min_angle = 60, + .max_angle = 120, + .begin_lifespan = 1, + .end_lifespan = 2, + } + ); +} diff --git a/game/preview/SmokeSubScene.h b/game/preview/SmokeSubScene.h new file mode 100644 index 0000000..93d8a2d --- /dev/null +++ b/game/preview/SmokeSubScene.h @@ -0,0 +1,10 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class SmokeSubScene { +public: + SmokeSubScene(crepe::Scene & scn); +}; |