1 /** 2d scene camera */
2 
3 module re.ng.camera.cam2d;
4 
5 import re.ecs;
6 import re.time;
7 import re.math;
8 import re.ng.camera.base;
9 import re.gfx.raytypes;
10 import std.math;
11 static import raylib;
12 
13 /// represents a camera for a 2D scene
14 class SceneCamera2D : SceneCamera {
15     mixin Reflect;
16     private raylib.Camera2D _camera;
17 
18     this() {
19         _camera = raylib.Camera2D();
20     }
21 
22     override void setup() {
23         // defaults
24         _camera.zoom = 1;
25     }
26 
27     @property ref raylib.Camera2D camera() return  {
28         return _camera;
29     }
30 
31     /// gets the camera offset (displacement from target)
32     @property Vector2 offset() {
33         return _camera.offset;
34     }
35 
36     /// sets the camera offset (displacement from target)
37     @property Vector2 offset(Vector2 value) {
38         return _camera.offset = value;
39     }
40 
41     override void update() {
42         super.update();
43 
44         // copy entity to camera transform
45         _camera.target = entity.transform.position2;
46         _camera.rotation = entity.transform.rotation_z;
47         _camera.zoom = entity.transform.scale.x;
48     }
49 }
50 
51 class CameraFollow2D : Component, Updatable {
52     mixin Reflect;
53     // private SceneCamera2D cam;
54     /// the target entity to follow
55     public Entity target;
56     public float lerp;
57     private SceneCamera2D cam;
58 
59     this(Entity target, float lerp) {
60         this.target = target;
61         this.lerp = lerp;
62     }
63 
64     override void setup() {
65         cam = entity.get_component!SceneCamera2D();
66     }
67 
68     void update() {
69         // set offset to half-resolution (so that our target is centered)
70         cam.offset = Vector2(entity.scene.resolution.x / 2, entity.scene.resolution.y / 2);
71         
72         // get vector to target
73         auto to_target = target.position2 - entity.position2;
74         auto scroll = Vector2(to_target.x * lerp, to_target.y * lerp);
75         entity.position2 = entity.position2 + scroll;
76     }
77 }