aboutsummaryrefslogtreecommitdiff
path: root/src/example/replay.cpp
blob: 00a6502bccb79a07ac8605e6921cb1eabc160da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <crepe/api/Config.h>
#include <crepe/api/Engine.h>
#include <crepe/api/Script.h>

using namespace crepe;
using namespace std;

class AnimationScript : public Script {
	Transform * transform;
	float t = 0;

	void init() { transform = &get_component<Transform>(); }

	void update() {
		t += 0.05;
		transform->position = {sin(t), cos(t)};
	}
};

class Timeline : public Script {
	unsigned i = 0;
	recording_t recording;

	void update() {
		switch (i++) {
			default:
				break;
			case 10:
				logf("record start");
				replay.record_start();
				break;
			case 60:
				logf("record end, playing recording");
				this->recording = replay.record_end();
				replay.play(this->recording);
				break;
			case 61:
				logf("done, releasing recording");
				replay.release(this->recording);
				break;
			case 72:
				logf("exit");
				queue_event<ShutDownEvent>();
				break;
		};
	}
};

class TestScene : public Scene {
public:
	using Scene::Scene;

	void load_scene() {
		Mediator & mediator = this->mediator;
		ComponentManager & mgr = mediator.component_manager;

		GameObject cam = mgr.new_object("cam");
		cam.add_component<Camera>(
			ivec2 {640, 480}, vec2 {3, 3},
			Camera::Data {
				.bg_color = Color::WHITE,
			}
		);

		GameObject square = mgr.new_object("square");
		square.add_component<Sprite>(
			Asset {"asset/texture/square.png"},
			Sprite::Data {
				.size = {0.5, 0.5},
			}
		);
		square.add_component<BehaviorScript>().set_script<AnimationScript>();

		GameObject scapegoat = mgr.new_object("");
		scapegoat.add_component<BehaviorScript>().set_script<Timeline>();
	}

	string get_name() const { return "scene1"; }
};

int main(int argc, char * argv[]) {
	Config & cfg = Config::get_instance();
	cfg.log.level = Log::Level::DEBUG;

	Engine engine;

	engine.add_scene<TestScene>();
	return engine.main();
}