Path: blob/main/public/games/files/garbage-collector/js/effects/camera-path.js
1036 views
/*globals define*/1define([2'base-object',3'geometry/rect',4'geometry/intersection',5'utils'6], function( BaseObject, Rect, Intersection, Utils ) {7'use strict';89// Absolute width, height, and angle.10function Keyframe( width, height, angle ) {11BaseObject.call( this );1213this.width = width || 0;14this.height = height || 0;15this.angle = angle || 0;16}1718Keyframe.prototype = new BaseObject();19Keyframe.prototype.constructor = Keyframe;202122function CameraPath( x, y, width, height ) {23Rect.call( this, x, y, width, height );2425this.camera = null;26// This is usually the player.27this.target = null;2829this.start = new Keyframe();30this.end = new Keyframe();31}3233CameraPath.prototype = new Rect();34CameraPath.prototype.constructor = CameraPath;3536CameraPath.Keyframe = Keyframe;3738CameraPath.prototype.update = function() {39if ( !this.target || !this.camera ) {40return;41}4243var x = this.target.x,44y = this.target.y;4546if ( !this.contains( x, y ) ) {47return;48}4950var x0 = this.left,51y0 = this.top,52x1 = this.right,53y1 = this.bottom;5455var t = Intersection.closestPointOnLineParameter( x, y, x0, y0, x1, y1 );56if ( 0 > t || t > 1 ) {57return;58}5960if ( t === 0 ) {61this.camera.set( this.start );62}6364if ( t === 1 ) {65this.camera.set( this.end );66}6768this.camera.width = Utils.lerp( this.start.width, this.end.width, t );69this.camera.height = Utils.lerp( this.start.height, this.end.height, t );70this.camera.angle = Utils.lerp( this.start.angle, this.end.angle, t );71};7273CameraPath.prototype.relativeTo = function( keyframe, ratio, rotation ) {74this.start.set( keyframe );75this.end.set({76width: keyframe.width * ratio,77height: keyframe.height * ratio,78angle: keyframe.angle + rotation79});80};8182return CameraPath;83});848586