adding option run "shift"

This commit is contained in:
AleaJactaEst 2022-02-03 22:05:04 +01:00
parent 4608af0f80
commit e398c8d8e1

View file

@ -1,6 +1,12 @@
extends CharacterBody3D extends CharacterBody3D
const SPEED = 5.0 # Up
# |
# Strafe Left --- + --- Strafe Rigth
# |
# Down
#const SPEED = 5.0
const JUMP_FORCE = 4.5 const JUMP_FORCE = 4.5
# Get the gravity from the project settings to be synced with RigidDynamicBody nodes. # Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
@ -9,12 +15,30 @@ var starting_point = Vector2(DisplayServer.window_get_size().x / 2, DisplayServe
var camera_rotate_y = 0.0 var camera_rotate_y = 0.0
var camera_rotate_x = 0.0 var camera_rotate_x = 0.0
var player_rotate_y = 0.0 var player_rotate_y = 0.0
var speed_rotate_1sec = PI
var max_angle = PI / 2 const SPEED_WALK_UP = 5.0
const TWO_PI = 2.0 * PI 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
const PI_2 = PI / 2.0 const PI_2 = PI / 2.0
var debug:bool = false var debug:bool = false
var reconciliate_rotate_camer_player:bool = true var reconciliate_rotate_camer_player:bool = true
var is_run:bool = false
# var speed = SPEED_WALK
func _init(): func _init():
@ -35,6 +59,10 @@ func _input(event):
elif Input.is_action_just_released ( "ui_strafe" ): elif Input.is_action_just_released ( "ui_strafe" ):
reconciliate_rotate_camer_player = true reconciliate_rotate_camer_player = true
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.is_action_just_pressed( "move_run" ):
is_run = true
elif Input.is_action_just_released( "move_run" ):
is_run = false
if Input.is_action_just_released("ui_cut"): if Input.is_action_just_released("ui_cut"):
print("debug on") print("debug on")
debug = true debug = true
@ -46,9 +74,9 @@ func _input(event):
if event is InputEventMouseMotion: if event is InputEventMouseMotion:
camera_rotate_y -= event.relative.x *0.01 camera_rotate_y -= event.relative.x *0.01
if camera_rotate_y > PI: if camera_rotate_y > PI:
camera_rotate_y -= TWO_PI camera_rotate_y -= TAU
elif camera_rotate_y <= -PI: elif camera_rotate_y <= -PI:
camera_rotate_y += TWO_PI camera_rotate_y += TAU
$camera_root/horizontal_root.rotate_y( -event.relative.x *0.01 ) $camera_root/horizontal_root.rotate_y( -event.relative.x *0.01 )
var new_camera_rotate_x = camera_rotate_x + event.relative.y * 0.01 var new_camera_rotate_x = camera_rotate_x + event.relative.y * 0.01
@ -59,6 +87,12 @@ func _input(event):
func _physics_process(delta): func _physics_process(delta):
var input_dir: Vector2 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
# Add the gravity. # Add the gravity.
if not is_on_floor(): if not is_on_floor():
@ -67,10 +101,22 @@ func _physics_process(delta):
# Handle Jump. # Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): if Input.is_action_just_pressed("ui_accept") and is_on_floor():
motion_velocity.y = JUMP_FORCE motion_velocity.y = JUMP_FORCE
#
# if is_run:
# speed = SPEED_RUN
# else:
# speed = SPEED_WALK
# Get the input direction and handle the movement/deceleration. # Get the input direction and handle the movement/deceleration.
if Input.is_action_pressed("ui_strafe"): if Input.is_action_pressed("ui_strafe"):
input_dir = Input.get_vector("ui_right", "ui_left", "ui_down", "ui_up") 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
else: else:
var y = 0 var y = 0
if Input.is_action_pressed("ui_right"): if Input.is_action_pressed("ui_right"):
@ -78,22 +124,57 @@ func _physics_process(delta):
if Input.is_action_pressed("ui_left"): if Input.is_action_pressed("ui_left"):
y += 1 y += 1
if y != 0: if y != 0:
var dt = y * delta * speed_rotate_1sec var dt = y * delta * SPEED_ROTATE
camera_rotate_y += dt camera_rotate_y += dt
if camera_rotate_y > PI: if camera_rotate_y > PI:
camera_rotate_y -= TWO_PI camera_rotate_y -= TAU
elif camera_rotate_y <= -PI: elif camera_rotate_y <= -PI:
camera_rotate_y += TWO_PI camera_rotate_y += TAU
$camera_root/horizontal_root.rotate_y( dt ) $camera_root/horizontal_root.rotate_y( dt )
input_dir = Input.get_vector("nothing", "nothing", "ui_down", "ui_up") # 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
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() if is_run:
if direction: if move_strafe:
motion_velocity.x = direction.x * SPEED if move_up:
motion_velocity.z = direction.z * SPEED 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: else:
motion_velocity.x = move_toward(motion_velocity.x, 0, SPEED) if move_strafe:
motion_velocity.z = move_toward(motion_velocity.z, 0, SPEED) 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)
var direction = (transform.basis * Vector3(input_x, 0, input_y)).normalized()
if direction:
motion_velocity.x = direction.x * speed
motion_velocity.z = direction.z * speed
else:
motion_velocity.x = move_toward(motion_velocity.x, 0, speed)
motion_velocity.z = move_toward(motion_velocity.z, 0, speed)
move_and_slide() move_and_slide()
@ -106,9 +187,9 @@ func _process( delta ):
var diff = camera_rotate_y - player_rotate_y var diff = camera_rotate_y - player_rotate_y
if diff > PI: if diff > PI:
diff = camera_rotate_y - player_rotate_y - TWO_PI diff = camera_rotate_y - player_rotate_y - TAU
elif diff < -PI: elif diff < -PI:
diff = camera_rotate_y - player_rotate_y + TWO_PI diff = camera_rotate_y - player_rotate_y + TAU
var absdiff = diff var absdiff = diff
if absdiff < 0.0: if absdiff < 0.0:
@ -120,13 +201,13 @@ func _process( delta ):
player_rotate_y = camera_rotate_y player_rotate_y = camera_rotate_y
else: else:
if diff >= 0.0: if diff >= 0.0:
diff = delta * speed_rotate_1sec diff = delta * SPEED_ROTATE
else: else:
diff = -delta * speed_rotate_1sec diff = -delta * SPEED_ROTATE
rotate_y( diff ) rotate_y( diff )
$camera_root/horizontal_root.rotate_y( -diff ) $camera_root/horizontal_root.rotate_y( -diff )
player_rotate_y += diff player_rotate_y += diff
if player_rotate_y > PI: if player_rotate_y > PI:
player_rotate_y -= TWO_PI player_rotate_y -= TAU
elif player_rotate_y <= -PI: elif player_rotate_y <= -PI:
player_rotate_y += TWO_PI player_rotate_y += TAU