Fix the sync and add shooting
This commit is contained in:
148
godot/scenes/player/hud.tscn
Normal file
148
godot/scenes/player/hud.tscn
Normal file
@ -0,0 +1,148 @@
|
||||
[gd_scene load_steps=10 format=3 uid="uid://btlkodvngm634"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://oopj5mj1vdp0" path="res://assets/crosshairs/crosshair_default.png" id="1_u13st"]
|
||||
|
||||
[sub_resource type="LabelSettings" id="LabelSettings_3bk8i"]
|
||||
font_size = 70
|
||||
|
||||
[sub_resource type="LabelSettings" id="LabelSettings_adbcb"]
|
||||
font_size = 100
|
||||
font_color = Color(0.756874, 0, 0.223924, 1)
|
||||
|
||||
[sub_resource type="Environment" id="Environment_i2xeo"]
|
||||
sdfgi_enabled = true
|
||||
glow_enabled = true
|
||||
|
||||
[sub_resource type="Animation" id="Animation_falg4"]
|
||||
length = 0.001
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath(".:position")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0.125),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 0,
|
||||
"values": [Vector3(0, 0, 0)]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_fhivu"]
|
||||
resource_name = "idle"
|
||||
|
||||
[sub_resource type="Animation" id="Animation_mkk0p"]
|
||||
resource_name = "move"
|
||||
length = 0.5
|
||||
loop_mode = 2
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath(".:position")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0, 0.125, 0.25, 0.375, 0.5),
|
||||
"transitions": PackedFloat32Array(1, 2, 1, 2, 1),
|
||||
"update": 0,
|
||||
"values": [Vector3(0, 0, 0), Vector3(-0.1, 0.1, -0.1), Vector3(0, 0, 0), Vector3(-0.1, 0.1, -0.1), Vector3(0, 0, 0)]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_ah507"]
|
||||
resource_name = "shooting"
|
||||
length = 0.2
|
||||
loop_mode = 1
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath(".:position")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0, 0.1, 0.2),
|
||||
"transitions": PackedFloat32Array(1, 0.233258, 8.57419),
|
||||
"update": 0,
|
||||
"values": [Vector3(0, 0, 0), Vector3(0, -0.01, -0.01), Vector3(0, 0, 0)]
|
||||
}
|
||||
|
||||
[sub_resource type="AnimationLibrary" id="AnimationLibrary_u1b6p"]
|
||||
_data = {
|
||||
"RESET": SubResource("Animation_falg4"),
|
||||
"idle": SubResource("Animation_fhivu"),
|
||||
"move": SubResource("Animation_mkk0p"),
|
||||
"shooting": SubResource("Animation_ah507")
|
||||
}
|
||||
|
||||
[node name="HUD" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
mouse_filter = 2
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -36.0
|
||||
offset_top = -36.0
|
||||
offset_right = 36.0
|
||||
offset_bottom = 36.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
texture = ExtResource("1_u13st")
|
||||
|
||||
[node name="HealthIndicator" type="Label" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 2
|
||||
anchor_top = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_top = -23.0
|
||||
offset_right = 40.0
|
||||
grow_vertical = 0
|
||||
text = "100"
|
||||
label_settings = SubResource("LabelSettings_3bk8i")
|
||||
|
||||
[node name="FPS" type="Label" parent="." groups=["player_placeholder"]]
|
||||
layout_mode = 1
|
||||
offset_right = 40.0
|
||||
offset_bottom = 23.0
|
||||
text = "0"
|
||||
label_settings = SubResource("LabelSettings_adbcb")
|
||||
|
||||
[node name="SubViewportContainer" type="SubViewportContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
stretch = true
|
||||
|
||||
[node name="SubViewport" type="SubViewport" parent="SubViewportContainer"]
|
||||
transparent_bg = true
|
||||
handle_input_locally = false
|
||||
msaa_2d = 1
|
||||
msaa_3d = 1
|
||||
canvas_cull_mask = 4294443008
|
||||
size = Vector2i(1152, 648)
|
||||
render_target_update_mode = 4
|
||||
|
||||
[node name="Camera3D" type="Camera3D" parent="SubViewportContainer/SubViewport"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.30409, -0.371306)
|
||||
cull_mask = 524288
|
||||
environment = SubResource("Environment_i2xeo")
|
||||
fov = 50.0
|
||||
|
||||
[node name="GunMount" type="Node3D" parent="SubViewportContainer/SubViewport/Camera3D"]
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="SubViewportContainer/SubViewport/Camera3D/GunMount"]
|
||||
libraries = {
|
||||
"": SubResource("AnimationLibrary_u1b6p")
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://cwqhnknctn83"]
|
||||
[gd_scene load_steps=5 format=3 uid="uid://bm80m5x5me4mp"]
|
||||
|
||||
[ext_resource type="Script" path="res://scenes/player/server_node.gd" id="1_djt0m"]
|
||||
[ext_resource type="PackedScene" uid="uid://ddwrs0so7swxn" path="res://scenes/characters/y-bot/character.tscn" id="2_a22jl"]
|
||||
@ -15,15 +15,6 @@ properties/1/spawn = true
|
||||
properties/1/replication_mode = 1
|
||||
|
||||
[node name="PlayerPlaceholder" type="PlayerPlaceholder"]
|
||||
_import_path = NodePath("")
|
||||
unique_name_in_owner = false
|
||||
process_mode = 0
|
||||
process_priority = 0
|
||||
process_physics_priority = 0
|
||||
process_thread_group = 0
|
||||
physics_interpolation_mode = 0
|
||||
auto_translate_mode = 0
|
||||
editor_description = ""
|
||||
collision_layer = 4
|
||||
collision_mask = 5
|
||||
script = ExtResource("1_djt0m")
|
||||
|
@ -3,7 +3,7 @@
|
||||
# client side, and send the changes to the server node
|
||||
# ---------------------------------------------------------------------
|
||||
extends Node3D
|
||||
class_name PlayerInputController
|
||||
class_name PlayerNode
|
||||
|
||||
var jumping: bool = false
|
||||
var shooting: bool = false
|
||||
@ -21,6 +21,9 @@ var controlled_node: ServerNode = null
|
||||
@onready var shared_node: CharacterBody3D = $SharedNode
|
||||
@onready var camera_mount: Node3D = $SharedNode/CameraMount
|
||||
@onready var camera: Camera3D = $SharedNode/CameraMount/Camera3D
|
||||
var hud: Control
|
||||
const DEFAULT_WEAPON := "ak"
|
||||
var first_slot_weapon: WeaponController
|
||||
|
||||
func _ready() -> void:
|
||||
set_multiplayer_authority(multiplayer.get_unique_id())
|
||||
@ -28,9 +31,25 @@ func _ready() -> void:
|
||||
shared_node.set_collision_mask_value(1, true)
|
||||
shared_node.set_collision_mask_value(2, true)
|
||||
camera.make_current()
|
||||
print(controlled_node.shared_node.global_position)
|
||||
var hud_scene: PackedScene = ResourceLoader.load("res://scenes/player/hud.tscn")
|
||||
hud = hud_scene.instantiate()
|
||||
camera.add_child(hud)
|
||||
_load_weapon()
|
||||
# Load the default weapon and set the current attack properties
|
||||
|
||||
func _load_weapon() -> void:
|
||||
var path_tmpl := "res://scenes/weapon/guns/%s/with_hands.tscn"
|
||||
var path := path_tmpl % DEFAULT_WEAPON
|
||||
var scene: PackedScene = ResourceLoader.load(path)
|
||||
var node: WeaponController = scene.instantiate()
|
||||
first_slot_weapon = node
|
||||
#first_slot_weapon.position = Vector3(-1, -1, -1)
|
||||
hud.find_child("GunMount").add_child(first_slot_weapon)
|
||||
|
||||
|
||||
func initial_position_sync():
|
||||
|
||||
print(controlled_node.shared_node.global_position)
|
||||
shared_node.global_position = controlled_node.shared_node.global_position
|
||||
shared_node.rotation = controlled_node.shared_node.rotation
|
||||
|
||||
@ -42,13 +61,16 @@ func _input(event):
|
||||
look_dir = event.relative * 1
|
||||
shared_node.rotation.y -= look_dir.x * camera_sens * 1.0
|
||||
camera_mount.rotation.x = clamp(camera_mount.rotation.x - look_dir.y * camera_sens * 1.0, -1.5, 1.5)
|
||||
controlled_node.set_rotation_x.rpc_id(1, shared_node.rotation.x)
|
||||
controlled_node.set_rotation_x.rpc_id(1, camera_mount.rotation.x)
|
||||
controlled_node.set_rotation_y.rpc_id(1, shared_node.rotation.y)
|
||||
if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right") or Input.is_action_pressed("move_forward") or Input.is_action_pressed("move_backwards"):
|
||||
if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right") or Input.is_action_pressed("move_forward") or Input.is_action_pressed("move_backward"):
|
||||
moving = true
|
||||
else:
|
||||
moving = false
|
||||
input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
||||
controlled_node.set_input_direction.rpc_id(1, input_direction)
|
||||
if Input.is_action_just_pressed("shoot"): shooting = true
|
||||
if Input.is_action_just_released("shoot"): shooting = false
|
||||
|
||||
func jump():
|
||||
jumping = true
|
||||
@ -58,7 +80,6 @@ func _physics_process(delta: float) -> void:
|
||||
if multiplayer.get_unique_id() == get_multiplayer_authority():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
controlled_node.set_input_direction.rpc_id(1, input_direction)
|
||||
if not shared_node.is_on_floor():
|
||||
shared_node.velocity += shared_node.get_gravity() * delta
|
||||
if shared_node.is_on_floor() && jumping:
|
||||
@ -67,16 +88,40 @@ func _physics_process(delta: float) -> void:
|
||||
#if shooting:
|
||||
jumping = false
|
||||
|
||||
if shooting:
|
||||
controlled_node.shoot.rpc_id(1)
|
||||
first_slot_weapon.shoot()
|
||||
|
||||
var direction := (shared_node.transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
|
||||
if shared_node.is_on_floor():
|
||||
if direction:
|
||||
#first_view_legs_anim.play("Run Forward")
|
||||
shared_node.velocity.x = direction.x * consts.DEFAULT_CHARACTER_SPEED
|
||||
shared_node.velocity.z = direction.z * consts.DEFAULT_CHARACTER_SPEED
|
||||
else:
|
||||
shared_node.velocity.x = move_toward(shared_node.velocity.x, 0, consts.DEFAULT_CHARACTER_SPEED)
|
||||
shared_node.velocity.z = move_toward(shared_node.velocity.z, 0, consts.DEFAULT_CHARACTER_SPEED)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
shared_node.move_and_slide()
|
||||
|
||||
# -- This rpc should be called by the server in order to
|
||||
# -- make the client send its position to the server
|
||||
@rpc("any_peer", "call_local", "reliable")
|
||||
func verify_position() -> void:
|
||||
var desired_position: Vector3 = shared_node.global_position
|
||||
controlled_node.send_position.rpc_id(1, desired_position.x, desired_position.y, desired_position.z)
|
||||
|
||||
@rpc("authority", "call_local")
|
||||
func adjust_position(x: float, y: float, z: float):
|
||||
var new_position: Vector3 = Vector3(x, y, z)
|
||||
shared_node.global_position = new_position
|
||||
|
||||
@rpc("any_peer", "call_local", "reliable")
|
||||
func verify_rotation() -> void:
|
||||
var desired_rotation: Vector3 = shared_node.global_rotation
|
||||
controlled_node.send_rotation.rpc_id(1, desired_rotation.x, desired_rotation.y, desired_rotation.z)
|
||||
|
||||
@rpc("authority", "call_local")
|
||||
func adjust_rotation(x: float, y: float, z: float):
|
||||
var new_rotation: Vector3 = Vector3(x, y, z)
|
||||
shared_node.rotation = new_rotation
|
||||
|
@ -7,5 +7,3 @@
|
||||
script = ExtResource("1_q00bg")
|
||||
|
||||
[node name="SharedNode" parent="." instance=ExtResource("2_4mkad")]
|
||||
|
||||
[node name="CSGSphere3D" type="CSGSphere3D" parent="."]
|
||||
|
@ -3,23 +3,45 @@ class_name ServerNode
|
||||
|
||||
var jumping := false
|
||||
var input_direction: Vector2
|
||||
var player_node: PlayerNode
|
||||
|
||||
# TODO: Shoould not be defined here
|
||||
const DEFAULT_WEAPON := "ak"
|
||||
var first_slot_weapon: WeaponController
|
||||
|
||||
@export var owner_id: int = 0
|
||||
|
||||
@onready var camera_mount: Node3D = $SharedNode/CameraMount
|
||||
@onready var shared_node: CharacterBody3D = $SharedNode
|
||||
@onready var bullet_starting_point: Node3D = $SharedNode/CameraMount/BulletStartingPoint
|
||||
@onready var map_controller: MapController
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
shared_node.set_collision_layer_value(3, true)
|
||||
shared_node.set_collision_mask_value(1, true)
|
||||
shared_node.set_collision_mask_value(3, true)
|
||||
# Deploy a local controller node
|
||||
map_controller = find_parent("Map")
|
||||
_load_weapon()
|
||||
# Load the default weapon and set the current attack properties
|
||||
|
||||
func _load_weapon() -> void:
|
||||
var path_tmpl := "res://scenes/weapon/guns/%s/with_hands.tscn"
|
||||
var path := path_tmpl % DEFAULT_WEAPON
|
||||
var scene: PackedScene = ResourceLoader.load(path)
|
||||
var node: WeaponController = scene.instantiate()
|
||||
first_slot_weapon = node
|
||||
first_slot_weapon.set_map_controller(map_controller)
|
||||
|
||||
func bind_player_node() -> void:
|
||||
if multiplayer.get_unique_id() == owner_id:
|
||||
player_node = get_tree().get_root().find_child("PlayerController", true, false)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not shared_node.is_on_floor():
|
||||
shared_node.velocity += shared_node.get_gravity() * delta
|
||||
if shared_node.is_on_floor() && jumping:
|
||||
shared_node.velocity.y = consts.DEFAULT_JUMP_VELOCITY
|
||||
|
||||
|
||||
#if shooting:
|
||||
jumping = false
|
||||
|
||||
@ -49,3 +71,45 @@ func set_rotation_x(x: float):
|
||||
@rpc("any_peer", "call_local", "unreliable")
|
||||
func set_rotation_y(y: float):
|
||||
shared_node.rotation.y = y
|
||||
|
||||
func _on_reconciliation_timer_timeout() -> void:
|
||||
if multiplayer.is_server():
|
||||
player_node.verify_position.rpc_id(owner_id)
|
||||
player_node.verify_rotation.rpc_id(owner_id)
|
||||
$ReconciliationTimer.start()
|
||||
|
||||
@rpc("any_peer", "call_local", "reliable")
|
||||
func send_position(x: float, y: float, z: float):
|
||||
if multiplayer.is_server():
|
||||
var desired_position: Vector3 = Vector3(x, y, z)
|
||||
if multiplayer.is_server():
|
||||
var real_position: Vector3 = shared_node.global_position
|
||||
var difference: Vector3 = desired_position - real_position
|
||||
if is_vector_a_lower_than_b(difference, Vector3(0.2, 0.2, 0.2)):
|
||||
shared_node.global_position = desired_position
|
||||
elif is_vector_a_lower_than_b(difference, Vector3(0.4, 0.4, 0.4)):
|
||||
var new_position: Vector3 = desired_position.lerp(real_position, 0.5)
|
||||
push_warning("player position is not valid, lerping")
|
||||
player_node.adjust_position.rpc_id(owner_id, new_position.x, new_position.y, new_position.z)
|
||||
else:
|
||||
push_warning("player position is not valid, adjusting")
|
||||
player_node.adjust_position.rpc_id(owner_id, real_position.x, real_position.y, real_position.z)
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func send_rotation(x: float, y: float, z: float):
|
||||
var desired_rotation: Vector3 = Vector3(x, y, z)
|
||||
if multiplayer.is_server():
|
||||
var real_rotation: Vector3 = shared_node.rotation
|
||||
var difference: Vector3 = desired_rotation - real_rotation
|
||||
if is_vector_a_lower_than_b(difference, Vector3(0.3, 0.3, 0.3)):
|
||||
shared_node.rotation = desired_rotation
|
||||
else:
|
||||
var new_rotation: Vector3 = desired_rotation.lerp(real_rotation, 0.5)
|
||||
player_node.adjust_rotation.rpc_id(owner_id, new_rotation.x, new_rotation.y, new_rotation.z)
|
||||
|
||||
@rpc("any_peer", "call_local", "unreliable_ordered")
|
||||
func shoot():
|
||||
first_slot_weapon.attack(bullet_starting_point)
|
||||
|
||||
func is_vector_a_lower_than_b(vec_a: Vector3, vec_b: Vector3) -> bool:
|
||||
return vec_a.x < vec_b.x and vec_a.y < vec_b.y and vec_a.z < vec_b.z
|
||||
|
@ -21,3 +21,9 @@ script = ExtResource("1_bau14")
|
||||
|
||||
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
|
||||
replication_config = SubResource("SceneReplicationConfig_2dhi2")
|
||||
|
||||
[node name="ReconciliationTimer" type="Timer" parent="."]
|
||||
wait_time = 0.1
|
||||
autostart = true
|
||||
|
||||
[connection signal="timeout" from="ReconciliationTimer" to="." method="_on_reconciliation_timer_timeout"]
|
||||
|
@ -12,11 +12,9 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)
|
||||
shape = SubResource("CapsuleShape3D_r38x6")
|
||||
|
||||
[node name="CameraMount" type="Node3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.6036, 2.18128)
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.6036, -0.127098)
|
||||
|
||||
[node name="Camera3D" type="Camera3D" parent="CameraMount"]
|
||||
|
||||
[node name="BulletStartingPoint" type="Node3D" parent="CameraMount"]
|
||||
|
||||
[node name="CSGBox3D" type="CSGBox3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||
transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, 0, 0, 0)
|
||||
|
Reference in New Issue
Block a user