summaryrefslogtreecommitdiff
path: root/godot/scripts/characters/player_controller_new.gd
blob: a6a0827e0bc089e2f9c87bd84496047fda044c30 (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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
extends RigidBody

# Game
export var team: String = "RED"
export var health: int = 100
var weapon: Node = null
var world: Spatial = null

# Camera
export var mouse_sensitivity:float = 12.0
export var FOV:float = 90.0
var mouse_axis: Vector2 = Vector2.ZERO
onready var head: Spatial = $Neck/Head
onready var neck: Spatial = $Neck

# Move
var velocity := Vector3()
var direction := Vector3()
var move_axis := Vector2()
var floorspeed := Vector3()
var jumping:bool = false
var can_jump:bool = true
onready var nav: NavigationAgent = $NavigationAgent

# Walk
const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
export var jump_height: float = 300.0
var in_water: bool = false
var swim_speed: float = 450.0
var climb_speed: float = 5.0

# 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

#physics
var player_state: PhysicsDirectBodyState = null
var is_on_floor:bool = false
var floor_normal: Vector3 = Vector3.UP
var acceleration:float = 80.0
export var walk_speed:float = 5.0
var c_friction:float = 4.0
var air_control:float = 0.3

# Called when the node enters the scene tree
func _ready() -> void:
	weapon = preload("res://scenes/weapons/w_Rockets.tscn").instance()
	add_child(weapon)
	world = get_tree().get_root().get_node("GAMEWORLD")

	$"%UseRay".add_exception(self)
	$"%MeleeRay".add_exception(self)
	$"%UseRay".add_exception($AreaDetect)
	$"%MeleeRay".add_exception($AreaDetect)

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

# Called every frame. 'delta' is the elapsed time since the previous frame
func _process(_delta: float) -> void:
	if is_player and !world.is_chatting:
		if Input.is_action_just_pressed("use"):
			initiate_use()

		if controlling_machine:
			if Input.is_action_just_pressed("fire"):
				machine.attack1()
			if Input.is_action_just_pressed("fire2"):
				machine.attack2()
			machine.direction_input(Input.get_action_strength("move_forward"),Input.get_action_strength("move_backward"), Input.get_action_strength("move_right"),Input.get_action_strength("move_left"), Input.get_action_strength("alt_right"),Input.get_action_strength("alt_left"))
			machine.misc_input(Input.get_action_strength("move_duck"),Input.get_action_strength("move_jump"),Input.get_action_strength("move_walk"))
			machine.mouse_input(Input.get_action_strength("fire"), Input.get_action_strength("fire3"),Input.get_action_strength("fire2"))
		else:
			jumping = Input.get_action_strength("move_jump")
			if Input.is_action_just_pressed("fire"):
				weapon.attack1()
			move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
			move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")


func initiate_use():
	if controlling_machine:
		machine.relinquish_control()
		return
	if ladder_m != null:
		leave_ladder()
		return
	if $"%UseRay".is_colliding():
		var area_c = $"%UseRay".get_collider()
		match area_c.name:
			"SteerArea":
				world.rpc_id(1, "_call_on_server", "_client_request_control_vehicle", {"id" : world.client_id, "machine_path" : area_c.get_parent().get_path(), "char_name" : name})
			"LadderArea":
				mount_ladder(area_c.get_parent())
			"TugArea":
				pass
			"PickupArea":
				pass
			_:
				pass

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

# Called every physics tick. 'delta' is constant
func _physics_process(delta: float) -> void:
	if is_network_master():
		if ladder_m != null:
			climb_ladder(delta)
		elif !on_floor_test() and in_water:
			swim(delta)
		else:
			walk(delta)
		is_on_floor = false #reset whether is on floor in between frames

# called each physics frame
func on_floor_test() -> bool:
	if $Feet.is_colliding():
		is_on_floor = true
		floor_normal = Vector3.UP
		floorspeed = $Feet.get_collider().get_linear_velocity() if $Feet.get_collider().has_method("get_linear_velocity") else Vector3.ZERO
		return true
	if player_state:
		for i in range(player_state.get_contact_count()):
			var contact_angle_from_up : float = Vector3.UP.angle_to(player_state.get_contact_local_normal(i))
			if contact_angle_from_up < FLOOR_MAX_ANGLE:
				floor_normal = player_state.get_contact_local_normal(i)
				is_on_floor = true
				return true
	return false

#modify simulated physics results
func _integrate_forces(state: PhysicsDirectBodyState) -> void:
	if !is_network_master():
		return
	player_state = state
	velocity = state.get_linear_velocity()
	$normal_vis.look_at($normal_vis.global_transform.origin + global_transform.basis.z, floor_normal)
	for i in range(player_state.get_contact_count()):
		var contact_angle_from_up : float = Vector3.UP.angle_to(player_state.get_contact_local_normal(i))
		if contact_angle_from_up > FLOOR_MAX_ANGLE and !is_on_floor:
			friction = 0
			break
		if i == player_state.get_contact_count() - 1:
			friction = 1
	
	rpc("set_phys_transform", transform, linear_velocity)
	if global_transform.origin.y < -30:
		rpc("damage", 500000, "drown", [1, "Davy Jones"])

func walk(_delta: float) -> void:
	return
	# Input
	direction = Vector3()
	var aim: Basis = head.get_global_transform().basis
	direction += -move_axis.x * aim.z + move_axis.y * aim.x
	direction.y = 0
	direction = direction.normalized()
	
	if floor_normal != Vector3.UP: direction = direction.rotated(floor_normal.cross(Vector3.UP).normalized(), Vector3.UP.angle_to(floor_normal))
	
	# Jump
	if is_player and jumping and is_on_floor and can_jump:
		jump()
	
	#max walk speed
	var _speed = walk_speed
	var _temp_accel: float = acceleration
	var _cspeed = sqrt(pow(velocity.x-floorspeed.x,2)+pow(velocity.z-floorspeed.z,2))
	
	if not is_on_floor:
		_temp_accel *= air_control

	var projVel = Vector2(velocity.x-floorspeed.x,velocity.z-floorspeed.z).dot(Vector2(direction.x,direction.z))
	
	if is_on_floor:
		add_central_force(-mass*_cspeed*linear_velocity.normalized()*c_friction)#friction
		if _speed - _cspeed > 0:
			add_central_force (mass*Vector3(direction.x*_temp_accel, 0, direction.z*_temp_accel))#velocity.x += direction.x*_temp_accel
		else:
			add_central_force(mass*Vector3(direction.x*(_speed-projVel), 0, direction.z*(_speed-projVel)))
	elif 1.0 - projVel > 0:
		add_central_force (mass*Vector3(direction.x*_temp_accel, 0, direction.z*_temp_accel))

func jump():
	can_jump = false
	apply_central_impulse(Vector3.UP*jump_height)
	yield(get_tree().create_timer(0.05),"timeout")
	can_jump = true

func swim(_delta: float):
	#drag and buoyancy
	add_central_force(Vector3.UP*weight*1.0)
	add_central_force(-1*linear_velocity*100)
	#controls
	var dir: Basis = head.get_global_transform().basis
	var m_dir: Vector3 = -move_axis.x * dir.z + move_axis.y * dir.x
	m_dir = m_dir.normalized()
	add_central_force(swim_speed*m_dir)
	if jumping:
		add_central_force(Vector3.UP*weight*0.5)
	
func enter_water():
	in_water = true

func exit_water():
	in_water = false

func mount_ladder(target_ladder: Spatial):
	var ladder_tracker = Spatial.new()
	ladder_tracker.name = name
	target_ladder.add_child(ladder_tracker)
	ladder_tracker.transform = target_ladder.bottom.transform
	
	ladder_tracker.global_transform.origin = target_ladder.get_nearest_point_to_route(global_transform.origin)
	look_at(global_transform.origin + target_ladder.global_transform.basis.x, target_ladder.global_transform.basis.y)
	
	ladder_m = ladder_tracker
	global_transform.origin = ladder_m.global_transform.origin
	global_transform.basis = ladder_m.global_transform.basis.orthonormalized()
	linear_velocity = Vector3.ZERO
	set_gravity_scale(0.0)

#called each frame while climbing ladder
func climb_ladder(delta: float):
	var new_ladder_pos = ladder_m.global_transform.origin + ladder_m.global_transform.basis.y.normalized() * move_axis.x * delta * climb_speed
	var prog = ladder_m.get_parent().get_climb_scalar(new_ladder_pos)
	if prog >= 0.0 and prog <= 1.0:
		ladder_m.global_transform.origin = new_ladder_pos
	global_transform.origin = ladder_m.global_transform.origin
	global_transform.basis = ladder_m.global_transform.basis.orthonormalized()

func leave_ladder():
	if (ladder_m.get_parent().top.global_transform.origin - global_transform.origin).length_squared() < 0.01:
		apply_central_impulse(-400*ladder_m.global_transform.basis.z)
	global_transform.basis = world.global_transform.basis
	set_gravity_scale(1.0)
	ladder_m.queue_free()
	ladder_m = 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)