2020-06-14 00:29:58 +02:00
|
|
|
extends KinematicBody
|
|
|
|
|
|
|
|
const GRAVITY = 9.8
|
2020-06-14 00:59:06 +02:00
|
|
|
const JUMP_VELOCITY = 400
|
2020-06-14 01:37:44 +02:00
|
|
|
const WALK_VELOCITY = 550
|
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 00:29:58 +02:00
|
|
|
var velocity = Vector3.ZERO
|
|
|
|
|
2020-06-14 00:38:15 +02:00
|
|
|
var walkDirection = Vector2.ZERO
|
|
|
|
|
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():
|
|
|
|
self.velocity.y -= GRAVITY
|
|
|
|
|
2020-06-14 01:46:37 +02:00
|
|
|
remote func walk(direction: Vector2):
|
2020-06-14 02:07:37 +02:00
|
|
|
#print ("Walk: ", direction)
|
2020-06-14 01:46:37 +02:00
|
|
|
|
|
|
|
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 01:46:37 +02:00
|
|
|
velocity.x = walkVelocity.y
|
2020-06-14 01:37:44 +02:00
|
|
|
velocity.z = - walkVelocity.x
|
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 01:05:03 +02:00
|
|
|
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)
|
|
|
|
camera.rotate_x(-rel.y * MOUSE_SENSITIVITY)
|
|
|
|
|
2020-06-14 00:29:58 +02:00
|
|
|
func motion(delta):
|
2020-06-14 02:09:52 +02:00
|
|
|
self.move_and_slide(velocity.rotated(Vector3.UP, self.rotation.y) * delta, Vector3.UP)
|
2020-06-14 01:05:03 +02:00
|
|
|
|
2020-06-14 00:29:58 +02:00
|
|
|
func _physics_process(delta):
|
2020-06-14 00:38:15 +02:00
|
|
|
gravity()
|
2020-06-14 01:32:44 +02:00
|
|
|
|
2020-06-14 01:41:51 +02:00
|
|
|
rpc("walk", walkDirection)
|
|
|
|
walk(walkDirection)
|
2020-06-14 01:32:44 +02:00
|
|
|
|
2020-06-14 00:29:58 +02:00
|
|
|
motion(delta)
|
|
|
|
|
2020-06-14 01:05:03 +02:00
|
|
|
func _input(event):
|
2020-06-14 01:32:44 +02:00
|
|
|
#print(event)
|
|
|
|
|
2020-06-14 02:07:37 +02:00
|
|
|
# Moouselook
|
|
|
|
if event is InputEventMouseMotion:
|
|
|
|
var rel = event["relative"]
|
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 01:32:44 +02:00
|
|
|
|
2020-06-14 02:07:37 +02:00
|
|
|
# Walk
|
|
|
|
|
2020-06-14 01:32:44 +02:00
|
|
|
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():
|
|
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
|
#func _process(delta):
|
|
|
|
# pass
|