adding fly

This commit is contained in:
AleaJactaEst 2022-02-05 19:58:58 +01:00
parent 4714617c0e
commit dce7602fb6
3 changed files with 225 additions and 12 deletions

View file

@ -1,5 +1,8 @@
extends CharacterBody3D
enum StatePlayer {FLY = 0, SWIM = 1, WALK = 2}
# Constant
const SPEED_WALK_UP = 5.0
const SPEED_WALK_UP_STRAFE = 3.0
@ -22,6 +25,8 @@ const SPEED_ROTATE = PI
const PI_2 = PI / 2.0
const JUMP_FORCE = 4.5
const SPEED_FLY = 1.0
# 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)
@ -30,7 +35,8 @@ var camera_rotate_y = 0.0
var camera_rotate_x = 0.0
# Player position
var player_rotate_y = 0.0
#var debug:bool = false
var player_rotate_x = 0.0
var player_head_rotate_x = 0.0
# Activate reconciliation between camera & player
var reconciliate_rotate_camera_player:bool = true
# Player run
@ -38,6 +44,7 @@ var is_run:bool = false
var zoom:float = 1.0
var tps:bool = true
var animation_object:AnimationPlayer = null
var anim_idle:String = "CHAR_idle_bored"
var anim_run:String = "CHAR_run"
var anim_run_backward:String = "CHAR_run backward"
@ -46,10 +53,31 @@ 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"
var anim_fly_idle:String = "CHAR_sitting_ground_idle"
var anim_fly_run:String = "CHAR_sitting_ground_idle"
var anim_fly_run_backward:String = "CHAR_sitting_ground_idle"
var anim_fly_sitting_ground_idle:String = "CHAR_sitting_ground_idle"
var anim_fly_strafe_left_walk:String = "CHAR_sitting_ground_idle"
var anim_fly_strafe_right_walk:String = "CHAR_sitting_ground_idle"
var anim_fly_walk:String = "CHAR_sitting_ground_idle"
var anim_fly_walk_backward:String = "CHAR_sitting_ground_idle"
var anim_swim_idle:String = "CHAR_walk"
var anim_swim_run:String = "CHAR_run"
var anim_swim_run_backward:String = "CHAR_run backward"
var anim_swim_sitting_ground_idle:String = "CHAR_walk"
var anim_swim_strafe_left_walk:String = "CHAR_walk"
var anim_swim_strafe_right_walk:String = "CHAR_walk"
var anim_swim_walk:String = "CHAR_walk"
var anim_swim_walk_backward:String = "CHAR_walk_backward"
var current_anim:String = anim_idle
var player_sit:bool = false
var player_automove:bool = false
var state_player:StatePlayer = StatePlayer.WALK
func search_animation( obj:Node , root:String = "/") -> bool:
var ret:bool = false
@ -84,6 +112,10 @@ func _on_AnimationPlayer_animation_finished(name:String):
func _init():
print("wall_min_slide_angle: ", get_wall_min_slide_angle())
# set_wall_min_slide_angle(1.0)
set_floor_max_angle(4.0)
# set_floor_snap_length(0.4)
pass
@ -103,6 +135,8 @@ func _input(event):
var zoom3D:Vector3 = Vector3(0.0, -ZOOM_STEP_Y, -ZOOM_STEP_Z)
$camera_root/horizontal_root/vertical_root/Camera3D_TPS.translate_object_local(zoom3D)
if zoom < ZOOM_MIN_Z:
$camera_root/Camera3D_FPS.rotate_x( -player_head_rotate_x )
player_head_rotate_x = 0
$camera_root/Camera3D_FPS.make_current()
tps = false
reconciliate_rotate_camera_player = false
@ -112,8 +146,6 @@ func _input(event):
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_Z
@ -127,6 +159,20 @@ func _input(event):
player_sit = not player_sit
elif Input.is_action_just_pressed("player_automove"):
player_automove = not player_automove
elif Input.is_action_just_pressed("switch_state_player"):
match state_player:
StatePlayer.WALK:
print("FLY")
state_player = StatePlayer.FLY
$carpet.show()
StatePlayer.FLY:
print("SWIM")
$carpet.hide()
state_player = StatePlayer.WALK
# StatePlayer.SWIM:
# print("WALK")
# $carpet.hide()
# state_player = StatePlayer.WALK
# If right mouse button is pressed and mouse moves, pan horizontally camera
# and rotate vertically
@ -141,12 +187,6 @@ func _input(event):
is_run = true
elif Input.is_action_just_released( "move_run" ):
is_run = false
# 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
if Input.is_mouse_button_pressed( 2 ):
if event is InputEventMouseMotion:
@ -171,8 +211,32 @@ func _input(event):
camera_rotate_y = player_rotate_y
rotate_y( -event.relative.x *0.01 )
var new_camera_rotate_x = player_head_rotate_x + event.relative.y * 0.01
if new_camera_rotate_x <= PI_2 and new_camera_rotate_x >= - PI_2:
$camera_root/Camera3D_FPS.rotate_x( event.relative.y * 0.01 )
player_head_rotate_x = new_camera_rotate_x
match state_player:
StatePlayer.WALK:
pass
StatePlayer.FLY:
pass
StatePlayer.SWIM:
pass
func _physics_process(delta):
# Add the gravity.
match state_player:
StatePlayer.WALK:
_physics_process_walk(delta)
StatePlayer.FLY:
_physics_process_fly(delta)
StatePlayer.SWIM:
pass
func _physics_process_walk(delta):
var input_dir: Vector2
var input_x: float
var input_y: float
@ -294,13 +358,149 @@ func _physics_process(delta):
motion_velocity.x = move_toward(motion_velocity.x, 0, speed)
motion_velocity.z = move_toward(motion_velocity.z, 0, speed)
#print("pos:", get_floor_angle(), " - ", get_wall_min_slide_angle(), " - ", get_floor_max_angle())
#motion_velocity.y = 0.5
move_and_slide()
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
input_z *= (-camera_rotate_x) * SPEED_FLY
print("z: ", input_z, " ... ", camera_rotate_x)
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:
motion_velocity.x = direction.x * speed
motion_velocity.y = direction.y * speed
motion_velocity.z = direction.z * speed
else:
motion_velocity.x = move_toward(motion_velocity.x, 0, speed)
motion_velocity.y = move_toward(motion_velocity.y, 0, speed)
motion_velocity.z = move_toward(motion_velocity.z, 0, speed)
print('motion_velocity:', motion_velocity)
move_and_slide()
func _process( delta ):
# if debug:
# print(reconciliate_rotate_camera_player, ", ", camera_rotate_y,", ", player_rotate_y)
match state_player:
StatePlayer.WALK:
_process_walk(delta)
StatePlayer.FLY:
_process_walk(delta)
StatePlayer.SWIM:
_process_walk(delta)
func _process_walk( delta ):
if reconciliate_rotate_camera_player:
var diff = camera_rotate_y - player_rotate_y
@ -329,3 +529,5 @@ func _process( delta ):
player_rotate_y -= TAU
elif player_rotate_y <= -PI:
player_rotate_y += TAU

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=5 format=3 uid="uid://dwxrvijqyym70"]
[gd_scene load_steps=6 format=3 uid="uid://dwxrvijqyym70"]
[ext_resource type="Script" path="res://player/player.gd" id="1_myhjw"]
[ext_resource type="PackedScene" uid="uid://ccctktndxnskh" path="res://player/character/character.tscn" id="1_rqqo8"]
@ -8,6 +8,8 @@
radius = 0.220923
height = 2.83965
[sub_resource type="PlaneMesh" id="PlaneMesh_mwqg4"]
[node name="player" type="CharacterBody3D"]
script = ExtResource( "1_myhjw" )
@ -32,3 +34,7 @@ transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0,
[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.56633, 0.222061)
[node name="carpet" type="MeshInstance3D" parent="."]
visible = false
mesh = SubResource( "PlaneMesh_mwqg4" )

View file

@ -75,3 +75,8 @@ player_automove={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
]
}
switch_state_player={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"store_command":true,"alt_pressed":false,"shift_pressed":false,"meta_pressed":false,"command_pressed":false,"pressed":false,"keycode":67,"physical_keycode":0,"unicode":99,"echo":false,"script":null)
]
}