WIP: Add weapon and jumps
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2025-01-30 21:43:15 +01:00
parent 5a45278545
commit 5159ef9e27
9 changed files with 285 additions and 40 deletions

View File

@ -7,6 +7,9 @@ class_name PlayerPlaceholder
@onready var client_node: CharacterBody3D = $PlayerControlledNode
@onready var server_node: CharacterBody3D = $ServerControlledNode
@export var character_speed: int = 5
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
set_multiplayer_authority(1)
@ -16,6 +19,7 @@ func _ready() -> void:
client_node.global_position = server_node.global_position
client_node.rotation.y = server_node.rotation.y
client_node.rotation.x = server_node.rotation.x
if multiplayer.is_server():
$Timer.start()
if owner_id != multiplayer.get_unique_id():

View File

@ -1,13 +1,79 @@
[gd_scene load_steps=6 format=3 uid="uid://bmm3brgvr4d86"]
[gd_scene load_steps=14 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="Script" path="res://scenes/player/server_player_controller.gd" id="3_f1bhn"]
[ext_resource type="PackedScene" uid="uid://dtvo21mk1webd" path="res://scenes/weapon/guns/ak/with_hands.tscn" id="3_xxv4y"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_taqso"]
radius = 0.4
height = 1.6
[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="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_mkk0p"]
resource_name = "move"
length = 0.5
loop_mode = 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.125, 0.25, 0.375, 0.5),
"transitions": PackedFloat32Array(1, 2, 1, 2, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0.001, 0.001, -0.001), Vector3(0, 0, 0), Vector3(-0.001, 0.001, -0.001), 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"),
"move": SubResource("Animation_mkk0p"),
"shooting": SubResource("Animation_ah507")
}
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_7vlrn"]
properties/0/path = NodePath("ServerControlledNode:position")
properties/0/spawn = true
@ -34,9 +100,96 @@ 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.240438)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29568, -0.128668)
[node name="Camera3D" type="Camera3D" parent="PlayerControlledNode/CameraMount"]
cull_mask = 524287
[node name="HUD" type="Control" parent="PlayerControlledNode/CameraMount/Camera3D"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 8.0
offset_right = 8.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="TextureRect" type="TextureRect" parent="PlayerControlledNode/CameraMount/Camera3D/HUD"]
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("3_8ulsx")
[node name="HealthIndicator" type="Label" parent="PlayerControlledNode/CameraMount/Camera3D/HUD"]
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="PlayerControlledNode/CameraMount/Camera3D/HUD" 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="PlayerControlledNode/CameraMount/Camera3D/HUD"]
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="PlayerControlledNode/CameraMount/Camera3D/HUD/SubViewportContainer"]
transparent_bg = true
handle_input_locally = false
msaa_2d = 1
msaa_3d = 1
canvas_cull_mask = 4294443008
size = Vector2i(1920, 1964)
render_target_update_mode = 4
[node name="Camera3D" type="Camera3D" parent="PlayerControlledNode/CameraMount/Camera3D/HUD/SubViewportContainer/SubViewport"]
cull_mask = 524288
fov = 40.0
[node name="WithHands" parent="PlayerControlledNode/CameraMount/Camera3D/HUD/SubViewportContainer/SubViewport/Camera3D" instance=ExtResource("3_xxv4y")]
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0.423577, -0.565285, -1.76074)
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="GunMount" type="Node3D" parent="PlayerControlledNode/CameraMount"]
visible = false
[node name="AnimationPlayer" type="AnimationPlayer" parent="PlayerControlledNode/CameraMount/GunMount"]
libraries = {
"": SubResource("AnimationLibrary_u1b6p")
}
[node name="Node3D" type="Node3D" parent="PlayerControlledNode"]
@ -48,9 +201,6 @@ script = ExtResource("3_f1bhn")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.786919, 0)
shape = SubResource("CapsuleShape3D_taqso")
[node name="CSGBox3D" type="CSGBox3D" parent="ServerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.91303, 0)
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_7vlrn")

View File

@ -13,14 +13,41 @@ extends CharacterBody3D
@onready var camera = $CameraMount/Camera3D
@onready var placeholder: Node3D = $'..'
var paused := false
const JUMP_VELOCITY = 4.5
#func _ready() -> void:
var current_gun: String = "ak"
@onready var gun_mount: Node3D = $CameraMount/GunMount
@onready var gun_mount_anim: AnimationPlayer = $CameraMount/GunMount/AnimationPlayer
var gun_with_hands: Node3D = null
@onready var bullet_starting_point: Node3D = $CameraMount/BulletStartingPoint
@onready var aim_ray: RayCast3D = $CameraMount/BulletStartingPoint/AimRay
@onready var gun_camera: Camera3D = $CameraMount/Camera3D/HUD/SubViewportContainer/SubViewport/Camera3D
var current_weapon_damage: int
var current_weapon_bullet_speed: int
var current_weapon_cooldown_interwal: float
var look_dir: Vector2
func _ready() -> void:
global_position = $"..".initial_position
# -- TODO: It should not be hardcoded
# Define a format string with placeholder '%s'
var path_tmpl := "res://scenes/weapon/guns/%s/with_hands.tscn"
var path := path_tmpl % current_gun
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)
current_weapon_bullet_speed = node.bullet_speed
current_weapon_cooldown_interwal = node.cooldown
current_weapon_damage = node.damage
gun_with_hands = node
gun_camera.add_child(node)
func _input(event):
if multiplayer.get_unique_id() == get_multiplayer_authority():
if Input.is_action_just_pressed("jump"): jumping = true
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
@ -37,12 +64,23 @@ func _process(delta: float) -> void:
server_node.set_input_direction.rpc_id(1, input_direction)
input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
if Input.is_action_just_pressed("jump"):
server_node.jump.rpc_id(1)
jump.rpc_id(1)
if Input.is_action_pressed("shot"): _shoot()
var cant_shoot: bool = false
func _shoot():
if not cant_shoot:
gun_with_hands.shoot()
cant_shoot = true
await get_tree().create_timer(current_weapon_cooldown_interwal).timeout
cant_shoot = false
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
if is_on_floor() && jumping:
velocity.y = JUMP_VELOCITY
jumping = false
if multiplayer.get_unique_id() == get_multiplayer_authority():
var direction := (transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
if is_on_floor():

View File

@ -1,5 +1,6 @@
class_name ServerControlledPlayer
extends CharacterBody3D
const JUMP_VELOCITY = 4.5
func _ready() -> void:
global_position = $"..".initial_position
@ -20,11 +21,17 @@ func set_rotation_y(new_rotation_y: float):
@rpc("call_local", "any_peer", "unreliable_ordered")
func set_rotation_x(new_rotation_x: float):
$"..".rotation.x = new_rotation_x
@rpc("call_local", "any_peer", "unreliable_ordered")
func jump():
jumping = true
var jumping := false
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
if is_on_floor() && jumping:
velocity.y = JUMP_VELOCITY
jumping = false
var direction := (transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
#if is_on_floor():