Fix the aiming and bullet creation

This commit is contained in:
2025-02-01 18:03:00 +01:00
parent 33f1d57a50
commit bd91818682
19 changed files with 334 additions and 8089 deletions

View File

@ -7,6 +7,7 @@ class_name PlayerPlaceholder
@onready var client_node: CharacterBody3D = $PlayerControlledNode
@onready var server_node: PlayerServerNode = $ServerControlledNode
@export var character_speed: int = 5
@onready var bullet_starting_poing: Node3D = $ServerControlledNode/BulletStartingPoint
@ -109,7 +110,5 @@ func adjust_rotation(x: float, y: float, z: float):
push_warning("player rotation is not valid, adjusting")
client_node.rotation = desired_rotation
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

View File

@ -1,9 +1,10 @@
[gd_scene load_steps=13 format=3 uid="uid://bmm3brgvr4d86"]
[gd_scene load_steps=15 format=3 uid="uid://bmm3brgvr4d86"]
[ext_resource type="Script" path="res://scenes/player/player_input_controller.gd" id="1_m6tul"]
[ext_resource type="Script" path="res://scenes/player/placeholder.gd" id="1_ts455"]
[ext_resource type="Texture2D" uid="uid://oopj5mj1vdp0" path="res://assets/crosshairs/crosshair_default.png" id="3_8ulsx"]
[ext_resource type="PackedScene" uid="uid://dtvo21mk1webd" path="res://scenes/weapon/guns/ak/with_hands.tscn" id="3_xxv4y"]
[ext_resource type="PackedScene" uid="uid://1txob6jskn5s" path="res://scenes/characters/blue/dummy.tscn" id="5_16l76"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_taqso"]
radius = 0.4
@ -66,9 +67,13 @@ tracks/0/keys = {
"values": [Vector3(0, 0, 0), Vector3(0, -0.01, -0.01), Vector3(0, 0, 0)]
}
[sub_resource type="Animation" id="Animation_fhivu"]
resource_name = "idle"
[sub_resource type="AnimationLibrary" id="AnimationLibrary_u1b6p"]
_data = {
"RESET": SubResource("Animation_falg4"),
"idle": SubResource("Animation_fhivu"),
"move": SubResource("Animation_mkk0p"),
"shooting": SubResource("Animation_ah507")
}
@ -86,6 +91,18 @@ properties/2/replication_mode = 1
properties/3/path = NodePath(".:initial_position")
properties/3/spawn = true
properties/3/replication_mode = 2
properties/4/path = NodePath("PlayerControlledNode/CameraMount/BulletStartingPoint:position")
properties/4/spawn = true
properties/4/replication_mode = 1
properties/5/path = NodePath("PlayerControlledNode/CameraMount/BulletStartingPoint:rotation")
properties/5/spawn = true
properties/5/replication_mode = 1
properties/6/path = NodePath("PlayerControlledNode/CameraMount:rotation")
properties/6/spawn = true
properties/6/replication_mode = 1
properties/7/path = NodePath("ServerControlledNode/BulletStartingPoint:rotation")
properties/7/spawn = true
properties/7/replication_mode = 1
[node name="Placeholder" type="Node3D"]
script = ExtResource("1_ts455")
@ -99,7 +116,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.786919, 0)
shape = SubResource("CapsuleShape3D_taqso")
[node name="CameraMount" type="Node3D" parent="PlayerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29568, -0.412934)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29568, -0.355108)
[node name="Camera3D" type="Camera3D" parent="PlayerControlledNode/CameraMount"]
cull_mask = 524287
@ -173,23 +190,17 @@ fov = 40.0
[node name="GunMount" type="Node3D" parent="PlayerControlledNode/CameraMount/Camera3D/HUD/SubViewportContainer/SubViewport/Camera3D"]
[node name="AnimationPlayer" type="AnimationPlayer" parent="PlayerControlledNode/CameraMount/Camera3D/HUD/SubViewportContainer/SubViewport/Camera3D/GunMount"]
active = false
libraries = {
"": SubResource("AnimationLibrary_u1b6p")
}
[node name="WithHands" parent="PlayerControlledNode/CameraMount/Camera3D/HUD/SubViewportContainer/SubViewport/Camera3D/GunMount" instance=ExtResource("3_xxv4y")]
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0.51335, -0.655058, -1.67097)
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, -0.3, -0.4, 0)
visible = false
[node name="BulletStartingPoint" type="Node3D" parent="PlayerControlledNode/CameraMount"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -0.42185)
[node name="AimRay" type="RayCast3D" parent="PlayerControlledNode/CameraMount/BulletStartingPoint"]
target_position = Vector3(0, 0, 1000)
hit_from_inside = true
collide_with_areas = true
[node name="Node3D" type="Node3D" parent="PlayerControlledNode"]
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
@ -205,4 +216,13 @@ collision_layer = 4
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.786919, 0)
shape = SubResource("CapsuleShape3D_taqso")
[node name="BulletStartingPoint" type="Node3D" parent="ServerControlledNode"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 1.287, -0.54568)
[node name="Dummy" parent="ServerControlledNode" instance=ExtResource("5_16l76")]
visible = false
[node name="CameraMount" type="Node3D" parent="ServerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.15245, -0.0611187)
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

View File

@ -7,6 +7,7 @@
extends CharacterBody3D
@export var jumping := false
var shooting := false
@export var input_direction := Vector2()
@onready var camera_mount = $CameraMount
@ -28,6 +29,7 @@ var current_weapon_damage: int
var current_weapon_bullet_speed: int
var current_weapon_cooldown_interwal: float
var look_dir: Vector2
var moving = false
func _ready() -> void:
global_position = $"..".initial_position
# -- TODO: It should not be hardcoded
@ -37,7 +39,7 @@ func _ready() -> void:
var scene: PackedScene = ResourceLoader.load(path)
var node: Node3D = scene.instantiate()
node.scale = Vector3(0.5,0.5,0.5)
node.position = Vector3(0.5, -0.5, -1.5)
node.position = Vector3(-0.3, -0.4, 0)
current_weapon_bullet_speed = node.bullet_speed
current_weapon_cooldown_interwal = node.cooldown
current_weapon_damage = node.damage
@ -47,14 +49,19 @@ func _ready() -> void:
func _input(event):
if multiplayer.get_unique_id() == get_multiplayer_authority():
if Input.is_action_just_pressed("jump"): jumping = true
if Input.is_action_pressed("shoot"): shooting = true
if Input.is_action_just_released("shoot"): shooting = false
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
look_dir = event.relative * 1
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)
server_node.set_rotation_y.rpc_id(1, rotation.y)
server_node.set_rotation_x.rpc_id(1, rotation.x)
server_node.set_rotation_x.rpc_id(1, -camera_mount.rotation.x)
#server_node.set_input_direction.rpc_id(1, rotation.x, 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"):
moving = true
else:
moving = false
@onready var server_node: PlayerServerNode = $"../ServerControlledNode"
# Called every frame. 'delta' is the elapsed time since the previous frame.
@ -64,27 +71,33 @@ func _physics_process(delta: float) -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
server_node.set_input_direction.rpc_id(1, input_direction)
if Input.is_action_just_pressed("jump"):
server_node.jump.rpc_id(1)
if Input.is_action_pressed("shot"): _shoot()
if multiplayer.get_unique_id() == get_multiplayer_authority():
if not is_on_floor():
velocity += get_gravity() * delta
if is_on_floor() && jumping:
server_node.jump.rpc_id(1)
velocity.y = JUMP_VELOCITY
if shooting:
server_node.shoot.rpc_id(1)
jumping = false
var direction := (transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
if is_on_floor():
if direction:
#first_view_legs_anim.play("Run Forward")
gun_mount_anim.play("move")
velocity.x = direction.x * placeholder.character_speed
velocity.z = direction.z * placeholder.character_speed
else:
velocity.x = move_toward(velocity.x, 0, placeholder.character_speed)
velocity.z = move_toward(velocity.z, 0, placeholder.character_speed)
func _process(delta: float) -> void:
move_and_slide()
if moving:
gun_mount_anim.play("move")
else:
gun_mount_anim.stop()
move_and_slide()
var cant_shoot: bool = false