2022-02-02 23:01:07 +00:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-06 13:38:17 +00:00
|
|
|
enum StatePlayer {FLY = 0, SWIM = 1, WALK = 2, WALK_WATER=3}
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-03 21:19:58 +00:00
|
|
|
# Constant
|
2022-02-03 21:05:04 +00:00
|
|
|
const SPEED_WALK_UP = 5.0
|
|
|
|
const SPEED_WALK_UP_STRAFE = 3.0
|
|
|
|
const SPEED_WALK_STRAFE = 2.5
|
|
|
|
const SPEED_WALK_DOWN_STRAFE = 0.6
|
|
|
|
const SPEED_WALK_DOWN = 0.8
|
|
|
|
|
2022-02-12 01:00:16 +00:00
|
|
|
const DIFF_RUN_WALK = 2.0
|
|
|
|
const SPEED_RUN_UP = SPEED_WALK_UP * DIFF_RUN_WALK
|
|
|
|
const SPEED_RUN_UP_STRAFE = SPEED_WALK_UP_STRAFE * DIFF_RUN_WALK
|
|
|
|
const SPEED_RUN_STRAFE = SPEED_WALK_STRAFE * DIFF_RUN_WALK
|
|
|
|
const SPEED_RUN_DOWN_STRAFE = SPEED_WALK_DOWN_STRAFE * DIFF_RUN_WALK
|
|
|
|
const SPEED_RUN_DOWN = SPEED_WALK_DOWN * DIFF_RUN_WALK
|
2022-02-03 21:05:04 +00:00
|
|
|
|
2022-02-05 00:59:43 +00:00
|
|
|
const ZOOM_STEP_Y = 0.05
|
|
|
|
const ZOOM_STEP_Z = 0.1
|
|
|
|
const ZOOM_MIN_Z = 0.2
|
|
|
|
const ZOOM_MAX_Z = 3.0
|
2022-02-03 22:49:49 +00:00
|
|
|
|
2022-02-03 21:05:04 +00:00
|
|
|
const SPEED_ROTATE = PI
|
2022-02-02 23:01:07 +00:00
|
|
|
const PI_2 = PI / 2.0
|
2022-02-03 21:19:58 +00:00
|
|
|
const JUMP_FORCE = 4.5
|
2022-02-12 01:00:16 +00:00
|
|
|
const STEP_FORCE = 50.0
|
2022-02-03 21:19:58 +00:00
|
|
|
|
2022-02-06 00:49:25 +00:00
|
|
|
const MUL_SPEED_FLY = 2.0
|
|
|
|
const MUL_SPEED_SWIM = 0.5
|
2022-02-06 13:38:17 +00:00
|
|
|
const FACTOR_WALK_WATER = 0.3
|
2022-02-12 01:00:16 +00:00
|
|
|
const MUL_SPEED_STEP = 0.2
|
|
|
|
const SPEED_COLLIDED = 0.0001
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-03 21:19:58 +00:00
|
|
|
# Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
var starting_point = Vector2(DisplayServer.window_get_size().x / 2, DisplayServer.window_get_size().y / 2)
|
|
|
|
# Camera position
|
|
|
|
var camera_rotate_y = 0.0
|
|
|
|
var camera_rotate_x = 0.0
|
|
|
|
# Player position
|
|
|
|
var player_rotate_y = 0.0
|
2022-02-05 18:58:58 +00:00
|
|
|
var player_rotate_x = 0.0
|
2022-02-03 21:19:58 +00:00
|
|
|
# Activate reconciliation between camera & player
|
|
|
|
var reconciliate_rotate_camera_player:bool = true
|
2022-02-03 22:49:49 +00:00
|
|
|
# Player run
|
2022-02-03 21:05:04 +00:00
|
|
|
var is_run:bool = false
|
2022-02-03 22:49:49 +00:00
|
|
|
var zoom:float = 1.0
|
|
|
|
var tps:bool = true
|
2022-02-03 23:32:39 +00:00
|
|
|
var animation_object:AnimationPlayer = null
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-03 23:32:39 +00:00
|
|
|
var anim_idle:String = "CHAR_idle_bored"
|
|
|
|
var anim_run:String = "CHAR_run"
|
|
|
|
var anim_run_backward:String = "CHAR_run backward"
|
|
|
|
var anim_sitting_ground_idle:String = "CHAR_sitting_ground_idle"
|
|
|
|
var anim_strafe_left_walk:String = "CHAR_strafe_left_walk"
|
|
|
|
var anim_strafe_right_walk:String = "CHAR_strafe_right_walk"
|
|
|
|
var anim_walk:String = "CHAR_walk"
|
|
|
|
var anim_walk_backward:String = "CHAR_walk_backward"
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-06 15:02:02 +00:00
|
|
|
var anim_fly_idle:String = "CHAR_fly"
|
|
|
|
var anim_fly_run:String = "CHAR_fly"
|
|
|
|
var anim_fly_run_backward:String = "CHAR_fly"
|
|
|
|
var anim_fly_sitting_ground_idle:String = "CHAR_fly_idle"
|
|
|
|
var anim_fly_strafe_left_walk:String = "CHAR_fly"
|
|
|
|
var anim_fly_strafe_right_walk:String = "CHAR_fly"
|
|
|
|
var anim_fly_walk:String = "CHAR_fly"
|
|
|
|
var anim_fly_walk_backward:String = "CHAR_fly"
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-06 00:55:11 +00:00
|
|
|
var anim_swim_idle:String = "CHAR_swim_idle"
|
|
|
|
var anim_swim_run:String = "CHAR_swim_slow"
|
|
|
|
var anim_swim_run_backward:String = "CHAR_swim_idle"
|
|
|
|
var anim_swim_sitting_ground_idle:String = "CHAR_swim_idle"
|
|
|
|
var anim_swim_strafe_left_walk:String = "CHAR_swim_idle"
|
|
|
|
var anim_swim_strafe_right_walk:String = "CHAR_swim_idle"
|
|
|
|
var anim_swim_walk:String = "CHAR_swim_slow"
|
|
|
|
var anim_swim_walk_backward:String = "CHAR_swim_idle"
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-03 23:32:39 +00:00
|
|
|
var current_anim:String = anim_idle
|
2022-02-05 00:59:43 +00:00
|
|
|
var player_sit:bool = false
|
2022-02-05 01:06:50 +00:00
|
|
|
var player_automove:bool = false
|
2022-02-03 23:32:39 +00:00
|
|
|
|
2022-02-05 18:58:58 +00:00
|
|
|
var state_player:StatePlayer = StatePlayer.WALK
|
2022-02-06 00:49:25 +00:00
|
|
|
var level_water:float
|
2022-02-06 13:38:17 +00:00
|
|
|
var heigh_underwater = 1.0
|
2022-02-09 22:36:58 +00:00
|
|
|
var stop_move:bool = false
|
2022-02-12 01:00:16 +00:00
|
|
|
var max_collided_speed = 0.0
|
2022-02-06 00:49:25 +00:00
|
|
|
@onready var camera_fps:Camera3D = $camera_root/Camera3D_FPS_WALK
|
2022-02-12 01:00:16 +00:00
|
|
|
@onready var height_step:Vector3 = $RayCastStep.get_position() - $RayCastGround.get_position()
|
2022-02-06 00:49:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
func switch_state(new_state):
|
|
|
|
if state_player == new_state:
|
|
|
|
return
|
|
|
|
state_player = new_state
|
|
|
|
match state_player:
|
2022-02-06 13:38:17 +00:00
|
|
|
StatePlayer.WALK_WATER:
|
2022-02-06 15:02:02 +00:00
|
|
|
print("switch camera WALK (WALK_WATER)")
|
|
|
|
#$carpet.hide()
|
|
|
|
camera_fps.rotate_x( -player_rotate_x )
|
|
|
|
player_rotate_x = 0
|
2022-02-06 22:41:32 +00:00
|
|
|
if get_viewport().get_camera_3d() == camera_fps:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_WALK
|
|
|
|
camera_fps.make_current()
|
|
|
|
else:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_WALK
|
2022-02-06 00:49:25 +00:00
|
|
|
StatePlayer.WALK:
|
|
|
|
print("switch camera WALK")
|
2022-02-06 15:02:02 +00:00
|
|
|
#$carpet.hide()
|
|
|
|
camera_fps.rotate_x( -player_rotate_x )
|
|
|
|
player_rotate_x = 0
|
2022-02-06 22:41:32 +00:00
|
|
|
if get_viewport().get_camera_3d() == camera_fps:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_WALK
|
|
|
|
camera_fps.make_current()
|
|
|
|
else:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_WALK
|
2022-02-06 00:49:25 +00:00
|
|
|
StatePlayer.FLY:
|
|
|
|
print("switch camera FLY")
|
2022-02-06 15:02:02 +00:00
|
|
|
#$carpet.show()
|
|
|
|
camera_fps.rotate_x( -player_rotate_x )
|
|
|
|
player_rotate_x = 0
|
2022-02-06 22:41:32 +00:00
|
|
|
if get_viewport().get_camera_3d() == camera_fps:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_FLY
|
|
|
|
camera_fps.make_current()
|
|
|
|
else:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_FLY
|
2022-02-06 00:49:25 +00:00
|
|
|
StatePlayer.SWIM:
|
|
|
|
print("switch camera SWIM")
|
2022-02-06 15:02:02 +00:00
|
|
|
#$carpet.hide()
|
|
|
|
camera_fps.rotate_x( -player_rotate_x )
|
|
|
|
player_rotate_x = 0
|
2022-02-06 22:41:32 +00:00
|
|
|
if get_viewport().get_camera_3d() == camera_fps:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_SWIM
|
|
|
|
camera_fps.make_current()
|
|
|
|
else:
|
|
|
|
camera_fps = $camera_root/Camera3D_FPS_SWIM
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-03 23:32:39 +00:00
|
|
|
|
|
|
|
func search_animation( obj:Node , root:String = "/") -> bool:
|
|
|
|
var ret:bool = false
|
|
|
|
for i in obj.get_children():
|
|
|
|
print("DEBUG - search_animation: " + root + String(i.get_name()))
|
|
|
|
if i.get_name() == "AnimationPlayer" or i.get_name() == "animation_player":
|
|
|
|
animation_object = i
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
ret = search_animation(i, root + String(i.get_name()) + "/" )
|
|
|
|
if ret == true:
|
|
|
|
return ret
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
func print_list_animation():
|
|
|
|
for key in animation_object.get_animation_list():
|
|
|
|
print('Anim detected: ', key)
|
|
|
|
|
|
|
|
|
|
|
|
func switch_animation(name:String):
|
|
|
|
#print(">", name)
|
|
|
|
if name != current_anim and animation_object.has_animation(name):
|
|
|
|
current_anim = name
|
|
|
|
animation_object.play( name )
|
2022-02-05 13:14:17 +00:00
|
|
|
if not animation_object.is_connected("animation_finished", self._on_AnimationPlayer_animation_finished.bind(name)):
|
|
|
|
animation_object.connect("animation_finished", self._on_AnimationPlayer_animation_finished.bind(name))
|
2022-02-03 23:32:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _on_AnimationPlayer_animation_finished(name:String):
|
|
|
|
print("End animation: " , name)
|
2022-02-02 23:01:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _init():
|
2022-02-05 18:58:58 +00:00
|
|
|
print("wall_min_slide_angle: ", get_wall_min_slide_angle())
|
2022-02-09 22:36:58 +00:00
|
|
|
print("get_floor_max_angle: ", get_floor_max_angle())
|
|
|
|
print("get_floor_snap_length: ", get_floor_snap_length())
|
2022-02-05 18:58:58 +00:00
|
|
|
# set_wall_min_slide_angle(1.0)
|
2022-02-09 22:36:58 +00:00
|
|
|
#set_floor_max_angle(4.0)
|
2022-02-05 18:58:58 +00:00
|
|
|
# set_floor_snap_length(0.4)
|
2022-02-02 23:01:07 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
# Place the mouse at the center of the screen
|
|
|
|
get_viewport().warp_mouse(starting_point)
|
2022-02-09 22:36:58 +00:00
|
|
|
#$RayCastGround.rotate_x( -get_floor_max_angle() )
|
2022-02-03 23:32:39 +00:00
|
|
|
search_animation($Mesh/character, "/")
|
|
|
|
print_list_animation()
|
2022-02-06 00:49:25 +00:00
|
|
|
switch_state(StatePlayer.WALK)
|
2022-02-02 23:01:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _input(event):
|
2022-02-03 22:49:49 +00:00
|
|
|
# Managed zoom (FPS or TPS)
|
|
|
|
if Input.is_action_pressed("camera_zoom_in"):
|
|
|
|
if tps:
|
2022-02-05 00:59:43 +00:00
|
|
|
if zoom > ZOOM_MIN_Z:
|
|
|
|
zoom -= ZOOM_STEP_Z
|
|
|
|
var zoom3D:Vector3 = Vector3(0.0, -ZOOM_STEP_Y, -ZOOM_STEP_Z)
|
2022-02-03 22:49:49 +00:00
|
|
|
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.translate_object_local(zoom3D)
|
2022-02-05 00:59:43 +00:00
|
|
|
if zoom < ZOOM_MIN_Z:
|
2022-02-06 00:49:25 +00:00
|
|
|
camera_fps.rotate_x( -player_rotate_x )
|
|
|
|
player_rotate_x = 0
|
|
|
|
camera_fps.make_current()
|
2022-02-03 22:49:49 +00:00
|
|
|
tps = false
|
|
|
|
reconciliate_rotate_camera_player = false
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
$camera_root/horizontal_root.rotate_y( player_rotate_y - camera_rotate_y )
|
|
|
|
camera_rotate_y = player_rotate_y
|
|
|
|
elif Input.is_action_pressed("camera_zoom_out"):
|
|
|
|
if not tps:
|
|
|
|
# Reposition camera TPS back to player
|
|
|
|
reconciliate_rotate_camera_player = true
|
|
|
|
tps = true
|
2022-02-05 00:59:43 +00:00
|
|
|
zoom += ZOOM_STEP_Z
|
2022-02-03 22:49:49 +00:00
|
|
|
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.make_current()
|
2022-02-05 00:59:43 +00:00
|
|
|
if zoom >= ZOOM_MAX_Z:
|
|
|
|
zoom = ZOOM_MAX_Z
|
2022-02-03 22:49:49 +00:00
|
|
|
else:
|
2022-02-05 00:59:43 +00:00
|
|
|
var zoom3D:Vector3 = Vector3(0.0, ZOOM_STEP_Y, ZOOM_STEP_Z)
|
2022-02-03 22:49:49 +00:00
|
|
|
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.translate_object_local(zoom3D)
|
2022-02-05 01:06:50 +00:00
|
|
|
elif Input.is_action_just_pressed("player_sit"):
|
2022-02-05 00:59:43 +00:00
|
|
|
player_sit = not player_sit
|
2022-02-05 01:06:50 +00:00
|
|
|
elif Input.is_action_just_pressed("player_automove"):
|
|
|
|
player_automove = not player_automove
|
2022-02-05 18:58:58 +00:00
|
|
|
elif Input.is_action_just_pressed("switch_state_player"):
|
|
|
|
match state_player:
|
|
|
|
StatePlayer.WALK:
|
2022-02-06 00:49:25 +00:00
|
|
|
switch_state(StatePlayer.FLY)
|
2022-02-05 18:58:58 +00:00
|
|
|
StatePlayer.FLY:
|
2022-02-06 00:49:25 +00:00
|
|
|
switch_state(StatePlayer.WALK)
|
2022-02-03 22:49:49 +00:00
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
# If right mouse button is pressed and mouse moves, pan horizontally camera
|
|
|
|
# and rotate vertically
|
2022-02-03 22:49:49 +00:00
|
|
|
if tps:
|
|
|
|
if Input.is_action_just_pressed ( "ui_strafe" ):
|
|
|
|
reconciliate_rotate_camera_player = false
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
|
|
|
|
elif Input.is_action_just_released ( "ui_strafe" ):
|
|
|
|
reconciliate_rotate_camera_player = true
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
2022-02-03 21:05:04 +00:00
|
|
|
if Input.is_action_just_pressed( "move_run" ):
|
|
|
|
is_run = true
|
|
|
|
elif Input.is_action_just_released( "move_run" ):
|
|
|
|
is_run = false
|
2022-02-03 11:55:05 +00:00
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
if Input.is_mouse_button_pressed( 2 ):
|
|
|
|
if event is InputEventMouseMotion:
|
2022-02-03 22:49:49 +00:00
|
|
|
if tps:
|
|
|
|
camera_rotate_y -= event.relative.x *0.01
|
|
|
|
if camera_rotate_y > PI:
|
|
|
|
camera_rotate_y -= TAU
|
|
|
|
elif camera_rotate_y <= -PI:
|
|
|
|
camera_rotate_y += TAU
|
|
|
|
$camera_root/horizontal_root.rotate_y( -event.relative.x *0.01 )
|
|
|
|
|
|
|
|
var new_camera_rotate_x = camera_rotate_x + event.relative.y * 0.01
|
|
|
|
if new_camera_rotate_x <= PI_2 and new_camera_rotate_x >= - PI_2:
|
|
|
|
$camera_root/horizontal_root/vertical_root.rotate_x( event.relative.y * 0.01 )
|
|
|
|
camera_rotate_x = new_camera_rotate_x
|
|
|
|
else:
|
|
|
|
if player_rotate_y > PI:
|
|
|
|
player_rotate_y -= TAU
|
|
|
|
elif player_rotate_y <= -PI:
|
|
|
|
player_rotate_y += TAU
|
|
|
|
camera_rotate_y = player_rotate_y
|
2022-02-02 23:01:07 +00:00
|
|
|
|
2022-02-06 22:41:32 +00:00
|
|
|
player_rotate_y += event.relative.x *0.01
|
|
|
|
rotate_y( -event.relative.x *0.01 )
|
2022-02-06 00:49:25 +00:00
|
|
|
var new_camera_rotate_x = player_rotate_x + event.relative.y * 0.01
|
2022-02-05 18:58:58 +00:00
|
|
|
if new_camera_rotate_x <= PI_2 and new_camera_rotate_x >= - PI_2:
|
2022-02-06 00:49:25 +00:00
|
|
|
camera_fps.rotate_x( event.relative.y * 0.01 )
|
|
|
|
player_rotate_x = new_camera_rotate_x
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
|
|
|
|
func _physics_process(delta):
|
2022-02-05 18:58:58 +00:00
|
|
|
# Add the gravity.
|
2022-02-09 22:36:58 +00:00
|
|
|
if stop_move:
|
|
|
|
return
|
2022-02-05 18:58:58 +00:00
|
|
|
match state_player:
|
2022-02-06 13:38:17 +00:00
|
|
|
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)
|
2022-02-05 18:58:58 +00:00
|
|
|
StatePlayer.WALK:
|
|
|
|
_physics_process_walk(delta)
|
|
|
|
StatePlayer.FLY:
|
|
|
|
_physics_process_fly(delta)
|
|
|
|
StatePlayer.SWIM:
|
2022-02-06 00:49:25 +00:00
|
|
|
_physics_process_swim(delta)
|
2022-02-05 18:58:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process_walk(delta):
|
2022-02-03 12:52:23 +00:00
|
|
|
var input_dir: Vector2
|
2022-02-03 21:05:04 +00:00
|
|
|
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
|
2022-02-09 22:36:58 +00:00
|
|
|
var mulstep: float = 1.0
|
2022-02-03 12:52:23 +00:00
|
|
|
|
2022-02-06 00:49:25 +00:00
|
|
|
#print(get_position())
|
2022-02-02 23:01:07 +00:00
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
motion_velocity.y -= gravity * delta
|
|
|
|
|
|
|
|
# Handle Jump.
|
2022-02-03 21:19:58 +00:00
|
|
|
# if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
|
|
# motion_velocity.y = JUMP_FORCE
|
2022-02-02 23:01:07 +00:00
|
|
|
|
2022-02-03 12:52:23 +00:00
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
if Input.is_action_pressed("ui_strafe"):
|
2022-02-03 21:05:04 +00:00
|
|
|
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
|
2022-02-03 13:16:48 +00:00
|
|
|
else:
|
2022-02-03 11:55:05 +00:00
|
|
|
var y = 0
|
2022-02-03 19:46:03 +00:00
|
|
|
if Input.is_action_pressed("ui_right"):
|
|
|
|
y -= 1
|
|
|
|
if Input.is_action_pressed("ui_left"):
|
|
|
|
y += 1
|
|
|
|
if y != 0:
|
2022-02-03 21:05:04 +00:00
|
|
|
var dt = y * delta * SPEED_ROTATE
|
2022-02-03 22:49:49 +00:00
|
|
|
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 )
|
2022-02-03 21:05:04 +00:00
|
|
|
# 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
|
2022-02-05 01:06:50 +00:00
|
|
|
player_automove = false
|
2022-02-03 21:05:04 +00:00
|
|
|
elif Input.is_action_pressed("ui_down"):
|
2022-02-05 01:06:50 +00:00
|
|
|
player_automove = false
|
2022-02-03 21:05:04 +00:00
|
|
|
input_y = -1.0
|
|
|
|
move_down = true
|
2022-02-05 01:06:50 +00:00
|
|
|
elif player_automove:
|
|
|
|
input_y = 1.0
|
|
|
|
move_up = true
|
2022-02-03 21:05:04 +00:00
|
|
|
|
|
|
|
if is_run:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_RUN_UP_STRAFE
|
2022-02-03 23:32:39 +00:00
|
|
|
switch_animation(anim_run)
|
2022-02-03 21:05:04 +00:00
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_RUN_DOWN_STRAFE
|
2022-02-03 23:32:39 +00:00
|
|
|
switch_animation(anim_run_backward)
|
2022-02-03 21:05:04 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_STRAFE
|
2022-02-03 23:32:39 +00:00
|
|
|
if input_x > 0.0:
|
|
|
|
switch_animation(anim_strafe_left_walk)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_strafe_right_walk)
|
2022-02-03 21:05:04 +00:00
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_RUN_UP
|
2022-02-03 23:32:39 +00:00
|
|
|
switch_animation(anim_run)
|
2022-02-03 21:05:04 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_DOWN
|
2022-02-03 23:32:39 +00:00
|
|
|
if move_down:
|
|
|
|
switch_animation(anim_run_backward)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_idle)
|
2022-02-03 21:05:04 +00:00
|
|
|
else:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_WALK_UP_STRAFE
|
2022-02-03 23:32:39 +00:00
|
|
|
switch_animation(anim_walk)
|
2022-02-03 21:05:04 +00:00
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_WALK_DOWN_STRAFE
|
2022-02-03 23:32:39 +00:00
|
|
|
switch_animation(anim_walk_backward)
|
2022-02-03 21:05:04 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_STRAFE
|
2022-02-03 23:32:39 +00:00
|
|
|
if input_x > 0.0:
|
|
|
|
switch_animation(anim_strafe_left_walk)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_strafe_right_walk)
|
2022-02-03 21:05:04 +00:00
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_WALK_UP
|
2022-02-03 23:32:39 +00:00
|
|
|
switch_animation(anim_walk)
|
2022-02-03 21:05:04 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_DOWN
|
2022-02-03 23:32:39 +00:00
|
|
|
if move_down:
|
|
|
|
switch_animation(anim_walk_backward)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_idle)
|
|
|
|
if input_x == 0.0 and input_y == 0.0:
|
2022-02-05 00:59:43 +00:00
|
|
|
if player_sit:
|
|
|
|
switch_animation(anim_sitting_ground_idle)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_idle)
|
2022-02-03 21:05:04 +00:00
|
|
|
|
|
|
|
var direction = (transform.basis * Vector3(input_x, 0, input_y)).normalized()
|
2022-02-03 11:55:05 +00:00
|
|
|
if direction:
|
2022-02-09 22:36:58 +00:00
|
|
|
motion_velocity.x = direction.x * speed * mulstep
|
|
|
|
motion_velocity.z = direction.z * speed * mulstep
|
2022-02-03 11:55:05 +00:00
|
|
|
else:
|
2022-02-09 22:36:58 +00:00
|
|
|
motion_velocity.x = move_toward(motion_velocity.x, 0, speed * mulstep)
|
|
|
|
motion_velocity.z = move_toward(motion_velocity.z, 0, speed * mulstep)
|
2022-02-02 23:01:07 +00:00
|
|
|
|
2022-02-12 01:00:16 +00:00
|
|
|
var save_motion_velocity:Vector3 = motion_velocity
|
|
|
|
|
2022-02-09 22:36:58 +00:00
|
|
|
var collided = move_and_slide()
|
2022-02-12 01:00:16 +00:00
|
|
|
|
|
|
|
if collided and is_on_floor() and (input_x != 0.0 or input_y != 0.0) and $RayCastGround.is_colliding() and !$RayCastStep.is_colliding():
|
|
|
|
var delta_1:Vector3 = get_position_delta()
|
|
|
|
var areawalk = delta_1.x * delta_1.x + delta_1.z * delta_1.z
|
|
|
|
if (areawalk * 10.0 < max_collided_speed) or (is_run and (areawalk * 10.0 < max_collided_speed * DIFF_RUN_WALK )):
|
|
|
|
var jump_step:Vector3 = height_step
|
|
|
|
translate(jump_step)
|
|
|
|
motion_velocity = save_motion_velocity - delta_1 # (save_motion_velocity - delta_1) * MUL_SPEED_STEP
|
|
|
|
move_and_slide()
|
|
|
|
motion_velocity.x = 0.0
|
|
|
|
motion_velocity.y = -STEP_FORCE
|
|
|
|
motion_velocity.z = 0.0
|
|
|
|
collided = move_and_slide()
|
|
|
|
else:
|
|
|
|
var delta_1:Vector3 = get_position_delta()
|
|
|
|
var areawalk = (delta_1.x * delta_1.x + delta_1.z * delta_1.z) / speed
|
|
|
|
if !is_run and max_collided_speed < areawalk:
|
|
|
|
max_collided_speed = areawalk
|
2022-02-05 18:58:58 +00:00
|
|
|
|
|
|
|
|
2022-02-06 13:38:17 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2022-02-05 18:58:58 +00:00
|
|
|
func _physics_process_fly(delta):
|
|
|
|
var input_dir: Vector2
|
|
|
|
var input_x: float
|
|
|
|
var input_y: float
|
|
|
|
var input_z: float
|
|
|
|
var speed: float
|
|
|
|
var move_up:bool = false
|
|
|
|
var move_down:bool = false
|
|
|
|
var move_strafe: bool = false
|
|
|
|
|
|
|
|
# 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
|
|
|
|
input_z = 1.0
|
|
|
|
move_up = true
|
|
|
|
player_automove = false
|
|
|
|
elif Input.is_action_pressed("ui_down"):
|
|
|
|
player_automove = false
|
|
|
|
input_y = -1.0
|
|
|
|
input_z = -1.0
|
|
|
|
move_down = true
|
|
|
|
elif player_automove:
|
|
|
|
input_y = 1.0
|
|
|
|
input_z = 1.0
|
|
|
|
move_up = true
|
|
|
|
|
2022-02-06 00:49:25 +00:00
|
|
|
if tps:
|
|
|
|
input_z *= (-camera_rotate_x)
|
|
|
|
else:
|
|
|
|
input_z *= (-player_rotate_x)
|
2022-02-05 18:58:58 +00:00
|
|
|
|
|
|
|
if is_run:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_RUN_UP_STRAFE
|
|
|
|
switch_animation(anim_fly_run)
|
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_RUN_DOWN_STRAFE
|
|
|
|
switch_animation(anim_fly_run_backward)
|
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_STRAFE
|
|
|
|
if input_x > 0.0:
|
|
|
|
switch_animation(anim_fly_strafe_left_walk)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_fly_strafe_right_walk)
|
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_RUN_UP
|
|
|
|
switch_animation(anim_fly_run)
|
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_DOWN
|
|
|
|
if move_down:
|
|
|
|
switch_animation(anim_fly_run_backward)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_fly_idle)
|
|
|
|
else:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_WALK_UP_STRAFE
|
|
|
|
switch_animation(anim_fly_walk)
|
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_WALK_DOWN_STRAFE
|
|
|
|
switch_animation(anim_fly_walk_backward)
|
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_STRAFE
|
|
|
|
if input_x > 0.0:
|
|
|
|
switch_animation(anim_fly_strafe_left_walk)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_fly_strafe_right_walk)
|
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_WALK_UP
|
|
|
|
switch_animation(anim_fly_walk)
|
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_DOWN
|
|
|
|
if move_down:
|
|
|
|
switch_animation(anim_fly_walk_backward)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_fly_idle)
|
|
|
|
if input_x == 0.0 and input_y == 0.0:
|
|
|
|
if player_sit:
|
|
|
|
switch_animation(anim_fly_sitting_ground_idle)
|
|
|
|
else:
|
|
|
|
switch_animation(anim_fly_idle)
|
|
|
|
|
|
|
|
var direction = (transform.basis * Vector3(input_x, input_z, input_y)).normalized()
|
|
|
|
if direction:
|
2022-02-06 00:49:25 +00:00
|
|
|
motion_velocity.x = direction.x * speed * MUL_SPEED_FLY
|
|
|
|
motion_velocity.y = direction.y * speed * MUL_SPEED_FLY
|
|
|
|
motion_velocity.z = direction.z * speed * MUL_SPEED_FLY
|
2022-02-05 18:58:58 +00:00
|
|
|
else:
|
2022-02-06 00:49:25 +00:00
|
|
|
motion_velocity.x = move_toward(motion_velocity.x, 0, speed * MUL_SPEED_FLY)
|
|
|
|
motion_velocity.y = move_toward(motion_velocity.y, 0, speed * MUL_SPEED_FLY)
|
|
|
|
motion_velocity.z = move_toward(motion_velocity.z, 0, speed * MUL_SPEED_FLY)
|
2022-02-05 18:58:58 +00:00
|
|
|
|
2022-02-06 00:49:25 +00:00
|
|
|
#print('motion_velocity:', motion_velocity)
|
2022-02-02 23:01:07 +00:00
|
|
|
move_and_slide()
|
|
|
|
|
2022-02-03 13:16:48 +00:00
|
|
|
|
2022-02-06 00:49:25 +00:00
|
|
|
func _physics_process_swim(delta):
|
|
|
|
var input_dir: Vector2
|
|
|
|
var input_x: float
|
|
|
|
var input_y: float
|
|
|
|
var input_z: float
|
|
|
|
var speed: float
|
|
|
|
var move_up:bool = false
|
|
|
|
var move_down:bool = false
|
|
|
|
var move_strafe: bool = false
|
|
|
|
|
|
|
|
# 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
|
|
|
|
input_z = 1.0
|
|
|
|
move_up = true
|
|
|
|
player_automove = false
|
|
|
|
elif Input.is_action_pressed("ui_down"):
|
|
|
|
player_automove = false
|
|
|
|
input_y = -1.0
|
|
|
|
input_z = -1.0
|
|
|
|
move_down = true
|
|
|
|
elif player_automove:
|
|
|
|
input_y = 1.0
|
|
|
|
input_z = 1.0
|
|
|
|
move_up = true
|
|
|
|
|
|
|
|
if tps:
|
|
|
|
input_z *= (-camera_rotate_x)
|
|
|
|
else:
|
|
|
|
input_z *= (-player_rotate_x)
|
|
|
|
|
|
|
|
if is_run:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_RUN_UP_STRAFE
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_run)
|
2022-02-06 00:49:25 +00:00
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_RUN_DOWN_STRAFE
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_run_backward)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_STRAFE
|
|
|
|
if input_x > 0.0:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_strafe_left_walk)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_strafe_right_walk)
|
2022-02-06 00:49:25 +00:00
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_RUN_UP
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_run)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_DOWN
|
|
|
|
if move_down:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_run_backward)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_idle)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_WALK_UP_STRAFE
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_walk)
|
2022-02-06 00:49:25 +00:00
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_WALK_DOWN_STRAFE
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_walk_backward)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_STRAFE
|
|
|
|
if input_x > 0.0:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_strafe_left_walk)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_strafe_right_walk)
|
2022-02-06 00:49:25 +00:00
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_WALK_UP
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_walk)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_DOWN
|
|
|
|
if move_down:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_walk_backward)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_idle)
|
2022-02-06 00:49:25 +00:00
|
|
|
if input_x == 0.0 and input_y == 0.0:
|
|
|
|
if player_sit:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_sitting_ground_idle)
|
2022-02-06 00:49:25 +00:00
|
|
|
else:
|
2022-02-06 00:55:11 +00:00
|
|
|
switch_animation(anim_swim_idle)
|
2022-02-06 00:49:25 +00:00
|
|
|
|
|
|
|
var direction = (transform.basis * Vector3(input_x, input_z, input_y)).normalized()
|
|
|
|
if direction:
|
|
|
|
motion_velocity.x = direction.x * speed * MUL_SPEED_SWIM
|
|
|
|
motion_velocity.y = direction.y * speed * MUL_SPEED_SWIM
|
|
|
|
motion_velocity.z = direction.z * speed * MUL_SPEED_SWIM
|
|
|
|
else:
|
|
|
|
motion_velocity.x = move_toward(motion_velocity.x, 0, speed * MUL_SPEED_SWIM)
|
|
|
|
motion_velocity.y = move_toward(motion_velocity.y, 0, speed * MUL_SPEED_SWIM)
|
|
|
|
motion_velocity.z = move_toward(motion_velocity.z, 0, speed * MUL_SPEED_SWIM)
|
|
|
|
var col = move_and_slide()
|
|
|
|
if col == false and level_water <= get_position().y:
|
|
|
|
# force always under water (if not collision)
|
|
|
|
var tmp = get_position()
|
|
|
|
tmp.y = level_water
|
|
|
|
set_position(tmp)
|
2022-02-06 13:38:17 +00:00
|
|
|
elif col == true and get_position().y >= level_water - heigh_underwater:
|
|
|
|
switch_state(StatePlayer.WALK_WATER)
|
2022-02-06 00:49:25 +00:00
|
|
|
|
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
func _process( delta ):
|
2022-02-05 18:58:58 +00:00
|
|
|
match state_player:
|
2022-02-06 13:38:17 +00:00
|
|
|
StatePlayer.WALK_WATER:
|
|
|
|
_process_walk(delta)
|
2022-02-05 18:58:58 +00:00
|
|
|
StatePlayer.WALK:
|
|
|
|
_process_walk(delta)
|
|
|
|
StatePlayer.FLY:
|
|
|
|
_process_walk(delta)
|
|
|
|
StatePlayer.SWIM:
|
|
|
|
_process_walk(delta)
|
2022-02-03 13:02:38 +00:00
|
|
|
|
2022-02-05 18:58:58 +00:00
|
|
|
|
|
|
|
func _process_walk( delta ):
|
2022-02-03 21:19:58 +00:00
|
|
|
if reconciliate_rotate_camera_player:
|
2022-02-03 13:02:38 +00:00
|
|
|
var diff = camera_rotate_y - player_rotate_y
|
|
|
|
|
|
|
|
if diff > PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
diff = camera_rotate_y - player_rotate_y - TAU
|
2022-02-03 13:02:38 +00:00
|
|
|
elif diff < -PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
diff = camera_rotate_y - player_rotate_y + TAU
|
2022-02-03 13:02:38 +00:00
|
|
|
|
|
|
|
var absdiff = diff
|
|
|
|
if absdiff < 0.0:
|
|
|
|
absdiff = -absdiff
|
|
|
|
|
|
|
|
if absdiff <= 0.5 * delta:
|
|
|
|
rotate_y( diff )
|
|
|
|
$camera_root/horizontal_root.rotate_y( -diff )
|
|
|
|
player_rotate_y = camera_rotate_y
|
2022-02-02 23:01:07 +00:00
|
|
|
else:
|
2022-02-03 13:02:38 +00:00
|
|
|
if diff >= 0.0:
|
2022-02-03 21:05:04 +00:00
|
|
|
diff = delta * SPEED_ROTATE
|
2022-02-03 13:02:38 +00:00
|
|
|
else:
|
2022-02-03 21:05:04 +00:00
|
|
|
diff = -delta * SPEED_ROTATE
|
2022-02-03 13:02:38 +00:00
|
|
|
rotate_y( diff )
|
|
|
|
$camera_root/horizontal_root.rotate_y( -diff )
|
|
|
|
player_rotate_y += diff
|
|
|
|
if player_rotate_y > PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
player_rotate_y -= TAU
|
2022-02-03 13:02:38 +00:00
|
|
|
elif player_rotate_y <= -PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
player_rotate_y += TAU
|
2022-02-05 18:58:58 +00:00
|
|
|
|
|
|
|
|
2022-02-06 00:49:25 +00:00
|
|
|
func enter_underwater():
|
|
|
|
# function called by Area3D (Water Object)
|
|
|
|
level_water = get_position().y
|
2022-02-06 13:38:17 +00:00
|
|
|
switch_state(StatePlayer.WALK_WATER)
|
2022-02-06 00:49:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
func exit_underwater():
|
|
|
|
# function called by Area3D (Water Object)
|
|
|
|
switch_state(StatePlayer.WALK)
|