From bbdad2716b9392ffae14d3f7b4d11bb082e63611 Mon Sep 17 00:00:00 2001 From: AleaJactaEst Date: Sun, 6 Feb 2022 14:38:17 +0100 Subject: [PATCH] update action between walk and swim --- player/player.gd | 148 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) diff --git a/player/player.gd b/player/player.gd index 0021e6d..e9f1b78 100644 --- a/player/player.gd +++ b/player/player.gd @@ -1,7 +1,7 @@ extends CharacterBody3D -enum StatePlayer {FLY = 0, SWIM = 1, WALK = 2} +enum StatePlayer {FLY = 0, SWIM = 1, WALK = 2, WALK_WATER=3} # Constant const SPEED_WALK_UP = 5.0 @@ -27,6 +27,7 @@ const JUMP_FORCE = 4.5 const MUL_SPEED_FLY = 2.0 const MUL_SPEED_SWIM = 0.5 +const FACTOR_WALK_WATER = 0.3 # Get the gravity from the project settings to be synced with RigidDynamicBody nodes. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") @@ -78,6 +79,7 @@ var player_automove:bool = false var state_player:StatePlayer = StatePlayer.WALK var level_water:float +var heigh_underwater = 1.0 @onready var camera_fps:Camera3D = $camera_root/Camera3D_FPS_WALK @@ -86,6 +88,10 @@ func switch_state(new_state): return state_player = new_state match state_player: + StatePlayer.WALK_WATER: + print("switch camera WALK_WATER") + $carpet.hide() + camera_fps = $camera_root/Camera3D_FPS_WALK StatePlayer.WALK: print("switch camera WALK") $carpet.hide() @@ -238,6 +244,12 @@ func _input(event): func _physics_process(delta): # Add the gravity. match state_player: + StatePlayer.WALK_WATER: + if get_position().y > level_water - heigh_underwater: + _physics_process_walk_water(delta) + else: + switch_state(StatePlayer.SWIM) + _physics_process_swim(delta) StatePlayer.WALK: _physics_process_walk(delta) StatePlayer.FLY: @@ -374,6 +386,134 @@ func _physics_process_walk(delta): move_and_slide() +func _physics_process_walk_water(delta): + var input_dir: Vector2 + var input_x: float + var input_y: float + var speed: float + var move_up:bool = false + var move_down:bool = false + var move_strafe: bool = false + + #print(get_position()) + # Add the gravity. + if not is_on_floor(): + motion_velocity.y -= gravity * delta + + # Handle Jump. +# if Input.is_action_just_pressed("ui_accept") and is_on_floor(): +# motion_velocity.y = JUMP_FORCE + + # Get the input direction and handle the movement/deceleration. + if Input.is_action_pressed("ui_strafe"): + if Input.is_action_pressed("ui_left"): + input_x = 1.0 + move_strafe = true + elif Input.is_action_pressed("ui_right"): + input_x = -1.0 + move_strafe = true + else: + var y = 0 + if Input.is_action_pressed("ui_right"): + y -= 1 + if Input.is_action_pressed("ui_left"): + y += 1 + if y != 0: + var dt = y * delta * SPEED_ROTATE + if tps: + camera_rotate_y += dt + if camera_rotate_y > PI: + camera_rotate_y -= TAU + elif camera_rotate_y <= -PI: + camera_rotate_y += TAU + $camera_root/horizontal_root.rotate_y( dt ) + else: + player_rotate_y += dt + if player_rotate_y > PI: + player_rotate_y -= TAU + elif player_rotate_y <= -PI: + player_rotate_y += TAU + camera_rotate_y = player_rotate_y + rotate_y( dt ) + # Disable vector on ui_right/ui_left (used to rotate and not strafe) + input_x = 0 + if Input.is_action_pressed("ui_up"): + input_y = 1.0 + move_up = true + player_automove = false + elif Input.is_action_pressed("ui_down"): + player_automove = false + input_y = -1.0 + move_down = true + elif player_automove: + input_y = 1.0 + move_up = true + + if is_run: + if move_strafe: + if move_up: + speed = SPEED_RUN_UP_STRAFE + switch_animation(anim_run) + elif move_down: + speed = SPEED_RUN_DOWN_STRAFE + switch_animation(anim_run_backward) + else: + speed = SPEED_RUN_STRAFE + if input_x > 0.0: + switch_animation(anim_strafe_left_walk) + else: + switch_animation(anim_strafe_right_walk) + elif move_up: + speed = SPEED_RUN_UP + switch_animation(anim_run) + else: + speed = SPEED_RUN_DOWN + if move_down: + switch_animation(anim_run_backward) + else: + switch_animation(anim_idle) + else: + if move_strafe: + if move_up: + speed = SPEED_WALK_UP_STRAFE + switch_animation(anim_walk) + elif move_down: + speed = SPEED_WALK_DOWN_STRAFE + switch_animation(anim_walk_backward) + else: + speed = SPEED_WALK_STRAFE + if input_x > 0.0: + switch_animation(anim_strafe_left_walk) + else: + switch_animation(anim_strafe_right_walk) + elif move_up: + speed = SPEED_WALK_UP + switch_animation(anim_walk) + else: + speed = SPEED_WALK_DOWN + if move_down: + switch_animation(anim_walk_backward) + else: + switch_animation(anim_idle) + if input_x == 0.0 and input_y == 0.0: + if player_sit: + switch_animation(anim_sitting_ground_idle) + else: + switch_animation(anim_idle) + + var direction = (transform.basis * Vector3(input_x, 0, input_y)).normalized() + if direction: + motion_velocity.x = direction.x * speed * FACTOR_WALK_WATER + motion_velocity.z = direction.z * speed * FACTOR_WALK_WATER + else: + motion_velocity.x = move_toward(motion_velocity.x, 0, speed * FACTOR_WALK_WATER) + motion_velocity.z = move_toward(motion_velocity.z, 0, speed * FACTOR_WALK_WATER) + + #print("pos:", get_floor_angle(), " - ", get_wall_min_slide_angle(), " - ", get_floor_max_angle()) + #motion_velocity.y = 0.5 + move_and_slide() + + func _physics_process_fly(delta): var input_dir: Vector2 var input_x: float @@ -633,10 +773,14 @@ func _physics_process_swim(delta): var tmp = get_position() tmp.y = level_water set_position(tmp) + elif col == true and get_position().y >= level_water - heigh_underwater: + switch_state(StatePlayer.WALK_WATER) func _process( delta ): match state_player: + StatePlayer.WALK_WATER: + _process_walk(delta) StatePlayer.WALK: _process_walk(delta) StatePlayer.FLY: @@ -679,7 +823,7 @@ func _process_walk( delta ): func enter_underwater(): # function called by Area3D (Water Object) level_water = get_position().y - switch_state(StatePlayer.SWIM) + switch_state(StatePlayer.WALK_WATER) print("SWIM")