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
|
class_name Wintermaul
extends Stage
signal lives_changed
class Teams extends Resource:
var top := Team.new()
var bottom := Team.new()
func list() -> Array[Team]: return [top, bottom]
class Team extends Resource:
var lives := 0
var players: Array[Player] = []
var teams := Teams.new()
var income_frequency := 15.0
func _init():
super._init()
set_player_script(preload("res://Stages/Wintermaul/player.gd"))
teams.top.lives = 10
teams.bottom.lives = 10
func _ready():
super._ready()
# initialize player resources
if multiplayer.is_server():
for player in Network.get_players():
Network.update_player.rpc(player.id, {
"money": 50,
"income": 5,
})
# set camera limits
$Camera.limit_left = $Map.get_used_rect().position.x * %Map.tile_set.tile_size.x - %Map.tile_set.tile_size.x
$Camera.limit_right = $Map.get_used_rect().end.x * %Map.tile_set.tile_size.x - %Map.tile_set.tile_size.x
$Camera.limit_top = %Map.get_used_rect().position.y * %Map.tile_set.tile_size.y
$Camera.limit_bottom = %Map.get_used_rect().end.y * %Map.tile_set.tile_size.y
# add players to teams
Network.player_joined.connect(add_player)
for id in Network.get_ordered_player_ids():
add_player(Network.get_player(id))
# set camera for local player
if get_team(Client.player) == teams.top:
$Camera.set_center($BuilderCollisions/TeamTop/CollisionShape2D.global_position)
elif get_team(Client.player) == teams.bottom:
$Camera.set_center($BuilderCollisions/TeamBottom/CollisionShape2D.global_position)
# set players for HUD teams
%HUD.get_node("%TeamTop").players = teams.top.players
%HUD.get_node("%TeamBottom").players = teams.bottom.players
# initialize timer and start
$IncomeTimer.wait_time = income_frequency
$IncomeTimer.start()
# initialize lives display
update_lives("top", 0)
update_lives("bottom", 0)
%HUD.attack_value_changed.connect(func(value: int):
Network.update_player.rpc(Client.player.id, {
"money": -(value * 10),
})
)
func _process(_delta: float) -> void:
%HUD.time.text = "Time: %.0fs" % clamp($IncomeTimer.time_left + 0.5, 0, $IncomeTimer.wait_time)
@rpc("any_peer", "call_local")
func place_tower(remote_data: Dictionary):
var data: Tower.NetworkData = dict_to_inst(remote_data)
var tower = Tower.from_network_data(data)
var player = Network.get_player(tower.owner_id)
player.towers[tower.global_position] = tower
player.money -= 5
Network.players_changed.emit()
#Network.update_player.rpc(player.id, {"money": -5})
_place_tower(%Towers, tower)
Client.placed_tower.emit(tower)
@rpc("any_peer", "call_local")
func spawn_unit(remote_data: Dictionary):
var data: Unit.NetworkData = dict_to_inst(remote_data)
var unit := Unit.from_network_data(data)
var player = Network.get_player(unit.owner_id)
player.units.append(unit)
if multiplayer.is_server():
unit.reached_goal.connect(func():
var team = get_team(player)
if team == teams.top:
update_lives.rpc("bottom", -1)
elif team == teams.bottom:
update_lives.rpc("top", -1)
)
#Network.update_player.rpc(player.id, {
#"money": -5,
#"income": 1,
#})
player.money -= 5
player.income += 1
Network.players_changed.emit()
_spawn_unit(%Towers, unit)
func can_place_tower():
if Client.player.money < 5:
print("Not enough money to build tower")
return false
return true
func can_spawn_unit():
if Client.player.money < 5:
print("Not enough money to spawn unit")
return false
return true
func add_player(player: Player):
if teams.bottom.players.size() < teams.top.players.size():
teams.bottom.players.append(player)
else:
teams.top.players.append(player)
func get_team(player: Player) -> Team:
for team in teams.list():
if player in team.players:
return team
return null
func get_spawn():
return %Spawn
func get_overwrite_target():
var team = get_team(Client.player)
if team == teams.top:
return [$Paths/PathNodeLeftDown, $Paths/PathNodeRightDown].pick_random()
elif team == teams.bottom:
return [$Paths/PathNodeLeftUp, $Paths/PathNodeRightUp].pick_random()
func get_builder_collision_masks():
var team = get_team(Client.player)
if team == teams.top:
return [28]
elif team == teams.bottom:
return [27]
func _on_income_timer_timeout() -> void:
if multiplayer.is_server():
for player in Network.get_players():
Network.update_player.rpc(player.id, {
"money": player.income,
})
reset_timer.rpc()
@rpc("authority", "call_local")
func reset_timer():
$IncomeTimer.wait_time = income_frequency
$IncomeTimer.start()
@rpc("authority", "call_local")
func update_lives(team: String, lives: int):
teams[team].lives += lives
lives_changed.emit()
|