summaryrefslogtreecommitdiff
path: root/Stages/Wintermaul/unit_manager.gd
blob: 8690966774c719378699e2757a696093bc44df20 (plain)
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
class_name UnitManager
extends Node


@export var stage: Stage
@export var units_container: Node2D

@export var money_manager: MoneyManager
@export var path_manager: PathManager
@export var notification_manager: NotificationManager


func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null):
	if not can_spawn_unit(unit):
		return
	
	var network_id = multiplayer.get_unique_id()
	unit.owner_id = network_id
	unit.name = "Unit@" + str(network_id) + "@" + str(Time.get_ticks_usec())
	
	unit.global_position = spawn.spawn_position
	unit.target = spawn.next_node
	
	if overwrite_target:
		unit.target = overwrite_target
	
	spawn_unit_in_stage.rpc(inst_to_dict(unit.to_network_data()))


@rpc("any_peer", "call_local")
func spawn_unit_in_stage(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)
	
	player.money -= unit.unit_resource.cost
	player.income += unit.unit_resource.income
	Network.players_changed.emit()
	
	#if multiplayer.is_server(): # TODO: TeamManager
		#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)
		#)
	
	unit.sprite.modulate = player.get_color()
	
	units_container.add_child(unit, true)


func can_spawn_unit(unit: Unit):
	if Client.player.money < unit.unit_resource.cost:
		notification_manager.add_status_message("Not enough money to spawn unit")
		return false
	
	return true