-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSharedData.gd
140 lines (124 loc) · 5.12 KB
/
SharedData.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
extends Node
const TILE_SIZE = 50
const TROOP_ANIMATION_SPEED = 30
const TROOP_SPRITE_SIZE = 128
const TROOP_SPRITE_SHEET_FRAMES = 10
############################
# Scen Setup #
############################
var loading_file_path
var starting_faction_id
var starting_scenario_config
var custom_factions
var selected_scenario_start_year
###########################
# Resources #
###########################
const PERSON_PORTRAIT_DEFAULT_MALE_OFFICER = -1
const PERSON_PORTRAIT_DEFAULT_MALE_MARTIAL = -2
const PERSON_PORTRAIT_DEFAULT_FEMALE = -3
const PERSON_PORTRAIT_BLANK = -9999
var person_portraits = {}
var troop_images = {}
var troop_sounds = {}
var troop_sprite_frames = {}
func _init():
GameConfig.load_game_config()
_load_troop_images()
_load_troop_sounds()
_load_person_portraits()
func _load_troop_images():
var dir = Directory.new()
dir.open("res://Images/Troop")
dir.list_dir_begin() # TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
while true:
var in_dir_name = dir.get_next()
if in_dir_name == "":
break
elif not in_dir_name.begins_with(".") and dir.current_is_dir() and in_dir_name.to_int() > 0:
var path = "res://Images/Troop/" + in_dir_name
var attack = load(path + "/Attack.png")
var be_attacked = load(path + "/BeAttacked.png")
var move = load(path + "/Move.png")
if attack != null and be_attacked != null and move != null:
var textures = {
"attack": attack,
"be_attacked": be_attacked,
"move": move
}
troop_images[in_dir_name.to_int()] = textures
func _load_troop_sprite_frames(military_kinds):
for military_kind in military_kinds:
var textures = SharedData.troop_images.get(military_kind.id, null)
if textures != null:
var sprite_frame = SpriteFrames.new()
var directions = ['ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'n']
for i in range(0, 8):
__set_frames(sprite_frame, "move_" + directions[i], textures["move"].get_data(), TROOP_SPRITE_SIZE * i)
__set_frames(sprite_frame, "be_attacked_" + directions[i], textures["be_attacked"].get_data(), TROOP_SPRITE_SIZE * i)
__set_frames(sprite_frame, "attack_" + directions[i], textures["attack"].get_data(), TROOP_SPRITE_SIZE * i)
troop_sprite_frames[military_kind.id] = sprite_frame
func __set_frames(sprite_frame, animation, texture, spritesheet_offset):
sprite_frame.add_animation_library(animation)
sprite_frame.set_animation_speed(animation, TROOP_ANIMATION_SPEED)
for i in range(0, TROOP_SPRITE_SHEET_FRAMES):
var sprite = Image.new()
sprite.create(TROOP_SPRITE_SIZE, TROOP_SPRITE_SIZE, false, texture.get_format())
sprite.blit_rect(texture, Rect2(i * TROOP_SPRITE_SIZE, spritesheet_offset, TROOP_SPRITE_SIZE, TROOP_SPRITE_SIZE), Vector2(0, 0))
var image = ImageTexture.new()
image.create_from_image(sprite)
sprite_frame.add_frame(animation, image)
func _load_troop_sounds():
var dir = Directory.new()
dir.open("res://Sounds/Troop")
dir.list_dir_begin() # TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
while true:
var in_dir_name = dir.get_next()
if in_dir_name == "":
break
elif not in_dir_name.begins_with(".") and dir.current_is_dir() and in_dir_name.to_int() > 0:
var path = "res://Sounds/Troop/" + in_dir_name
var attack = load(path + "/Attack.wav")
var moving = load(path + "/Moving.wav")
var critical = load(path + "/CriticalAttack.wav")
if attack != null and moving != null:
var sounds = {
"attack": attack,
"moving": moving,
"critical": critical
}
troop_sounds[in_dir_name.to_int()] = sounds
func __load_sound_file(file):
var wav_file = File.new()
var stream
wav_file.open(file, File.READ)
stream = AudioStreamWAV.new()
stream.format = AudioStreamWAV.FORMAT_IMA_ADPCM
stream.data = wav_file.get_buffer(wav_file.get_length())
stream.stereo = true
wav_file.close()
return stream
func _load_person_portraits():
var dir = Directory.new()
var path = "res://" + GameConfig.mod_directory + "Images/PersonPortrait"
dir.open(path)
dir.list_dir_begin() # TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
while true:
var in_file_name = dir.get_next()
# https://godotengine.org/qa/59637/cannot-traverse-asset-directory-in-android?show=59637#q59637
in_file_name = in_file_name.replace('.import', '')
if in_file_name == "":
break
elif not in_file_name.begins_with(".") and (in_file_name.ends_with('.jpg') or in_file_name.ends_with('.png')):
if in_file_name.to_int() > 0:
__load_portrait(path + "/" + in_file_name, in_file_name.to_int())
elif in_file_name == 'default-male-martial.jpg':
__load_portrait(path + "/" + in_file_name, PERSON_PORTRAIT_DEFAULT_MALE_MARTIAL)
elif in_file_name == 'default-male-officer.jpg':
__load_portrait(path + "/" + in_file_name, PERSON_PORTRAIT_DEFAULT_MALE_OFFICER)
elif in_file_name == 'default-female.jpg':
__load_portrait(path + "/" + in_file_name, PERSON_PORTRAIT_DEFAULT_FEMALE)
elif in_file_name == 'blank.jpg':
__load_portrait(path + "/" + in_file_name, PERSON_PORTRAIT_BLANK)
func __load_portrait(path, id):
person_portraits[id] = ResourceLoader.load(path)