From 6a7810445ba4169f25f3ec3063d3b3236605420d Mon Sep 17 00:00:00 2001 From: AleaJactaEst Date: Thu, 3 Feb 2022 23:49:49 +0100 Subject: [PATCH] adding zoom and FPS --- player/player.gd | 108 +++++++++++++++++++++++++++++++++------------ player/player.tscn | 5 ++- 2 files changed, 84 insertions(+), 29 deletions(-) diff --git a/player/player.gd b/player/player.gd index dd6e225..41738bb 100644 --- a/player/player.gd +++ b/player/player.gd @@ -1,11 +1,5 @@ extends CharacterBody3D -# Up -# | -# Strafe Left --- + --- Strafe Rigth -# | -# Down - # Constant const SPEED_WALK_UP = 5.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 = 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 PI_2 = PI / 2.0 const JUMP_FORCE = 4.5 @@ -34,7 +34,10 @@ var player_rotate_y = 0.0 #var debug:bool = false # Activate reconciliation between camera & player var reconciliate_rotate_camera_player:bool = true +# Player run var is_run:bool = false +var zoom:float = 1.0 +var tps:bool = true func _init(): @@ -47,14 +50,44 @@ func _ready(): 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 # and rotate vertically - 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) + 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) if Input.is_action_just_pressed( "move_run" ): is_run = true elif Input.is_action_just_released( "move_run" ): @@ -68,17 +101,27 @@ func _input(event): if Input.is_mouse_button_pressed( 2 ): if event is InputEventMouseMotion: - 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 ) + 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 + 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: + 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): @@ -114,12 +157,21 @@ func _physics_process(delta): y += 1 if y != 0: var dt = y * delta * SPEED_ROTATE - 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 ) + 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"): diff --git a/player/player.tscn b/player/player.tscn index 7524bf1..aba6d1e 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -27,5 +27,8 @@ script = ExtResource( "2_xvdvx" ) [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) -[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) + +[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)