aboutsummaryrefslogtreecommitdiff
path: root/src/example/AITest.cpp
blob: 857eb947415712421c80e22daff36882a2b05653 (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
#include <crepe/api/AI.h>
#include <crepe/api/BehaviorScript.h>
#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/LoopManager.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Scene.h>
#include <crepe/api/Script.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
#include <crepe/manager/Mediator.h>
#include <crepe/types.h>

using namespace crepe;
using namespace std;

class Script1 : public Script {
	bool shutdown(const ShutDownEvent & event) {
		// Very dirty way of shutting down the game
		throw "ShutDownEvent";
		return true;
	}

	bool mousemove(const MouseMoveEvent & event) {
		/*RefVector<AI> aivec = this->get_components<AI>();
		AI & ai = aivec.front().get();
		ai.flee_target
			= vec2{static_cast<float>(event.mouse_x), static_cast<float>(event.mouse_y)};*/
		return true;
	}

	void init() {
		subscribe<ShutDownEvent>(
			[this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); });
		subscribe<MouseMoveEvent>(
			[this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); });
	}
};

class Scene1 : public Scene {
public:
	void load_scene() override {
		Mediator & mediator = this->mediator;
		ComponentManager & mgr = mediator.component_manager;

		GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1);
		GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1);

		Texture img = Texture("asset/texture/test_ap43.png");
		game_object1.add_component<Sprite>(img, Color::MAGENTA,
										   Sprite::FlipSettings{false, false}, 1, 1, 195);
		AI & ai = game_object1.add_component<AI>(3000);
		// ai.arrive_on();
		// ai.flee_on();
		ai.path_follow_on();
		ai.make_circle_path(1000, {0, -1000}, 1.5707, true);
		ai.make_circle_path(1000, {0, 1000}, 4.7124, false);
		game_object1.add_component<Rigidbody>(Rigidbody::Data{
			.mass = 0.5f, .max_linear_velocity = {40, 40}, // sqrt(21^2 + 21^2) = 30
		});
		game_object1.add_component<BehaviorScript>().set_script<Script1>();

		game_object2.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{5000, 5000},
										   1.0f);
	}

	string get_name() const override { return "Scene1"; }
};

int main() {
	LoopManager engine;
	engine.add_scene<Scene1>();
	engine.start();

	return 0;
}