-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
195 lines (167 loc) · 5.67 KB
/
app.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//////////////////////////
// Landscaper
//////////////////////////
// By Christian Unger
//////////////////////////
// Player Object
//////////////////////////
const player = {
// set up initial game state:
wallet: 0,
toolArr: ['teeth'],
workDay: 1,
// Cut Grass Function:
mowLawns: () => {
player.wallet += player.workDay;
checkState();
document.getElementById('wallet').innerHTML = `$${player.wallet}`;
// console.log(player.wallet);
},
// Add Tools Method:
getTool: (obj) => {
if (player.toolArr.includes(obj.prerequisite) && (player.wallet >= obj.cost)) {
if (player.toolArr.includes('teeth')) {
player.toolArr = [];
player.workDay = 0;
}
obj.prerequisite = 1;
player.toolArr.push(obj.name, 1);
player.wallet -= obj.cost;
player.workDay += obj.workDay;
}
document.getElementById('mow-lawns').innerHTML = `$${player.workDay}`;
document.getElementById('wallet').innerHTML = `$${player.wallet}`;
// console.log(player.toolArr, player.wallet);
},
// Sell Tools Methhod:
sellTool: (obj) => {
let arr = player.toolArr;
for (let i = 0; i < arr; i++) {
if (arr[i] === obj.name) {
arr.splice(i, 1);
}
}
player.wallet += obj.cost;
checkState();
player.workDay -= obj.workDay;
document.getElementById('mow-lawns').innerHTML = `$${player.workDay}`;
document.getElementById('wallet').innerHTML = `$${player.wallet}`;
},
};
//////////////////////////
// Tool Objects
//////////////////////////
const tools = {
scissors: {
name: 'scissors',
prerequisite: 'teeth',
cost: 5,
workDay: 5,
buyMessage: 'You bought a rusty pair of scissors!',
sellMessage: 'You sold a pair of scissors!'
},
pushMower: {
name: 'old-timey push mower',
prerequisite: 'scissors',
cost: 25,
workDay: 50,
buyMessage: 'You bought an old-timey push mower!',
sellMessage: 'You sould an old-timey push mower!'
},
batteryMower: {
name: 'battery-powered mower',
prerequisite: 'old-timey push mower',
cost: 250,
workDay: 100,
buyMessage: 'You bought a fancy battery-powered lawnmower!',
sellMessage: 'You sold a battery-powered lawnmower!'
},
team: {
name: 'team',
prerequisite: 'battery-powered mower',
cost: 500,
workDay: 250,
buyMessage: 'You hired a team of starving students!',
sellMessage: 'You fired a team of starving students!'
}
}
//////////////////////////
// Event Listeners
//////////////////////////
// Mow Lawns:
let mowLawnsL = document.getElementById('mow-lawns');
mowLawnsL.addEventListener("click", player.mowLawns);
//////////////////////////
// Buy Tool Listeners
//////////////////////////
// Buy Scissors:
document.getElementById('buy-scissors').addEventListener("click", function(){player.getTool(tools.scissors)});
// Buy Push Mower:
document.getElementById('buy-push-mower').addEventListener("click", function(){player.getTool(tools.pushMower)});
// Buy Battery Mower:
document.getElementById('buy-battery-mower').addEventListener("click", function(){player.getTool(tools.batteryMower)});
// Hire Team:
document.getElementById('hire-team').addEventListener("click", function(){player.getTool(tools.team)});
//////////////////////////
// Sell Tool Listeners
//////////////////////////
// sell Scissors:
document.getElementById('sell-scissors').addEventListener("click", function(){player.sellTool(tools.scissors)});
// sell Push Mower:
document.getElementById('sell-push-mower').addEventListener("click", function(){player.sellTool(tools.pushMower)});
// sell Battery Mower:
document.getElementById('sell-battery-mower').addEventListener("click", function(){player.sellTool(tools.batteryMower)});
// Hire Team:
document.getElementById('hire-team').addEventListener("click", function(){player.sellTool(tools.team)});
//////////////////////////
// Reset
//////////////////////////
const reset = () => {
player.wallet = 0;
player.toolArr = ['teeth'];
player.workDay = 1;
document.getElementById('mow-lawns').innerHTML = `$${player.workDay}`;
document.getElementById('wallet').innerHTML = `$${player.wallet}`;
}
// Listener:
document.getElementById('reset').addEventListener("click", reset);
const checkState = () => {
if (player.wallet >= 5) {
document.getElementById('buy-scissors').classList.remove('grey')
}
if (player.wallet >= 25) {
document.getElementById('buy-push-mower').classList.remove('grey')
}
if (player.wallet >= 250) {
document.getElementById('buy-battery-mower').classList.remove('grey')
}
if (player.wallet >= 500) {
document.getElementById('hire-team').classList.remove('grey')
}
if (player.wallet >= 1000) {
alert('You win!');
}
}
//////////////////////////
// In-Game Messages
//////////////////////////
const updateBoard = (obj, buySellWin) => {
let board = document.getElementById('hero-txt');
let message = '';
switch(buySellWin) {
case 'buy':
message += obj.buyMessage;
break;
case 'sell':
message += obj.sellMessage;
break;
case 'win':
message += 'YOU WIN!';
break;
}
board.innerHTML = message;
}
////////////////////////////////////////////////
// fix infinite sell bug
// finish messaging logic
// add logic to grey out buttons if wallet drops below threshold - on anything that decrements wallet