-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal.js
430 lines (372 loc) · 11.5 KB
/
animal.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Global variables
var canvas; // canvas element on document
var goToMouse = false; // target for pet to go to
var choice = 10; // randomized var for movement. Started at 10.
var updating; // a setInterval function consistently updating pet data such as hunger
var canvascolor; // color of canvas
var temp_f; // temperature which changes based on results from WeatherUnderground API
// vector object, used for determing pet location and movement
function Vector (x, y) {
this.x = x;
this.y = y;
}
// pet object
function Entity (location, image, width, height) {
this.location = location;
this.image = image;
this.velocity = new Vector(0.0, 0.0);
this.width = height;
this.height = height;
this.updateCall= function() {};
}
//function to set object's velocity
Entity.prototype.setVel = function(xv, yv) {
this.velocity = new Vector(xv, yv);
};
Entity.prototype.setVel = function(vel) {
this.velocity = vel;
};
// function which updates this objects properties, particularly velocity and location
Entity.prototype.update = function() {
if(this.location.y<this.height/2 ||
this.location.y>(canvas.height-this.height/2)) {
this.velocity.y = -this.velocity.y;
if (this.location.y < this.height/2) this.location.y = this.height/2 + 1;
if (this.location.y > canvas.height - this.height/2) this.location.y = canvas.height - this.height/2 - 1.0;
}
if(this.location.x<this.width/2 ||
this.location.x>(canvas.width-this.width/2)) {
this.velocity.x = -this.velocity.x
if (this.location.x < this.width/2) this.location.x = this.width/2 + 1;
if (this.location.x > canvas.width - this.width/2) this.location.x = canvas.width - this.width/2 - 1.0;
}
this.location = new Vector(this.location.x + this.velocity.x,
this.location.y + this.velocity.y)
this.updateCall();
};
// method to draw this object to canvas
Entity.prototype.draw = function(context) {
context.save();
context.translate(this.location.x, this.location.y);
context.drawImage(this.image, -this.width/2, -this.height/2,
this.width, this.height);
context.restore();
};
// state-based pet interaction
var sleep = 0;
var chase = 1;
var roam = 2;
var eat = 3;
var toFood = 4;
var stand = 5;
var dead = 6;
// Handler methods: --------
// Food: food.createFood();
// Ball: ball.click();
// Groom: animal.groom();
// Clean: animal.clean();
// Entity objects
var animal;
var ball;
var food;
var ballImg = new Image();
var foodImg = new Image();
var deathImg = new Image();
// initialization method. Called when body onLoad.
function init() {
var type = document.getElementById("type").value;
//pulls weatherUnderground data
jQuery(document).ready(function($) {
$.ajax({
url: "http://api.wunderground.com/api/865fa01c49781a95/geolookup/conditions/q/autoip.json",
dataType: "jsonp",
success: function(parsed_json) {
var location = parsed_json['location']['city'];
temp_f = parsed_json['current_observation']['temp_f'];
//alert("Current temperature in "+location+" is: " + temp_f);
var maindiv = document.getElementById("maindiv");
if(temp_f < 50) {
maindiv.style.backgroundColor = "#f7b4f3";
}
else {
maindiv.style.backgroundColor = "#b0e5fc";
}
}
});
});
// get canvas from document
canvas = document.getElementById("canvas");
//import external images
imgObj = new Image();
if(type == "Cat") {
imgObj.src='cat.png';
}
else {
imgObj.src="dog.png";
}
deathImg = new Image();
deathImg.src = 'dead.jpg';
foodImg.src = 'cookies.jpg';
ballImg.src = 'ball.jpg';
// Create animal ------------------------------------------
animal = new Entity(new Vector(100.0, 100.0), imgObj, 100.0, 90.0);
animal.state = roam;
animal.timer = 500;
animal.full = 100.0;
animal.dirty = 100.0;
animal.shaggy = 100.0;
animal.energy = 100.0;
animal.happiness = 100.0;
// Animal update proxy
animal.updateCall = function() {
if (animal.state != dead) {
animal.timer -= 1;
if (animal.timer < 0) {
animal.timer = 0;
}
// Handle full/hunger
animal.full -= .03;
if (animal.full <= 0) {
animal.image = deathImg;
animal.setVel(new Vector(0.0, 0.0));
animal.full = 0;
animal.state = dead;
clearInterval(update);
}
if (animal.full < 10) {
if (food.visible) {
animal.state = toFood;
}
}
if (animal.full > 100) {
animal.full = 100;
}
// Handle dirtiness
animal.dirty -= 0.015;
if (animal.state == eat) {
animal.dirty -= 0.01
}
if (animal.dirty > 100.0) animal.dirty = 100.0;
if (animal.dirty < 0.0) animal.dirty = 0.0;
// Handle shagginess
animal.shaggy -= -0.01;
if (animal.shaggy > 100.0) animal.shaggy = 100.0;
if (animal.shaggy < 0.0) animal.shaggy = 0.0;
// Handle energy
if (animal.state == chase) {
animal.energy -= 0.05;
} else if (animal.state == roam) {
animal.energy -= 0.02;
} else if (animal.state == sleep) {
animal.energy += 0.1;
if (animal.timer == 0 || animal.energy >= 100) {
animal.state = roam;
animal.roam();
}
}
if (animal.energy < 0.0) animal.energy = 0.0;
if (animal.energy > 100.0) animal.energy = 100.0;
// Handle happiness
if (animal.state == chase) {
animal.happiness += 0.1;
} else if (animal.state == roam) {
animal.happiness -= 0.01;
} else if (animal.state == sleep) {
animal.happiness -= 0.08;
} else if (animal.state == eat){
animal.happiness += 0.05;
} else {
animal.happiness -= 0.05
}
if (animal.happiness < 0.0) {
animal.happiness = 0.0;
if (animal.timer == 0) animal.state = chase;
}
if (animal.happiness > 100.0) animal.happiness = 100.0;
}
};
// Animal roam handler
animal.roam = function() {
//original roam - 1 line
animal.setVel(new Vector(Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0));
};
// Animal eating handler
animal.eating = function() {
animal.full = animal.full + 0.5;
if (animal.full > 100.0) {
animal.full = 100.0;
animal.state = roam;
food.visible = false;
}
};
// Animal groom handler
animal.groom = function() {
animal.shaggy -= 10.0;
if (animal.shaggy < 0.0) animal.shaggy = 0.0;
};
// Animal clean handler
animal.clean = function() {
if (animal.state != dead) {
animal.dirty += 10.0;
animal.happiness -= 5.0;
if (animal.dirty > 100.0) animal.dirty = 100.0;
if (animal.happiness < 0) animal.happiness = 0.0;
}
};
// Animal playing handler
animal.play = function() {
if (animal.state != dead) {
if (animal.state != sleep) animal.happiness += 5.0;
if(animal.happiness > 100) animal.happiness = 100;
}
};
// Animal tosleep handler
animal.sleep = function() {
if (animal.state != dead) {
animal.state=sleep;
animal.timer=500;
}
};
// Create ball ------------------------------------------
ball = new Entity(new Vector(200.0, 200.0), ballImg, 30.0, 30.0);
ball.setVel(new Vector(0,0));
// ball.setVel(new Vector(Math.random()*8-4, Math.random()*8-4));
ball.updateCall = function() {
var ballXDir = ball.velocity.x / Math.abs(ball.velocity.x);
var ballYDir = ball.velocity.y / Math.abs(ball.velocity.y);
var newXVel = (ball.velocity.x != 0) ? ball.velocity.x - ballXDir/200.0 : 0.0;
var newYVel = (ball.velocity.y != 0) ? ball.velocity.y - ballYDir/200.0 : 0.0;
ball.setVel(new Vector((Math.abs(newXVel) < .02) ? 0.0 : newXVel, (Math.abs(newYVel) < .02) ? 0.0 : newYVel));
};
// Ball click handler
ball.click = function() {
ball.setVel(new Vector(Math.random() * 8.0 - 4.0, Math.random() * 8.0 - 4.0));
animal.play();
};
// Create food ------------------------------------------
food = new Entity(new Vector(400.0, 100.0), foodImg, 50.0, 50.0);
food.visible = false;
// Food creation handler
food.createFood = function() {
// animal.state = eat;
// animal.timer = 300;
if (!food.visible) {
food.location.x = Math.random() * (canvas.width - 200) + 100;
food.location.y = Math.random() * (canvas.height - 200) + 100;
food.visible = true;
}
if (animal.state != dead && (animal.timer == 0 || animal.full < 10)) {
animal.state = toFood;
}
};
animal.setVel(new Vector(1, 1));
ctx = canvas.getContext("2d");
updating = setInterval("update()", 1000/100);
drawing = setInterval("draw()", 1000/60);
//setInterval("ball.click()", 3000);
}
function randomize() {
dx+= 2*((Math.random()-.5)-(dx/20));
dy+= 2*((Math.random()-.5)-(dy/20));
}
function update() {
if (animal.state != dead && animal.state != sleep && (ball.velocity.x != 0 || ball.velocity.y != 0 && animal.timer == 0)) {
animal.state = chase;
}
// Handle emergency sleep!
if (animal.energy < 2.0){
animal.state = sleep;
animal.timer = 1000;
}
// handle emergency eating
// Handle chasing state
if (animal.state == chase) {
xDelta = -animal.location.x + ball.location.x;
yDelta = -animal.location.y + ball.location.y;
if (Math.abs(xDelta) < animal.width/2 && Math.abs(yDelta) < animal.height/2) {
animal.state = roam;
animal.roam();
animal.timer = 300;
} else {
animal.setVel(new Vector(xDelta / Math.abs(xDelta),
yDelta / Math.abs(yDelta)));
}
// Handle walking to food
} else if (animal.state == toFood) {
xDelta = -animal.location.x + food.location.x;
yDelta = -animal.location.y + food.location.y;
if (Math.abs(xDelta) < animal.width/2 && Math.abs(yDelta) < animal.height/2) {
animal.state = eat;
animal.timer = 500;
animal.setVel(new Vector(0.0, 0.0));
} else {
animal.setVel(new Vector(xDelta / Math.abs(xDelta),
yDelta / Math.abs(yDelta)));
}
// Handle eating state
//} else if (animal.full < eat) {
} else if (animal.state == eat) {
if (animal.timer > 0) {
animal.full += .1;
} else {
animal.state = roam;
animal.roam();
animal.timer = 300;
food.visible = false;
}
} else if (animal.state == sleep) {
animal.setVel(new Vector(0.0, 0.0));
}
animal.update();
ball.update();
}
// Draws all objects on the canvas
function draw() {
ctx.restore();
clearCanvas();
ctx.save();
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 1000, 1000);
ctx.fillStyle="black";
ctx.translate(15, 10);
ctx.fillText("Hunger", 0,0);
drawStatusRect(0, 3, animal.full);
ctx.translate(150, 0);
ctx.fillText("Happiness", 0, 0);
drawStatusRect(0, 3, animal.happiness);
ctx.translate(150, 0);
ctx.fillText("Energy", 0, 0);
drawStatusRect(0, 3, animal.energy);
ctx.translate(150, 0);
ctx.fillText("Hygeine", 0, 0);
drawStatusRect(0, 3, animal.dirty);
ctx.restore();
animal.draw(ctx);
ball.draw(ctx);
if (food.visible) food.draw(ctx);
}
//draw status boxes
function drawStatusRect(x, y, width) {
ctx.save();
ctx.fillStyle = '#fff';
ctx.strokeRect(x-1, y-1, 102, 12);
if (width > 20) ctx.fillStyle = '#0f0';
else ctx.fillStyle = '#f00';
ctx.fillRect(x, y, width, 10);
ctx.restore();
}
function onMouseOver(evt) {
goToMouse = true;
if(evt.offsetX) {
mx = evt.offsetX;
my = evt.offsetY;
}
else if(evt.layerX) {
mx = evt.layerX;
my = evt.layerY;
}
}
// clears canvas. draws white rectangle.
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}