2020-06-14 00:29:58 +02:00
|
|
|
extends KinematicBody
|
|
|
|
|
2020-06-14 21:54:52 +02:00
|
|
|
const GRAVITY = 9.8 * 1.5
|
2020-06-19 00:30:39 +02:00
|
|
|
const JUMP_VELOCITY = 400
|
|
|
|
const WALK_VELOCITY = 550
|
2020-06-14 21:01:48 +02:00
|
|
|
|
|
|
|
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-20 00:35:42 +02:00
|
|
|
const MOUSE_SENSITIVITY = 1.0 / 1000
|
2020-06-14 02:07:37 +02:00
|
|
|
|
2020-06-17 22:01:12 +02:00
|
|
|
export var max_health = 150
|
|
|
|
onready var health = max_health
|
|
|
|
|
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-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
|
|
|
|
|
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-23 01:14:10 +02:00
|
|
|
var bulletHitEffect = preload("res://Assets/Effects/BulletHit.tscn")
|
|
|
|
|
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-14 21:23:10 +02:00
|
|
|
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)
|
2020-06-14 21:54:52 +02:00
|
|
|
debug.text += "\nvelocity: " + String(velocity)
|
2020-06-14 21:19:37 +02:00
|
|
|
debug.text += "\nis_on_floor(): " + String(is_on_floor())
|
2020-06-17 22:01:12 +02:00
|
|
|
debug.text += "\nhealth: " + String(health)
|
|
|
|
|
2020-06-14 21:54:52 +02:00
|
|
|
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 21:20:49 +02:00
|
|
|
if is_on_floor():
|
|
|
|
velocity.y = JUMP_VELOCITY
|
2020-06-14 00:59:06 +02:00
|
|
|
|
2020-09-16 19:09:44 +02:00
|
|
|
remote func mouselook_abs(x, y):
|
|
|
|
camera.rotation.x = x
|
|
|
|
rotation.y = y
|
2020-09-16 19:06:33 +02:00
|
|
|
|
2020-06-14 02:11:02 +02:00
|
|
|
remote func mouselook(rel):
|
2020-06-20 00:35:42 +02:00
|
|
|
var sensitivity = MOUSE_SENSITIVITY * game.mouse_sensitivity_multiplier
|
2020-09-16 19:09:44 +02:00
|
|
|
rotate_y(- rel.x * sensitivity)
|
2020-06-20 00:35:42 +02:00
|
|
|
camera.rotation.x = clamp(camera.rotation.x-rel.y * sensitivity, -PI/2, PI/2)
|
2020-09-16 19:06:33 +02:00
|
|
|
|
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
|
|
|
|
2020-06-14 00:29:58 +02:00
|
|
|
func motion(delta):
|
2020-09-16 19:09:44 +02:00
|
|
|
var slide_velocity = move_and_slide(velocity * delta, Vector3.UP, true)
|
|
|
|
#var slide_velocity = move_and_collide(velocity * delta, Vector3.UP)
|
2020-09-12 22:54:59 +02:00
|
|
|
|
|
|
|
debug.text += "\nslide_velocity: " + String( slide_velocity )
|
|
|
|
debug.text += "\nslide dot product: " + String( velocity.normalized().dot(slide_velocity.normalized()) )
|
|
|
|
debug.text += "\nslide count: " + String( self.get_slide_count() )
|
|
|
|
for i in range(0, self.get_slide_count() ):
|
|
|
|
debug.text += "\nslide " + String(i) + ": " + String( self.get_slide_collision(i).normal )
|
2020-09-14 00:25:53 +02:00
|
|
|
var dot_product = self.get_slide_collision(i).normal.dot(velocity)
|
|
|
|
debug.text += "\nslide dot " + String(i) + ": " + String( dot_product )
|
2020-09-12 22:54:59 +02:00
|
|
|
|
2020-09-14 23:51:03 +02:00
|
|
|
#if dot_product < 0:
|
|
|
|
# # Push represents the component vector pushing into the surface
|
|
|
|
# var push = dot_product * self.get_slide_collision(i).normal
|
|
|
|
# debug.text += "\npush " + String(i) + ": " + String( push )
|
|
|
|
#
|
|
|
|
# velocity -= push
|
|
|
|
|
|
|
|
velocity = slide_velocity / delta
|
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
velocity -= get_floor_normal() * 150
|
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()
|
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-15 15:24:26 +02:00
|
|
|
|
|
|
|
rset("translation", translation)
|
2020-06-14 00:29:58 +02:00
|
|
|
|
2020-06-17 22:01:12 +02:00
|
|
|
master func on_hit():
|
|
|
|
health -= 30
|
2020-09-17 23:09:28 +02:00
|
|
|
|
|
|
|
if health <= 0:
|
|
|
|
rpc("kill")
|
2020-06-17 22:01:12 +02:00
|
|
|
|
2020-06-19 01:21:17 +02:00
|
|
|
master func kill():
|
|
|
|
health = 0
|
|
|
|
spawn()
|
|
|
|
|
|
|
|
func spawn():
|
|
|
|
health = 150
|
|
|
|
game.get_spawn_point().spawn(self)
|
|
|
|
|
2020-06-17 22:01:12 +02:00
|
|
|
func shoot():
|
2020-09-14 22:59:57 +02:00
|
|
|
|
|
|
|
var gun = find_node("Weapon")
|
|
|
|
|
|
|
|
gun.shoot()
|
|
|
|
|
2020-06-17 22:01:12 +02:00
|
|
|
var space_state = get_world().direct_space_state
|
2020-09-14 11:58:10 +02:00
|
|
|
var crosshair_pos = get_viewport().size / 2
|
|
|
|
|
|
|
|
print(OS.get_real_window_size()/2)
|
2020-06-17 22:01:12 +02:00
|
|
|
|
|
|
|
var from = $Camera.project_ray_origin(crosshair_pos)
|
|
|
|
var to = from + $Camera.project_ray_normal(crosshair_pos) * 1000
|
|
|
|
|
|
|
|
var result = space_state.intersect_ray(from, to)
|
|
|
|
|
|
|
|
if "collider" in result:
|
|
|
|
var hit = result.collider
|
|
|
|
print(hit)
|
|
|
|
if hit.has_method("on_hit"):
|
|
|
|
hit.rpc("on_hit")
|
2020-06-23 01:14:10 +02:00
|
|
|
else:
|
|
|
|
var effect = bulletHitEffect.instance()
|
|
|
|
effect.global_transform.origin = result.position
|
|
|
|
get_tree().root.call_deferred("add_child", effect)
|
|
|
|
|
2020-06-17 22:01:12 +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 01:32:44 +02:00
|
|
|
|
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
|
|
|
# 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-17 22:01:12 +02:00
|
|
|
if event.is_action_pressed("WeaponPrimary"):
|
|
|
|
shoot()
|
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
|
2020-06-19 00:11:03 +02:00
|
|
|
if name != String(get_tree().get_network_unique_id()):
|
2020-06-14 22:16:42 +02:00
|
|
|
debug.hide()
|
2020-06-17 22:01:12 +02:00
|
|
|
print(get_tree().get_network_unique_id())
|
|
|
|
print(name)
|
2020-06-14 00:29:58 +02:00
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
|
#func _process(delta):
|
|
|
|
# pass
|