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