-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.js
206 lines (178 loc) · 5.76 KB
/
track.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const TRAIN_HEIGHT = Domino.height();
const TRAIN_WIDTH = Math.floor(Domino.height() * 1.5);
// Each player has a track with their name
class Track {
constructor(username, layer, dominos, x, y) {
this.username = username;
this.layer = layer;
this.dominos = dominos;
// These are the upper left hand coordinates of where the track starts
this.x = x;
this.y = y;
this.usernameText = null;
this.handLengthText = null;
this.trainImg = null;
this.ellipsisImg = null;
this.animation = null;
this._onLoad(layer);
}
static height() {
return Domino.height() + 2 * this.verticalPadding();
}
static verticalPadding() {
return 5;
}
_onLoad(layer) {
this._createTrainImage(layer);
this._createEllipsisImage(layer);
this.usernameText = new Konva.Text({
x: this.x,
y: this.y + 15,
text: this.username,
fontSize: 25,
fontFamily: 'Calibri',
fill: 'black',
listening: false,
});
layer.add(this.usernameText);
if (this.username != "Public") {
this.handLengthText = new Konva.Text({
x: this.usernameText.x()+100,
y: this.usernameText.y()+10,
text: "99",
fontSize: 15,
fontFamily: 'Calibri',
fill: 'black',
listening: false,
});
layer.add(this.handLengthText);
}
var bottomLine = new Konva.Line({
points: [this.x, this.y + Track.height(), window.innerWidth, this.y + Track.height()],
stroke: 'black',
strokeWidth: 5,
lineCap: 'round',
lineJoin: 'round',
listening: false,
});
layer.add(bottomLine);
}
// Callback that is triggered when the gamestate changes
onGamestateChanged(gamestate){
const MAX_DOMINOES_TO_SHOW_PER_TRAIN = 6;
const X_OFFSET = 120 + this.x;
if (this.username == gamestate.currentUser) {
this.usernameText.fontStyle('Bold');
}
this.usernameText.fill(gamestate.userWithCurrentTurn == this.username ? 'red' : 'black');
if (this.handLengthText != null) {
this.handLengthText.text('' + gamestate.hands[this.username].length);
}
var dominos = gamestate.track(this.username);
if ( dominos.length > MAX_DOMINOES_TO_SHOW_PER_TRAIN) {
// Remove dominos in the middle-ish, and make invisible
var removed = dominos.splice(1, dominos.length - MAX_DOMINOES_TO_SHOW_PER_TRAIN);
for(var i=0; i<removed.length; i++) {
this.dominos[Domino.originalKey(removed[i])].img.visible(false);
}
// Add in a ellipsis
dominos.splice(1, 0, "ellipsis");
} else {
// Triggered on a reset
this.ellipsisImg.visible(false);
}
for (var i=0; i<dominos.length; i++) {
var key = dominos[i];
if (key == "ellipsis") {
this.ellipsisImg.x(Domino.width()*i + X_OFFSET + (Domino.width() - this.ellipsisImg.width())/2);
this.ellipsisImg.y(this.y + 5);
this.ellipsisImg.visible(true);
} else {
var domino = this.dominos[Domino.originalKey(key)];
domino.rotateToMatch(key);
domino.relativeX(Domino.width()*i + X_OFFSET);
domino.relativeY(this.y + 5);
domino.img.visible(true);
domino.img.draggable(false);
}
}
// Draw train
if (this.username != "Public") {
this.trainImg.x(Domino.width() * dominos.length + X_OFFSET + 10);
this.trainImg.y(this.y + 5);
this.trainImg.visible(gamestate.trainEngineUp(this.username));
}
if (this.username == gamestate.winner) {
this._setWinner(gamestate);
}
}
_setWinner(gamestate) {
// This is cheesy, but people love this shit.
const train = gamestate.track(gamestate.winner);
this.animation = new Konva.Animation(function(frame) {
var scale1 = Math.abs(Math.sin((frame.time * 2 * Math.PI) / 2000)) + 0.5;
this.usernameText.fontSize(30 * scale1);
for(var i=0; i<train.length; i++) {
var dominoImg = this.dominos[Domino.originalKey(train[i])].img;
var scale2 = Math.abs(Math.sin(((frame.time + i*100) * 2 * Math.PI) / 2000)) + 0.5;
dominoImg.width(Domino.width() * scale2);
dominoImg.height(Domino.height() * scale2);
}
// Stop animation after 10 seconds
if (frame.time > 10 * 1000) {
console.log("Stopping animation and resetting");
this.animation.stop();
this.usernameText.fontSize(30);
// Reset all dominos
for (var key in this.dominos) {
this.dominos[key].reset();
}
}
}.bind(this), this.layer);
this.animation.start();
}
// Returns true if the coordinate is within the track
contains(x, y) {
return this.y < y && (this.y + Track.height()) > y && x > this.x;
}
_createTrainImage(layer) {
var imageObj = new Image();
imageObj.src = './assets/train-engine.png';
this.trainImg = new Konva.Image({
image: imageObj,
x: 0,
y: 0,
width: TRAIN_WIDTH,
height: TRAIN_HEIGHT,
draggable: false,
rotation: 0,
visible: false,
name: 'train-' + this.username,
listening: false,
});
imageObj.onload = function() {
layer.add(this.trainImg);
layer.batchDraw();
}.bind(this);
}
_createEllipsisImage(layer) {
var imageObj = new Image();
imageObj.src = './assets/ellipsis.jpg';
this.ellipsisImg = new Konva.Image({
image: imageObj,
x: 0,
y: 0,
width: Domino.height(),
height: Domino.height(),
draggable: false,
rotation: 0,
visible: false,
name: 'ellipsis-' + this.username,
listening: false,
});
imageObj.onload = function() {
layer.add(this.ellipsisImg);
layer.batchDraw();
}.bind(this);
}
}