update fly and adding swim

This commit is contained in:
AleaJactaEst 2022-02-06 01:49:25 +01:00
parent 17118e0f45
commit 6a3e4f21f3
2 changed files with 236 additions and 35 deletions

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=9 format=3 uid="uid://bkcvep4ijijmc"]
[gd_scene load_steps=13 format=3 uid="uid://bkcvep4ijijmc"]
[ext_resource type="Shader" path="res://maps/shaders/water.gdshader" id="1_j5guc"]
[ext_resource type="Script" path="res://maps/water-zone.gd" id="1_s4mf8"]
[ext_resource type="PackedScene" uid="uid://ip4lkyho0gry" path="res://maps/water_space.tscn" id="2_nnfoe"]
[ext_resource type="Texture2D" uid="uid://dppjl2jpko3lx" path="res://maps/textures/water_normal_a.png" id="3_vwt8h"]
[ext_resource type="Texture2D" uid="uid://b8tu2ncerig3r" path="res://maps/textures/water_normal_b.png" id="4_gohr5"]
@ -34,11 +35,55 @@ shader_param/caustic_sampler = SubResource( "Texture2DArray_um4oc" )
[sub_resource type="PlaneMesh" id="PlaneMesh_uh76x"]
material = SubResource( "ShaderMaterial_tfcq0" )
[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_7h41h"]
points = PackedVector3Array(1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1)
[sub_resource type="ShaderMaterial" id="ShaderMaterial_bmkr3"]
render_priority = 0
shader = ExtResource( "1_j5guc" )
shader_param/wave_speed = null
shader_param/wave_a = null
shader_param/wave_b = null
shader_param/wave_c = null
shader_param/sampler_scale = null
shader_param/sampler_direction = null
shader_param/uv_sampler_scale = null
shader_param/uv_sampler_strength = null
shader_param/foam_level = null
shader_param/refraction = null
shader_param/color_deep = Color(0, 0, 0.243137, 1)
shader_param/color_shallow = Color(0, 0.454902, 0.870588, 1)
shader_param/beers_law = null
shader_param/depth_offset = null
shader_param/projector = null
[sub_resource type="BoxMesh" id="BoxMesh_1ovik"]
material = SubResource( "ShaderMaterial_bmkr3" )
subdivide_width = 128
subdivide_height = 128
subdivide_depth = 2
[node name="water-zone" type="Node3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.964225, 0)
script = ExtResource( "1_s4mf8" )
[node name="water-level" type="MeshInstance3D" parent="."]
transform = Transform3D(20, 0, 0, 0, 1, 0, 0, 0, 20, -0.242517, -1.07318, 0.480083)
mesh = SubResource( "PlaneMesh_uh76x" )
[node name="underwater" type="Area3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.49012e-08, -5.96046e-08, 2.98023e-08)
[node name="CollisionShape3D" type="CollisionShape3D" parent="underwater"]
transform = Transform3D(20, 0, 0, 0, 3, 0, 0, 0, 20, 0, -4.10377, 0)
shape = SubResource( "ConvexPolygonShape3D_7h41h" )
[node name="water-space" parent="." instance=ExtResource( "2_nnfoe" )]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(20, 0, 0, 0, 3, 0, 0, 0, 20, 0, -3.96834, 0)
visible = false
mesh = SubResource( "BoxMesh_1ovik" )
[connection signal="body_entered" from="underwater" to="." method="_on_underwater_body_entered"]
[connection signal="body_exited" from="underwater" to="." method="_on_underwater_body_exited"]

View file

@ -25,7 +25,8 @@ const SPEED_ROTATE = PI
const PI_2 = PI / 2.0
const JUMP_FORCE = 4.5
const SPEED_FLY = 1.0
const MUL_SPEED_FLY = 2.0
const MUL_SPEED_SWIM = 0.5
# Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@ -36,7 +37,6 @@ var camera_rotate_x = 0.0
# Player position
var player_rotate_y = 0.0
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
@ -77,6 +77,27 @@ var player_sit:bool = false
var player_automove:bool = false
var state_player:StatePlayer = StatePlayer.WALK
var level_water:float
@onready var camera_fps:Camera3D = $camera_root/Camera3D_FPS_WALK
func switch_state(new_state):
if state_player == new_state:
return
state_player = new_state
match state_player:
StatePlayer.WALK:
print("switch camera WALK")
$carpet.hide()
camera_fps = $camera_root/Camera3D_FPS_WALK
StatePlayer.FLY:
print("switch camera FLY")
$carpet.show()
camera_fps = $camera_root/Camera3D_FPS_FLY
StatePlayer.SWIM:
print("switch camera SWIM")
$carpet.hide()
camera_fps = $camera_root/Camera3D_FPS_SWIM
func search_animation( obj:Node , root:String = "/") -> bool:
@ -124,6 +145,7 @@ func _ready():
get_viewport().warp_mouse(starting_point)
search_animation($Mesh/character, "/")
print_list_animation()
switch_state(StatePlayer.WALK)
func _input(event):
@ -135,9 +157,9 @@ 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()
camera_fps.rotate_x( -player_rotate_x )
player_rotate_x = 0
camera_fps.make_current()
tps = false
reconciliate_rotate_camera_player = false
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
@ -163,16 +185,12 @@ func _input(event):
match state_player:
StatePlayer.WALK:
print("FLY")
state_player = StatePlayer.FLY
$carpet.show()
#state_player = StatePlayer.FLY
switch_state(StatePlayer.FLY)
StatePlayer.FLY:
print("SWIM")
$carpet.hide()
state_player = StatePlayer.WALK
# StatePlayer.SWIM:
# print("WALK")
# $carpet.hide()
# state_player = StatePlayer.WALK
#state_player = StatePlayer.WALK
switch_state(StatePlayer.WALK)
# If right mouse button is pressed and mouse moves, pan horizontally camera
# and rotate vertically
@ -211,18 +229,10 @@ 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
var new_camera_rotate_x = player_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
camera_fps.rotate_x( event.relative.y * 0.01 )
player_rotate_x = new_camera_rotate_x
func _physics_process(delta):
@ -233,7 +243,7 @@ func _physics_process(delta):
StatePlayer.FLY:
_physics_process_fly(delta)
StatePlayer.SWIM:
pass
_physics_process_swim(delta)
func _physics_process_walk(delta):
@ -245,6 +255,7 @@ func _physics_process_walk(delta):
var move_down:bool = false
var move_strafe: bool = false
#print(get_position())
# Add the gravity.
if not is_on_floor():
motion_velocity.y -= gravity * delta
@ -421,8 +432,10 @@ func _physics_process_fly(delta):
input_z = 1.0
move_up = true
input_z *= (-camera_rotate_x) * SPEED_FLY
print("z: ", input_z, " ... ", camera_rotate_x)
if tps:
input_z *= (-camera_rotate_x)
else:
input_z *= (-player_rotate_x)
if is_run:
if move_strafe:
@ -478,18 +491,150 @@ func _physics_process_fly(delta):
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
motion_velocity.x = direction.x * speed * MUL_SPEED_FLY
motion_velocity.y = direction.y * speed * MUL_SPEED_FLY
motion_velocity.z = direction.z * speed * MUL_SPEED_FLY
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)
motion_velocity.x = move_toward(motion_velocity.x, 0, speed * MUL_SPEED_FLY)
motion_velocity.y = move_toward(motion_velocity.y, 0, speed * MUL_SPEED_FLY)
motion_velocity.z = move_toward(motion_velocity.z, 0, speed * MUL_SPEED_FLY)
print('motion_velocity:', motion_velocity)
#print('motion_velocity:', motion_velocity)
move_and_slide()
func _physics_process_swim(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
if tps:
input_z *= (-camera_rotate_x)
else:
input_z *= (-player_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 * MUL_SPEED_SWIM
motion_velocity.y = direction.y * speed * MUL_SPEED_SWIM
motion_velocity.z = direction.z * speed * MUL_SPEED_SWIM
else:
motion_velocity.x = move_toward(motion_velocity.x, 0, speed * MUL_SPEED_SWIM)
motion_velocity.y = move_toward(motion_velocity.y, 0, speed * MUL_SPEED_SWIM)
motion_velocity.z = move_toward(motion_velocity.z, 0, speed * MUL_SPEED_SWIM)
var col = move_and_slide()
if col == false and level_water <= get_position().y:
# force always under water (if not collision)
var tmp = get_position()
tmp.y = level_water
set_position(tmp)
func _process( delta ):
match state_player:
StatePlayer.WALK:
@ -531,3 +676,14 @@ func _process_walk( delta ):
player_rotate_y += TAU
func enter_underwater():
# function called by Area3D (Water Object)
level_water = get_position().y
switch_state(StatePlayer.SWIM)
print("SWIM")
func exit_underwater():
# function called by Area3D (Water Object)
switch_state(StatePlayer.WALK)
print("WALK")