@@ -3,20 +3,35 @@ import { useState } from 'react';
3
3
function App ( ) {
4
4
//app compoment
5
5
const [ points , setPoints ] = useState ( 0 ) ; //set initial points to 0
6
+ const [ pointsPerSecond , setPointsPerSecond ] = useState ( 0 ) ;
6
7
const [ clicks , setClicks ] = useState ( 0 ) ; //set initial clicks pressed to 0
7
8
const [ clickMultiplier , setClickMultiplier ] = useState ( 1 ) ;
9
+ useEffect ( ( ) => {
10
+ const interval = setInterval ( ( ) => {
11
+ //increase points every second
12
+ setPoints ( points + pointsPerSecond ) ;
13
+ } , 1000 ) ;
14
+ return ( ) => clearInterval ( interval ) ; //clear interval when component unmounts
15
+ } ) ;
8
16
function addPointsFromClick ( ) {
9
17
//add points from clicking a button
10
18
setPoints ( points + clickMultiplier ) ; //increase points by 1 when button clicked
11
19
setClicks ( clicks + clickMultiplier ) ; //increase clicks made by 1
12
20
}
13
21
function upgradeClicker ( ) {
14
- //upgrade clicker
15
- if ( points >= Math . pow ( 2 , clickMultiplier ) ) {
22
+ //upgrade clicker (points per click)
23
+ if ( points >= 10 * Math . pow ( 2 , clickMultiplier - 1 ) ) {
16
24
setPoints ( points - 10 * Math . pow ( 2 , clickMultiplier - 1 ) ) ; //spend points
17
25
setClickMultiplier ( clickMultiplier + 1 ) ; //increase click multiplier by 1
18
26
}
19
27
}
28
+ function upgradePointsPerSecond ( ) {
29
+ //upgrade points per second
30
+ if ( points >= Math . pow ( 2 , pointsPerSecond ) ) {
31
+ setPoints ( points - Math . pow ( 2 , pointsPerSecond ) ) ;
32
+ setPointsPerSecond ( pointsPerSecond + 1 ) ; //increase points per second
33
+ }
34
+ }
20
35
return (
21
36
//dynamic app HTML output
22
37
< div className = "App" >
@@ -32,12 +47,18 @@ function App() {
32
47
</ p >
33
48
{ /*increase points from clicking a button*/ }
34
49
< button onClick = { ( ) => addPointsFromClick ( ) } > Click to Add Points</ button >
35
- { /*upgrade clicker button */ }
50
+ { /*upgrade clicker (points per click) */ }
36
51
< button onClick = { ( ) => upgradeClicker ( ) } >
37
- Upgrade Clicker
52
+ Upgrade Clicker (Points Per Click)
38
53
< br />
39
54
Cost: { 10 * Math . pow ( 2 , clickMultiplier - 1 ) }
40
55
</ button >
56
+ { /*upgrade points per second*/ }
57
+ < button onClick = { ( ) => upgradePointsPerSecond ( ) } >
58
+ Upgrade Points Per Second
59
+ < br />
60
+ Cost: { 10 * Math . pow ( 2 , pointsPerSecond ) }
61
+ </ button >
41
62
</ div >
42
63
) ;
43
64
}
0 commit comments