project_teddy/characters/teddy/Teddy.gd

241 lines
8.8 KiB
GDScript3
Raw Normal View History

2023-01-24 12:43:19 -07:00
extends CharacterBody3D
2023-03-25 15:36:02 -06:00
@export var SPEED = 5.0
@export var JUMP_VELOCITY = 6
2023-01-24 12:43:19 -07:00
2023-03-25 15:36:02 -06:00
@onready var speed = SPEED
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
@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():
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-05-04 13:35:20 -06:00
$CollisionShape3D/Neck/Camera3D/Gun.visible = true
2023-04-15 13:02:34 -06:00
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
2023-01-26 13:23:13 -07:00
2023-01-24 12:43:19 -07:00
func _unhandled_input(event):
if not Global.playerDisable:
if is_multiplayer_authority():
Global.selfTeddy = selfTeddy
if event is InputEventMouseButton:
2023-04-15 13:02:34 -06:00
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)
2023-01-24 12:43:19 -07:00
func _physics_process(delta):
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:
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
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:
2023-05-15 12:42:36 -06:00
if Global.gamemode == "Runner" and Global.iAmSpecial:
pass
else:
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
if Global.roundInSession == false:
2023-03-05 22:08:14 -07:00
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()
2023-02-22 21:31:22 -07:00
@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
@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()
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
2023-04-06 13:27:41 -06:00
@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