project_teddy/characters/teddy/Teddy.gd

164 lines
5.5 KiB
GDScript3
Raw Normal View History

2023-01-24 12:43:19 -07:00
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")
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
@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-01-26 12:17:54 -07:00
var globalscene
2023-02-28 12:30:49 -07:00
var ranRemovePlayer = false
2023-01-26 12:17:54 -07:00
2023-01-26 13:23:13 -07:00
func _ready():
name = str(teddyAuthority)
2023-01-26 13:23:13 -07:00
Global.playingGame = true
Global.selfTeddy = selfTeddy
2023-02-22 21:31:22 -07:00
selfTeddy = self.get_path()
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'
#$nameLabel.set_text(str(Global.playerName))
2023-02-22 21:31:22 -07:00
teddyParent = get_node(selfTeddy).get_parent()
2023-02-25 14:31:15 -07:00
Global.teddyAuthorityID = teddyAuthority
2023-01-26 13:23:13 -07:00
2023-01-24 12:43:19 -07:00
func _unhandled_input(event):
2023-02-22 21:31:22 -07:00
Global.selfTeddy = selfTeddy
2023-01-24 12:43:19 -07:00
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:
2023-01-26 12:17:54 -07:00
var mousesense = Global.mouseSensitivity
neck.rotate_y(-event.relative.x * mousesense)
camera.rotate_x(-event.relative.y * mousesense)
2023-01-27 12:47:13 -07:00
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-50), deg_to_rad(60))
2023-02-28 11:48:41 -07:00
if menuOpen == false:
2023-01-26 12:17:54 -07:00
if Input.is_action_just_pressed("menu"):
2023-02-28 11:48:41 -07:00
menuOpen = true
2023-01-26 12:17:54 -07:00
var scene_trs = load("res://scenes/mainmenu.tscn")
var scene = scene_trs.instantiate()
globalscene = scene
add_child(scene)
2023-02-28 11:48:41 -07:00
elif menuOpen == true:
2023-01-26 12:17:54 -07:00
if Input.is_action_just_pressed("menu"):
2023-02-28 11:48:41 -07:00
menuOpen = false
2023-01-26 12:17:54 -07:00
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if globalscene:
globalscene.queue_free()
2023-01-24 12:43:19 -07:00
func _physics_process(delta):
2023-02-22 21:31:22 -07:00
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
2023-02-22 21:31:22 -07:00
position.z = 0 #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
#get_node(teddyCollider).disabled = true
rpc("remove_dead_teddy")
2023-02-22 21:31:22 -07:00
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"):
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
rpc("remote_set_position", global_position, $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")
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
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()
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)