bazar_alea/client/player/PlayerNetworkingComponent.gd

180 lines
6.1 KiB
GDScript3
Raw Permalink Normal View History

2023-11-14 21:41:08 +00:00
extends Node
class_name PlayerNetworkingComponent
#@export var character_movement_component : NodePath
#@onready var player_ref = get_node(character_movement_component)
## Refrence to character mesh, should be assigned to a [Node3D] that is a parent to the actual mesh (Skeleton3D)
@export var mesh_ref : Node
## Refrence to a [RayCast3D] that should detect if character is on ground
@export var ground_check : RayCast3D
## Reference to Object CharacterMovementComponent
@export var character_movement_component : CharacterMovementComponent
## Reference Player
@export var player_ref:CharacterBody3D
2023-11-14 21:41:08 +00:00
var tick:int = 0
var jump:Vector3 = Vector3(0.0, 0.0, 0.0)
var tickjump: int = 0
2023-11-14 21:41:08 +00:00
2023-12-11 23:19:21 +00:00
var target_pos:Vector3 = Vector3(0.0, 0.0, 0.0)
var target_rot:Vector3 = Vector3(0.0, 0.0, 0.0)
var target_vol:Vector3 = Vector3(0.0, 0.0, 0.0)
2023-11-14 21:41:08 +00:00
@export var id:int = 0:
get:
return id
set(value):
id = value
2023-11-20 22:18:02 +00:00
func set_id(value:int) -> void:
2023-11-14 21:41:08 +00:00
id = value
func is_local_authority() -> bool:
if Multi.is_offline():
return true
else:
return id == Multi.get_id()
2023-12-11 23:19:21 +00:00
2023-11-14 21:41:08 +00:00
#sync player on clients
2023-12-11 23:19:21 +00:00
func _process(delta):
if is_local_authority():
return
var diffpos = target_pos - get_my_position()
if target_pos == get_my_position():
Global.msg_info("delta:%f diffpos:%s", [delta, str(diffpos)])
#character_movement_component.input_is_moving = false
return
else:
Global.msg_info("delta:%f target_pos:%s get_my_position:%s diffpos:%s", [delta, str(target_pos), str(get_my_position), str(diffpos)])
#character_movement_component.input_is_moving = true
var diffrot = target_rot - get_my_rotation()
var direction = Vector3(diffpos.x,
0.0,
diffpos.z)
#print(diffpos)
#var h_rotation = -diffrot.y
#print(diffrot,", ", target_rot , ", ", get_my_rotation())
# .get_euler().y
# var hh:Basis = Basis.from_scale(diffrot)
# var h_rotation = hh.get_euler().y
# #print("hh:", hh, " -> ", h_rotation, " / ", diffrot)
# direction = direction.rotated(Vector3.UP,h_rotation).normalized()
#print("diffpos:", diffpos.x,", ", diffpos.z )
# if target_vol == Vector3(0.0, 0.0, 0.0):
# var diffrot = target_rot - get_my_rotation()
# var h_rotation = diffrot.y
# direction = direction.rotated(Vector3.UP,h_rotation) #.normalized()
# print(diffrot,", ", target_rot , ", ", get_my_rotation())
# Rotation apply directly when we move the player on specific direction
if character_movement_component.gait == Global.GAIT.SPRINTING :
character_movement_component.add_movement_input(direction, character_movement_component.current_movement_data.sprint_speed,character_movement_component.current_movement_data.sprint_acceleration)
elif character_movement_component.gait == Global.GAIT.RUNNING:
character_movement_component.add_movement_input(direction, character_movement_component.current_movement_data.run_speed,character_movement_component.current_movement_data.run_acceleration)
else:
character_movement_component.add_movement_input(direction, character_movement_component.current_movement_data.walk_speed,character_movement_component.current_movement_data.walk_acceleration)
func get_my_position() -> Vector3:
#return $SpringArm3D/Camera3D.get_global_position()
return player_ref.get_global_position()
func get_my_rotation() -> Vector3:
return mesh_ref.get_global_rotation()
func set_first_position_player(ctick:int, pos:Vector3, rot:Vector3):
set_my_position(pos)
set_my_rotation(rot)
func update_player(ctick:int, pos:Vector3, rot:Vector3, extra:int, ctickjump:int, vel:Vector3):
#print(tick, " - ", ctick)
if tick != ctick:
#set_my_rotation(rot)
2023-12-11 23:19:21 +00:00
Global.msg_info("extra:%s ctickjump:%s tickjump:%s", [str(extra), str(ctickjump), str(tickjump)])
if extra & 1:
set_my_position_onlyground(pos)
if tickjump == ctickjump:
2023-12-11 23:19:21 +00:00
Global.msg_info("Already Jump", [])
return
tickjump = ctickjump - 1
character_movement_component.jump()
2023-12-11 23:19:21 +00:00
Global.msg_info("Go Jump", [])
elif tickjump == 0 or ground_check.is_colliding():
2023-12-11 23:19:21 +00:00
Global.msg_info("No Jump", [])
tickjump = 0
2023-12-11 23:19:21 +00:00
update_my_position(pos, rot)
#set_my_position(pos)
#set_my_rotation(rot)
else:
2023-12-11 23:19:21 +00:00
set_my_position_onlyground(pos)
Global.msg_info("Always Jump", [])
tick = ctick
func set_my_position(pos:Vector3):
player_ref.set_global_position(pos)
2023-12-11 23:19:21 +00:00
target_pos = pos
func set_my_position_onlyground(pos:Vector3):
var newpos:Vector3 = Vector3(pos.x, player_ref.get_global_position().y ,pos.z)
2023-12-11 23:19:21 +00:00
target_pos = newpos
#player_ref.set_global_position(newpos)
func set_my_rotation(rot:Vector3):
2023-12-11 23:19:21 +00:00
Global.msg_info("Update %s %s", [str(rot), str(mesh_ref.get_global_rotation())])
mesh_ref.set_global_rotation(rot)
#$CharacterMovementComponent.input_direction = rot
2023-12-11 23:19:21 +00:00
target_rot = rot
2023-12-11 23:19:21 +00:00
func update_my_position(pos:Vector3, rot:Vector3):
# if target_pos == pos:
# # input_is_moving
# character_movement_component.input_is_moving = false
# else:
# target_pos = pos
# character_movement_component.input_is_moving = true
target_pos = pos
target_rot = rot
# var diffpos = get_my_position() - pos
# var diffrot = get_my_rotation() - rot
# var direction = Vector3(diffpos.x,
# 0.0,
# diffpos.z)
# var h_rotation = 0.0 #diffrot.y
# direction = direction.rotated(Vector3.UP,h_rotation).normalized()
# if character_movement_component.gait == Global.GAIT.SPRINTING :
# character_movement_component.add_movement_input(direction, character_movement_component.current_movement_data.sprint_speed,character_movement_component.current_movement_data.sprint_acceleration)
# elif character_movement_component.gait == Global.GAIT.RUNNING:
# character_movement_component.add_movement_input(direction, character_movement_component.current_movement_data.run_speed,character_movement_component.current_movement_data.run_acceleration)
# else:
# character_movement_component.add_movement_input(direction, character_movement_component.current_movement_data.walk_speed,character_movement_component.current_movement_data.walk_acceleration)
# pass
func is_jump() -> bool:
return jump != Vector3( 0.0, 0.0, 0.0)
func get_jump() -> Vector3:
var ret:Vector3 = jump
jump = Vector3(0.0, 0.0, 0.0)
return ret
func get_tickjump() -> int:
return tickjump
func get_comment() -> String:
return ""
func start_jump(vertical_velocity:Vector3):
jump = vertical_velocity
tickjump += 1