-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairlab.html
191 lines (155 loc) · 3.28 KB
/
airlab.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!doctype html>
<html>
<head>
<style type='text/css'>
<script type="text/javascript">
canvas {
border: 1px solid #aaa;
cursor: none;
}
</style>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript">
var context;
var width = 500;
var height = 500;
function setBackground() {
context.clearRect(0, 0, width, height);
context.fillStyle = "black";
context.fillRect(0,0,width,height);
}
function point(x,y) {
this.x = x;
this.y = y;
}
var cats = [];
function setupCats() {
cats = [];
for (var i = 0; i < 30; i++) {
var cat = new point(Math.random() * width, Math.random() * height);
cats.push(cat);
}
}
var dogs = [];
var dogVectors = [];
function setupDogs() {
dogs = [];
dogVectors = [];
for (var i = 0; i < 30; i++) {
var dog = new point(0,0);
var dVector = new point(1 + (Math.random() * 3), 1 + (Math.random() * 3));
dogs.push(dog);
dogVectors.push(dVector);
}
}
setupCats();
setupDogs();
var mouse = new point(0,0);
var cat = new point(150,200);
var score = 0;
var dog = new point(0, 0);
var dogVector = new point(1,1);
var bone = new point(50,50);
function getVector(a,b) {
var dist = distance(a,b);
if (dist == 0) {
return new point(0,0);
}
var distX = Math.abs(a.x-b.x);
var distY = Math.abs(a.y-b.y);
total = distX + distY;
distX = distX / total;
distY = distY / total;
if (b.x < a.x) {
distX = -distX;
}
if (b.y < a.y) {
distY = -distY;
}
return new point(distX,distY);
}
function distance(a,b) {
var xdist = (a.x - b.x) * (a.x - b.x);
var ydist = (a.y - b.y) * (a.y - b.y);
return Math.sqrt(xdist * ydist);
}
function drawCircle(p,colour,radius) {
context.fillStyle = colour;
context.beginPath();
context.arc(p.x,p.y,radius,0,Math.PI*2,true);
context.closePath();
context.fill();
}
function showScore() {
context.fillStyle = "white";
context.fillText("Score:" + score, 0, height - 20);
}
function relocateCat(c) {
c.x = Math.random() * width;
c.y = Math.random() * height;
}
function processFindingCat() {
for (var i = 0; i < cats.length; i++) {
var c = cats[i];
if(distance(mouse, c) < 5) {
score = score + 50;
setupCats();
//relocateCat(c);
}
}
}
function showPlayer() {
drawCircle(mouse, "blue", 10);
}
function showAnimals() {
for (var i = 0; i < cats.length; i++) {
var c = cats[i];
drawCircle(c, "green", 5);
}
}
function moveDogs() {
drawCircle(dogs[0], "red", 5);
for (var i = 0; i < dogs.length; i++) {
var d = dogs[i];
drawCircle(d, "red", 5);
if (distance(mouse, d) < 20) {
score = score - 1;
}
var dVector = dogVectors[i];
d.x = d.x + dVector.x;
d.y = d.y + dVector.y;
if (d.x > width || d.y > height) {
d.x = 0;
d.y = 0;
}
}
}
function dogAttack() {
}
function draw() {
setBackground();
showPlayer();
showAnimals();
processFindingCat();
showScore();
moveDogs();
dogAttack();
}
$(document).ready(function() {
var canvas = $('#canvas');
context = canvas.get(0).getContext('2d');
setBackground();
setInterval(draw, 10);
canvas.mousemove(function(e) {
var mousex = e.pageX - canvas.offset().left;
var mousey = e.pageY - canvas.offset().top;
mouse.x = mousex;
mouse.y = mousey;
});
});
</script>
</head>
<body>
<canvas id='canvas' width=500 height=500></canvas>
</body>
</html>