-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.js
More file actions
99 lines (80 loc) · 3.34 KB
/
camera.js
File metadata and controls
99 lines (80 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pc.script.attribute("distance", "number", 7);
pc.script.attribute("lead", "number", 7);
pc.script.attribute('up', 'number', 0)
pc.script.attribute('zoomFactor', 'number', 0.4);
pc.script.attribute('zoomHeight', 'number', 20);
pc.script.attribute('minZoom', 'number', -15);
pc.script.attribute('maxZoom', 'number', 10);
pc.script.create('camera', function (context) {
var dir = new pc.Vec3();
var zoom = new pc.Vec3();
var forward = new pc.Vec3();
var desiredPosition = new pc.Vec3();
var lastTargetPosition = new pc.Vec3();
var MODE_FOLLOW = 0;
var MODE_FOLLOW_TOP = 1;
var MODE_NOFOLLOW_FAR = 2;
var MODE_NOFOLLOW = 3;
var MODE_GAMEOVER = 4;
// Creates a new Camera instance
var Camera = function (entity) {
this.entity = entity;
this.origin = new pc.Vec3(0,0,0);
};
Camera.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.game = context.root.findByName('Game').script.game;
this.shaker = this.entity.script.shake;
this.reset();
this.game.on('reset', this.reset, this);
this.game.on('gameover', this.gameover, this);
this.game.on('collision', this.onCollision, this);
this.game.on('fuelout', this.onCollision, this);
},
reset: function () {
this.distanceOffset = 0;
this.minCameraHeight = -100;
this.mode = MODE_FOLLOW;
},
gameover: function () {
this.mode = MODE_GAMEOVER;
},
onCollision: function () {
this.shaker.shake(5, 1);
},
// Called every frame, dt is time in seconds since last update
postUpdate: function (dt) {
if (context.keyboard.wasPressed(pc.input.KEY_T)) {
this.mode = (this.mode + 1)%4;
this.origin.y = this.mode === 1 ? -150 : 0;
}
if (this.mode === MODE_NOFOLLOW_FAR) {
this.entity.setPosition(200, 30, 0);
this.entity.lookAt(this.origin);
} else {
if (this.mode === MODE_FOLLOW || this.mode === MODE_FOLLOW_TOP || this.mode === MODE_GAMEOVER) {
if (this.mode !== MODE_GAMEOVER || this.entity.getPosition().y > 5) {
var player = this.game.player.entity;
var lead = this.lead;
var up = this.up;
var playerPos = player.getPosition();
dir.sub2(playerPos, this.origin).normalize();
zoom.copy(dir).scale(this.distance + this.distanceOffset);
forward.cross(pc.Vec3.UP, dir).scale(lead + this.distanceOffset);
lastTargetPosition.copy(playerPos).add(zoom).add(forward);
lastTargetPosition.y += up;
}
}
desiredPosition.copy(lastTargetPosition);
var shake = this.shaker.getShake(dt);
desiredPosition.add(shake);
this.entity.setPosition(desiredPosition);
if (desiredPosition.sub(this.origin).lengthSq() > 0.01) {
this.entity.lookAt(this.origin);
}
}
}
};
return Camera;
});