Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 4.58 KB | None | 0 0
  1. package;
  2.  
  3. import flixel.math.FlxRect;
  4. import openfl.display.Graphics;
  5. import flixel.math.FlxVector;
  6. import flixel.math.FlxPoint;
  7. import flixel.FlxCamera;
  8. import flixel.FlxG;
  9. import flixel.FlxObject;
  10. import flixel.FlxSprite;
  11. import flixel.math.FlxVelocity;
  12.  
  13. using zero.ext.FloatExt;
  14. using zero.ext.flx.FlxPointExt;
  15.  
  16. class Dolly extends FlxObject {
  17.     var target:Null<BaseEntity>;
  18.     var zoom:Float;
  19.     var lerp:Float;
  20.     var mode:Mode;
  21.     var radius:Float = 20;
  22.     var forward_offset:Float = 5;
  23.  
  24.     public function new(target:BaseEntity) {
  25.         super(0, 0, radius * 2, radius * 2);
  26.         lerp = 0.25;
  27.         switch_target(target, true);
  28.         init();
  29.     }
  30.  
  31.     function init() {
  32.         FlxG.camera.follow(this, FlxCameraFollowStyle.LOCKON);
  33.         // FlxG.camera.followLead.set(0, 20);
  34.         FlxG.camera.deadzone.set((FlxG.camera.width - width).half(), (FlxG.camera.height - height).half(), width, height);
  35.     }
  36.  
  37.     function focus(position:FlxPoint) {
  38.         x = position.x - width.half();
  39.         y = position.y - height.half();
  40.     }
  41.  
  42.     /**
  43.      *  Switches the camera target
  44.      *  @param target   the target to follow
  45.      *  @param snap     whether or not to snap to target immediately
  46.      */
  47.     public function switch_target(?target:BaseEntity, snap:Bool = false):Void {
  48.         this.target = target;
  49.         if (target == null) return;
  50.         if (snap) focus(target.getPosition().add(target.width.half(), target.height));
  51.     }
  52.  
  53.     override public function update(dt:Float):Void {
  54.         if (target == null) {
  55.             super.update(dt);
  56.             return;
  57.         }
  58.  
  59.         var pos = getMidpoint();
  60.         var tarPos = target.getMidpoint();
  61.  
  62.         forward_focus(pos, tarPos);
  63.         update_position(pos, tarPos);
  64.         camera_angle();
  65.  
  66.         super.update(dt);
  67.         pos.put();
  68.         tarPos.put();
  69.     }
  70.  
  71.     function forward_focus(pos:FlxPoint, tarPos:FlxPoint) {
  72.         var v = FlxVelocity.velocityFromAngle(target.relative_rotation, forward_offset);
  73.         setPosition(x + (v.x * 0.3), y + (v.y * 0.3));
  74.         v.put();
  75.     }
  76.  
  77.     function update_position(pos:FlxPoint, tarPos:FlxPoint) {
  78.         var d = pos.distance(tarPos);
  79.  
  80.         if (d > radius) {
  81.             var a = pos.get_angle_between(tarPos);
  82.             var o = (d - radius) * 1.5;
  83.             var v = FlxVelocity.velocityFromAngle(a, o);
  84.             setPosition(x + (v.x * lerp), y + (v.y * lerp));
  85.             v.put();
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Sets the angle of the camera based on:
  91.      * - if the target is looking at another sprite
  92.      * - The target's velocity
  93.      */
  94.     function camera_angle() {
  95.         var lerp_angle:Float = 0;
  96.         if (target.target != null) {
  97.             lerp_angle = 0.09;
  98.         } else if (target.rotation > 175 || target.rotation < 5) {
  99.             lerp_angle = target.velocity.vector_length() > 0.5 ? 0.009 : 0.01;
  100.         } else {
  101.             lerp_angle = target.velocity.vector_length() > 0.5 ? 0 : 0.09;
  102.         }
  103.  
  104.         if (lerp_angle != 0) {
  105.             FlxG.camera.angle = FlxG.camera.angle.translate_to_nearest_angle(-target.relative_rotation - 90);
  106.             FlxG.camera.angle += (-target.relative_rotation - 90 - FlxG.camera.angle) * lerp_angle;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Tells if a point 'p' lies within a ellipse of center 'c' and dimensions 'wh'.
  112.      */
  113.     function targetInEllipse():Bool {
  114.         // Get Center
  115.         var c = FlxVector.get();
  116.         getMidpoint(c);
  117.         // Get Target Center
  118.         var p = FlxVector.get();
  119.         target.getMidpoint(p);
  120.         // Get Angle Between
  121.         var pc = p.subtractNew(c);
  122.         var a = pc.degrees;
  123.         // Recycle all our FlxVectors
  124.         pc.put();
  125.         c.put();
  126.         p.put();
  127.         // Do our math
  128.         var ca = Math.cos(a) * radius;
  129.         var sa = Math.sin(a) * radius;
  130.         return p.distSquared(c) <= ca * ca + sa * sa;
  131.     }
  132.  
  133.     #if FLX_DEBUG
  134.     /**
  135.      * Override this function to draw custom "debug mode" graphics to the
  136.      * specified camera while the debugger's `drawDebug` mode is toggled on.
  137.      *
  138.      * @param   Camera   Which camera to draw the debug visuals to.
  139.      */
  140.     override public function drawDebugOnCamera(camera:FlxCamera):Void {
  141.         if (!camera.visible || !camera.exists || !isOnScreen(camera))
  142.             return;
  143.  
  144.         var rect = getBoundingBox(camera);
  145.         var gfx:Graphics = beginDrawDebug(camera);
  146.         drawDebugBoundingEllipse(gfx, rect, allowCollisions, immovable);
  147.         endDrawDebug(camera);
  148.     }
  149.  
  150.     function drawDebugBoundingEllipse(gfx:Graphics, rect:FlxRect, allowCollisions:Int, partial:Bool) {
  151.         // Find the color to use
  152.         var color:Null<Int> = debugBoundingBoxColor;
  153.         if (color == null) {
  154.             if (target != null)
  155.                 color = targetInEllipse() ? debugBoundingBoxColorPartial : debugBoundingBoxColorSolid;
  156.             else
  157.                 color = debugBoundingBoxColorNotSolid;
  158.         }
  159.         // fill static graphics object with square shape
  160.         gfx.lineStyle(1, color, 0.5);
  161.         gfx.drawEllipse(rect.x, rect.y, rect.width, rect.height);
  162.     }
  163.     #end
  164. }
  165.  
  166. @:enum
  167. abstract Mode(Int) from Int to Int {
  168.     var FOLLOW = 0;
  169.     var TARGET = 1;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement