aboutsummaryrefslogtreecommitdiff
path: root/game/preview
diff options
context:
space:
mode:
Diffstat (limited to 'game/preview')
-rw-r--r--game/preview/NpcScript.cpp17
-rw-r--r--game/preview/NpcScript.h5
-rw-r--r--game/preview/NpcSubScene.cpp10
-rw-r--r--game/preview/PrevPlayerScript.cpp71
-rw-r--r--game/preview/PrevPlayerScript.h17
-rw-r--r--game/preview/PrevPlayerSubScene.cpp19
-rw-r--r--game/preview/PreviewReplaySubScript.cpp55
-rw-r--r--game/preview/PreviewReplaySubScript.h24
-rw-r--r--game/preview/PreviewStartRecSubScript.cpp20
-rw-r--r--game/preview/PreviewStartRecSubScript.h11
-rw-r--r--game/preview/PreviewStopRecSubScript.cpp20
-rw-r--r--game/preview/PreviewStopRecSubScript.h11
12 files changed, 223 insertions, 57 deletions
diff --git a/game/preview/NpcScript.cpp b/game/preview/NpcScript.cpp
index c4148f2..86117d4 100644
--- a/game/preview/NpcScript.cpp
+++ b/game/preview/NpcScript.cpp
@@ -7,26 +7,23 @@
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 npc = this->get_components<Sprite>();
auto & transform = this->get_component<Transform>();
- if (transform.position.x < -990) {
+ if (transform.position.x < 200) {
rb.data.linear_velocity.x *= -1;
}
- if (transform.position.x > 990) {
+ if (transform.position.x > 700) {
rb.data.linear_velocity.x *= -1;
}
if (rb.data.linear_velocity.x < 0) {
- npc.data.flip = {true, false};
+ npc.front().get().data.flip = {true, false};
+ npc.back().get().data.flip = {true, false};
} else {
- npc.data.flip = {false, false};
+ npc.front().get().data.flip = {false, false};
+ npc.back().get().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
index 8d856fd..d278f83 100644
--- a/game/preview/NpcScript.h
+++ b/game/preview/NpcScript.h
@@ -1,11 +1,8 @@
+#pragma once
#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
index bd6cfb2..5ededb6 100644
--- a/game/preview/NpcSubScene.cpp
+++ b/game/preview/NpcSubScene.cpp
@@ -15,11 +15,7 @@
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);
+ GameObject npc = scn.new_object("npc", "npc_tag", vec2 {500, 0}, 0, 1);
Asset npc_body {"asset/workers/worker1Body.png"};
Asset npc_head {"asset/workers/worker1Head.png"};
@@ -53,7 +49,7 @@ NpcSubScene::NpcSubScene(Scene & scn) {
.looping = true,
}
);
- npc.add_component<BoxCollider>(vec2 {50, 50});
+ npc.add_component<BoxCollider>(vec2 {40, 50});
npc.add_component<Rigidbody>(Rigidbody::Data {
.mass = 10,
@@ -61,7 +57,7 @@ NpcSubScene::NpcSubScene(Scene & scn) {
.body_type = Rigidbody::BodyType::DYNAMIC,
.linear_velocity = {-50, 0},
//.max_linear_velocity = 40,
- .collision_layers = {COLL_LAY_BOT_TOP, COLL_LAY_PLAYER},
+ .collision_layers = {COLL_LAY_BOT_TOP, 100},
.collision_layer = COLL_LAY_PLAYER,
});
diff --git a/game/preview/PrevPlayerScript.cpp b/game/preview/PrevPlayerScript.cpp
index 2657b8d..ae25dad 100644
--- a/game/preview/PrevPlayerScript.cpp
+++ b/game/preview/PrevPlayerScript.cpp
@@ -1,12 +1,11 @@
#include "PrevPlayerScript.h"
#include "../missile/SpawnEvent.h"
-#include "api/Transform.h"
+
#include <crepe/api/AudioSource.h>
#include <crepe/api/Camera.h>
+#include <crepe/api/Transform.h>
#include <crepe/manager/SaveManager.h>
-#include <iostream>
-#include <ostream>
using namespace crepe;
@@ -22,10 +21,6 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) {
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);
@@ -59,25 +54,20 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) {
this->head_anim->set_anim(7);
break;
case Keycode::LEFT:
- this->head->data.angle_offset -= 1;
+ this->get_component<Transform>().rotation += 10;
break;
case Keycode::RIGHT:
- this->head->data.angle_offset += 1;
+ this->get_component<Transform>().rotation -= 10;
break;
case Keycode::UP:
- this->head->data.scale_offset += 0.1;
+ this->head->data.position_offset += 10;
break;
case Keycode::DOWN:
- this->head->data.scale_offset -= 0.1;
+ this->head->data.position_offset -= 10;
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;
+ this->get_components_by_name<AudioSource>("background_music").front().get().active
+ = true;
break;
case Keycode::J:
this->get_components_by_name<Transform>("camera").front().get().position.x
@@ -98,11 +88,6 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) {
case Keycode::M:
trigger_event<MissileSpawnEvent>(MissileSpawnEvent {});
break;
- //todo
- case Keycode::PAGE_UP:
- case Keycode::PAGE_DOWN:
- case Keycode::HOME:
- break;
default:
break;
}
@@ -110,6 +95,8 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) {
}
void PrevPlayerScript::init() {
+ this->rb = get_component<Rigidbody>();
+
auto animations = this->get_components<Animator>();
body_anim = animations[0];
head_anim = animations[1];
@@ -121,12 +108,50 @@ void PrevPlayerScript::init() {
subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool {
return this->key_pressed(ev);
});
+ subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool {
+ if (ev.repeat) return false;
+ return this->on_key_down(ev);
+ });
+ subscribe<KeyReleaseEvent>([this](const KeyReleaseEvent & ev) -> bool {
+ return this->on_key_up(ev);
+ });
};
void PrevPlayerScript::fixed_update(crepe::duration_t dt) {
+ if (this->get_key_state(Keycode::SPACE)) {
+ this->rb->add_force_linear(
+ vec2(0, -1) * (engine_gravity * PLAYER_GRAVITY_SCALE * dt.count())
+ );
+ }
+
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);
};
+
+bool PrevPlayerScript::on_key_down(const KeyPressEvent & ev) {
+ if (ev.key == Keycode::SPACE) {
+ const vec2 UP = {0, -1};
+ this->help_kick(UP);
+ }
+ return false;
+}
+
+bool PrevPlayerScript::on_key_up(const KeyReleaseEvent & ev) {
+ if (ev.key == Keycode::SPACE) {
+ const vec2 DOWN = {0, 1};
+ this->help_kick(DOWN);
+ }
+ return false;
+}
+
+void PrevPlayerScript::help_kick(const vec2 & direction) {
+ // softly "kick" the player (at start/end of flight)
+ vec2 & velocity = this->rb->data.linear_velocity;
+ float kick_amount = std::min(
+ velocity.length() * PLAYER_HELP_KICK_SCALE, engine_gravity * PLAYER_HELP_KICK_MAX
+ );
+ velocity += direction * kick_amount;
+}
diff --git a/game/preview/PrevPlayerScript.h b/game/preview/PrevPlayerScript.h
index cc3184e..ae66449 100644
--- a/game/preview/PrevPlayerScript.h
+++ b/game/preview/PrevPlayerScript.h
@@ -1,10 +1,10 @@
-
+#include <crepe/api/Animator.h>
+#include <crepe/api/Config.h>
#include <crepe/api/Event.h>
+#include <crepe/api/Rigidbody.h>
#include <crepe/api/Script.h>
-#include <crepe/util/OptionalRef.h>
-
-#include <crepe/api/Animator.h>
#include <crepe/api/Sprite.h>
+#include <crepe/util/OptionalRef.h>
class PrevPlayerScript : public crepe::Script {
private:
@@ -20,4 +20,13 @@ private:
void init();
void fixed_update(crepe::duration_t dt);
bool key_pressed(const crepe::KeyPressEvent & ev);
+
+private:
+ bool on_key_down(const crepe::KeyPressEvent & ev);
+ bool on_key_up(const crepe::KeyReleaseEvent & ev);
+ void help_kick(const crepe::vec2 & direction);
+
+private:
+ float & engine_gravity = crepe::Config::get_instance().physics.gravity;
+ crepe::OptionalRef<crepe::Rigidbody> rb;
};
diff --git a/game/preview/PrevPlayerSubScene.cpp b/game/preview/PrevPlayerSubScene.cpp
index b59a0af..074cfb4 100644
--- a/game/preview/PrevPlayerSubScene.cpp
+++ b/game/preview/PrevPlayerSubScene.cpp
@@ -16,13 +16,8 @@
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);
+ GameObject player = scn.new_object("player", "player", vec2 {800, -100}, 0, 1);
Asset player_body_asset {"asset/barry/defaultBody.png"};
Sprite & player_body_sprite = player.add_component<Sprite>(
player_body_asset,
@@ -75,12 +70,18 @@ PrevPlayerSubScene::PrevPlayerSubScene(Scene & scn) {
}
);
player.add_component<Rigidbody>(Rigidbody::Data {
- .gravity_scale = 20,
+ .gravity_scale = 1,
.body_type = Rigidbody::BodyType::DYNAMIC,
.linear_velocity = vec2(100, 0),
- .collision_layers = {COLL_LAY_BOT_TOP},
+ .collision_layers = {COLL_LAY_BOT_TOP, 100},
.collision_layer = COLL_LAY_PLAYER,
});
- player.add_component<BoxCollider>(vec2(50, 50));
+ player.add_component<BoxCollider>(vec2(40, 50));
player.add_component<BehaviorScript>().set_script<PrevPlayerScript>();
+
+ GameObject music = scn.new_object("background_music", "background_music");
+ AudioSource & audio = music.add_component<AudioSource>(Asset {"asset/music/level.ogg"});
+ audio.loop = true;
+ audio.play_on_awake = true;
+ audio.active = false;
}
diff --git a/game/preview/PreviewReplaySubScript.cpp b/game/preview/PreviewReplaySubScript.cpp
new file mode 100644
index 0000000..580a608
--- /dev/null
+++ b/game/preview/PreviewReplaySubScript.cpp
@@ -0,0 +1,55 @@
+#include "PreviewReplaySubScript.h"
+#include "Config.h"
+#include "menus/ButtonReplaySubScript.h"
+#include <crepe/api/AudioSource.h>
+#include <crepe/types.h>
+
+using namespace crepe;
+using namespace std;
+
+void PreviewReplaySubScript::init() {
+ IButtonScript::init();
+ this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent & e) {
+ return this->on_button_press(e);
+ });
+ this->subscribe<StopPreviewRecording>([this](const StopPreviewRecording & e) {
+ return this->stop_recording();
+ });
+ this->subscribe<DeleteRecordingEvent>([this](const DeleteRecordingEvent & e) {
+ return this->delete_recording();
+ });
+ this->subscribe<StartPreviewRecording>([this](const StartPreviewRecording & e) {
+ return this->start_recording();
+ });
+}
+
+bool PreviewReplaySubScript::on_button_press(const ButtonPressEvent & e) {
+ if (DISABLE_REPLAY) return false;
+ replay.play(this->recording);
+ return false;
+}
+bool PreviewReplaySubScript::start_recording() {
+ if (DISABLE_REPLAY) return false;
+ if (record_saved) {
+ this->stop_recording();
+ this->delete_recording();
+ }
+ replay.record_start();
+ this->record_started = true;
+ return false;
+}
+
+bool PreviewReplaySubScript::stop_recording() {
+ if (DISABLE_REPLAY) return false;
+ if (this->record_started) this->recording = replay.record_end();
+ this->record_saved = true;
+ return false;
+}
+
+bool PreviewReplaySubScript::delete_recording() {
+ if (DISABLE_REPLAY) return false;
+ if (this->record_started) this->stop_recording();
+ if (this->record_saved) replay.release(this->recording);
+ this->record_saved = false;
+ return false;
+}
diff --git a/game/preview/PreviewReplaySubScript.h b/game/preview/PreviewReplaySubScript.h
new file mode 100644
index 0000000..59b78c3
--- /dev/null
+++ b/game/preview/PreviewReplaySubScript.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "menus/IButtonScript.h"
+
+#include <crepe/api/Script.h>
+
+struct StartPreviewRecording : public crepe::Event {};
+struct StopPreviewRecording : public crepe::Event {};
+
+class PreviewReplaySubScript : public IButtonScript {
+public:
+ void init() override;
+ bool on_button_press(const crepe::ButtonPressEvent & e);
+
+private:
+ crepe::recording_t recording = 0;
+ bool start_recording();
+ bool stop_recording();
+ bool delete_recording();
+
+private:
+ bool record_saved = false;
+ bool record_started = false;
+};
diff --git a/game/preview/PreviewStartRecSubScript.cpp b/game/preview/PreviewStartRecSubScript.cpp
new file mode 100644
index 0000000..8a2f54c
--- /dev/null
+++ b/game/preview/PreviewStartRecSubScript.cpp
@@ -0,0 +1,20 @@
+#include "PreviewStartRecSubScript.h"
+#include "PreviewReplaySubScript.h"
+
+#include <crepe/api/AudioSource.h>
+#include <crepe/types.h>
+
+using namespace crepe;
+using namespace std;
+
+void PreviewStartRecSubScript::init() {
+ IButtonScript::init();
+ this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent & e) {
+ return this->on_button_press(e);
+ });
+}
+
+bool PreviewStartRecSubScript::on_button_press(const ButtonPressEvent & e) {
+ this->trigger_event<StartPreviewRecording>();
+ return false;
+}
diff --git a/game/preview/PreviewStartRecSubScript.h b/game/preview/PreviewStartRecSubScript.h
new file mode 100644
index 0000000..a54a085
--- /dev/null
+++ b/game/preview/PreviewStartRecSubScript.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "menus/IButtonScript.h"
+
+#include <crepe/api/Script.h>
+
+class PreviewStartRecSubScript : public IButtonScript {
+public:
+ void init() override;
+ bool on_button_press(const crepe::ButtonPressEvent & e);
+};
diff --git a/game/preview/PreviewStopRecSubScript.cpp b/game/preview/PreviewStopRecSubScript.cpp
new file mode 100644
index 0000000..a229da8
--- /dev/null
+++ b/game/preview/PreviewStopRecSubScript.cpp
@@ -0,0 +1,20 @@
+#include "PreviewStopRecSubScript.h"
+#include "PreviewReplaySubScript.h"
+
+#include <crepe/api/AudioSource.h>
+#include <crepe/types.h>
+
+using namespace crepe;
+using namespace std;
+
+void PreviewStopRecSubScript::init() {
+ IButtonScript::init();
+ this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent & e) {
+ return this->on_button_press(e);
+ });
+}
+
+bool PreviewStopRecSubScript::on_button_press(const ButtonPressEvent & e) {
+ this->trigger_event<StopPreviewRecording>();
+ return false;
+}
diff --git a/game/preview/PreviewStopRecSubScript.h b/game/preview/PreviewStopRecSubScript.h
new file mode 100644
index 0000000..b2dd73b
--- /dev/null
+++ b/game/preview/PreviewStopRecSubScript.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "menus/IButtonScript.h"
+
+#include <crepe/api/Script.h>
+
+class PreviewStopRecSubScript : public IButtonScript {
+public:
+ void init() override;
+ bool on_button_press(const crepe::ButtonPressEvent & e);
+};