diff --git a/TODO_LIST b/TODO_LIST index 3294133..1ed96d6 100644 --- a/TODO_LIST +++ b/TODO_LIST @@ -2,7 +2,7 @@ Current plans: ^ Add world model ^ Separate Viewmodel ^ Hide view from players and world from player - Rework player states + ^ Rework player states Player teams Team spawns Bomb diff --git a/scenes/player.tscn b/scenes/player.tscn index a3668dd..922bbf1 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=17 format=3 uid="uid://dpsr6ug3pkb40"] +[gd_scene load_steps=22 format=3 uid="uid://dpsr6ug3pkb40"] [ext_resource type="Script" uid="uid://3dphlay25fih" path="res://scripts/player/player.gd" id="1_g2els"] [ext_resource type="Script" uid="uid://dalwlndejfdhm" path="res://scripts/player/crosshair.gd" id="3_dqkch"] @@ -8,7 +8,12 @@ [ext_resource type="Script" uid="uid://bmecgup3kcua7" path="res://scripts/weapon_system/weapon_system.gd" id="4_qlg0r"] [ext_resource type="Script" uid="uid://dd5mp72dq43v6" path="res://scripts/multiplayer/own_visibility_toggle.gd" id="4_smehm"] [ext_resource type="PackedScene" uid="uid://djwjl8xll53vn" path="res://scenes/weapons/starting_pistol.tscn" id="7_fjrip"] +[ext_resource type="Script" uid="uid://3777rkbebgjm" path="res://scripts/state_machine/machine.gd" id="8_f1ej7"] [ext_resource type="PackedScene" uid="uid://c2r8dbudbs7l3" path="res://models/molikman_ingame.glb" id="8_smehm"] +[ext_resource type="Script" uid="uid://bv8sgx78s8hwn" path="res://scripts/player/states/crouching.gd" id="9_oprun"] +[ext_resource type="Script" uid="uid://u0e2b2mvij1k" path="res://scripts/player/states/standing.gd" id="10_a8ls1"] +[ext_resource type="Script" uid="uid://cwasvwhm5yg0o" path="res://scripts/player/states/walk.gd" id="11_qfm1y"] +[ext_resource type="Script" uid="uid://cq4i0afwesdm3" path="res://scripts/player/states/falling.gd" id="12_fulsm"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_u8vuu"] @@ -120,12 +125,9 @@ properties/4/path = NodePath(".:hp") properties/4/spawn = true properties/4/replication_mode = 1 -[node name="Player" type="CharacterBody3D" node_paths=PackedStringArray("animation_player", "stand_up_area")] +[node name="Player" type="CharacterBody3D"] collision_layer = 2 script = ExtResource("1_g2els") -animation_player = NodePath("AnimationPlayer") -stand_up_area = NodePath("StandArea") -CROUCH_TIME = 0.1 [node name="molikman_ingame" parent="." instance=ExtResource("8_smehm")] transform = Transform3D(0.75, 0, 0, 0, 0.75, 0, 0, 0, 0.75, 0, 1.1793717, 0) @@ -262,6 +264,30 @@ crosses_offset = 3.0 [node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] replication_config = SubResource("SceneReplicationConfig_qhqgy") +[node name="BodyStateMachine" type="Node" parent="." node_paths=PackedStringArray("current_state")] +script = ExtResource("8_f1ej7") +current_state = NodePath("Stand") +metadata/_custom_type_script = "uid://3777rkbebgjm" + +[node name="Crouch" type="Node" parent="BodyStateMachine" node_paths=PackedStringArray("stand_up_area", "player", "animation_player")] +script = ExtResource("9_oprun") +stand_up_area = NodePath("../../StandArea") +player = NodePath("../..") +animation_player = NodePath("../../AnimationPlayer") + +[node name="Stand" type="Node" parent="BodyStateMachine" node_paths=PackedStringArray("player")] +script = ExtResource("10_a8ls1") +player = NodePath("../..") + +[node name="Walk" type="Node" parent="BodyStateMachine" node_paths=PackedStringArray("player")] +script = ExtResource("11_qfm1y") +player = NodePath("../..") + +[node name="Fall" type="Node" parent="BodyStateMachine" node_paths=PackedStringArray("player")] +script = ExtResource("12_fulsm") +player = NodePath("../..") +SPEED = 5.0 + [node name="WeaponSystem" type="Node" parent="." node_paths=PackedStringArray("default_pistol", "animation_player", "camera")] script = ExtResource("4_qlg0r") default_pistol = NodePath("StartingPistol") diff --git a/scripts/player/player.gd b/scripts/player/player.gd index 3e63cf7..aa6cdd0 100644 --- a/scripts/player/player.gd +++ b/scripts/player/player.gd @@ -16,82 +16,17 @@ const MAX_HP = 100 get: return hp -@export var animation_player: AnimationPlayer -@export var stand_up_area: Area3D - -@export var CROUCH_TIME: float = 1.0 -@export var SPEED: float = 5.0 -@export var JUMP_VELOCITY: float = 4.5 -# Replace with settings -@export var TOGGLE_CROUCH: bool = true -@export var WALK_MODIFIER: float = 0.5 - - - -@export var crouched: bool = false: - set(value): - if value != crouched and stand_up_area.has_overlapping_bodies() == false: - crouched = value - update_crouch() - SPEED = (SPEED*WALK_MODIFIER) if crouched else (SPEED/WALK_MODIFIER) - potential_crouched = value - get: - return crouched - @onready var TEMP_start_pos = global_position -var potential_crouched: bool = crouched - func _enter_tree() -> void: set_multiplayer_authority(str(name).to_int()) -func _process(_delta: float) -> void: +func _physics_process(_delta: float) -> void: if not is_multiplayer_authority(): return - if potential_crouched != crouched: - crouched = potential_crouched - -func _physics_process(delta: float) -> void: - if not is_multiplayer_authority(): - return - # Add the gravity. - if not is_on_floor(): - velocity += get_gravity() * delta - - # Handle jump. - if Input.is_action_just_pressed("plr_jump") and is_on_floor(): - velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var input_dir := Input.get_vector("plr_strafe_r","plr_strafe_l", "plr_back","plr_forward") - var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() - if direction: - velocity.x = direction.x * SPEED - velocity.z = direction.z * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) - velocity.z = move_toward(velocity.z, 0, SPEED) - + move_and_slide() -func update_crouch(): - if crouched: - animation_player.play("Crouch",-1,1/CROUCH_TIME) - else: - animation_player.play("Crouch",-1,-1/CROUCH_TIME,true) - -func _input(event: InputEvent) -> void: - if not is_multiplayer_authority(): - return - if event.is_action_pressed("plr_crouch"): - if TOGGLE_CROUCH: - crouched = not crouched - else: - crouched = true - elif event.is_action_released("plr_crouch") and TOGGLE_CROUCH == false: - crouched = false - func die() -> void: global_position = TEMP_start_pos hp = MAX_HP diff --git a/scripts/player/states/crouching.gd b/scripts/player/states/crouching.gd new file mode 100644 index 0000000..52eddd3 --- /dev/null +++ b/scripts/player/states/crouching.gd @@ -0,0 +1,41 @@ +extends State + +@export var SPEED: float = 2.5 +@export var toggle: bool = false +@export var stand_up_area: Area3D +@export var player: Player +@export var animation_player: AnimationPlayer +@export var crouch_time: float = 0.1 + +func enter() -> void: + animation_player.play("Crouch",-1,1/crouch_time) + +func exit() -> void: + animation_player.play("Crouch",-1,-1/crouch_time,true) + +func physics_update(_delta: float) -> void: + if not is_multiplayer_authority(): + return + if Input.is_action_just_pressed("plr_jump") and player.is_on_floor() and toggle: + transition.emit("Stand") + return + + if not player.is_on_floor(): + transition.emit("Fall") + return + + var input_dir := Input.get_vector("plr_strafe_r","plr_strafe_l", "plr_back","plr_forward") + var direction := (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + player.velocity.x = direction.x * SPEED + player.velocity.z = direction.z * SPEED + else: + player.velocity.x = move_toward(player.velocity.x, 0, SPEED) + player.velocity.z = move_toward(player.velocity.z, 0, SPEED) + +func _input(event: InputEvent) -> void: + if not is_multiplayer_authority(): + return + if (toggle == true and event.is_action_pressed("plr_crouch")) or (toggle == false and event.is_action_released("plr_crouch")): + if stand_up_area.has_overlapping_bodies() == false: + transition.emit("Stand") diff --git a/scripts/player/states/crouching.gd.uid b/scripts/player/states/crouching.gd.uid new file mode 100644 index 0000000..7f2bd43 --- /dev/null +++ b/scripts/player/states/crouching.gd.uid @@ -0,0 +1 @@ +uid://bv8sgx78s8hwn diff --git a/scripts/player/states/falling.gd b/scripts/player/states/falling.gd new file mode 100644 index 0000000..fe53303 --- /dev/null +++ b/scripts/player/states/falling.gd @@ -0,0 +1,28 @@ +extends State + +@export var player: Player +@export var SPEED: float + +func enter() -> void: + pass + +func exit() -> void: + pass + + +func physics_update(delta: float) -> void: + if not is_multiplayer_authority(): + return + if player.is_on_floor(): + transition.emit("Stand") + + player.velocity += player.get_gravity() * delta + + var input_dir := Input.get_vector("plr_strafe_r","plr_strafe_l", "plr_back","plr_forward") + var direction := (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + player.velocity.x = direction.x * SPEED + player.velocity.z = direction.z * SPEED + else: + player.velocity.x = move_toward(player.velocity.x, 0, SPEED) + player.velocity.z = move_toward(player.velocity.z, 0, SPEED) diff --git a/scripts/player/states/falling.gd.uid b/scripts/player/states/falling.gd.uid new file mode 100644 index 0000000..58629cd --- /dev/null +++ b/scripts/player/states/falling.gd.uid @@ -0,0 +1 @@ +uid://cq4i0afwesdm3 diff --git a/scripts/player/states/standing.gd b/scripts/player/states/standing.gd new file mode 100644 index 0000000..05a81d7 --- /dev/null +++ b/scripts/player/states/standing.gd @@ -0,0 +1,42 @@ +extends State + +@export var SPEED: float = 5.0 +@export var JUMP_VELOCITY: float = 4.5 +@export var player: Player + +func enter() -> void: + pass + +func exit() -> void: + pass + +func physics_update(_delta: float) -> void: + if not is_multiplayer_authority(): + return + if Input.is_action_just_pressed("plr_jump") and player.is_on_floor(): + player.velocity.y = JUMP_VELOCITY + transition.emit("Fall") + return + + if not player.is_on_floor(): + transition.emit("Fall") + return + + var input_dir := Input.get_vector("plr_strafe_r","plr_strafe_l", "plr_back","plr_forward") + var direction := (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + player.velocity.x = direction.x * SPEED + player.velocity.z = direction.z * SPEED + else: + player.velocity.x = move_toward(player.velocity.x, 0, SPEED) + player.velocity.z = move_toward(player.velocity.z, 0, SPEED) + +func _input(event: InputEvent) -> void: + if not is_multiplayer_authority(): + return + + if event.is_action_pressed("plr_crouch"): + transition.emit("Crouch") + + if event.is_action_pressed("plr_walk"): + transition.emit("Walk") diff --git a/scripts/player/states/standing.gd.uid b/scripts/player/states/standing.gd.uid new file mode 100644 index 0000000..24314dc --- /dev/null +++ b/scripts/player/states/standing.gd.uid @@ -0,0 +1 @@ +uid://u0e2b2mvij1k diff --git a/scripts/player/states/walk.gd b/scripts/player/states/walk.gd new file mode 100644 index 0000000..5e71712 --- /dev/null +++ b/scripts/player/states/walk.gd @@ -0,0 +1,41 @@ +extends State + +@export var SPEED: float = 2.5 +@export var JUMP_VELOCITY: float = 4.5 +@export var player: Player + +func enter() -> void: + pass + +func exit() -> void: + pass + +func physics_update(_delta: float) -> void: + if not is_multiplayer_authority(): + return + if Input.is_action_just_pressed("plr_jump") and player.is_on_floor(): + player.velocity.y = JUMP_VELOCITY + transition.emit("Fall") + return + + if not player.is_on_floor(): + transition.emit("Fall") + return + + var input_dir := Input.get_vector("plr_strafe_r","plr_strafe_l", "plr_back","plr_forward") + var direction := (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + player.velocity.x = direction.x * SPEED + player.velocity.z = direction.z * SPEED + else: + player.velocity.x = move_toward(player.velocity.x, 0, SPEED) + player.velocity.z = move_toward(player.velocity.z, 0, SPEED) + +func _input(event: InputEvent) -> void: + if not is_multiplayer_authority(): + return + if event.is_action_released("plr_walk"): + transition.emit("Stand") + + if event.is_action_pressed("plr_crouch"): + transition.emit("Crouch") diff --git a/scripts/player/states/walk.gd.uid b/scripts/player/states/walk.gd.uid new file mode 100644 index 0000000..dddeccb --- /dev/null +++ b/scripts/player/states/walk.gd.uid @@ -0,0 +1 @@ +uid://cwasvwhm5yg0o