-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
170 lines (140 loc) Β· 4.68 KB
/
app.js
File metadata and controls
170 lines (140 loc) Β· 4.68 KB
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
console.log('π¦π¦π
')
// πΎ DATA (GAME STATE) ---------------------------------------
let animals = [
{
name: 'harambe',
emoji: 'π¦',
hunger: 100,
status: 'π'
},
{
name: 'whiskers',
emoji: 'π
',
hunger: 100,
status: 'π'
},
{
name: 'dr.strange',
emoji: 'π¦',
hunger: 100,
status: 'π'
},
]
let bank = 0
// π§ β‘ LOGICAL FUNCTIONS (functions that change data) ----------------
function animalsHunger() {
animals.forEach((animal) => {
animal.hunger -= 2
if (animal.hunger < 0) animal.hunger = 0 // keeps the hunger from going negative
});
// console.log(animals)
updateAnimalsStatuses()
drawAnimals()
}
// start the interval so the animals get hungry over time
setInterval(animalsHunger, 1000)
function feedAnimalByName(animalName) {
if (bank < 10) return // return here, stops the function early
bank -= 10
console.log('π', animalName)
let animalToFeed = getAnimal(animalName)
animalToFeed.hunger += 5
if (animalToFeed.hunger > 100) animalToFeed.hunger = 100 // don't let the hunger go over 100
updateAnimalsStatuses()
drawAnimals()
drawBank()
}
// takes in a name and returns the animal object from 'animals' but will warn you if that animal doesnt exist
function getAnimal(animalName) {
let selectedAnimal = animals.find((animal) => animal.name == animalName)
console.log('π', selectedAnimal)
if (selectedAnimal == undefined) console.warn(`Could not find: ${animalName}`)
return selectedAnimal
}
// looks at the animals hunger and changes their status 'emoji'
function updateAnimalsStatuses() {
animals.forEach((animal) => {
if (animal.hunger > 80) {
animal.status = 'π'
} else if (animal.hunger > 30) {
animal.status = 'π'
} else if (animal.hunger > 0) {
animal.status = 'π'
} else {
animal.status = 'π΅'
}
})
drawCurrentPayCheck()
}
// function get's the paycheck amount from calculatePaycheck(), adds it to the bank, draws the bank
function collectMoney() {
bank += calculatePaycheck()
drawBank()
}
setInterval(collectMoney, 1000 * 10)
// NOTE separating out the calculation from adding the paycheck to the bank, let's us re-use this function, so it can be used for both modifying the back value in "collectMoney" and draw the paycheck number in "drawCurrentPaycheck"
function calculatePaycheck() {
let paycheck = 0
animals.forEach((animal) => {
// paycheck += 50
// switch statements "Fall through"
switch (animal.status) {
case 'π': // utilizing fall through
case 'π': paycheck += 50
break // break stops the fall through effect
case 'π': paycheck += 35
break
case 'π': paycheck += 15
break
case 'π΅': paycheck -= 10
}
})
console.log('π°', paycheck)
return paycheck
}
// ποΈπ¨ VISUAL FUNCTIONS (functions that draw data to page)-------------
// element references
let bankElement = document.getElementById('bank-amount')
let payheckElement = document.getElementById('paycheck-amount')
// iterates through the animals, and draws each of their stats
function drawAnimals() {
animals.forEach((animal) => {
let animalStatsElement = document.getElementById(`${animal.name}-stats`)
// NOTE while not used, this helper function could assist with getting elements off the page
// let animalStatsElement = getAnimalStatsElement(animal.name)
// console.log(`${animal.name}-stats`, animalStatsElement);
animalStatsElement.innerHTML = `${animal.hunger}${animal.status}`
})
}
// NOTE a "helper function" could be used to better get animal elements since getting elements dynamically in loops can often raise errors when not careful
function getAnimalStatsElement(animalName) {
let animalStatsId = `${animalName.toLowerCase()}-stats`
let animalStatsElement = document.getElementById(animalStatsId)
if (animalStatsElement == null) {
console.warn("The animal name is messed up dude, or there is no element there:", animalStatsId)
}
return animalStatsElement
}
// updates the page with the banks current value
function drawBank() {
bankElement.innerText = `${bank}` // turns the number into a string
updateCursor()
}
// Takes paycheck amount from 'calculatePaycheck()' and draws it to the page
function drawCurrentPayCheck() {
let paycheck = calculatePaycheck()
payheckElement.innerText = `${paycheck}` // `${paycheck}` makes the paycheck number a string
}
// just something fun to update the cursor when you can't afford something
function updateCursor() {
if (bank < 10) {
document.body.style = `
cursor: not-allowed !important;
`
} else {
document.body.style = `
cursor: unset;
`
}
}
drawBank()