2022-02-02 23:01:07 +00:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
2022-02-03 21:05:04 +00:00
|
|
|
# Up
|
|
|
|
# |
|
|
|
|
# Strafe Left --- + --- Strafe Rigth
|
|
|
|
# |
|
|
|
|
# Down
|
|
|
|
|
|
|
|
#const SPEED = 5.0
|
2022-02-02 23:01:07 +00:00
|
|
|
const JUMP_FORCE = 4.5
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
var camera_rotate_y = 0.0
|
|
|
|
var camera_rotate_x = 0.0
|
|
|
|
var player_rotate_y = 0.0
|
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
|
|
|
|
|
|
|
|
const SPEED_RUN_UP = SPEED_WALK_UP * 2.0
|
|
|
|
const SPEED_RUN_UP_STRAFE = SPEED_WALK_UP_STRAFE * 2.0
|
|
|
|
const SPEED_RUN_STRAFE = SPEED_WALK_STRAFE * 2.0
|
|
|
|
const SPEED_RUN_DOWN_STRAFE = SPEED_WALK_DOWN_STRAFE * 2.0
|
|
|
|
const SPEED_RUN_DOWN = SPEED_WALK_DOWN * 2.0
|
|
|
|
|
|
|
|
#const SPEED_WALK = 5.0
|
|
|
|
#const SPEED_RUN = 10.0
|
|
|
|
#const MUL_SPEED_STRAFE = 0.5
|
|
|
|
#const MUL_SPEED_DOWN = 0.2
|
|
|
|
|
|
|
|
const SPEED_ROTATE = PI
|
2022-02-02 23:01:07 +00:00
|
|
|
const PI_2 = PI / 2.0
|
2022-02-03 12:44:16 +00:00
|
|
|
var debug:bool = false
|
2022-02-03 13:02:38 +00:00
|
|
|
var reconciliate_rotate_camer_player:bool = true
|
2022-02-03 21:05:04 +00:00
|
|
|
var is_run:bool = false
|
|
|
|
# var speed = SPEED_WALK
|
2022-02-02 23:01:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _init():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
# Place the mouse at the center of the screen
|
|
|
|
get_viewport().warp_mouse(starting_point)
|
|
|
|
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
# If right mouse button is pressed and mouse moves, pan horizontally camera
|
|
|
|
# and rotate vertically
|
2022-02-03 11:55:05 +00:00
|
|
|
if Input.is_action_just_pressed ( "ui_strafe" ):
|
2022-02-03 13:02:38 +00:00
|
|
|
reconciliate_rotate_camer_player = false
|
2022-02-03 11:55:05 +00:00
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
|
|
|
|
elif Input.is_action_just_released ( "ui_strafe" ):
|
2022-02-03 13:02:38 +00:00
|
|
|
reconciliate_rotate_camer_player = true
|
2022-02-03 11:55:05 +00:00
|
|
|
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 12:44:16 +00:00
|
|
|
if Input.is_action_just_released("ui_cut"):
|
|
|
|
print("debug on")
|
|
|
|
debug = true
|
|
|
|
if Input.is_action_just_released("ui_copy"):
|
|
|
|
print("debug off")
|
|
|
|
debug = 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 11:55:05 +00:00
|
|
|
camera_rotate_y -= event.relative.x *0.01
|
2022-02-03 12:52:23 +00:00
|
|
|
if camera_rotate_y > PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
camera_rotate_y -= TAU
|
2022-02-02 23:01:07 +00:00
|
|
|
elif camera_rotate_y <= -PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
camera_rotate_y += TAU
|
2022-02-03 11:55:05 +00:00
|
|
|
$camera_root/horizontal_root.rotate_y( -event.relative.x *0.01 )
|
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(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-03 12:52:23 +00:00
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
# 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
|
2022-02-03 21:05:04 +00:00
|
|
|
#
|
|
|
|
# if is_run:
|
|
|
|
# speed = SPEED_RUN
|
|
|
|
# else:
|
|
|
|
# speed = SPEED_WALK
|
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
|
|
|
|
# speed *= MUL_SPEED_STRAFE
|
|
|
|
move_strafe = true
|
|
|
|
elif Input.is_action_pressed("ui_right"):
|
|
|
|
input_x = -1.0
|
|
|
|
# speed *= MUL_SPEED_STRAFE
|
|
|
|
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 19:46:03 +00:00
|
|
|
camera_rotate_y += dt
|
|
|
|
if camera_rotate_y > PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
camera_rotate_y -= TAU
|
2022-02-03 19:46:03 +00:00
|
|
|
elif camera_rotate_y <= -PI:
|
2022-02-03 21:05:04 +00:00
|
|
|
camera_rotate_y += TAU
|
2022-02-03 19:46:03 +00:00
|
|
|
$camera_root/horizontal_root.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
|
|
|
|
elif Input.is_action_pressed("ui_down"):
|
|
|
|
input_y = -1.0
|
|
|
|
move_down = true
|
|
|
|
# speed *= MUL_SPEED_DOWN
|
|
|
|
|
|
|
|
if is_run:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_RUN_UP_STRAFE
|
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_RUN_DOWN_STRAFE
|
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_STRAFE
|
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_RUN_UP
|
|
|
|
else:
|
|
|
|
speed = SPEED_RUN_DOWN
|
|
|
|
else:
|
|
|
|
if move_strafe:
|
|
|
|
if move_up:
|
|
|
|
speed = SPEED_WALK_UP_STRAFE
|
|
|
|
elif move_down:
|
|
|
|
speed = SPEED_WALK_DOWN_STRAFE
|
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_STRAFE
|
|
|
|
elif move_up:
|
|
|
|
speed = SPEED_WALK_UP
|
|
|
|
else:
|
|
|
|
speed = SPEED_WALK_DOWN
|
|
|
|
|
|
|
|
print(speed)
|
2022-02-03 12:52:23 +00:00
|
|
|
|
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-03 21:05:04 +00:00
|
|
|
motion_velocity.x = direction.x * speed
|
|
|
|
motion_velocity.z = direction.z * speed
|
2022-02-03 11:55:05 +00:00
|
|
|
else:
|
2022-02-03 21:05:04 +00:00
|
|
|
motion_velocity.x = move_toward(motion_velocity.x, 0, speed)
|
|
|
|
motion_velocity.z = move_toward(motion_velocity.z, 0, speed)
|
2022-02-02 23:01:07 +00:00
|
|
|
|
|
|
|
move_and_slide()
|
|
|
|
|
2022-02-03 13:16:48 +00:00
|
|
|
|
2022-02-02 23:01:07 +00:00
|
|
|
func _process( delta ):
|
2022-02-03 12:44:16 +00:00
|
|
|
if debug:
|
2022-02-03 13:02:38 +00:00
|
|
|
print(reconciliate_rotate_camer_player, ", ", camera_rotate_y,", ", player_rotate_y)
|
|
|
|
|
|
|
|
if reconciliate_rotate_camer_player:
|
|
|
|
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
|