1 /// contains the Scene class
2 module ysge.scene;
3 
4 import ysge.project;
5 public import ysge.objects.simpleBox;
6 
7 /// individual scene containing objects
8 class Scene {
9 	GameObject[]     objects;
10 	UIElement[]      ui;
11 	SDL_Color        bg;
12 	Vec2!int         camera;
13 	bool             cameraFollowsObject;
14 	Vec2!int*        cameraFollow;
15 	AnimationManager animations;
16 
17 	/// called when this scene is set as the project's current scene
18 	abstract void Init(Project parent);
19 	/// run every frame
20 	abstract void Update(Project parent);
21 	/// run when an event is created
22 	abstract void HandleEvent(Project parent, SDL_Event e);
23 	/// run after an object is rendered
24 	abstract void PostRender(Project parent, GameObject obj);
25 
26 	/// makes the camera follow an object
27 	void CameraFollowObject(SimpleBox obj) {
28 		cameraFollowsObject = true;
29 		cameraFollow        = cast(Vec2!int*) &obj.box;
30 	}
31 
32 	/// stops the camera from following an object
33 	void StopFollowingObject() {
34 		cameraFollowsObject = false;
35 		cameraFollow        = null;
36 	}
37 
38 	/// adds a game object to the scene's object array
39 	void AddObject(GameObject object) {
40 		objects ~= object;
41 	}
42 
43 	/// adds a UI element to the scene
44 	void AddUI(UIElement element) {
45 		ui ~= element;
46 	}
47 
48 	/**
49 	* sets up a scene
50 	* should not be called by the user
51 	*/
52 	void Setup() {
53 		objects    = [];
54 		ui         = [];
55 		animations = new AnimationManager();
56 	}
57 
58 	/**
59 	* makes the camera follow an object if enabled
60 	* should not be called by the user
61 	*/
62 	void UpdateCamera(Project parent) {
63 		if (cameraFollowsObject) {
64 			auto     screenRes = parent.GetResolution();
65 			Vec2!int pos       = Vec2!int(cameraFollow.x, cameraFollow.y);
66 			Vec2!int size      = Vec2!int(cameraFollow.x, cameraFollow.y);
67 
68 			camera.x = pos.x - (screenRes.x / 2);
69 			camera.y = pos.y - (screenRes.y / 2);
70 		}
71 	}
72 
73 	/**
74 	* calls the update function of all objects in the scene
75 	* should not be called by the user
76 	*/
77 	void UpdateObjects(Project parent) {
78 		foreach (ref object ; objects) {
79 			object.Update(parent);
80 		}
81 	}
82 
83 	/**
84 	* sends event to UI elements
85 	* should not be called by the user
86 	*/
87 	bool HandleUIEvent(Project parent, SDL_Event e) {
88 		foreach (ref element ; ui) {
89 			if (element.HandleEvent(parent, e)) {
90 				return true;
91 			}
92 		}
93 		return false;
94 	}
95 
96 	/// render the scene, should not be called by the user
97 	void Render(Project parent) {
98 		SDL_SetRenderDrawColor(parent.renderer, bg.r, bg.g, bg.b, bg.a);
99 		SDL_RenderClear(parent.renderer);
100 
101 		foreach (ref object ; objects) {
102 			object.Render(parent);
103 			PostRender(parent, object);
104 		}
105 		
106 		foreach (ref element ; ui) {
107 			element.Render(parent);
108 		}
109 
110 		SDL_RenderPresent(parent.renderer);
111 	}
112 }