adding zoom and FPS

This commit is contained in:
AleaJactaEst 2022-02-03 23:49:49 +01:00
parent 318fec80ee
commit 6a7810445b
2 changed files with 84 additions and 29 deletions

View file

@ -1,11 +1,5 @@
extends CharacterBody3D extends CharacterBody3D
# Up
# |
# Strafe Left --- + --- Strafe Rigth
# |
# Down
# Constant # Constant
const SPEED_WALK_UP = 5.0 const SPEED_WALK_UP = 5.0
const SPEED_WALK_UP_STRAFE = 3.0 const SPEED_WALK_UP_STRAFE = 3.0
@ -19,6 +13,12 @@ const SPEED_RUN_STRAFE = SPEED_WALK_STRAFE * 2.0
const SPEED_RUN_DOWN_STRAFE = SPEED_WALK_DOWN_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_RUN_DOWN = SPEED_WALK_DOWN * 2.0
const ZOOM_Y = 0.05
const ZOOM_Z = 0.1
const ZOOM_STEP = 0.1
const ZOOM_MIN = 0.2
const ZOOM_MAX = 2.0
const SPEED_ROTATE = PI const SPEED_ROTATE = PI
const PI_2 = PI / 2.0 const PI_2 = PI / 2.0
const JUMP_FORCE = 4.5 const JUMP_FORCE = 4.5
@ -34,7 +34,10 @@ var player_rotate_y = 0.0
#var debug:bool = false #var debug:bool = false
# Activate reconciliation between camera & player # Activate reconciliation between camera & player
var reconciliate_rotate_camera_player:bool = true var reconciliate_rotate_camera_player:bool = true
# Player run
var is_run:bool = false var is_run:bool = false
var zoom:float = 1.0
var tps:bool = true
func _init(): func _init():
@ -47,14 +50,44 @@ func _ready():
func _input(event): func _input(event):
# Managed zoom (FPS or TPS)
if Input.is_action_pressed("camera_zoom_in"):
if tps:
if zoom > ZOOM_MIN:
zoom -= ZOOM_STEP
var zoom3D:Vector3 = Vector3(0.0, -ZOOM_Y, -ZOOM_Z)
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.translate_object_local(zoom3D)
if zoom < ZOOM_MIN:
$camera_root/Camera3D_FPS.make_current()
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
# $camera_root/horizontal_root.rotate_y( player_rotate_y - camera_rotate_y )
# camera_rotate_y = player_rotate_y
reconciliate_rotate_camera_player = true
tps = true
zoom += ZOOM_STEP
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.make_current()
if zoom >= ZOOM_MAX:
zoom = ZOOM_MAX
else:
var zoom3D:Vector3 = Vector3(0.0, ZOOM_Y, ZOOM_Z)
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.translate_object_local(zoom3D)
# If right mouse button is pressed and mouse moves, pan horizontally camera # If right mouse button is pressed and mouse moves, pan horizontally camera
# and rotate vertically # and rotate vertically
if Input.is_action_just_pressed ( "ui_strafe" ): if tps:
reconciliate_rotate_camera_player = false if Input.is_action_just_pressed ( "ui_strafe" ):
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) reconciliate_rotate_camera_player = false
elif Input.is_action_just_released ( "ui_strafe" ): Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
reconciliate_rotate_camera_player = true elif Input.is_action_just_released ( "ui_strafe" ):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) reconciliate_rotate_camera_player = true
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.is_action_just_pressed( "move_run" ): if Input.is_action_just_pressed( "move_run" ):
is_run = true is_run = true
elif Input.is_action_just_released( "move_run" ): elif Input.is_action_just_released( "move_run" ):
@ -68,17 +101,27 @@ func _input(event):
if Input.is_mouse_button_pressed( 2 ): if Input.is_mouse_button_pressed( 2 ):
if event is InputEventMouseMotion: if event is InputEventMouseMotion:
camera_rotate_y -= event.relative.x *0.01 if tps:
if camera_rotate_y > PI: camera_rotate_y -= event.relative.x *0.01
camera_rotate_y -= TAU if camera_rotate_y > PI:
elif camera_rotate_y <= -PI: camera_rotate_y -= TAU
camera_rotate_y += TAU elif camera_rotate_y <= -PI:
$camera_root/horizontal_root.rotate_y( -event.relative.x *0.01 ) 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 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: 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_root/horizontal_root/vertical_root.rotate_x( event.relative.y * 0.01 )
camera_rotate_x = new_camera_rotate_x camera_rotate_x = new_camera_rotate_x
else:
player_rotate_y += event.relative.x *0.01
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( -event.relative.x *0.01 )
#$camera_root/horizontal_root.rotate_y( event.relative.x *0.01 )
func _physics_process(delta): func _physics_process(delta):
@ -114,12 +157,21 @@ func _physics_process(delta):
y += 1 y += 1
if y != 0: if y != 0:
var dt = y * delta * SPEED_ROTATE var dt = y * delta * SPEED_ROTATE
camera_rotate_y += dt if tps:
if camera_rotate_y > PI: camera_rotate_y += dt
camera_rotate_y -= TAU if camera_rotate_y > PI:
elif camera_rotate_y <= -PI: camera_rotate_y -= TAU
camera_rotate_y += TAU elif camera_rotate_y <= -PI:
$camera_root/horizontal_root.rotate_y( dt ) 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) # Disable vector on ui_right/ui_left (used to rotate and not strafe)
input_x = 0 input_x = 0
if Input.is_action_pressed("ui_up"): if Input.is_action_pressed("ui_up"):

View file

@ -27,5 +27,8 @@ script = ExtResource( "2_xvdvx" )
[node name="vertical_root" type="Node3D" parent="camera_root/horizontal_root"] [node name="vertical_root" type="Node3D" parent="camera_root/horizontal_root"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.55554, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.55554, 0)
[node name="Camera3D" type="Camera3D" parent="camera_root/horizontal_root/vertical_root"] [node name="Camera3D_TPS" type="Camera3D" parent="camera_root/horizontal_root/vertical_root"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -2.14563) transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -2.14563)
[node name="Camera3D_FPS" type="Camera3D" parent="camera_root"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 1.49909, 0.0923875)