253 lines
9.2 KiB
GDScript
253 lines
9.2 KiB
GDScript
extends CharacterBody3D
|
|
|
|
|
|
@export var SPEED = 5.0
|
|
@export var JUMP_VELOCITY = 6
|
|
|
|
@onready var speed = SPEED
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
@export var Bullet = preload("res://objects/Bullet/Bullet.tscn")
|
|
@onready var neck := $CollisionShape3D/Neck
|
|
@onready var camera := $CollisionShape3D/Neck/Camera3D
|
|
@onready var teddyAuthority = get_multiplayer_authority()
|
|
var selfTeddy
|
|
var selfTeddyNode
|
|
var teddyCollider
|
|
var teddyName # Player name label
|
|
var teddyParent
|
|
var menuOpen = false
|
|
var ranRemovePlayer = false
|
|
var mousesense = Global.mouseSensitivity
|
|
var controllerAxisValue_x = 0
|
|
var controllerAxisValue_y = 0
|
|
var runnerDead = false
|
|
var menuScene
|
|
|
|
func _ready():
|
|
name = str(teddyAuthority)
|
|
Global.playingGame = true
|
|
selfTeddy = self.get_path()
|
|
Global.selfTeddy = selfTeddy
|
|
selfTeddyNode = get_node(selfTeddy)
|
|
teddyCollider = str(self.get_path()) + '/CollisionShape3D'
|
|
teddyName = str(self.get_path()) + '/nameLabel'
|
|
$nameLabel.set_text("")
|
|
teddyParent = get_node(selfTeddy).get_parent()
|
|
if Global.teddyAuthorityID == null:
|
|
Global.teddyAuthorityID = teddyAuthority
|
|
if is_multiplayer_authority():
|
|
$CollisionShape3D/Neck/Teddy.visible = false
|
|
$CollisionShape3D/Neck/Camera3D/Gun.visible = true
|
|
if not Global.addedMenuScene:
|
|
Global.addedMenuScene = true
|
|
var scene_trs = load("res://scenes/mainmenu.tscn")
|
|
menuScene = scene_trs.instantiate()
|
|
selfTeddyNode.add_child(menuScene)
|
|
menuScene.visible = false
|
|
|
|
func _unhandled_input(event):
|
|
if not Global.playerDisable:
|
|
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:
|
|
mousesense = Global.mouseSensitivity
|
|
if event is InputEventMouseMotion:
|
|
neck.rotate_y(-event.relative.x * mousesense)
|
|
camera.rotate_x(-event.relative.y * mousesense)
|
|
## FOR CONTROLLERS
|
|
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:
|
|
controllerAxisValue_x = -event.get_axis_value() * mousesense
|
|
controllerAxisValue_y = -event.get_axis_value() * mousesense
|
|
##################
|
|
|
|
# Clamps the camera
|
|
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(90))
|
|
if Input.is_action_just_pressed("menu"):
|
|
if menuScene.visible == false:
|
|
menuScene.visible = true
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
elif menuScene.visible == true:
|
|
menuScene.visible = false
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
|
|
func _physics_process(delta):
|
|
if Global.kickAllPlayers:
|
|
Global.kickAllPlayers = false
|
|
rpc("kick_all_players")
|
|
if not Global.playerDisable:
|
|
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:
|
|
if not Global.respawnLocationChosen:
|
|
Global.respawnLocationChosen = true
|
|
get_node(teddyCollider).disabled = false
|
|
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
|
|
velocity = Vector3.ZERO
|
|
await get_tree().create_timer(0.5).timeout
|
|
Global.reset_variables()
|
|
ranRemovePlayer = false
|
|
rpc("teddy_dead_nomore")
|
|
|
|
## If player falls off the map, kill them!
|
|
if velocity.y < Global.playerYDeath:
|
|
Global.playerHealth = 0
|
|
if Global.playerHealth <= 0:
|
|
Global.player_dead()
|
|
|
|
if ranRemovePlayer == false:
|
|
if Global.playerAlive == false:
|
|
ranRemovePlayer = true
|
|
get_node(teddyCollider).disabled = true
|
|
rpc("remove_dead_teddy")
|
|
|
|
if Global.playerAlive:
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity.y -= gravity * delta
|
|
# Handle Jump.
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
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
|
|
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()
|
|
if input_dir:
|
|
if not Input.is_action_pressed("sprint"):
|
|
rpc("teddy_play_anim", "walk")
|
|
else:
|
|
rpc("teddy_play_anim", "idle")
|
|
if Input.is_action_pressed("sprint"):
|
|
if Global.fatigue > 5:
|
|
speed = SPEED * 2
|
|
if Global.fatigue < 5:
|
|
speed = SPEED
|
|
if Input.is_action_pressed("sprint"):
|
|
rpc("teddy_play_anim", "run")
|
|
elif Global.fatigue > 5:
|
|
speed = SPEED
|
|
if Input.is_action_just_released("sprint"):
|
|
speed = SPEED
|
|
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
|
|
if Input.is_action_just_pressed("shoot") and Global.roundInSession and menuScene.visible == false:
|
|
if menuOpen == false:
|
|
if Global.multiplayerCurrent == true:
|
|
if Global.gamemode == "Runner" and Global.iAmSpecial:
|
|
pass
|
|
else:
|
|
rpc("shoot_bullet")
|
|
shoot_bullet()
|
|
else:
|
|
shoot_bullet()
|
|
if Global.consoleEnabled:
|
|
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
|
|
if Global.roundInSession == false:
|
|
if teddyAuthority == 1:
|
|
if Global.HUDStartLabelText == "":
|
|
Global.HUDStartLabelText = "PRESS ENTER/RETURN TO START THE ROUND"
|
|
if Input.is_action_just_pressed("start_game"):
|
|
if teddyAuthority == 1:
|
|
Global.roundInSession = true
|
|
rpc("round_start")
|
|
if Global.spawnCotton == true:
|
|
Global.spawnCotton = false
|
|
rpc("spawn_cotton")
|
|
spawn_cotton()
|
|
rpc("remote_set_position", global_position, $CollisionShape3D/Neck/Teddy.global_rotation.y, $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_position, $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_rotation)
|
|
rpc("set_teddy_name", teddyName, Global.playerName)
|
|
move_and_slide()
|
|
|
|
@rpc("any_peer", "unreliable")
|
|
func remote_set_position(authority_position, teddy_rotation, bulletgenerator_position, bulletgenerator_rotation):
|
|
global_position = authority_position
|
|
$CollisionShape3D/Neck/Teddy.rotation.y = teddy_rotation
|
|
$CollisionShape3D/Neck/Camera3D/BulletGenerator.global_position = bulletgenerator_position
|
|
$CollisionShape3D/Neck/Camera3D/BulletGenerator.global_rotation = bulletgenerator_rotation
|
|
|
|
@rpc("any_peer", "unreliable")
|
|
func teddy_play_anim(animation):
|
|
$CollisionShape3D/Neck/Teddy/AnimationPlayer.play(animation)
|
|
|
|
@rpc("any_peer", "unreliable")
|
|
func shoot_bullet():
|
|
var b = Bullet.instantiate()
|
|
b.set_multiplayer_authority(teddyAuthority)
|
|
teddyParent.add_child(b)
|
|
b.transform = $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_transform
|
|
b.velocity = -b.global_transform.basis.z * b.muzzle_velocity
|
|
$sound.stream = load("res://sounds/pistol_shoot.wav")
|
|
$sound.play()
|
|
|
|
@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
|
|
|
|
@rpc("any_peer", "unreliable")
|
|
func set_teddy_name(node, name):
|
|
get_node(node).set_text(name)
|
|
|
|
@rpc("authority", "reliable", "call_remote")
|
|
func round_start():
|
|
Global.roundInSession = true
|
|
|
|
@rpc("any_peer", "unreliable")
|
|
func spawn_cotton():
|
|
print("SPAWNED COTTON (HOPEFULLY)")
|
|
$CPUParticles3D.emitting = true
|
|
await get_tree().create_timer(0.5).timeout
|
|
$CPUParticles3D.emitting = false
|
|
|
|
@rpc("any_peer", "reliable")
|
|
func kick_all_players():
|
|
Global.playingGame = false
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
get_tree().change_scene_to_file("res://scenes/mainmenu-background.tscn")
|