-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerview.js
132 lines (123 loc) · 4.39 KB
/
playerview.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// <summary> UI for player spinning mothership. </summary>
var PlayerView= function(paper, size, startPos, colorscheme){
// Available schemes.
var COLOR_SCHEMES = {
"SEA": [ "lightblue", "blue",
{width: 13, color: "blue"} ],
"FOREST": [ "lightgreen", "green",
{width: 13, color: "green", opacity: 0.7} ]
};
this.colorScheme = COLOR_SCHEMES[colorscheme];
// Angle to rotate per animation step.
var SHIP_ROTATION_SPEED = 36;
// Additional speed when moving.
var SHIP_ROTATION_SPEED_MOVE = 6;
// Ship translation speed.
var SHIP_SPEED = 12;
// Current ship angle.
this.shipAngle = 0;
// Bounding box of ship.
this.boundingBoxSize = size;
// Ship center coordinate.
this.shipCoord = new Point(startPos.x, startPos.y);
this.animationOffset = new Point(0, 0);
/// <summary> Location of the player ship center in UI space. </summary>
this.getLoc = function(){
return this.animationOffset.add(this.shipCoord);
}.bind(this)
// Setup the ship graphics.
this.canvasElement = function(){
var set = paper.set();
var halfSize = size / 2;
var triangleSide = (3 / Math.sqrt(3)) * halfSize;
var triangleHt = (Math.sqrt(3) / 2) * triangleSide;
var triangleHalfSize = triangleSide / 2;
var trianglePath = "M" + startPos.x + "," + (startPos.y - halfSize) + "," +
"L" + (startPos.x + triangleHalfSize) + "," +
(startPos.y - halfSize + triangleHt) + "," +
"L" + (startPos.x - triangleHalfSize) + "," +
(startPos.y - halfSize + triangleHt) + "," +
"L" + startPos.x + "," + (startPos.y - halfSize);
set.push(
paper.circle(startPos.x, startPos.y, halfSize * 0.8),
paper.path(trianglePath)
);
set[0].attr("fill", this.colorScheme[0]);
set[1].attr("fill", this.colorScheme[1]);
return set;
}.apply(this)
// Map of keys pressed.
this.keysDown = {"UP": 0, "DOWN": 0, "LEFT": 0, "RIGHT": 0};
/// <summary> Move the ship offset from its current location. </summary>
this.animate = function(offset){
var tformStr = "t" + offset.x + "," + offset.y +
"r" + this.shipAngle + "," +
this.shipCoord.x + "," + this.shipCoord.y;
this.canvasElement.animate( { transform: tformStr }, ANIMATION_TIMER_MS)
}.bind(this)
/// <summary> Timer service routine. </summary>
/// <remarks>
/// <para> The timer service responds to I/O commands and plays
/// the idle ship animation.
/// </para>
/// </remarks>
this.timerService = function(){
// Update the rotation.
this.shipAngle += SHIP_ROTATION_SPEED;
// Animate the ship position.
if(this.keysDown["UP"]){
this.animationOffset.y -= SHIP_SPEED;
this.shipAngle += SHIP_ROTATION_SPEED_MOVE;
}
if(this.keysDown["DOWN"]){
this.animationOffset.y += SHIP_SPEED;
this.shipAngle += SHIP_ROTATION_SPEED_MOVE;
}
if(this.keysDown["LEFT"]){
this.animationOffset.x -= SHIP_SPEED;
this.shipAngle += SHIP_ROTATION_SPEED_MOVE;
}
if(this.keysDown["RIGHT"]){
this.animationOffset.x += SHIP_SPEED;
this.shipAngle += SHIP_ROTATION_SPEED_MOVE;
}
this.animate(this.animationOffset);
}.bind(this)
// Process key press.
this.onKey = function(flag, theKey){
if("keydown" === flag){
if(this.keysDown[theKey] === 0)
{
this.keysDown[theKey] = 1;
}
}
else if("keyup" === flag){
this.keysDown[theKey] = 0;
}
}.bind(this)
this.targetGlowParams = this.colorScheme[2];
/// <summary> Set the ships currently targeted node. </summary>
this.setCurrentTarget = function(node){
if(this.currentTarget !== node){
if(this.currentTargetGlow !== undefined){
this.currentTargetGlow.remove();
this.currentTargetGlow = undefined;
}
this.currentTarget = node;
if(node !== undefined){
this.currentTargetGlow =
node.canvasElement.glow(this.targetGlowParams);
}
}
};
/// <summary> Accessor for the current target. </summary>
this.getCurrentTarget = function(){ return this.currentTarget; }.bind(this)
this.destroyCanvasElements = function(){
this.clip.destroyCanvasElements();
this.canvasElement.remove();
this.canvasElement = null;
if(this.currentTargetGlow){
this.currentTargetGlow.remove();
}
}
}