-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
103 lines (95 loc) · 3.49 KB
/
main.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
var keys = {};
$(document).keydown(function (e) {
keys[e.keyCode] = true;
//console.log(e.keyCode);
});
$(document).keyup(function (e) {
keys[e.keyCode] = false;
});
$(document).ready(function () {
var canvas = $("#field");
canvas.drawRect({
fillStyle: "#ccc",
x: 0 + parseInt(canvas.prop("width")) / 2,
y: 0 + parseInt(canvas.prop("height")) / 2,
width: 640,
height: 480
});
var game = new Game(canvas, $("#gametable"));
deltas = [];
for (i = 0; i < game.players_count; ++i)
deltas.push(0);
var round = 1;
var gameFunction = function () {
for (i = 0; i < game.players_count; ++i) {
deltas[i] = 0;
if (keys[game.keys[i][0]]) deltas[i] --;
if (keys[game.keys[i][1]]) deltas[i] ++;
}
game.update(deltas);
if (game.alive_count <= 1) {
stopGame();
}
}
$("#roundDiv").html("Round " + round.toString() + "/" + ROUNDS.toString());
var gameCycle = window.setInterval(gameFunction, 20);
var stopGame = function () {
clearInterval(gameCycle);
setTimeout(function () {
game.awardWinner();
round++;
if (round <= ROUNDS) {
$(window).one("keypress", function () {
$("#roundDiv").html("Round " + round.toString() + "/" + ROUNDS.toString());
game.restart();
gameCycle = window.setInterval(gameFunction, 20);
});
}
else {
$(window).one("keypress", function () {
var best_player = "", best_score = -1, best_count = 1, i;
var player, score;
for (i = 0; i < game.players_count; ++i) {
score = parseInt($("#player" + (i + 1).toString() + " div.playerScore", this.table).html());
player = $("#player" + (i + 1).toString() + " div.playerName", this.table).html();
if (score > best_score) {
best_score = score;
best_player = player;
best_count = 1;
}
else if (score == best_score) {
best_count++;
best_player += ", " + player;
}
}
var greet;
if (best_count == 1)
greet = "Winner:\n";
else
greet = "Winners:\n";
game.canvas.drawRect({
fillStyle: "#ccc",
x: 0 + parseInt(canvas.prop("width")) / 2,
y: 0 + parseInt(canvas.prop("height")) / 2,
width: 640,
height: 480
});
game.canvas.drawText({
fillStyle: '#000',
x: parseInt(canvas.prop("width")) / 2,
y: parseInt(canvas.prop("height")) / 2,
fontSize: 60,
fontFamily: 'Verdana, sans-serif',
text: greet + best_player
});
});
}
}, 500);
}
/*var A, B, C, D;
A = new Point(0, 0);
B = new Point(1, 1);
C = new Point(0, 1);
D = new Point(1, 0);
console.log(inter(A, B, C, D));*/
});