tokc 2021-03-30 17:14:28 +02:00
commit a220840aa2
2 changed files with 47 additions and 31 deletions

View File

@ -1,13 +1,14 @@
extends KinematicBody extends KinematicBody
const GRAVITY = Vector3.DOWN * 9.8 * 1.5 const GRAVITY = Vector3.DOWN * 9.8 * 1.5
const JUMP_VELOCITY = 9 const JUMP_VELOCITY = 8
const WALK_VELOCITY = 8 const WALK_VELOCITY = 8
const AIR_CONTROL = 0.1 const AIR_CONTROL = 0.1
const JUMP_ACCEL = 1
const WALK_ACCEL = 5 const WALK_ACCEL = 5
const WALK_DECEL = 0.1 #const WALK_DECEL = 0.1
const MOUSE_SENSITIVITY = 1.0 / 1000 const MOUSE_SENSITIVITY = 1.0 / 1000
@ -21,6 +22,9 @@ onready var debug = $Debug
onready var weapon_bob_anim = $Camera/Hand/WeaponBobAnimationTree["parameters/playback"] onready var weapon_bob_anim = $Camera/Hand/WeaponBobAnimationTree["parameters/playback"]
var is_dead = true var is_dead = true
var is_on_floor = false
var jump_timeout = 0.0
var floor_normal = Vector3.UP
var was_on_floor = false var was_on_floor = false
const JETPACK_FUEL_MAX = 1 const JETPACK_FUEL_MAX = 1
@ -92,8 +96,9 @@ func on_bullet_flyby(from, to):
get_tree().root.call_deferred("add_child", flyby_noise) get_tree().root.call_deferred("add_child", flyby_noise)
remote func jump(): remote func jump():
if is_on_floor(): if is_on_floor:
velocity.y = JUMP_VELOCITY velocity.y = JUMP_VELOCITY
jump_timeout = 0.2
$Sounds/Jump.play() $Sounds/Jump.play()
weapon_bob_anim.travel("Jump") weapon_bob_anim.travel("Jump")
@ -115,25 +120,41 @@ func _physics_process(delta):
if str(get_tree().get_network_unique_id()) != name: if str(get_tree().get_network_unique_id()) != name:
return return
check_floor_collision()
walk(delta) walk(delta)
fall(delta) fall(delta)
var movement_vector = Vector3() var movement_vector = Vector3()
if Input.is_action_just_pressed("MoveJump"): if jump_timeout > 0:
movement_vector = move_and_slide(velocity, Vector3.UP) movement_vector = move_and_slide(velocity, Vector3.UP)
else: else:
var upvector = Vector3.UP var upvector = floor_normal
if is_on_floor():
upvector = get_floor_normal()
movement_vector = move_and_slide_with_snap(velocity, Vector3.DOWN, upvector, true) movement_vector = move_and_slide_with_snap(velocity, Vector3.DOWN, upvector, true)
velocity = movement_vector velocity = movement_vector
rset("translation", translation) rset("translation", translation)
func check_floor_collision():
var space_state = get_world().direct_space_state
var from = global_transform.xform(Vector3(0, 0.0, 0))
var to = global_transform.xform(Vector3(0, -0.3, 0.0))
var result = space_state.intersect_ray(from, to)
if jump_timeout > 0:
is_on_floor = false
elif result:
is_on_floor = true
floor_normal = result.normal
else:
is_on_floor = false
func walk(delta): func walk(delta):
jump_timeout -= delta
# Walk # Walk
walk_direction = Vector2() walk_direction = Vector2()
@ -151,31 +172,35 @@ func walk(delta):
var walking_speed = Vector2(velocity.x, velocity.z) var walking_speed = Vector2(velocity.x, velocity.z)
walking_speed = walking_speed.rotated(rotation.y) walking_speed = walking_speed.rotated(rotation.y)
walking_speed = lerp(walking_speed, walk_direction * WALK_VELOCITY, delta * WALK_ACCEL)
if is_on_floor:
walking_speed = lerp(walking_speed, walk_direction * WALK_VELOCITY, delta * WALK_ACCEL)
else:
walking_speed = lerp(walking_speed, walk_direction * JUMP_VELOCITY, delta * JUMP_ACCEL)
walking_speed = walking_speed.rotated(-rotation.y) walking_speed = walking_speed.rotated(-rotation.y)
velocity.x = walking_speed.x velocity.x = walking_speed.x
velocity.z = walking_speed.y velocity.z = walking_speed.y
if walking_speed.length() > 0 and is_on_floor(): # Make walking perpendicular to the floor
if is_on_floor:
velocity.y -= velocity.dot(floor_normal) / floor_normal.y
if walking_speed.length() > 0 and is_on_floor:
weapon_bob_anim.travel("Walk") weapon_bob_anim.travel("Walk")
elif walking_speed.length() == 0 and is_on_floor(): elif walking_speed.length() == 0 and is_on_floor:
weapon_bob_anim.travel("Idle") weapon_bob_anim.travel("Idle")
func fall(delta): func fall(delta):
print(is_on_floor()) if is_on_floor:
if is_on_floor():
velocity -= delta * get_floor_normal() * 20
if not was_on_floor: # if this is the first frame of ground conotact after a frame of no ground contact - we've just ended a fall if not was_on_floor: # if this is the first frame of ground conotact after a frame of no ground contact - we've just ended a fall
weapon_bob_anim.travel("Land") weapon_bob_anim.travel("Land")
else: else:
velocity += delta * GRAVITY velocity += delta * GRAVITY
was_on_floor = is_on_floor() was_on_floor = is_on_floor
master func on_hit(damage, location): master func on_hit(damage, location):
@ -199,15 +224,9 @@ master func kill():
$Sounds/Death.rpc("play") $Sounds/Death.rpc("play")
#print ("kill")
is_dead = true is_dead = true
#print ("set as dead")
set_health(0) set_health(0)
#print ("health:", health)
$CollisionShapeBody.disabled = true $CollisionShapeBody.disabled = true
$Camera/Hand.hide() $Camera/Hand.hide()

