summaryrefslogtreecommitdiff
path: root/godot/scripts/characters/NetworkedCharacter.gd
blob: c8e90a4fab7b82bbe2c6a80663d5dc07f398c9c2 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
extends RigidBody
class_name NetChar

# Game
export var team: String = "RED"
export var health: int = 100
export var weapon_slot1: Resource = null
export var weapon_slot2: Resource = null
export var weapon_slot3: Resource = null
export var weapon_slot4: Resource = null
export var weapon_slot5: Resource = null
var weapons = [null,null,null,null,null]
var weapon: Resource = null
var world: Spatial = null
var carrying: bool = false
var carrying_object: NetObject = null

# Control
var controlling_machine: bool = false #whether character is riding/controlling something
var machine: RigidBody = null
export var is_player: bool = false #whether character is currently controlled by a player
var ladder_m: Spatial = null

func get_init_info() -> Dictionary:
	return {"linear_velocity" : linear_velocity, "angular_velocity" : angular_velocity, "controlling_machine" : controlling_machine, "team" : team, "health" : health, "nametag" : $Nametag.text}

func mp_init(init_info: Dictionary):
	for variable in init_info.keys():
		set(variable, init_info[variable])
	$Nametag.text = init_info["nametag"]

remote func set_phys_transform(trfrm: Transform, lvel: Vector3):
	transform = trfrm
	linear_velocity = lvel

remotesync func set_net_owner(owner_id: int):
	$Nametag.text = ""
	set_network_master(owner_id)
	if owner_id != 1:
		$Nametag.text = world.players_info[owner_id][0]
	if get_tree().get_network_unique_id() != 1:
		if owner_id == world.client_id:
			$Nametag.visible = false
			world.player_char = self
			is_player = true
			world.cam.attach(self, "FIRSTPERSON", "./Neck/Head")
		else:
			$Nametag.visible = true
			is_player = false
		world.get_node("HUD").update_characters()

func deselect_character():
	if is_network_master():
		world.player_char = null
		if world.client_id != 1: world.cam.attach(world, "STATIC", "./DEFAULTCAM")
		rpc("set_net_owner", 1)

func take_control_of_machine(slave_machine: RigidBody):
	machine = slave_machine
	controlling_machine = true

func lose_machine():
	if is_network_master(): world.cam.attach(self, "FIRSTPERSON", "./Neck/Head")
	controlling_machine = false
	machine = null

remotesync func damage(dmg_amt: int, _type: String, shooter: Array, extra: String):
	health -= dmg_amt
	if health <= 0 and is_network_master():
		if shooter[0] != get_network_master() and shooter[0] != 1: world.rpc_id(shooter[0], "game_killsound")
		if get_network_master() == 1:
			world._call_on_server("_character_death", {"killer_id" : shooter[0], "killer" : shooter[1], "victim_mp_id" : get_network_master(), "victim" : name, "extra" : extra})
		else:
			world.rpc_id(1, "_call_on_server", "_character_death", {"killer_id" : shooter[0], "killer" : shooter[1], "victim_mp_id" : get_network_master(), "victim" : name, "extra" : extra})
	elif is_network_master():
		if shooter[0] != get_network_master() and shooter[0] != 1: world.rpc_id(shooter[0], "game_hitsound")

remotesync func remove_dead_character():
	if is_network_master() and machine != null:
		machine.relinquish_control()
	deselect_character()
	queue_free()

remotesync func net_apply_impulse(impulse_v: Vector3):
	apply_central_impulse(impulse_v)