-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-go-up.html
144 lines (118 loc) · 4.53 KB
/
number-go-up.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Number go up!</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
</head>
<body>
<script>
class NumberScene extends Phaser.Scene {
create() {
this.autoIncrease = 1;
this.clickIncrease = 10;
this.increaseMultiplier = 1.2;
this.threshold = 100;
this.number = 0;
this.timer = 0;
this.previousThreshold = 0;
this.thresholdsReached = 0
this.add.text(50, 16, 'Number go up!', { fontSize: '64px', fill: '#AAA' });
this.numberText = this.add.text(50, 200, 'Number: 0', { fontSize: '64px', fill: '#AAA' });
this.thresholdText = this.add.text(50, 300, 'Bar fills: 0', { fontSize: '32px', fill: '#AAA' });
this.clickButton = new Phaser.GameObjects.Text(this, 600, 400, 'Click me!', { fill: '#0ff' });
this.add.existing(this.clickButton);
this.clickButton
.setInteractive({ useHandCursor: true })
.on('pointerover', () => this.enterButtonHoverState())
.on('pointerout', () => this.enterButtonRestState())
.on('pointerdown', () => this.enterButtonActiveState())
.on('pointerup', () => {
this.updateNumberOnClick(this.clickIncrease);
this.enterButtonHoverState();
});
this.progressBar = this.makeBar(50, 500, 0xAAAAAA)
this.setBarValue(this.progressBar, 100)
this.healthBar = this.makeBar(50, 500, 0x00FF00)
this.setBarValue(this.healthBar, 0)
}
enterButtonHoverState() {
this.clickButton.setStyle({ fill: '#0f0' });
}
enterButtonRestState() {
this.clickButton.setStyle({ fill: '#0ff' });
}
enterButtonActiveState() {
this.clickButton.setStyle({ fill: '#ff0' });
}
makeBar(x, y, color) {
let bar = this.add.graphics();
bar.fillStyle(color, 1);
bar.fillRect(0, 0, 700, 25);
bar.x = x;
bar.y = y;
return bar;
}
setBarValue(bar, percentage) {
bar.scaleX = percentage / 100;
}
updateNumberOnClick(value) {
this.setNumber(value);
this.updateBar();
}
update(time, delta) {
this.timer += delta;
while (this.timer > 1000) {
this.setNumber(this.autoIncrease);
this.timer -= 1000;
this.updateBar();
}
if (this.number > this.threshold) {
this.previousThreshold = this.threshold;
this.thresholdsReached += 1;
this.autoIncrease += this.thresholdsReached;
this.clickIncrease += 10;
this.threshold *= this.increaseMultiplier;
this.updateBar();
this.updateThresholdsReached();
}
}
updateBar() {
let thresholdDelta = this.threshold - this.previousThreshold;
let gainedSinceThreshold = this.number - this.previousThreshold;
let percentReached = gainedSinceThreshold / thresholdDelta;
let percentage = Math.floor(percentReached * 100);
if (percentage > 100) {
percentage = 100
}
this.setBarValue(this.healthBar, percentage)
}
setNumber(value) {
this.number += value
this.numberText.setText('Number: ' + this.number)
}
updateThresholdsReached() {
this.thresholdText.setText(`Bar fills: ${this.thresholdsReached}`)
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: NumberScene
}
const game = new Phaser.Game(config);
</script>
</body>
<style>
body {
background-color: #383838;
}
canvas {
display: block;
margin: auto;
border-radius: 2%;
border: 1px solid black;
}
</style>
</html>