-
Notifications
You must be signed in to change notification settings - Fork 3
/
game_score.js
96 lines (88 loc) · 2.98 KB
/
game_score.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
// ************************************************
// Teugg Soccer Physics
// Rip-off of the once popular Soccer Physics game
// Horribly coded by : Kevin Le Teugg, 2020
// File : game_score.js
// Description : Game score class used for keeping track of the score
// ************************************************
class GameScore {
constructor() {
// ATTRIBUTES
this.score1 = 0;
this.score2 = 0;
this.previousFrameCount = 0;
this.sloMo = false;
}
// METHODS
scoreCheck(ball, goal1, goal2) {
// Player 1 scores
if (ball.body.position.x - ball.r >= CANVAS_WIDTH - goal2.areaWidth + (goal2.botBarW / 2) && ball.body.position.y >= CANVAS_HEIGHT - goal2.areaHeight) {
if (!this.sloMo) {
this.previousFrameCount = frameCount;
frameRate(20);
this.score1 = this.score1 + 1;
this.sloMo = true;
console.log('Entering slow mo')
}
// Putting this here is not the best solution since the text will disappear if the ball goes out the goal during SloMo
push();
textSize(80);
//rotateX((frameCount - this.previousFrameCount));
//rotateZ((frameCount - this.previousFrameCount) * 0.85);
text('GOAL', CANVAS_WIDTH/2, CANVAS_HEIGHT/2);
pop();
}
if(frameCount - this.previousFrameCount >= 40 && this.sloMo == true) {
gameManager.resetPlayers();
gameManager.resetBall();
this.previousFrameCount = frameCount;
frameRate(60);
this.sloMo = false;
console.log('Slow mo reset');
}
// Ball is out of right of canvas
else if (ball.body.position.x >= CANVAS_WIDTH + ball.r) {
gameManager.resetPlayers();
gameManager.resetBall();
}
// Player 2 scores
if (ball.body.position.x + ball.r <= goal1.areaWidth - goal1.botBarW && ball.body.position.y >= CANVAS_HEIGHT - goal1.areaHeight) {
if (!this.sloMo) {
this.previousFrameCount = frameCount;
frameRate(20);
this.score2 = this.score2 + 1;
this.sloMo = true;
console.log('Entering slo mo');
}
// Putting this here is not the best solution since the text will disappear if the ball goes out the goal during SloMo
push();
textSize(80);
//rotateX((frameCount - this.previousFrameCount));
//rotateZ((frameCount - this.previousFrameCount) * 0.85);
text('GOAL', CANVAS_WIDTH/2, CANVAS_HEIGHT/2);
pop();
}
if(frameCount - this.previousFrameCount >= 40 && this.sloMo == true) {
gameManager.resetPlayers();
gameManager.resetBall();
this.previousFrameCount = frameCount;
frameRate(60);
this.sloMo = false;
console.log('Slow mo reset');
}
// Ball is out of left of canvas
else if (ball.body.position.x <= 0 - ball.r) {
gameManager.resetPlayers();
gameManager.resetBall();
}
}
// DRAWING FUNCTION
show() {
push();
textSize(90);
stroke(0);
fill(255);
text(this.score1 + ' - ' + this.score2, (CANVAS_WIDTH / 2), 100);
pop();
}
}