-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_world.gd
168 lines (139 loc) · 5.16 KB
/
game_world.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
extends Node
# The GameWorld is meant to store references to all the other in-game nodes like units, buildings, and players. It is not intended to do any game logic beyond setup.
signal unit_deleted(unit)
export(PackedScene) var generic_player_scene
export(PackedScene) var swordsman_scene
export(PackedScene) var archer_scene
export(PackedScene) var cavalry_scene
onready var unit_scene_lookup = {
'swordsman': swordsman_scene,
'archer': archer_scene,
'cavalry': cavalry_scene,
}
export var print_debug_flags = {
'unit_death' : false,
'collision_flags' : false,
'combat_engagement' : false
}
var players_in_game = {} # {player_id : player}
var units_in_game = {} # {unit : player_id}
var player_group_tags = {} # {player_id : {type : group_tag}}
# Example: player_group_tags = {
# 1 : {
# 'selectable': 'P1_selectable_objects',
# 'units' : 'P1_units',
# 'buildings' : 'P1_buildings',
# ...
# }}
# Debug
func debug_print_multiple(debug_types, messages, crash=false):
"""
Print multiple messages if any of the provided debug_types are actively outputing.
Parameters
----------
debug_types : string or array of strings
The type or types of debug message to check for activity. Can be a
string representing one type or an array of strings for multiple types.
messages : array of a strings and/or arrays of objects
The messages to print if the debug type is active. The elements in the
messages array can be a single string or an array of objects that will
be converted into a string with the str function.
crash : bool=false
Indicates if the game should crash after printing message.
"""
if typeof(debug_types) == TYPE_STRING:
# Convert single string to array containing string
debug_types = [debug_types]
for db_type in print_debug_flags:
if db_type in debug_types and print_debug_flags[db_type]:
# At least one of the flags is active, print all of the messages
for message in messages:
if typeof(message) == TYPE_ARRAY:
# Convert an array of things into a string
message = PoolStringArray(message).join('')
print(message)
if crash:
assert(false)
break # only print messages once!
func debug_print(debug_types, message, crash=false):
"""
Print a message if any of the provided debug_types are actively outputing.
Parameters
----------
debug_types : string or array of strings
The type of debug message to check for activity. Can be a string
representing one type or an array of strings for multiple types.
message : string or array of objects
The message to print if the debug type is active. Can be a single
string or an array of objects that will be converted into a string (with
the str function).
crash : bool=false
Indicates if the game should crash after printing message
"""
debug_print_multiple(debug_types, [message], crash)
# Getters
func get_player_ids():
return players_in_game.keys()
func get_other_players_ids(player_id):
# Get the ids of everyone else but player_id
var others = []
for p_id in get_player_ids():
if p_id != player_id:
others.append(p_id)
return others
func get_player_group_tag(player_id, tag_type):
assert(tag_type in player_group_tags[player_id])
return player_group_tags[player_id][tag_type]
func get_other_players_group_tags(player_id, tag_type):
# Get the tags for everyone else but player_id
var others_tag_list = []
for other_id in get_other_players_ids(player_id):
others_tag_list.append(get_player_group_tag(other_id, tag_type))
return others_tag_list
# Checkers
func check_object_in_player_groups(object, player_id, tag_types):
var in_required_groups = true
for tag_type in tag_types:
var tag = get_player_group_tag(player_id, tag_type)
in_required_groups = in_required_groups and object.is_in_group(tag)
return in_required_groups
# Setters?
# Setup and Creation
func _ready():
pass # Replace with function body.
func create_player(player_id):
var new_player = generic_player_scene.instance()
new_player.setup(player_id)
add_child(new_player)
players_in_game[player_id] = new_player
player_group_tags[player_id] = {
'selectable' : 'P%d_selectable'%player_id,
'units' : 'P%d_units'%player_id,
}
return new_player
func create_unit(unit_type, unit_position, player_id):
var new_unit = unit_scene_lookup[unit_type].instance()
new_unit.setup({
"player" : players_in_game[player_id],
"position" : unit_position,
#"facing" : facing,
})
add_child(new_unit)
new_unit.add_to_group(player_group_tags[player_id]['selectable'])
new_unit.add_to_group(player_group_tags[player_id]['units'])
units_in_game[new_unit] = player_id
players_in_game[player_id].register_new_unit(new_unit)
new_unit.connect("unit_died", self, "delete_unit")
return new_unit
# Destruction
func delete_unit(dying_unit):
debug_print('unit_death', ["World responding to unit death: ", dying_unit])
if dying_unit in units_in_game:
debug_print('unit_death', ["World deleting: ", dying_unit])
var unit_id = dying_unit.get_instance_id()
var player_id = units_in_game[dying_unit]
players_in_game[player_id].deregister_dead_unit(dying_unit)
units_in_game.erase(dying_unit)
dying_unit.queue_free()
#yield(get_tree(), "idle_frame") # Wait a frame
emit_signal("unit_deleted", unit_id)