Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/effects/camera-path.js
1036 views
1
/*globals define*/
2
define([
3
'base-object',
4
'geometry/rect',
5
'geometry/intersection',
6
'utils'
7
], function( BaseObject, Rect, Intersection, Utils ) {
8
'use strict';
9
10
// Absolute width, height, and angle.
11
function Keyframe( width, height, angle ) {
12
BaseObject.call( this );
13
14
this.width = width || 0;
15
this.height = height || 0;
16
this.angle = angle || 0;
17
}
18
19
Keyframe.prototype = new BaseObject();
20
Keyframe.prototype.constructor = Keyframe;
21
22
23
function CameraPath( x, y, width, height ) {
24
Rect.call( this, x, y, width, height );
25
26
this.camera = null;
27
// This is usually the player.
28
this.target = null;
29
30
this.start = new Keyframe();
31
this.end = new Keyframe();
32
}
33
34
CameraPath.prototype = new Rect();
35
CameraPath.prototype.constructor = CameraPath;
36
37
CameraPath.Keyframe = Keyframe;
38
39
CameraPath.prototype.update = function() {
40
if ( !this.target || !this.camera ) {
41
return;
42
}
43
44
var x = this.target.x,
45
y = this.target.y;
46
47
if ( !this.contains( x, y ) ) {
48
return;
49
}
50
51
var x0 = this.left,
52
y0 = this.top,
53
x1 = this.right,
54
y1 = this.bottom;
55
56
var t = Intersection.closestPointOnLineParameter( x, y, x0, y0, x1, y1 );
57
if ( 0 > t || t > 1 ) {
58
return;
59
}
60
61
if ( t === 0 ) {
62
this.camera.set( this.start );
63
}
64
65
if ( t === 1 ) {
66
this.camera.set( this.end );
67
}
68
69
this.camera.width = Utils.lerp( this.start.width, this.end.width, t );
70
this.camera.height = Utils.lerp( this.start.height, this.end.height, t );
71
this.camera.angle = Utils.lerp( this.start.angle, this.end.angle, t );
72
};
73
74
CameraPath.prototype.relativeTo = function( keyframe, ratio, rotation ) {
75
this.start.set( keyframe );
76
this.end.set({
77
width: keyframe.width * ratio,
78
height: keyframe.height * ratio,
79
angle: keyframe.angle + rotation
80
});
81
};
82
83
return CameraPath;
84
});
85
86