-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
153 lines (140 loc) · 4.68 KB
/
app.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
var gamebordDim = {
width: 600,
height: 600
},
playerDim = {
height: 50,
width: 50
},
antennaDim = {
width: 10,
height: 40
},
ymotion = gamebordDim.height - playerDim.height,
x = 0,
motionCenter = {
y: gamebordDim.height,
x: gamebordDim.width
},
radius = motionCenter.x - x,
noOfAntennas = 6,
characterSize = 100,
characterObject,
currentCharacterPosition = {},
characterSwingLimit = 15,
characterDirection = 1,
checkProgress,
allAntennas,
greyLine = document.getElementById("grey-line"),
redLine = document.getElementById("red-line"),
score = 0,
gameStarted = false;
function stopCheckingProgress() {
clearInterval(checkProgress);
}
function initializeSizes() {
var gameboard = document.getElementById("gameboard");
gameboard.style.width = gamebordDim.width + "px";
gameboard.style.height = gamebordDim.height + "px";
gameboard.style.margin = "auto";
var player = document.getElementById("player");
player.style.width = playerDim.width + "px";
player.style.height = playerDim.height + "px";
characterObject = createCharacter();
player.appendChild(characterObject);
function keepCharacterInMotion() {
var currentShift = currentCharacterPosition.y - parseFloat(characterObject.style.top);
if(Math.abs(currentShift) > characterSwingLimit) {
characterDirection *= -1;
}
characterObject.style.top = parseFloat(characterObject.style.top) - characterDirection + "px";
}
setInterval(keepCharacterInMotion, 20);
}
function shiftPlayer(x, y) {
var player = document.getElementById("player");
player.style.left = x + "px";
player.style.top = y + "px";
}
function getRandomPosition(range) {
return Math.random()*range;
}
function getCircularMotion(x) {
var graphX = gamebordDim.width - x;
return gamebordDim.height - Math.sqrt(radius * radius - graphX * graphX);
}
function movePlayer() {
var player = document.getElementById("player");
if(x < 5 || gamebordDim.width - x > radius) {
x = gamebordDim.width - playerDim.width;
}
x = (x - 5) % gamebordDim.width;
shiftPlayer(x, getCircularMotion(x));
}
function isPlayerInRange() {
var playerInRange = false,
playerRect = player.getClientRects()[0],
antennaRect = {
x: 0,
y: 0,
width: 0,
height: 0
};
for(var antennaIndex in allAntennas) {
var antenna = allAntennas[antennaIndex];
antennaRect = antenna.antennaWrapper.getClientRects()[0];
var xCoverage = playerRect.x > (antennaRect.x - playerRect.width) && playerRect.x < (antennaRect.x + antennaRect.width),
yConverage = playerRect.y > (antennaRect.y - playerRect.height) && playerRect.y < (antennaRect.y + antennaRect.height);
if(xCoverage && yConverage) {
playerInRange = true;
break;
}
}
setProgressBar(playerInRange);
}
function setProgressBar(inRange) {
var greylineWidth = parseInt(greyLine.style.width),
redlineWidth = parseInt(redLine.style.width);
if(inRange) {
if(!greyLine.style.width) {
greyLine.style.width = "85px";
}
greyLine.style.width = greylineWidth + 10 + "px";
score++;
} else {
if(!redLine.style.width) {
redLine.style.width = "0px";
}
redLine.style.width = redlineWidth + 4 + "px";
}
if(redlineWidth >= greylineWidth + 5) {
var instruction = document.getElementById("instructions-board");
instruction.innerHTML = "You lost! Refresh page to play again";
instruction.style.display = "block";
stopCheckingProgress();
}
if(redlineWidth/gameboard.getClientRects()[0].width > 0.75 || greylineWidth/gameboard.getClientRects()[0].width > 0.75) {
redLine.style.width = redlineWidth/2 + "px";
greyLine.style.width = greylineWidth/2 + "px";
//stepup the level
levelUp(1);
score += 100;
}
document.getElementById("scoreboard").innerHTML = score;
}
initializeSizes();
function startGame(){
if(!gameStarted) {
var instructions = document.getElementById("instructions-board");
instructions.style.display = "none";
allAntennas = initializeBackground(noOfAntennas, gamebordDim.width, gamebordDim.height, antennaDim);
setProgressBar(true);
checkProgress = setInterval(isPlayerInRange, 100);
gameStarted = true;
}
}
function grabSignal(ev) {
var relativePosition = ev.x - gameboard.getClientRects()[0].left;
x = relativePosition;
movePlayer();
}