summaryrefslogtreecommitdiff
path: root/godot/scripts/characters/player_controller_new.gd
diff options
context:
space:
mode:
Diffstat (limited to 'godot/scripts/characters/player_controller_new.gd')
-rw-r--r--godot/scripts/characters/player_controller_new.gd67
1 files changed, 23 insertions, 44 deletions
diff --git a/godot/scripts/characters/player_controller_new.gd b/godot/scripts/characters/player_controller_new.gd
index 6df88f0..deaea79 100644
--- a/godot/scripts/characters/player_controller_new.gd
+++ b/godot/scripts/characters/player_controller_new.gd
@@ -10,8 +10,8 @@ var world
export(float) var mouse_sensitivity = 12.0
export(float) var FOV = 90.0
var mouse_axis := Vector2()
-onready var head: Spatial = $Head
-onready var cam: Camera = $Head/Camera
+onready var head: Spatial = $Neck/Head
+onready var neck: Spatial = $Neck
# Move
var velocity := Vector3()
@@ -38,6 +38,7 @@ var ladder_m = null
#physics
var player_state : PhysicsDirectBodyState
var is_on_floor:bool
+var floor_normal : Vector3 = Vector3.UP
var acceleration = 80.0
export(int) var walk_speed = 5
var c_friction = 4.0
@@ -48,15 +49,11 @@ func _ready() -> void:
weapon = preload("res://scenes/weapons/w_Rockets.tscn").instance()
add_child(weapon)
world = get_tree().get_root().get_node("GAMEWORLD")
- if is_player:
- cam.current = true
- $Head/Camera/UseRay.add_exception(self)
- $Head/Camera/MeleeRay.add_exception(self)
- $Head/Camera/UseRay.add_exception($AreaDetect)
- $Head/Camera/MeleeRay.add_exception($AreaDetect)
- Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
- cam.fov = FOV
+ $"%UseRay".add_exception(self)
+ $"%MeleeRay".add_exception(self)
+ $"%UseRay".add_exception($AreaDetect)
+ $"%MeleeRay".add_exception($AreaDetect)
func get_init_info():
return {"linear_velocity" : linear_velocity, "angular_velocity" : angular_velocity, "controlling_machine" : controlling_machine, "team" : team, "health" : health, "nametag" : $Nametag.text}
@@ -82,6 +79,8 @@ func _process(_delta: float) -> void:
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"):
@@ -97,8 +96,8 @@ func initiate_use():
if ladder_m != null:
leave_ladder()
return
- if $Head/Camera/UseRay.is_colliding():
- var area_c = $Head/Camera/UseRay.get_collider()
+ 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})
@@ -121,17 +120,16 @@ remotesync func set_net_owner(owner_id):
$Nametag.visible = false
world.player_char = self
is_player = true
- cam.current = true
+ world.cam.attach(self, "FIRSTPERSON", "./Neck/Head")
else:
$Nametag.visible = true
- cam.current = false
is_player = false
world.get_node("HUD").update_characters()
func deselect_character():
if is_network_master():
world.player_char = null
- world.get_node("DEFAULTCAM").current = true
+ if world.client_id != 1: world.cam.attach(world, "STATIC", "./DEFAULTCAM")
rpc("set_net_owner", 1)
func take_control_of_machine(slave_machine):
@@ -139,8 +137,7 @@ func take_control_of_machine(slave_machine):
controlling_machine = true
func lose_machine():
- if is_player:
- cam.current = true
+ if is_network_master(): world.cam.attach(self, "FIRSTPERSON", "./Neck/Head")
controlling_machine = false
machine = null
@@ -153,19 +150,20 @@ func _physics_process(delta: float) -> void:
swim(delta)
else:
walk(delta)
- rpc("set_phys_transform", transform, linear_velocity)
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
@@ -174,6 +172,7 @@ func on_floor_test() -> bool:
func _integrate_forces(state) -> void:
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:
@@ -181,13 +180,8 @@ func _integrate_forces(state) -> void:
break
if i == player_state.get_contact_count() - 1:
friction = 1
-
-# on input event
-func _input(event: InputEvent) -> void:
- if is_player:
- if event is InputEventMouseMotion:
- mouse_axis = event.relative
- camera_rotation()
+ if is_network_master():
+ rpc("set_phys_transform", transform, linear_velocity)
func walk(_delta:float) -> void:
# Input
@@ -197,6 +191,8 @@ func walk(_delta:float) -> void:
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()
@@ -212,7 +208,7 @@ func walk(_delta:float) -> void:
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*linear_velocity.normalized()*c_friction)#friction
+ 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:
@@ -231,7 +227,7 @@ func swim(_delta):
add_central_force(Vector3.UP*weight*1.0)
add_central_force(-1*linear_velocity*75)
#controls
- var dir: Basis = cam.get_global_transform().basis
+ 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)
@@ -293,22 +289,5 @@ remotesync func remove_dead_character():
deselect_character()
queue_free()
-func camera_rotation() -> void:
- if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
- return
- if mouse_axis.length() > 0:
- var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
- var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
-
- mouse_axis = Vector2()
-
- head.rotate_y(deg2rad(horizontal))
- cam.rotate_x(deg2rad(vertical))
-
- # Clamp mouse rotation
- var temp_rot: Vector3 = cam.rotation_degrees
- temp_rot.x = clamp(temp_rot.x, -90, 90)
- cam.rotation_degrees = temp_rot
-
remotesync func net_apply_impulse(impulse_v):
apply_central_impulse(impulse_v)