forked from komali2/buttonclicker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
click.js
122 lines (117 loc) · 3.1 KB
/
click.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
let count = 0;
const COUNTER = document.querySelector('.click-counter');
class Button {
constructor(type, cb) {
this.type = type;
this.external_callback = cb || function(){};
this.element = document.createElement('button');
this.element.classList.add(this.type);
this.element.innerHTML = this.type.charAt(0).toUpperCase() + this.type.slice(1);
this.element.onclick = this.onclick.bind(this);
}
onclick() {
this.external_callback();
}
static basicClick() {
this.incrementCount();
}
}
class Game {
constructor() {
this.arena = document.querySelector('.arena');
this.upgrades = document.querySelector('.upgrades');
this.state = {
basic_increment: 1,
upgrades: [],
};
this.arena = new DomArea('main .arena', this.state);
this.upgrades = new UpgradesList('main .upgrades', this.state);
}
stateCheck(count) {
if (count === 10 && this.state.upgrades.indexOf('addClicker') === -1) {
this.state.upgrades.push('addClicker');
this.addButtonToUpgrades(
new Button('addClicker', this.upgradeAddBasicButton.bind(this)),
);
} else if (count >= 1000 && this.state.upgrades.indexOf('autoclicker') === -1) {
this.state.upgrades.push('autoclicker');
this.addButtonToUpgrades(
new Button('autoClicker', this.upgradeAddAutoClicker.bind(this)),
);
}
}
addBasicButtonToArena() {
const button = new Button('click', this.incrementCount.bind(this));
this.arena.addButton(button);
}
addAutoClicker() {
setInterval(function(){
this.incrementCount();
}.bind(this), 1000);
}
addButtonToUpgrades(button) {
this.upgrades.addButton(button);
}
incrementCount() {
count += this.state.basic_increment;
COUNTER.innerHTML = count;
this.stateCheck(count);
}
decrementCount(num) {
count -= num;
COUNTER.innerHTML = count;
this.stateCheck();
}
upgradeAddBasicButton() {
if (count >= 10) {
this.addBasicButtonToArena.call(this);
this.state.basic_increment++;
this.decrementCount(10);
}
}
upgradeAddAutoClicker() {
if (count >= 1000) {
this.addAutoClicker();
this.decrementCount(1000);
}
}
}
class DomArea {
constructor(selector, state) {
this.state = state;
this.element = document.querySelector(selector);
this.domlist = [];
}
addButton(button) {
let el;
if (button instanceof Button) {
el = button.element;
} else {
el = button;
}
this.domlist.push(el);
this.element.appendChild(el);
return this.element;
}
}
class UpgradesList extends DomArea {
constructor(selector) {
super(selector);
}
addButton(button) {
let el;
if (button instanceof Button) {
el = button.element;
} else {
el = button;
}
this.domlist.push(el);
const upgrades_list = this.element.querySelector('ul');
const new_upgrade_list_item = document.createElement('li');
new_upgrade_list_item.appendChild(el);
upgrades_list.appendChild(new_upgrade_list_item);
return this.upgrades_list;
}
}
const game = new Game();
game.addBasicButtonToArena();