forked from yura-chaikovsky/autobots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ironhide.html
56 lines (44 loc) · 1.43 KB
/
ironhide.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Ironhide</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.js"></script>
</head>
<body>
<script>
(function() {
var socket = io('http://localhost:8000');
var token = 'ironhide';
var currentId;
socket.emit('join-game', { token: token });
socket.on('registration', function(data) {
currentId = data.playerId;
});
socket.on('state-update', function(data) {
var me = data.autobots.filter(function(bot) {
return bot.playerId === currentId;
})[0];
var he = data.autobots.filter(function(bot) {
return bot.playerId !== currentId;
})[0];
var action = {};
if (me.busyCount) {
console.log('waiting');
return;
}
action.action = 'move';
action.options = {};
action.options.rotation = me.position.y > he.position.y ? 'down' : 'up';
action.options.direction = me.position.x > he.position.x ? 'left' : 'right';
if (me.position.x === he.position.x && me.direction === action.options.rotation) {
socket.emit('send-commands', { action: 'fire'});
console.log('fire');
return;
}
socket.emit('send-commands', action);
});
})();
</script>
</body>
</html>