This repository has been archived on 2022-01-09. You can view files and clone it, but cannot push or open issues/pull-requests.
liblast/Game/Classes/Player/Player.gd

439 lines
11 KiB
GDScript
Raw Normal View History

class_name Player
2020-06-14 00:29:58 +02:00
extends KinematicBody
2021-03-18 00:05:57 +01:00
const GRAVITY = Vector3.DOWN * 9.8 * 1.5
2021-03-30 16:03:30 +02:00
const JUMP_VELOCITY = 8
2021-03-18 00:05:57 +01:00
const WALK_VELOCITY = 8
2020-06-14 21:01:48 +02:00
const AIR_CONTROL = 0.1
2021-03-30 16:03:30 +02:00
const JUMP_ACCEL = 1
2021-03-18 00:05:57 +01:00
const WALK_ACCEL = 5
2021-03-30 16:03:30 +02:00
#const WALK_DECEL = 0.1
2020-06-14 00:29:58 +02:00
const MOUSE_SENSITIVITY = 1.0 / 1000
2020-06-14 02:07:37 +02:00
export var show_healthbar = true
2020-06-17 22:01:12 +02:00
export var max_health = 150
2020-09-29 00:17:19 +02:00
onready var health = max_health setget set_health
2020-06-17 22:01:12 +02:00
2020-06-14 02:07:37 +02:00
onready var camera = $Camera
2020-06-14 21:29:36 +02:00
onready var debug = $Debug
2020-06-14 02:07:37 +02:00
onready var weapon_bob_anim = $Camera/Hand/WeaponBobAnimationTree["parameters/playback"]
2020-10-14 00:45:11 +02:00
var is_dead = true
2021-03-30 16:03:30 +02:00
var is_on_floor = false
var jump_timeout = 0.0
var floor_normal = Vector3.UP
var was_on_floor = false
2020-09-26 00:16:13 +02:00
const JETPACK_FUEL_MAX = 0.6
const JETPACK_REFILL_RATE = 0.2
const JETPACK_THRUST = 25
var jetpack_active = false # is the jetpack active?
2021-04-07 22:54:52 +02:00
var jetpack_fireing = false # Is the jetpack fireing?
var jetpack_used = false # Is the jetpack recharging?
var jetpack_fuel = JETPACK_FUEL_MAX # max fuel (in seconds)
2021-03-31 11:50:53 +02:00
onready var weapons = $Camera/Hand/Weapons
onready var active_weapon = weapons.switch_to_weapon(0)
#onready var sfx_foosteps = [$"Sounds/Footstep-Concrete-01",
# $"Sounds/Footstep-Concrete-02",
# $"Sounds/Footstep-Concrete-03",
# $"Sounds/Footstep-Concrete-04"]
2020-09-18 00:33:51 +02:00
#var sfx_footsteps_last = 0
#var sfx_footsteps_next = 0
#var sfx_footsteps_delay = 0.2
#var sfx_footsteps_play = false
2020-09-18 00:33:51 +02:00
2020-06-17 22:38:26 +02:00
onready var game = get_parent().get_parent()
2020-06-14 00:29:58 +02:00
var velocity = Vector3.ZERO
2021-03-18 00:05:57 +01:00
var walk_direction = Vector2.ZERO
2020-06-14 21:01:48 +02:00
var walkDirInt = Vector2.ZERO
2020-06-14 00:38:15 +02:00
#var bulletHitEffect = preload("res://Assets/Effects/BulletHit.tscn")
var bodyHitEffect = preload("res://Assets/Effects/BodyHit.tscn")
onready var nickname = "guest" setget set_nickname
2021-02-26 22:47:10 +01:00
var player_class = "none"
2020-09-19 11:22:41 +02:00
#func sfx_play_footsteps():
# if not sfx_footsteps_play:
# sfx_footsteps_play = true
# while sfx_footsteps_next == sfx_footsteps_last:
# sfx_footsteps_next = randi() % len(sfx_foosteps)
# sfx_footsteps_last = sfx_footsteps_next
# print("Play footstep: ", String(sfx_footsteps_next) )
# sfx_foosteps[sfx_footsteps_next].play()
# yield(get_tree().create_timer(sfx_footsteps_delay),"timeout")
# sfx_footsteps_play = false
2020-09-18 00:33:51 +02:00
2020-09-29 00:17:19 +02:00
func set_health(value):
health = value
$HUD.updateHealth(value)
$Billboard.rpc("set_health", value)
#$Billboard.set_health(value)
2020-10-03 23:26:35 +02:00
func set_nickname(_nickname):
$Billboard.set_nickname(_nickname)
nickname = _nickname
2020-09-29 00:17:19 +02:00
2020-10-03 23:26:35 +02:00
remote func set_player_data(player):
nickname = player.nickname
func get_closest_point(_A: Vector3, _B: Vector3):
var A = transform.inverse().xform(_A)
var B = transform.inverse().xform(_B)
2020-09-19 14:10:30 +02:00
var diff = B - A
var result = A - (A.dot(diff) * diff) / (diff.length_squared())
2020-09-25 23:38:22 +02:00
return transform.xform(result)
2020-09-25 23:19:38 +02:00
func on_bullet_flyby(from, to):
var closest_point = get_closest_point(from, to)
var flyby_noise = preload("res://Classes/Audio/BulletFlyBySoundPlayer.tscn").instance()
2020-09-25 23:38:22 +02:00
flyby_noise.translation = closest_point
get_tree().root.call_deferred("add_child", flyby_noise)
2020-09-25 23:19:38 +02:00
2020-06-14 01:05:03 +02:00
remote func jump():
2021-03-30 16:03:30 +02:00
if is_on_floor:
2020-06-14 21:20:49 +02:00
velocity.y = JUMP_VELOCITY
2021-03-30 16:03:30 +02:00
jump_timeout = 0.2
2021-03-18 00:05:57 +01:00
$Sounds/Jump.play()
weapon_bob_anim.travel("Jump")
2020-06-14 00:59:06 +02:00
func jetpack(delta):
# Swap these to try the different versions of the jetpack.
2021-04-07 22:54:52 +02:00
#jetpack_grounded(delta)
#jetpack_empty(delta)
# activate visual jetpack effects when sound is playing:
2021-04-07 22:54:52 +02:00
$Sounds/Jetpack.stream_paused = !jetpack_fireing
$Effects/JetpackFlame.emitting = jetpack_fireing
$Effects/JetpackSmoke.emitting = jetpack_fireing
$Effects/JetpackLight.visible = jetpack_fireing
#print(get_tree().get_network_unique_id())
#$Effects/JetpackFlame.emitting = ! $Sounds/Jetpack.stream_paused
#$Effects/JetpackSmoke.emitting = ! $Sounds/Jetpack.stream_paused
#$Effects/JetpackLight.visible = ! $Sounds/Jetpack.stream_paused
func jetpack_empty(delta):
debug.text = "JP fuel: %s\nJP active: %s\nJP used: %s\nJP sound: %s" % [
jetpack_fuel, jetpack_active, jetpack_used, !$Sounds/Jetpack.stream_paused
]
# Enable jetpack when it is fully charged.
if jetpack_fuel > (JETPACK_FUEL_MAX - 0.001):
jetpack_used = false
# Disable jetpack when it is empty.
elif jetpack_fuel <= 0 and not jetpack_active:
jetpack_used = true
if jetpack_active and not jetpack_used and jetpack_fuel > 0:
velocity.y += JETPACK_THRUST * delta
jetpack_fuel -= delta
$Sounds/Jetpack.stream_paused = false
else:
$Sounds/Jetpack.stream_paused = true
# Only charge when fully empty.
if jetpack_used:
jetpack_fuel = clamp(
jetpack_fuel + JETPACK_REFILL_RATE * delta,
0.0,
JETPACK_FUEL_MAX
)
# Charge when grounded variant.
func jetpack_grounded(delta):
debug.text = "JP fuel: %s\nJP active: %s\nJP sound: %s" % [
2021-03-28 03:13:26 +02:00
jetpack_fuel, jetpack_active, !$Sounds/Jetpack.stream_paused
]
# Only charge when grounded.
if is_on_floor:
jetpack_fuel = clamp(
jetpack_fuel + JETPACK_REFILL_RATE * delta,
0.0,
JETPACK_FUEL_MAX
)
2021-04-07 22:54:52 +02:00
jetpack_fireing = false
# Only use jetpack in the air.
else:
if jetpack_active and jetpack_fuel > 0:
2021-04-07 22:54:52 +02:00
velocity.y += JETPACK_THRUST * delta
jetpack_fuel -= delta
2021-04-07 22:54:52 +02:00
jetpack_fireing = true
else:
2021-04-07 22:54:52 +02:00
jetpack_fireing = false
remote func mouselook_abs(x, y):
camera.rotation.x = x
rotation.y = y
2020-06-14 02:11:02 +02:00
remote func mouselook(rel):
var sensitivity = MOUSE_SENSITIVITY * game.mouse_sensitivity_multiplier
rotate_y(- rel.x * sensitivity)
camera.rotation.x = clamp(camera.rotation.x-rel.y * sensitivity, -PI/2, PI/2)
2020-09-16 19:21:56 +02:00
rpc_unreliable("mouselook_abs", camera.rotation.x, rotation.y)
2020-06-14 02:11:02 +02:00
2021-03-18 00:05:57 +01:00
func _physics_process(delta):
if get_parent().name != "Players":
2021-03-18 00:05:57 +01:00
return
2021-03-30 16:03:30 +02:00
check_floor_collision()
2021-03-18 00:05:57 +01:00
walk(delta)
fall(delta)
2021-04-07 22:54:52 +02:00
jetpack_grounded(delta)
if str(get_tree().get_network_unique_id()) != name:
return
2021-03-18 00:05:57 +01:00
var movement_vector = Vector3()
2021-03-30 16:03:30 +02:00
if jump_timeout > 0:
2021-03-18 00:05:57 +01:00
movement_vector = move_and_slide(velocity, Vector3.UP)
else:
2021-03-30 16:03:30 +02:00
var upvector = floor_normal
2021-03-18 00:05:57 +01:00
movement_vector = move_and_slide_with_snap(velocity, Vector3.DOWN, upvector, true)
2021-03-30 16:03:30 +02:00
2021-03-18 00:05:57 +01:00
velocity = movement_vector
2020-09-14 23:51:03 +02:00
2021-03-18 00:05:57 +01:00
rset("translation", translation)
2020-06-14 01:05:03 +02:00
2021-03-30 16:03:30 +02:00
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
2021-03-18 00:05:57 +01:00
func walk(delta):
2021-03-30 16:03:30 +02:00
jump_timeout -= delta
2021-03-18 00:05:57 +01:00
# Walk
walk_direction = Vector2()
2020-09-26 00:16:13 +02:00
2021-03-18 00:05:57 +01:00
if Input.is_action_pressed("MoveForward"):
walk_direction.y -= 1
if Input.is_action_pressed("MoveBack"):
walk_direction.y += 1
if Input.is_action_pressed("MoveLeft"):
walk_direction.x -= 1
if Input.is_action_pressed("MoveRight"):
walk_direction.x += 1
2020-06-15 15:24:26 +02:00
2021-03-18 00:05:57 +01:00
walk_direction = walk_direction.normalized()
2021-03-18 00:05:57 +01:00
var walking_speed = Vector2(velocity.x, velocity.z)
2021-03-18 00:05:57 +01:00
walking_speed = walking_speed.rotated(rotation.y)
2021-03-30 16:03:30 +02:00
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)
2021-03-18 00:05:57 +01:00
walking_speed = walking_speed.rotated(-rotation.y)
2021-03-18 00:05:57 +01:00
velocity.x = walking_speed.x
velocity.z = walking_speed.y
2021-03-30 16:03:30 +02:00
# 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")
2021-03-30 16:03:30 +02:00
elif walking_speed.length() == 0 and is_on_floor:
weapon_bob_anim.travel("Idle")
2021-03-18 00:05:57 +01:00
func fall(delta):
2021-03-30 16:03:30 +02:00
if is_on_floor:
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")
2021-03-30 16:03:30 +02:00
2021-03-18 00:05:57 +01:00
else:
velocity += delta * GRAVITY
2021-03-30 16:03:30 +02:00
was_on_floor = is_on_floor
2020-06-14 00:29:58 +02:00
master func on_hit(damage, location):
2020-09-29 00:17:19 +02:00
set_health(health - 30)
2020-09-17 23:09:28 +02:00
rpc("blood_splatter", location)
2020-09-28 22:33:44 +02:00
if health <= 0:
2020-09-17 23:09:28 +02:00
rpc("kill")
else:
2020-09-28 00:31:59 +02:00
$Sounds/Pain.rpc("play")
2020-06-17 22:01:12 +02:00
2020-09-26 00:07:21 +02:00
sync func blood_splatter(location):
var effect = bodyHitEffect.instance()
get_tree().root.add_child(effect)
effect.global_transform.origin = location
2020-06-19 01:21:17 +02:00
master func kill():
2020-09-26 02:07:35 +02:00
if is_dead:
return
$Sounds/Death.rpc("play")
2020-09-26 02:07:35 +02:00
is_dead = true
2020-09-29 00:17:19 +02:00
set_health(0)
$CollisionShapeBody.disabled = true
2020-09-28 22:33:44 +02:00
$Camera/Hand.hide()
2020-09-30 02:08:16 +02:00
#$HUD.update_crosshair(false, false)
2020-09-28 22:33:44 +02:00
2020-09-23 21:17:14 +02:00
yield(get_tree().create_timer(3), "timeout")
2020-06-19 01:21:17 +02:00
spawn()
2020-09-23 21:17:14 +02:00
yield(get_tree().create_timer(3), "timeout")
2020-09-29 00:17:19 +02:00
# for i in gibs.get_children():
# i.queue_free()
# yield(get_tree().create_timer(rand_range(0.1, 1)), "timeout")
2020-09-23 21:17:14 +02:00
2020-09-29 00:17:19 +02:00
# gibs.queue_free()
2020-06-19 01:21:17 +02:00
func spawn():
rpc("unset_death")
set_health(max_health)
velocity = Vector3()
2020-12-11 22:54:40 +01:00
2020-06-19 01:21:17 +02:00
game.get_spawn_point().spawn(self)
$Camera/Hand.show()
2020-10-14 00:45:11 +02:00
$HUD.show()
$CollisionShapeBody.disabled = false
$Camera.rotation = Vector3.ZERO
rotation = Vector3.ZERO
2020-06-19 01:21:17 +02:00
sync func unset_death():
is_dead = false
2020-06-17 22:01:12 +02:00
func shoot():
2021-03-31 11:58:07 +02:00
# The underscore indicates an unused variable.
# Because it is declared in this scope, it will disappear as soon as the
# function returns. As is, it exists solely to catch the return value of shoot().
2021-03-31 11:50:53 +02:00
var _remaining_ammo = active_weapon.shoot($Camera)
2020-09-30 02:08:16 +02:00
func reload():
2021-03-31 11:50:53 +02:00
active_weapon.reload()
2020-06-17 22:01:12 +02:00
2021-03-31 12:52:20 +02:00
sync func switch_to_next_weapon():
active_weapon = weapons.next_weapon()
sync func switch_to_prev_weapon():
active_weapon = weapons.prev_weapon()
2020-06-14 01:05:03 +02:00
func _input(event):
2020-09-26 00:16:13 +02:00
if is_dead:
return
2020-06-15 15:24:26 +02:00
if str(get_tree().get_network_unique_id()) != name:
return
2020-06-17 22:38:26 +02:00
if game.GAME_MODE != "PLAYING":
return
2020-06-14 02:07:37 +02:00
# Moouselook
if event is InputEventMouseMotion:
2020-06-14 02:17:43 +02:00
var rel = event.relative
2020-06-14 02:11:02 +02:00
mouselook(rel)
2020-06-14 02:07:37 +02:00
2021-04-07 22:54:52 +02:00
# Jump
2020-06-14 01:05:03 +02:00
if event.is_action_pressed("MoveJump"):
2020-06-14 01:13:43 +02:00
rpc("jump")
2020-06-14 01:05:03 +02:00
jump()
2021-04-07 22:54:52 +02:00
# Jetpack
if event.is_action_pressed("MoveJetpack"):
2021-04-07 22:54:52 +02:00
rpc("set_jetpack_active", true)
if event.is_action_released("MoveJetpack"):
2021-04-07 22:54:52 +02:00
rpc("set_jetpack_active", false)
# Weapon
2020-06-17 22:01:12 +02:00
if event.is_action_pressed("WeaponPrimary"):
shoot()
2020-09-30 02:08:16 +02:00
if event.is_action_pressed("WeaponReload"):
reload()
2021-03-31 11:50:53 +02:00
if event.is_action_pressed("WeaponNext"):
2021-03-31 12:52:20 +02:00
rpc("switch_to_next_weapon")
if event.is_action_pressed("WeaponPrev"):
2021-03-31 12:52:20 +02:00
rpc("switch_to_prev_weapon")
2021-03-31 11:50:53 +02:00
2021-04-07 22:54:52 +02:00
sync func set_jetpack_active(active):
jetpack_active = active
2020-06-14 01:05:03 +02:00
func set_local_player():
set_network_master(get_tree().get_network_unique_id())
camera.current = true
2020-10-14 00:45:11 +02:00
#$HUD.show()
2020-10-02 20:55:46 +02:00
$Billboard.hide()
2020-06-14 00:29:58 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
$HUD/Health/HealthBar.max_value = max_health
2021-02-26 22:47:10 +01:00
# Set player class
var path = get_script().get_path()
if path.find("res://Assets/Characters/") != -1:
player_class = path.replace("res://Assets/Characters/", "").split("/")[0]
set_health(max_health)
# disabled the ragdoll collider
2020-09-29 00:17:19 +02:00
#for i in $Player/Gibs.get_children():
# i.get_child(1).disabled = true
#disabled = true
#$"Player/Gibs/PlayerGibs _cell /shape0".set_disabled(true)
2020-06-15 15:24:26 +02:00
rset_config("translation", MultiplayerAPI.RPC_MODE_SYNC)
if !show_healthbar:
$Billboard.hide()
2020-06-14 22:16:42 +02:00
# only show the debug label on local machine
if name != String(get_tree().get_network_unique_id()):
2020-06-14 22:16:42 +02:00
debug.hide()
# initialize sound looping
#$Sounds/Jetpack.stream.
2020-06-14 00:29:58 +02:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
2021-04-07 22:54:52 +02:00
func _process(delta):
jetpack(delta)