-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
143 lines (128 loc) · 3.4 KB
/
index.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
<!doctype html>
<html>
<head>
<title>Socket.IO test</title>
</head>
<body>
<!--<button onclick="test()">button</button>-->
<canvas id='field' width='1800' height='900' style="border:1px solid #000000;">
your browser does not support html5 canvas</canvas>
<script src="/socket.io/socket.io.js"></script>
<script src='/boid.js'></script>
<script src='/vector.js'></script>
<script>
var ctx = document.getElementById('field').getContext('2d');
//document.getElementById('field').onkeydown = keyPress;
window.addEventListener('keydown', keyPress, false);
window.addEventListener('keyup', keyRelease, false);
var socket = io();
var id;
var boids=[];
for (var i=0;i<699;i++){
boids["Boid"+i]=new boid(new vector(0,0),new vector(0,0));
}
boids["Player"]=new boid(new vector(0,0),new vector(0,0));
//boids["Player"].angle=0;
socket.emit('total');
socket.on('total', function(msg){
console.log('total');
for (var prop in msg){
if (msg.hasOwnProperty(prop)){
boids[prop].set(msg[prop]);
}
}
//boids=msg;
});
var i=0;
var test = function(){
i++;
socket.emit('click',{i:i,j:3,k:{i:i+1}});
}
var pos = {
x:250,
y:250
};
function keyPress(e){
e = e || event;
socket.emit('keypress', e.keyCode);
}
function keyRelease(e){
e = e || event;
socket.emit('keyrelease', e.keyCode);
}
socket.on('onconnected', function(msg){
id=msg.id;
console.log(id);
});
socket.on('click', function(msg){
console.log('click '+msg.i+' '+msg.k.i);
});
/*socket.on('key', function(msg){
ctx.clearRect(0,0,500,500);
ctx.fillRect(msg.x,msg.y,10,10);
pos.x=msg.x;
pos.y=msg.y;
});*/
function draw(p){
if (p.angle!=undefined){//p.hasOwnProperty('angle')){
ctx.moveTo(p.pos.x+25*Math.cos(p.angle),p.pos.y+20*Math.sin(p.angle));
ctx.lineTo(p.pos.x-11*Math.cos(p.angle-0.5),p.pos.y-11*Math.sin(p.angle-0.5));
ctx.lineTo(p.pos.x-11*Math.cos(p.angle+0.5),p.pos.y-11*Math.sin(p.angle+0.5));
ctx.lineTo(p.pos.x+25*Math.cos(p.angle),p.pos.y+20*Math.sin(p.angle));
} else {
ctx.moveTo(p.pos.x,p.pos.y);
ctx.lineTo(p.pos.x-p.vel.x,p.pos.y-p.vel.y);
}
//ctx.moveTo(p.pos.x,p.pos.y);
//ctx.lineTo(p.goal.x,p.goal.y);
}
var time = new Date().getTime();
socket.on('data', function(msg){
//console.log(msg);
//var newTime = new Date().getTime();
//var delta = newTime-time;
/*for (var prop in boids){
if (boids.hasOwnProperty(prop)){
//boids[prop].interact(boids,delta);
}
}*/
//console.log(delta);
//time=newTime;
//console.log(delta);
//console.log(boids);
/*if(msg.hasOwnProperty("Boid0")){
var now = new Date().getTime();
var delta = now-time;
console.log(delta);
time=now;
}*/
for (var prop in msg){
console.log(msg["Player"].angle);
if(msg.hasOwnProperty(prop)){
//console.log(prop+" "+boids[prop].vel.x+" "+boids[prop].vel.y+" "+msg[prop].xvel+" "+msg[prop].yvel);
boids[prop].set(msg[prop]);
//boids[prop].setGoal(msg[prop].xpos,msg[prop].ypos);
}
}
//console.log(boids['Boid0'].vel.x);
console.log(boids);
ctx.clearRect(0,0,1800,1000);
ctx.beginPath();
for (var prop in boids){
if (boids.hasOwnProperty(prop)){
draw(boids[prop]);
}
}
//console.log(boids);
ctx.strokeStyle="black";
ctx.closePath();
ctx.stroke();
});
function getNextFrame(){
socket.emit('frame');
window.requestAnimationFrame(getNextFrame);
}
window.requestAnimationFrame(getNextFrame);
</script>
</body>
</html>