View File

@ -136,7 +136,7 @@ script = ExtResource( 1 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.53592, -0.0651628 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.53592, -0.0651628 )
[node name="Hand" type="Spatial" parent="Camera"] [node name="Hand" type="Spatial" parent="Camera"]
transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0.335, -0.393263, -0.559 ) transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0.335, -0.396778, -0.559 )
[node name="WeaponBobAnimationTree" type="AnimationTree" parent="Camera/Hand"] [node name="WeaponBobAnimationTree" type="AnimationTree" parent="Camera/Hand"]
tree_root = SubResource( 12 ) tree_root = SubResource( 12 )
@ -152,9 +152,6 @@ anims/Walk = SubResource( 17 )
[node name="Weapon" parent="Camera/Hand" instance=ExtResource( 2 )] [node name="Weapon" parent="Camera/Hand" instance=ExtResource( 2 )]
[node name="SoundShoot" parent="Camera/Hand/Weapon" index="2"]
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0.973939, 0.369099, 0.299776 )
[node name="Billboard" type="MeshInstance" parent="."] [node name="Billboard" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.49795, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.49795, 0 )
mesh = SubResource( 18 ) mesh = SubResource( 18 )
@ -243,9 +240,9 @@ autoplay = true
attenuation_filter_cutoff_hz = 15000.0 attenuation_filter_cutoff_hz = 15000.0
[node name="HUD" parent="." instance=ExtResource( 3 )] [node name="HUD" parent="." instance=ExtResource( 3 )]
[connection signal="ammo_changed" from="Camera/Hand/Weapon" to="HUD" method="update_ammo"] [connection signal="ammo_changed" from="Camera/Hand/Weapon" to="HUD" method="update_ammo"]
[connection signal="damage_dealt" from="Camera/Hand/Weapon" to="HUD" method="update_crosshair" binds= [ true, true ]] [connection signal="damage_dealt" from="Camera/Hand/Weapon" to="HUD" method="update_crosshair" binds= [ true, true ]]
[editable path="Camera/Hand/Weapon"] [editable path="Camera/Hand/Weapon"]
[editable path="Camera/Hand/Weapon/Handgun"] [editable path="Camera/Hand/Weapon/Handgun"]