-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtanktastic.html
163 lines (146 loc) · 4.83 KB
/
tanktastic.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Tanktastic!</title>
<script type="text/javascript" src="lib/easeljs-0.4.2.min.js"></script>
<script type="text/javascript" src="lib/seedrandom.js"></script>
<script type="text/javascript" src="lib/tanktastic.js"></script>
<link rel="stylesheet" type="text/css" href="assets/css/tanktastic.css"></style>
<script type="text/javascript">
var stage;
var required = [];
var tanks = [
"lib/example-tanks/aggressivo.js",
// "my-tank.js",
"lib/example-tanks/coward.js",
"lib/example-tanks/moron.js"
]
var ticking = false;
exports = null;
var game;
function startGame()
{
stage = new Stage("arena");
var shape = stage.addChild(new Shape());
game = new tanktastic.Game(window.location.search, shape.graphics);
for(var i in required) game.register_tank(required[i]);
for(i in game.tanks) addHUD(game.tanks[i], i);
game.init();
game.render();
stage.update();
updateHUD();
}
function addHUD(tank, index)
{
var div = "<div id='" + tank.name + index + "-outer' class='outer'>" + tank.name + "<br>";
div += "<div id='" + tank.name + index + "-life' class='stat'>" + tank.life + "</div>";
div += "<div id='" + tank.name + index + "-score' class='stat'>" + tank.score + "</div>";
document.getElementById("hud").innerHTML += div;
}
function appendTank(file)
{
exports = {};
required.push(exports);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = file + "?r=" + Math.random();
document.getElementsByTagName("HEAD")[0].appendChild(script);
resolve();
}
function resolve()
{
if(exports && !exports.name)
{
setTimeout(resolve, 5);
return;
}
if(tanks.length) appendTank(tanks.pop());
else startGame();
}
function tick()
{
if(!game.is_complete())
{
game.step(!ticking);
stage.update();
updateHUD();
}
else
{
var podium = game.podium();
var winner = podium[podium.length - 1];
if(winner.life > 0)
{
var i = game.tanks.indexOf(winner);
var outerDiv = document.getElementById(winner.name + i + "-outer");
outerDiv.style["margin-left"] = "30px";
}
stopGame();
var div = document.getElementById("controls");
div.parentNode.removeChild(div);
}
}
function updateHUD()
{
var tanks = game.tanks;
for(var i in tanks)
{
var tank = tanks[i];
var div = document.getElementById(tank.name + i + "-outer");
div.style.color = tank.life > 0 ? tank.color : "#CCC";
div = document.getElementById(tank.name + i + "-life");
div.innerHTML = "life: " + Math.max(tank.life, 0).toFixed(1);
div = document.getElementById(tank.name + i + "-score");
div.innerHTML = "score: " + tank.score.toFixed(1);
}
}
function onStep(event)
{
event.preventDefault();
if(game && !game.is_complete())
{
stopGame();
tick();
}
}
function onPausePlay(event)
{
event.preventDefault();
var btn = document.getElementById("pause-play");
if(btn.className == "pause") stopGame();
else runGame();
}
function runGame(event)
{
var btn = document.getElementById("pause-play");
btn.className = "pause";
game.step(true); // take one step to catch time up
ticking = true;
Ticker.addListener(window);
}
function stopGame(event)
{
var btn = document.getElementById("pause-play");
btn.className = "play";
ticking = false;
Ticker.removeListener(window);
}
</script>
</head>
<body onload="resolve();">
<table>
<tr>
<td width="960">
<canvas id="arena" width="960" height="480"></canvas>
<div id="controls">
<a id="pause-play" class="play" href="#" onclick="onPausePlay(event)"></a>
<a class="step" href="#" onclick="onStep(event)"></a>
</div>
</td>
<td id="hud" width="300" valign="top">
</td>
</tr>
</table>
</body>
</html>