forked from panther-js/toskeiraspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackcat.js
128 lines (114 loc) · 4.5 KB
/
blackcat.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
var SPACE = SPACE || {};
window.startGame = function(){
SPACE.blackcat = {
randomPosition : function(excludePosition){
var position = excludePosition;
while( position === excludePosition){
var i = parseInt( Math.random() * 20 + 1 , 0);
if( i <= 5 ){
position = "LEFT";
}else if( i <= 10 ){
position = "TOP";
}else if( i <= 15 ){
position = "RIGHT";
}else if( i <= 20){
position = "BOTTOM";
}
}
return position;
},
getMaxAvailablePosition : function(position){
var max = ("TOP" === position || "BOTTOM" === position) ? window.innerWidth
: window.innerHeight;
return max ;
},
define2dPoint : function(position , value){
var point2d = { x : 0 , y : 0 };
if(position === "TOP"){
point2d = { x : value , y : 0 };
}
if(position === "BOTTOM"){
point2d = { x : value , y : window.innerHeight };
}
if(position === "LEFT"){
point2d = { x : 0 , y : value };
}
if(position === "RIGHT"){
point2d = { x : window.innerWidth , y : value };
}
return point2d;
},
prepareStartPosition: function(){
var position = this.randomPosition(null);
var max = this.getMaxAvailablePosition( position );
var endPosition = this.randomPosition(position);
var maxEnd = this.getMaxAvailablePosition( endPosition );
return {
startPosition:position,
startPoint: this.define2dPoint ( position , (Math.random() * max + 1 ) ),
endPosition:endPosition,
endPoint: this.define2dPoint ( endPosition , (Math.random() * maxEnd + 1 ) )
};
},
prepareInterval: function( points ){
var blackcat = document.getElementById("blackcat");
var xCurrent = points.startPoint.x;
var xDestination = points.endPoint.x;
var speedX = xDestination - xCurrent > 0 ? 3 : -3;
var yCurrent = points.startPoint.y;
var yDestination = points.endPoint.y;
var speedY = yDestination - yCurrent > 0 ? 3 : -3;
blackcat.style.left = xCurrent;
blackcat.style.top = yCurrent;
blackcat.style.display = 'block';
SPACE.blackcat.points = points;
this.interval = setInterval(
function(){
var xCurrent = points.startPoint.x;
var xDestination = points.endPoint.x;
var yCurrent = points.startPoint.y;
var yDestination = points.endPoint.y;
if ( ( xCurrent < 0 || xCurrent > window.innerWidth ) ||
( yCurrent < 0 || yCurrent > window.innerHeight) ) {
SPACE.blackcat.stopCat( true );
return;
}
xCurrent += speedX;
points.startPoint.x = xCurrent;
blackcat.style.left = xCurrent + "px";
yCurrent += speedY;
points.startPoint.y = yCurrent;
blackcat.style.top = yCurrent + "px";
}, 20 );
},
interval : null,
points : null,
runCatRun: function(){
var points = this.prepareStartPosition();
this.prepareInterval( points );
},
stopCat: function( shouldRestart ){
clearInterval( this.interval);
this.interval = null;
blackcat.style.display = 'none';
if(shouldRestart){
SPACE.blackcat.runCatRun();
}
}
};
var btnCat = document.getElementById("btnCat");
btnCat.onclick = function(e){
if( SPACE.blackcat.interval == null){
SPACE.blackcat.runCatRun();
btnCat.textContent = "Stop BlackCat";
}else{
SPACE.blackcat.stopCat();
btnCat.textContent = "Start BlackCat";
}
};
};
if(window.addEventListener) {
window.addEventListener('DOMContentLoaded', startGame, false);
} else {
window.attachEvent('onload', startGame );
}