-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
230 lines (197 loc) · 5.88 KB
/
script.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import data from './data.json' with { type: 'json' };
let ticks = 0;
const player = {
fishCount: 0,
statClick: 1,
statAuto: 0,
statSpeed: 0,
activeTab: 0,
activeItem: 0,
itemsOwned: [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
};
function onClick(e) {
if (e.target.id === 'water') {
clickWater(e);
} else if (e.target.id === 'purchase') {
Purchase();
} else if (e.target.className === 'tab') {
changeTab(e);
} else if (e.target.nodeName === 'IMG' && e.target.parentElement.id === 'upgrades') {
displayInfo(e);
}
}
function clickWater(e) {
// Increment fish count by click stat
player.fishCount += player.statClick;
// Animation stuff
const water = document.getElementById('water');
const splash = document.createElement('img');
splash.src = 'img/ripple.gif';
water.appendChild(splash);
splash.style.rotate = Math.floor(Math.random() * 360) + 'deg';
splash.style.top = e.y - splash.clientWidth / 2 + 'px';
splash.style.left = e.x - splash.clientHeight / 2 + 'px';
splash.className = 'jump';
}
function Purchase() {
const itemData = data[player.activeTab][player.activeItem];
const itemCount = player.itemsOwned[player.activeTab][player.activeItem];
// 1. if item max count is not exceeded & we have enough money for the purchase
// 2. Subtract item's price from players money
// 3. Increment purchased item in the owned counter
// 4. Add item stat to player stats
// 5. DisplayInfo so the owned count updates on store
const newPrice = itemData.price * (itemCount + 1);
if (itemCount < itemData.max && player.fishCount >= newPrice) {
player.fishCount -= newPrice;
player.itemsOwned[player.activeTab][player.activeItem]++;
if (itemData.stat === 'click') {
player.statClick += itemData.power;
} else if (itemData.stat === 'auto') {
player.statAuto += itemData.power;
} else {
player.statSpeed += itemData.power;
}
displayInfo();
}
}
function changeTab(e) {
const siblings = e.target.parentElement.children;
const index = Array.prototype.indexOf.call(siblings, e.target);
// 1. Update player obj to represent new tab
// 2. Reset background for all tabs
// 3. Apply background on active tab
// 4. Draw Items
player.activeTab = index;
document.querySelectorAll('.tab').forEach((i) => (i.style.backgroundColor = ''));
e.target.style.backgroundColor = 'var(--color-5)';
drawItems();
}
function drawItems() {
const parent = document.getElementById('upgrades');
const tabIndex = player.activeTab;
const tabItems = data[tabIndex];
// 1. Clear parent
// 2. Loop through the tab items in data.json
// 3. Assign the itemid as img.src
// 4. Add to DOM
parent.innerHTML = '';
for (let i of tabItems) {
const img = document.createElement('img');
img.src = './img/' + tabIndex + '/' + i.itemid + '.png';
parent.appendChild(img);
}
}
function updateHUD() {
// Counters on the top left of the screen
const parent = document.getElementById('stats');
parent.textContent = ''
const hud = {
'🐟Fish: ' : player.fishCount,
'⚓Click Power: ' : player.statClick,
'🕰️Fish per/s: ' : fishPerS()
};
for (let key of Object.keys(hud)) {
const content = key + format(hud[key])
const li = createLi(content)
parent.appendChild(li);
}
}
function displayInfo(e) {
if (e) {
const siblings = e.target.parentElement.children;
// If called at page load we dont come here
// 1. Reset background for all items
// 2. Apply background on clicked item
// 3. Update active item to player obj
player.activeItem = Array.prototype.indexOf.call(siblings, e.target);
document.querySelectorAll('#upgrades > img').forEach((i) => (i.style.backgroundColor = ''));
e.target.style.backgroundColor = 'var(--color-4-darkest)';
}
// Update infobox to dislay item's info
const item = data[player.activeTab][player.activeItem];
const itemCount = player.itemsOwned[player.activeTab][player.activeItem];
const header = document.querySelector('.header');
const grid = document.querySelector('.grid');
let content;
header.innerHTML = '';
content = [item.name, item.description];
content.forEach((i) => header.appendChild(createLi(i)));
grid.innerHTML = '';
content = [
'Bonus:',
[item.stat, " + ", item.power],
'Owned:',
[itemCount, " / ", item.max],
'Price:',
item.price * (itemCount + 1),
];
if (item.stat === 'speed') {
content[1].push('%')
}
content.forEach((i) => grid.appendChild(createLi(i)));
}
function createLi(item) {
const li = document.createElement('li');
// if (['click', 'auto', 'speed'].includes(item[0])) {
// const img = document.createElement('img')
// img.src = './img/stats/' + item[0] + ".png"
// img.style.scale = 2
// li.appendChild(img)
// }
if (Array.isArray(item)) {
item = item.join('')
}
li.appendChild(document.createTextNode(item));
return li;
}
function loadSave() {
console.log('object');
for (let entry of Object.entries(localStorage)) {
let key = entry[0];
let value = entry[1];
console.log(key, value);
player[key] = JSON.parse(localStorage.getItem(key));
}
}
function format(num) {
if (num >= 1e9) {
return (num / 1e9).toFixed(2) + "b"
} else if (num >= 1e6) {
return (num / 1e6).toFixed(2) + "m"
} else if (num >= 1e3) {
return (num / 1e3).toFixed(2) + "k"
}
return num.toFixed(1)
}
function fishPerS() {
return player.statAuto * (player.statSpeed / 100 + 1)
};
setInterval(() => {
// every 100ms
ticks++;
updateHUD();
// every 1s
if (ticks % 10 === 0) {
player.fishCount += fishPerS();
}
// every 10s
if (ticks % 100 === 0) {
Object.entries(player).forEach((i) => localStorage.setItem(i[0], JSON.stringify(i[1])));
console.log('saved');
}
}, 100);
/* ┳ •
┃┏┓┓╋
┻┛┗┗┗ */
document.addEventListener('click', onClick);
document.addEventListener('animationend', (e) => e.target.remove());
//document.addEventListener('wheel', onClick);
//localStorage.clear();
loadSave();
displayInfo();
drawItems();