-
Notifications
You must be signed in to change notification settings - Fork 2
/
Global.gd
204 lines (171 loc) · 5.24 KB
/
Global.gd
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
class_name Global
const SETTINGS_FILE := 'user://settings.json'
const SAVE_FILE := 'user://game.json'
enum StatType {
LEVEL,
POLLUTION,
POLLUTION_PER_CYCLE,
MONEY,
MONEY_PER_CYCLE,
PROFIT,
ADS,
POWER,
MINING,
POPULATION,
POPULATION_CAP,
POPULATION_INCREASE_PER_CYCLE,
REACH,
RECRUITERS,
ACTIONS,
ACTION_STATS,
PRICE,
TITLE,
DESCRIPTION,
BASE_PROFIT,
}
enum BuildingType {
FACTORY,
POWERPLANT,
MINE,
APARTMENT_BUILDING,
AD_AGENCY,
}
enum ActionType {
NORMAL,
BOOST,
}
static func get_building_types():
return [
BuildingType.FACTORY,
BuildingType.POWERPLANT,
BuildingType.MINE,
BuildingType.APARTMENT_BUILDING,
BuildingType.AD_AGENCY,
]
static func get_building_name(building_type) -> String:
var names = {
BuildingType.FACTORY: 'Factory',
BuildingType.POWERPLANT: 'Powerplant',
BuildingType.MINE: 'Mine',
BuildingType.APARTMENT_BUILDING: 'Apartment Building',
BuildingType.AD_AGENCY: 'Ad Agency',
}
return names[building_type]
static func get_building_tooltip_desc(building_type) -> String:
var tooltips = {
BuildingType.FACTORY: 'Produces money and pollution.',
BuildingType.POWERPLANT: 'Produces power, increasing factory production. Also produces pollution.',
BuildingType.MINE: 'Mines planet resources, incerasing factory production.',
BuildingType.APARTMENT_BUILDING: 'Increases population cap.',
BuildingType.AD_AGENCY: 'Produces ads, increasing reach (factory sales per population).',
}
return tooltips[building_type]
static func human_readable(value : int) -> String:
var suffixes = ['K', 'M', 'B', 'T']
var fvalue = float(value)
var i = -1
while fvalue >= 1000 and i < len(suffixes) - 1:
fvalue /= 1000.0
i += 1
if i >= 0:
var svalue : String = '%.1f' % fvalue
if svalue.ends_with('.0'):
svalue = svalue.substr(0, len(svalue) - 2)
return svalue + suffixes[i]
else:
return str(value)
static func get_level_upgrade_stats(current_level, next_level):
var stats = []
var key_to_stat_type = {
'base_profit_per_sale': StatType.BASE_PROFIT,
'base_pollution_per_cycle': StatType.POLLUTION_PER_CYCLE,
'base_power': StatType.POWER,
'base_ads': StatType.ADS,
'base_population_increment': StatType.POPULATION_INCREASE_PER_CYCLE,
'base_population_cap': StatType.POPULATION_CAP,
'base_mining': StatType.MINING,
'money_per_cycle': StatType.MONEY_PER_CYCLE,
}
for key in current_level:
if key in key_to_stat_type:
var multiplier = next_level[key] / current_level[key]
if multiplier > 1:
var stat = Stat.new()
stat.type = key_to_stat_type[key]
stat.value = 'x' + str(multiplier)
stats.append(stat)
elif multiplier == 1:
var diff = int(next_level[key] - current_level[key])
if diff > 0:
var stat = Stat.new()
stat.type = key_to_stat_type[key]
stat.value = '+' + str(diff)
stats.append(stat)
return stats
static func get_all_node_children(node: Node) -> Array:
var children := []
for child in node.get_children():
children.append(child)
children.append_array(get_all_node_children(child))
return children
static func save_settings():
var master_bus = AudioServer.get_bus_index('Master')
var sfx_bus = AudioServer.get_bus_index('SFX')
var music_bus = AudioServer.get_bus_index('Music')
var game_data = {
'master_volume': db2linear(AudioServer.get_bus_volume_db(master_bus)),
'sfx_volume': db2linear(AudioServer.get_bus_volume_db(sfx_bus)),
'music_volume': db2linear(AudioServer.get_bus_volume_db(music_bus)),
'fullscreen': OS.window_fullscreen,
}
var file = File.new()
file.open(SETTINGS_FILE, File.WRITE)
file.store_line(to_json(game_data))
static func load_settings():
var master_bus = AudioServer.get_bus_index('Master')
var sfx_bus = AudioServer.get_bus_index('SFX')
var music_bus = AudioServer.get_bus_index('Music')
var file = File.new()
if not file.file_exists(SETTINGS_FILE):
return
file.open(SETTINGS_FILE, File.READ)
var settings = parse_json(file.get_line())
if 'master_volume' in settings:
AudioServer.set_bus_volume_db(master_bus, linear2db(settings['master_volume']))
if 'sfx_volume' in settings:
AudioServer.set_bus_volume_db(sfx_bus, linear2db(settings['sfx_volume']))
if 'music_volume' in settings:
AudioServer.set_bus_volume_db(music_bus, linear2db(settings['music_volume']))
if 'fullscreen' in settings:
OS.window_fullscreen = settings['fullscreen']
static func new_stat(type: int, value):
var stat = Stat.new()
stat.type = type
stat.value = str(value)
return stat
static func save_game(world):
var serialized_world = world.serialize()
var save_file := File.new()
var ret := save_file.open(SAVE_FILE, File.WRITE)
if ret != OK:
push_error('Could not read save file for writing.')
return
save_file.store_line(to_json(serialized_world))
static func load_game():
var save_file := File.new()
if not save_file.file_exists(SAVE_FILE):
print('Save file does not exist.')
return
var ret := save_file.open(SAVE_FILE, File.READ)
if ret != OK:
push_error('Could not read save file for reading.')
return
return parse_json(save_file.get_line())
static func remove_save():
var dir = Directory.new()
dir.open('user://')
dir.remove(SAVE_FILE)
static func call_recursive(node: Node, method: String, args: Array):
node.callv(method, args)
for child in node.get_children():
call_recursive(child, method, args)