-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattle.html
160 lines (145 loc) · 5.78 KB
/
battle.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
<html>
<head>
<title>AjaxWar Battle</title>
<style type="text/css">
body { margin: 0px; padding: 20px; font-family: verdana; }
#screen {
width: 100%;
height: 100%;
background: url(/static/loading.gif) transparent no-repeat center;
position: relative;
overflow: hidden;
margin: auto;
}
#screen-bg {
width: 1000px;
height: 600px;
background-color: white;
margin: auto;
}
.box { width: 50px; height: 50px; position: absolute; }
#text { top: 20px; position: absolute; list-style-type: square;}
.chat { font-size: small; width: 150px; position: relative; bottom: 100px; height: 100px;}
/*.chat .wrap { position: absolute; bottom: 0px;}*/
form { position: absolute; top: 25px; left: 24px; width: 95%; display: inline;}
input { width: 95%; border: 0px; font-size: 14px; background:none;}
#tips { color: gray; font-size: x-small; position: absolute; bottom: 20px; }
</style>
<link rel="stylesheet" href="static/style.css" type="text/css" />
<script type="text/javascript" src="http://github.com/DmitryBaranovskiy/raphael/blob/master/raphael-min.js?raw=true"></script>
<script type="text/javascript" src="http://www.twiliort.com/v1/realtime-client.js"></script>
<script type="text/javascript" src="/static/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery-ui-1.8.4.custom.min.js"></script>
<script type="text/javascript" src="/static/js/lobby.js"></script>
<script type="text/javascript" src="/static/js/util.js"></script>
<script type="text/javascript" src="/static/js/top.js"></script>
<script type="text/javascript" src="/static/js/svg.js"></script>
<script type="text/javascript" src="/static/js/game.js"></script>
<script type="text/javascript" src="/static/js/player.js"></script>
<script type="text/javascript" src="/static/js/unit.js"></script>
<script type="text/javascript" src="/static/js/ui.js"></script>
<script type="text/javascript" src="/static/js/ajaxwar.js"></script>
<script type="text/javascript">
var TIMEOUT = 3000;
var PLAYERS = {{players}};
var game = null;
var lobby = null;
var state = 'lobby';
var color = null;
$(document).ready(function() {
color = setupColor();
lobby = new Lobby(PLAYERS, function() {
game = new Game(lobby.size, lobby.clientId, lobby.isHost);
state = 'game';
game.start();
}, function(players) { displayText("Waiting for "+players+" players..."); });
Realtime.init("{{account_sid}}", {accessToken: "{{access_token}}", legacyProxy: "/static/realtime-proxy.html"});
Realtime.listen("/{{battle}}", {
onconnect: function() {
lobby.join();
},
ondisconnect: function() {
console.error("disconnected.");
},
onerror: function(reason) {
console.error(reason);
},
onmessage: function(msg) {
if (msg.type) {
if (state == 'lobby' && msg.type.split(':')[0] == 'lobby') {
lobby.receive(msg);
} else if (state == 'game' && msg.type.split(':')[0] == 'game') {
game.receive(msg);
}
}
},
});
})
function setupColor() {
function colorByte() { return (rnd(128)+64).toString(16); }
color = colorByte() + colorByte() + colorByte();
$('body').css('background', '#'+color);
return color;
}
function getNormalPosition(id) {
return normalizePosition($('#'+id)[0])
}
function setNormalPosition(id, x, y) {
var pos = denormalizePosition(x, y);
$('#'+id).css('left', pos.x);
$('#'+id).css('top', pos.y);
}
function displayChat(player, text) {
displayText(player.getName() + ': ' + text, player.color);
}
function displayText(text, color) {
if (!color) color = 'black';
var textId = 'text-' + Date.now().toString();
$('#text').append('<li id="'+textId+'" style="color: '+color+';"><span style="color: black;">'+text+'</span></li>');
console.log(text);
setTimeout(function(){ $('#'+textId).remove(); }, 5000);
}
function sendChat() {
var chat = $('input').val();
if (chat.indexOf("/nick ") != -1) {
var nick = chat.split(' ')[1];
console.log("setting nick: "+nick);
game.setNick(nick);
} else {
displayText('me: ' + chat);
game.sendChat(chat);
}
$('input').val('');
$('input').blur();
return false;
}
function normalizePosition(el) {
return {x: el.offsetLeft/$('body')[0].offsetWidth, y: el.offsetTop/$('body')[0].offsetHeight} }
function denormalizePosition(x,y) {
return {x: x * $('body')[0].offsetWidth, y: y * $('body')[0].offsetHeight}
}
preloadTank = new Image(1,1);
preloadTank.src="/static/tank.png";
preloadTurret = new Image(1,1);
preloadTurret.src="/static/turret.png";
preloadProduction = new Image(1,1);
preloadProduction.src="/static/production.png";
</script>
</head>
<body>
<div id="screen-bg">
<div id="screen">
<ul id="text"></ul>
</div>
<div id="tips">
1: Build tank<br />
2: Build turret<br />
3: Build production<br />
R: Show attack radiuses<br />
Tab: Chat<br />
</div>
</div>
<div id="mouse_selector"></div>
<form onsubmit="if (state == 'game') sendChat(); return false;"><input type="text" /></form>
</body>
</html>