forked from colonistio/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
176 lines (142 loc) · 4.79 KB
/
start.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
var canvas = document.getElementById("canvas");
// Set canvas and app full screen
canvas.width = window.innerWidth
canvas.height = window.innerHeight
app.width = window.innerWidth
app.height = window.innerHeight
document.body.scrollTop = 0; // <-- pull the page back up to the top
document.body.style.overflow = 'hidden'; // <-- relevant addition
var net = new Net('net',app,'orange')
var PlayerOne = new Player('player-one',app,'blue',1)
var PlayerTwo = new Player('player-two',app,'red',2)
var roundBall = new RoundBall('roundball',app,'white')
var mainText = 50, secondText = 25;
var player1Score = new Text('score-two',(app.width/4) * 3, app.height / 4, mainText*1.5,"0",app)
var player2Score = new Text('score-one',app.width / 4, app.height / 4, mainText*1.5,"0",app)
var startText = new Text('start',app.width/2 + (mainText/2), app.height - 50, mainText,"Press 'Enter' to Start",app)
var pauseText = new Text('pause',app.width/2 + (mainText/2), app.height - 50, mainText,"",app)
var pauseInst = new Text('pauseInst',app.width/2 + (secondText/2), app.height - 25, secondText,"",app)
var state = 'START'
/**
* Add Game Music Level1.wav
* Created by https://juhanijunkala.com/
*/
var music = new Sound('sounds/level1.wav', true)
// Init
app.onInit = function(){
document.addEventListener('keydown', (event) => {
const keyName = event.key
if(keyName == 'w')
PlayerOne.moveUp(true)
if(keyName == 's')
PlayerOne.moveDown(true)
if(keyName == 'ArrowUp')
PlayerTwo.moveUp(true)
if(keyName == 'ArrowDown')
PlayerTwo.moveDown(true)
})
document.addEventListener('keyup', (event) => {
const keyName = event.key
if(keyName == 'w')
PlayerOne.moveUp(false)
if(keyName == 's')
PlayerOne.moveDown(false)
if(keyName == 'ArrowUp')
PlayerTwo.moveUp(false)
if(keyName == 'ArrowDown')
PlayerTwo.moveDown(false)
if(keyName == 'Enter'){
if(state == 'START'){
music.play() // Start Game Music
state = 'GAME'
}
}
if(keyName == ' '){
if(state == 'GAME' || state == 'PAUSE'){
this.pause()
}
}
if(keyName == 'r'){
if(state == 'PAUSE')
this.reset()
}
})
};
app.onUpdate = function(time){
let deltatime = time / (1000/60) //Run 60 frames per second (1000ms)
switch(state) {
case 'START':
break;
case 'GAME':
startText.setText("")
gameRuning(deltatime)
break
case 'PAUSE':
break
}
};
app.pause = function(){
if(state == 'GAME'){
music.pause() // Pause Game Music
state = 'PAUSE'
pauseText.setText("Pause")
pauseInst.setText("press 'R' for reset")
}
else{
music.play() // Resume Game Music
state = 'GAME'
pauseText.setText("")
pauseInst.setText("")
}
}
app.reset = function(){
PlayerOne.reset()
PlayerTwo.reset()
player1Score.setText("0")
player2Score.setText("0")
roundBall.reset(true)
state = 'START'
pauseText.setText("")
pauseInst.setText("")
startText.setText("Press 'Enter' to Start")
}
function gameRuning(deltatime) {
PlayerOne.update(deltatime)
PlayerTwo.update(deltatime)
roundBall.update(deltatime)
player1Score.setText(roundBall.score.one)
player2Score.setText(roundBall.score.two)
if(collision(roundBall.getNode(),PlayerOne.getNode())){
changeDirection(roundBall,PlayerOne,app)
}
if(collision(roundBall.getNode(),PlayerTwo.getNode())){
changeDirection(roundBall,PlayerTwo,app)
}
}
function collision(ball,player) {
// Check for collision between ball and player
let pTop = player.y
let pBottom = (player.y) + player.height
let pLeft = player.x
let pRight = (player.x) + player.width
let bTop = ball.y - ball.r
let bBottom = (ball.y) + ball.r
let bLeft = ball.x - ball.r
let bRight = (ball.x) + ball.r
// boolean for collision
return bRight > pLeft && bTop < pBottom && bLeft < pRight && bBottom > pTop
}
// Change ball direction once it collides with player
function changeDirection(ball,player,app) {
ball.playBounce() // Play Ball bounce
let collideHit = ball.getNode().y - (player.getNode().y + player.getNode().height / 2)
collideHit = collideHit / (player.getNode().height / 2)
let angle = (Math.PI/4) * collideHit
let direction = -1
if(ball.getNode().x < app.width/2){
direction = 1
}
ball.velocityX = direction * ball.speed * Math.cos(angle)
ball.velocityY = direction * ball.speed * Math.sin(angle)
ball.speed += 1
}