extends CharacterBody3D var SPEED = 5.0 const JUMP_VELOCITY = 4.5 # 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 globalscene var ranRemovePlayer = false func _ready(): name = str(teddyAuthority) Global.playingGame = true Global.selfTeddy = selfTeddy selfTeddy = self.get_path() selfTeddyNode = get_node(selfTeddy) teddyCollider = str(self.get_path()) + '/CollisionShape3D' teddyName = str(self.get_path()) + '/nameLabel' #$nameLabel.set_text(str(Global.playerName)) teddyParent = get_node(selfTeddy).get_parent() Global.teddyAuthorityID = teddyAuthority func _unhandled_input(event): 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: if event is InputEventMouseMotion: var mousesense = Global.mouseSensitivity neck.rotate_y(-event.relative.x * mousesense) camera.rotate_x(-event.relative.y * mousesense) camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-50), deg_to_rad(60)) if menuOpen == false: if Input.is_action_just_pressed("menu"): menuOpen = true var scene_trs = load("res://scenes/mainmenu.tscn") var scene = scene_trs.instantiate() globalscene = scene add_child(scene) elif menuOpen == true: if Input.is_action_just_pressed("menu"): menuOpen = false Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) if globalscene: globalscene.queue_free() func _physics_process(delta): if is_multiplayer_authority(): # Add the gravity. if not is_on_floor(): velocity.y -= gravity * delta # This variable gets set my the death scene, so we know the player needs to be respawned if Global.playerPleaseRespawn == true: position.x = 0 #Set player X position.y = 10 #Set player Y position.z = 0 #Set player Z 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: # Handle Jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY 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.is_action_pressed("sprint"): if Global.fatigue > 5: SPEED = 10.0 if Global.fatigue < 5: SPEED = 2.0 if Input.is_action_pressed("sprint"): pass 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 if Input.is_action_just_pressed("shoot"): if menuOpen == false: if Global.multiplayerCurrent == true: rpc("shoot_bullet") shoot_bullet() else: shoot_bullet() 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 rpc("remote_set_position", global_position, $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, bulletgenerator_position, bulletgenerator_rotation): global_position = authority_position $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_position = bulletgenerator_position $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_rotation = bulletgenerator_rotation @rpc("any_peer", "reliable") 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") func set_teddy_name(node, name): get_node(node).set_text(name)