-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (109 loc) · 3.23 KB
/
script.js
File metadata and controls
146 lines (109 loc) · 3.23 KB
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
var mouse = [0,0],
foods = [];
function getPixelValue(value, type) {
var side = window[['innerHeight', 'innerWidth'][['vh', 'vw'].indexOf(type)]];
return side * (value/100);
}
$(function() {
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var dot, eventDoc, doc, body, pageX, pageY;
event = event || window.event; // IE-ism
// If pageX/Y aren't available and clientX/Y are,
// calculate pageX/Y - logic taken from jQuery.
// (This is to support old IE)
if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
}
// Use event.pageX / event.pageY here
mouse = [event.pageX, event.pageY];
fish.turnToPointer();
fish.moveTo(...mouse);
}
addFood();
});
function Fish() {
this.position = [0,0];
this.size = 50;
this.element = $('#fish');
this.updateSize();
setInterval(function() {
foods.forEach(function(food) {
if(fish.isTouchingFood(food)) {
food.element.addClass('being-eaten');
setTimeout(function () {
if(fish.isTouchingFood(food) && $.contains(document, food.element[0]))fish.eatFood(food);
else food.element.removeClass('being-eaten');
}, 1000);
}
});
}, 100);
}
Fish.prototype.isTouchingFood = function(food) {
return (Math.abs(this.position[1] - getPixelValue(food.position[0], 'vw')) < 20)
&& (Math.abs(this.position[0] - getPixelValue(food.position[1], 'vh')) < 20);
};
Fish.prototype.updateSize = function() {
this.element.css('height', this.size);
};
Fish.prototype.eatFood = function (food) {
food.destroy();
this.size+=4;
this.updateSize();
};
Fish.prototype.turnToPointer = function () {
var deltaX = mouse[0] - this.position[0];
if(deltaX > 0)
this.element.css('transform', 'rotateY(180deg) translateY(-50%)');
else
this.element.css('transform', 'rotateY(0) translateY(-50%)');
};
Fish.prototype.moveTo = function (x,y) {
this.element.css({
top: y,
left: x
});
this.position = [x,y];
};
var fish = new Fish();
function addFood() {
var theFood = new Food();
var timer = (Math.random()*3);
// Add food in timer seconds
setTimeout(addFood, timer*1000);
// Destroy the food after 8 seconds
setTimeout(function(){
theFood.destroy();
}, 4000);
}
function Food() {
this.id = $('#container .food').length;
this.element = $('<div class="food" id="food-'+this.id+'"></div>');
this.setRandomPosition();
$('#container').append(this.element);
foods.push(this);
}
Food.prototype.setRandomPosition = function () {
var x = Math.floor(Math.random()*100),
y = Math.floor(Math.random()*100);
this.position = [x,y];
this.element.css({
top: this.position[0] + 'vw',
left: this.position[1] + 'vh'
});
};
Food.prototype.destroy = function() {
this.element.remove();
// Find the index of this food and remove it from the foods array
var index = foods.indexOf(this);
if(index > -1)
foods.splice(index,1);
};