2023-01-24 12:43:19 -07:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
|
|
|
|
|
|
|
var SPEED = 5.0
|
2023-03-08 22:34:11 -07:00
|
|
|
const JUMP_VELOCITY = 6
|
2023-01-24 12:43:19 -07:00
|
|
|
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
|
2023-01-27 12:47:13 -07:00
|
|
|
@export var Bullet = preload("res://objects/Bullet/Bullet.tscn")
|
2023-02-07 13:22:31 -07:00
|
|
|
@onready var neck := $CollisionShape3D/Neck
|
|
|
|
@onready var camera := $CollisionShape3D/Neck/Camera3D
|
2023-02-23 10:19:37 -07:00
|
|
|
@onready var teddyAuthority = get_multiplayer_authority()
|
2023-02-22 21:31:22 -07:00
|
|
|
var selfTeddy
|
2023-02-28 13:33:00 -07:00
|
|
|
var selfTeddyNode
|
2023-02-28 12:30:49 -07:00
|
|
|
var teddyCollider
|
2023-03-01 20:21:34 -07:00
|
|
|
var teddyName # Player name label
|
2023-02-22 21:31:22 -07:00
|
|
|
var teddyParent
|
2023-02-28 11:48:41 -07:00
|
|
|
var menuOpen = false
|
2023-02-28 12:30:49 -07:00
|
|
|
var ranRemovePlayer = false
|
2023-03-14 12:06:48 -06:00
|
|
|
var mousesense = Global.mouseSensitivity
|
2023-03-14 13:34:02 -06:00
|
|
|
var controllerAxisValue_x = 0
|
|
|
|
var controllerAxisValue_y = 0
|
2023-03-16 13:37:17 -06:00
|
|
|
var runnerDead = false
|
2023-03-18 22:08:51 -06:00
|
|
|
var menuScene
|
2023-01-26 12:17:54 -07:00
|
|
|
|
2023-01-26 13:23:13 -07:00
|
|
|
func _ready():
|
2023-02-23 10:19:37 -07:00
|
|
|
name = str(teddyAuthority)
|
2023-01-26 13:23:13 -07:00
|
|
|
Global.playingGame = true
|
2023-02-22 21:31:22 -07:00
|
|
|
selfTeddy = self.get_path()
|
2023-03-17 12:46:53 -06:00
|
|
|
Global.selfTeddy = selfTeddy
|
2023-02-28 13:33:00 -07:00
|
|
|
selfTeddyNode = get_node(selfTeddy)
|
2023-02-28 12:30:49 -07:00
|
|
|
teddyCollider = str(self.get_path()) + '/CollisionShape3D'
|
2023-03-01 20:21:34 -07:00
|
|
|
teddyName = str(self.get_path()) + '/nameLabel'
|
2023-03-02 12:52:49 -07:00
|
|
|
$nameLabel.set_text("")
|
2023-02-22 21:31:22 -07:00
|
|
|
teddyParent = get_node(selfTeddy).get_parent()
|
2023-03-16 21:42:32 -06:00
|
|
|
if Global.teddyAuthorityID == null:
|
|
|
|
Global.teddyAuthorityID = teddyAuthority
|
2023-03-09 13:26:09 -07:00
|
|
|
if is_multiplayer_authority():
|
|
|
|
$CollisionShape3D/Neck/Teddy.visible = false
|
2023-03-18 22:08:51 -06:00
|
|
|
var scene_trs = load("res://scenes/mainmenu.tscn")
|
|
|
|
menuScene = scene_trs.instantiate()
|
|
|
|
add_child(menuScene)
|
|
|
|
menuScene.visible = false
|
2023-01-26 13:23:13 -07:00
|
|
|
|
2023-01-24 12:43:19 -07:00
|
|
|
func _unhandled_input(event):
|
2023-03-09 13:26:09 -07:00
|
|
|
if is_multiplayer_authority():
|
|
|
|
Global.selfTeddy = selfTeddy
|
|
|
|
if event is InputEventMouseButton:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
elif event.is_action_pressed("ui_cancel"):
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
2023-03-14 12:06:48 -06:00
|
|
|
mousesense = Global.mouseSensitivity
|
2023-03-09 13:26:09 -07:00
|
|
|
if event is InputEventMouseMotion:
|
|
|
|
neck.rotate_y(-event.relative.x * mousesense)
|
|
|
|
camera.rotate_x(-event.relative.y * mousesense)
|
2023-03-14 12:06:48 -06:00
|
|
|
## FOR CONTROLLERS
|
2023-03-14 13:02:47 -06:00
|
|
|
if event is InputEventJoypadMotion:
|
|
|
|
var cam_dir = Input.is_action_pressed("cam_left") or Input.is_action_pressed("cam_right") or Input.is_action_pressed("cam_up") or Input.is_action_pressed("cam_down")
|
|
|
|
if cam_dir:
|
2023-03-14 13:34:02 -06:00
|
|
|
controllerAxisValue_x = -event.get_axis_value() * mousesense
|
|
|
|
controllerAxisValue_y = -event.get_axis_value() * mousesense
|
2023-03-14 12:06:48 -06:00
|
|
|
##################
|
|
|
|
|
|
|
|
# Clamps the camera
|
2023-03-09 13:26:09 -07:00
|
|
|
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-50), deg_to_rad(60))
|
2023-03-18 22:08:51 -06:00
|
|
|
if Input.is_action_just_pressed("menu"):
|
|
|
|
if menuScene.visible == false:
|
|
|
|
menuScene.visible = true
|
2023-03-17 12:11:47 -06:00
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
2023-03-18 22:08:51 -06:00
|
|
|
elif menuScene.visible == true:
|
|
|
|
menuScene.visible = false
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
2023-01-24 12:43:19 -07:00
|
|
|
|
|
|
|
func _physics_process(delta):
|
2023-02-22 21:31:22 -07:00
|
|
|
if is_multiplayer_authority():
|
|
|
|
# This variable gets set my the death scene, so we know the player needs to be respawned
|
|
|
|
if Global.playerPleaseRespawn == true:
|
2023-03-16 13:37:17 -06:00
|
|
|
get_node(teddyCollider).disabled = false
|
2023-03-16 22:59:05 -06:00
|
|
|
var ramMax = Global.spawnCoords_x.size() - 1
|
|
|
|
var ramnum = RandomNumberGenerator.new().randi_range(0, ramMax)
|
|
|
|
print("ramnum: ", ramnum)
|
|
|
|
print("ramMax: ", ramMax)
|
|
|
|
position.x = Global.spawnCoords_x[ramnum] #Set player X
|
|
|
|
position.y = Global.spawnCoords_y[ramnum] #Set player Y
|
|
|
|
position.z = Global.spawnCoords_z[ramnum] #Set player Z
|
2023-02-28 12:41:10 -07:00
|
|
|
await get_tree().create_timer(0.5).timeout
|
2023-02-22 21:31:22 -07:00
|
|
|
Global.reset_variables()
|
2023-02-28 12:30:49 -07:00
|
|
|
ranRemovePlayer = false
|
|
|
|
rpc("teddy_dead_nomore")
|
2023-02-22 21:31:22 -07:00
|
|
|
|
|
|
|
## If player falls off the map, kill them!
|
|
|
|
if velocity.y < Global.playerYDeath:
|
|
|
|
Global.playerHealth = 0
|
|
|
|
if Global.playerHealth <= 0:
|
|
|
|
Global.player_dead()
|
|
|
|
|
2023-02-28 12:30:49 -07:00
|
|
|
if ranRemovePlayer == false:
|
|
|
|
if Global.playerAlive == false:
|
|
|
|
ranRemovePlayer = true
|
2023-03-16 13:37:17 -06:00
|
|
|
get_node(teddyCollider).disabled = true
|
2023-02-28 12:30:49 -07:00
|
|
|
rpc("remove_dead_teddy")
|
2023-02-22 21:31:22 -07:00
|
|
|
|
|
|
|
if Global.playerAlive:
|
2023-03-16 13:37:17 -06:00
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity.y -= gravity * delta
|
|
|
|
# Handle Jump.
|
2023-03-05 22:08:14 -07:00
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
2023-02-22 21:31:22 -07:00
|
|
|
velocity.y = JUMP_VELOCITY
|
2023-03-14 13:34:02 -06:00
|
|
|
var cam_dir = Input.is_action_pressed("cam_left") or Input.is_action_pressed("cam_right") or Input.is_action_pressed("cam_up") or Input.is_action_pressed("cam_down")
|
|
|
|
if cam_dir:
|
|
|
|
if Input.is_action_pressed("cam_left"):
|
|
|
|
controllerAxisValue_x += 1 * mousesense
|
|
|
|
if Input.is_action_pressed("cam_right"):
|
|
|
|
controllerAxisValue_x -= 1 * mousesense
|
|
|
|
if Input.is_action_pressed("cam_up"):
|
|
|
|
controllerAxisValue_y += 1 * mousesense
|
|
|
|
if Input.is_action_pressed("cam_down"):
|
|
|
|
controllerAxisValue_y -= 1 * mousesense
|
|
|
|
neck.rotate_y(controllerAxisValue_x)
|
|
|
|
camera.rotate_x(controllerAxisValue_y)
|
|
|
|
else:
|
|
|
|
controllerAxisValue_x = 0
|
|
|
|
controllerAxisValue_y = 0
|
2023-02-22 21:31:22 -07:00
|
|
|
var input_dir = Input.get_vector("player_left", "player_right", "player_forward", "player_backward")
|
|
|
|
var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
2023-03-09 13:26:09 -07:00
|
|
|
if input_dir:
|
2023-03-10 23:24:39 -07:00
|
|
|
if not Input.is_action_pressed("sprint"):
|
|
|
|
rpc("teddy_play_anim", "walk")
|
|
|
|
else:
|
|
|
|
rpc("teddy_play_anim", "idle")
|
2023-02-22 21:31:22 -07:00
|
|
|
if Input.is_action_pressed("sprint"):
|
|
|
|
if Global.fatigue > 5:
|
|
|
|
SPEED = 10.0
|
|
|
|
if Global.fatigue < 5:
|
|
|
|
SPEED = 2.0
|
|
|
|
if Input.is_action_pressed("sprint"):
|
2023-03-10 23:24:39 -07:00
|
|
|
rpc("teddy_play_anim", "run")
|
2023-02-22 21:31:22 -07:00
|
|
|
elif Global.fatigue > 5:
|
|
|
|
SPEED = 5.0
|
|
|
|
if Input.is_action_just_released("sprint"):
|
|
|
|
SPEED = 5.0
|
|
|
|
if direction:
|
|
|
|
velocity.x = direction.x * SPEED
|
|
|
|
velocity.z = direction.z * SPEED
|
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
velocity.z = move_toward(velocity.z, 0, SPEED)
|
|
|
|
if Global.playerDead:
|
|
|
|
velocity.x = 0
|
|
|
|
velocity.z = 0
|
2023-03-19 01:35:35 -06:00
|
|
|
if Input.is_action_just_pressed("shoot") and Global.roundInSession and menuScene.visible == false:
|
2023-02-28 11:48:41 -07:00
|
|
|
if menuOpen == false:
|
2023-02-22 21:31:22 -07:00
|
|
|
if Global.multiplayerCurrent == true:
|
2023-02-24 23:49:49 -07:00
|
|
|
rpc("shoot_bullet")
|
|
|
|
shoot_bullet()
|
2023-02-22 21:31:22 -07:00
|
|
|
else:
|
2023-02-24 23:49:49 -07:00
|
|
|
shoot_bullet()
|
2023-02-22 21:31:22 -07:00
|
|
|
if Input.is_action_just_pressed("console"):
|
|
|
|
var scene_trs = load("res://scenes/console.tscn")
|
|
|
|
var scene = scene_trs.instantiate()
|
|
|
|
if not Global.consoleOpen:
|
|
|
|
Global.consoleOpen = true
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
add_child(scene)
|
|
|
|
elif Global.consoleOpen:
|
|
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
Global.consoleOpen = false # We remove the scene in console.gd
|
2023-03-05 22:08:14 -07:00
|
|
|
if Global.roundInSession == false:
|
|
|
|
if teddyAuthority == 1:
|
2023-03-16 21:42:32 -06:00
|
|
|
if Global.HUDStartLabelText == "":
|
|
|
|
Global.HUDStartLabelText = "PRESS ENTER/RETURN TO START THE ROUND"
|
2023-03-05 22:08:14 -07:00
|
|
|
if Input.is_action_just_pressed("start_game"):
|
|
|
|
if teddyAuthority == 1:
|
|
|
|
Global.roundInSession = true
|
|
|
|
rpc("round_start")
|
2023-03-09 13:26:09 -07:00
|
|
|
rpc("remote_set_position", global_position, $CollisionShape3D/Neck/Teddy.global_rotation.y, $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_position, $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_rotation)
|
2023-03-01 20:21:34 -07:00
|
|
|
rpc("set_teddy_name", teddyName, Global.playerName)
|
2023-02-22 21:31:22 -07:00
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
@rpc("any_peer", "unreliable")
|
2023-03-09 13:26:09 -07:00
|
|
|
func remote_set_position(authority_position, teddy_rotation, bulletgenerator_position, bulletgenerator_rotation):
|
2023-02-22 21:31:22 -07:00
|
|
|
global_position = authority_position
|
2023-03-09 13:26:09 -07:00
|
|
|
$CollisionShape3D/Neck/Teddy.rotation.y = teddy_rotation
|
2023-02-22 21:31:22 -07:00
|
|
|
$CollisionShape3D/Neck/Camera3D/BulletGenerator.global_position = bulletgenerator_position
|
|
|
|
$CollisionShape3D/Neck/Camera3D/BulletGenerator.global_rotation = bulletgenerator_rotation
|
|
|
|
|
2023-03-10 23:24:39 -07:00
|
|
|
@rpc("any_peer", "unreliable")
|
|
|
|
func teddy_play_anim(animation):
|
|
|
|
$CollisionShape3D/Neck/Teddy/AnimationPlayer.play(animation)
|
2023-03-09 13:26:09 -07:00
|
|
|
|
2023-03-01 20:27:07 -07:00
|
|
|
@rpc("any_peer", "unreliable")
|
2023-02-24 23:49:49 -07:00
|
|
|
func shoot_bullet():
|
2023-02-22 21:31:22 -07:00
|
|
|
var b = Bullet.instantiate()
|
2023-02-23 10:19:37 -07:00
|
|
|
b.set_multiplayer_authority(teddyAuthority)
|
2023-02-22 21:31:22 -07:00
|
|
|
teddyParent.add_child(b)
|
|
|
|
b.transform = $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_transform
|
|
|
|
b.velocity = -b.global_transform.basis.z * b.muzzle_velocity
|
2023-02-24 23:49:49 -07:00
|
|
|
$sound.stream = load("res://sounds/pistol_shoot.wav")
|
|
|
|
$sound.play()
|
2023-02-28 12:30:49 -07:00
|
|
|
|
|
|
|
@rpc("any_peer")
|
|
|
|
func remove_dead_teddy():
|
|
|
|
get_node(selfTeddy).visible = false
|
|
|
|
get_node(teddyCollider).disabled = true
|
|
|
|
|
|
|
|
@rpc("any_peer")
|
|
|
|
func teddy_dead_nomore():
|
|
|
|
get_node(selfTeddy).visible = true
|
|
|
|
get_node(teddyCollider).disabled = false
|
2023-03-01 20:21:34 -07:00
|
|
|
|
2023-03-01 20:27:07 -07:00
|
|
|
@rpc("any_peer", "unreliable")
|
2023-03-01 20:21:34 -07:00
|
|
|
func set_teddy_name(node, name):
|
|
|
|
get_node(node).set_text(name)
|
2023-03-05 22:08:14 -07:00
|
|
|
|
|
|
|
@rpc("authority", "reliable", "call_remote")
|
|
|
|
func round_start():
|
|
|
|
Global.roundInSession = true
|