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/Player.gd

135 lines
3.2 KiB
GDScript
Raw Normal View History

2020-06-14 00:29:58 +02:00
extends KinematicBody
const GRAVITY = 9.8 * 1.5
const JUMP_VELOCITY = 600
2020-06-14 21:01:48 +02:00
const WALK_VELOCITY = 700
const AIR_CONTROL = 0.1
2020-06-14 21:16:33 +02:00
const WALK_ACCEL = 0.25
2020-06-14 21:01:48 +02:00
const WALK_DECEL = 0.1
2020-06-14 00:29:58 +02:00
2020-06-14 02:11:02 +02:00
const MOUSE_SENSITIVITY = 1.0 / 300
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
2020-06-14 00:29:58 +02:00
var velocity = Vector3.ZERO
2020-06-14 00:38:15 +02:00
var walkDirection = Vector2.ZERO
2020-06-14 21:01:48 +02:00
var walkDirInt = Vector2.ZERO
2020-06-14 00:38:15 +02:00
2020-06-15 15:24:26 +02:00
2020-06-14 00:29:58 +02:00
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
2020-06-14 00:38:15 +02:00
func gravity():
2020-06-15 15:24:26 +02:00
#print("--------")
#print(get_tree().get_network_unique_id())
#print(name, " ", self.velocity.y)
#print(name, " ", self.translation.y)
if not is_on_floor():
self.velocity.y -= GRAVITY
2020-06-15 15:24:26 +02:00
2020-06-14 01:46:37 +02:00
remote func walk(direction: Vector2):
var walkDirectionNormalized = direction.normalized()
2020-06-14 01:39:42 +02:00
#print("Player walkDirection: ", walkDirectionNormalized)
2020-06-14 00:38:15 +02:00
2020-06-14 01:37:44 +02:00
var walkVelocity = WALK_VELOCITY * walkDirectionNormalized
2020-06-14 21:16:33 +02:00
var interpolation
var currentVelocity = Vector2(- velocity.z, velocity.x)
if walkVelocity.dot(currentVelocity) > 0:
interpolation = WALK_ACCEL
else:
interpolation = WALK_DECEL
2020-06-14 21:19:37 +02:00
if not is_on_floor():
interpolation *= AIR_CONTROL
2020-06-14 21:16:33 +02:00
debug.text = "Interpolation: " + String(interpolation)
debug.text += "\nwalkVelocity: " + String(walkVelocity)
debug.text += "\ncurrentVelocity: " + String(currentVelocity)
debug.text += "\nvelocity: " + String(velocity)
2020-06-14 21:19:37 +02:00
debug.text += "\nis_on_floor(): " + String(is_on_floor())
velocity.x = lerp(velocity.x, walkVelocity.rotated(- self.rotation.y).y, interpolation)
velocity.z = lerp(velocity.z, - walkVelocity.rotated(- self.rotation.y).x, interpolation)
2020-06-14 00:29:58 +02:00
2020-06-14 01:05:03 +02:00
remote func jump():
2020-06-14 01:13:43 +02:00
print("JUMP")
2020-06-14 21:20:49 +02:00
if is_on_floor():
velocity.y = JUMP_VELOCITY
2020-06-14 00:59:06 +02:00
2020-06-14 02:11:02 +02:00
remote func mouselook(rel):
self.rotate_y(- rel.x * MOUSE_SENSITIVITY)
2020-06-14 02:17:43 +02:00
camera.rotation.x = clamp(camera.rotation.x-rel.y * MOUSE_SENSITIVITY, -PI/2, PI/2)
2020-06-14 02:11:02 +02:00
2020-06-14 00:29:58 +02:00
func motion(delta):
self.move_and_slide(velocity * delta, Vector3.UP, true)
2020-06-14 01:05:03 +02:00
2020-06-14 00:29:58 +02:00
func _physics_process(delta):
2020-06-15 15:24:26 +02:00
if str(get_tree().get_network_unique_id()) != name:
return
2020-06-14 00:38:15 +02:00
gravity()
rpc("walk", walkDirection)
walk(walkDirection)
2020-06-14 00:29:58 +02:00
motion(delta)
2020-06-15 15:24:26 +02:00
rset("translation", translation)
2020-06-14 00:29:58 +02:00
2020-06-14 01:05:03 +02:00
func _input(event):
2020-06-15 15:24:26 +02:00
if str(get_tree().get_network_unique_id()) != name:
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
rpc("mouselook", rel)
2020-06-14 02:11:02 +02:00
mouselook(rel)
2020-06-14 02:07:37 +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()
2020-06-14 02:07:37 +02:00
# Walk
if event.is_action_pressed("MoveForward"):
walkDirection.x += 1
if event.is_action_pressed("MoveBack"):
walkDirection.x -= 1
if event.is_action_pressed("MoveRight"):
walkDirection.y += 1
if event.is_action_pressed("MoveLeft"):
walkDirection.y -= 1
if event.is_action_released("MoveForward"):
walkDirection.x += -1
if event.is_action_released("MoveBack"):
walkDirection.x -= -1
if event.is_action_released("MoveRight"):
walkDirection.y += -1
if event.is_action_released("MoveLeft"):
walkDirection.y -= -1
2020-06-14 01:05:03 +02:00
2020-06-14 00:29:58 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
2020-06-15 15:24:26 +02:00
rset_config("translation", MultiplayerAPI.RPC_MODE_SYNC)
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()):
debug.hide()
2020-06-14 00:29:58 +02:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass