cs-os/scripts/Player.gd

180 lines
5.3 KiB
GDScript

extends CharacterBody3D
const SENSETIVITY = 0.009;
enum Class_type {
Spectator = 0,
Astra = 1,
BlackArch = 2,
Kali = 3,
Debian = 4,
Arch = 5,
Windows11 = -1,
WindowsServer = -2,
MacOS = -3,
WindowsXP = -4,
Windows10 = -5
}
var Weapons = GameData.Weapons
var properties = {
HP = 100,
AP = 100,
HACK = 1.0,
class_type = 0,
is_playable = false,
internal_id = 0,
nickname = "Unnamed",
ready = false,
current_weapon = 0,
last_shot = 0
}
var speed = 0
var walk = 0
var run = 0
var jump = 0
var gravity = 0
var game_settings
var client_settings
var current_weapon = Weapons["knife"]
var can_shoot = true
var time_since_last_shot = 0
@onready var head = $Head
@onready var camera = $Head/Camera
@onready var playerCharacterBody = $"."
var HUD
var healthBar
func set_properties(props):
properties = props
$"..".name = "player" + str(properties["internal_id"])
func change_weapon(new_weapon):
var weapons = $"Head/Camera/Hand".get_children()
for weapon in weapons:
weapon.visible = false
weapons[new_weapon].visible = true
func _ready():
$"Head/Nickname".text = properties["nickname"]
change_weapon(properties["current_weapon"])
if (!properties["is_playable"]): return
camera.make_current()
var hud = load("res://scenes/hud.tscn").instantiate()
add_child(hud)
HUD = $"HUD"
healthBar = HUD.get_node("HealthBar")
playerCharacterBody.up_direction = Vector3.UP;
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func try_shoot():
var current_weapon = Weapons.find_key(properties["current_weapon"])
var current_weapon_settings = game_settings["weapons"][current_weapon]
if Input.is_action_pressed("shoot") and current_weapon_settings["fireRate"] >= 0.5 and not Input.is_action_just_pressed("shoot"):
return
time_since_last_shot = (Time.get_ticks_msec() - properties["last_shot"]) / 1000.
print("TSLS: " + str(time_since_last_shot) + " fireRate: " + str(current_weapon_settings["fireRate"]))
if (time_since_last_shot > current_weapon_settings["fireRate"]):
Networking.shot.rpc_id(1, multiplayer.get_unique_id())
properties["last_shot"] = Time.get_ticks_msec()
print("I am shooting")
func _unhandled_input(event):
if (!properties["is_playable"]): return
if Input.is_action_pressed("MWU"):
properties["current_weapon"] = wrap(properties["current_weapon"] + 1, 0, Weapons.size())
Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"])
if Input.is_action_pressed("MWD"):
properties["current_weapon"] = wrap(properties["current_weapon"] - 1, 0, Weapons.size())
Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"])
change_weapon(properties["current_weapon"])
#if(event.is_action_pressed("shoot")):
#print(event.position)
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSETIVITY)
camera.rotate_x(-event.relative.y * SENSETIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-50), deg_to_rad(60))
func _physics_process(delta):
if (!properties["is_playable"]): return
if (game_settings == null): return
if (multiplayer.multiplayer_peer == null):
properties["is_playable"] = false
healthBar.value = properties["HP"]
if Input.is_action_pressed("shoot"):
try_shoot()
var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if not is_on_floor():
velocity.y -= 4 * gravity * delta
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = jump
if Input.is_action_pressed("run"):
speed = run
else:
speed = walk
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 4)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 4)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta)
velocity.z = lerp(velocity.z, direction.z * speed, delta)
move_and_slide()
if (!multiplayer.is_server()):
Networking.sync_client.rpc_id(1, multiplayer.get_unique_id(), $".".position, {"y": head.rotation.y, "x": camera.rotation.x})
#print("I am " + $"..".name +", belonging of " + str(multiplayer.get_unique_id()) + ", with internal ID: " + str(properties["internal_id"]))
@rpc ("authority", "call_remote", "unreliable")
func sync_puppet(i_id, p, rot):
if(!properties["ready"]): return
if(properties["is_playable"]): return
if (int(i_id) == properties["internal_id"]):
playerCharacterBody.position = p
head.rotation.y = rot.y
camera.rotation.x = rot.x
@rpc ("authority", "call_remote", "reliable")
func set_hp(hp):
print("Got new hp" + str(hp))
if (!properties["is_playable"]): return
properties["HP"] = hp
healthBar.value = properties["HP"]
@rpc ("authority", "call_remote", "reliable")
func set_game_settings(s):
if(!properties["is_playable"]): return
print("Got settings from server: " + str(s))
game_settings = s
gravity = game_settings["moving"]["gravity"]
walk = game_settings["moving"]["walk"]
run = game_settings["moving"]["run"]
jump = game_settings["moving"]["jump"]
@rpc ("authority", "call_remote", "reliable")
func teleport(pos):
$".".position = pos
print("Teleporing to " + str(pos))
@rpc ("authority", "call_remote", "reliable")
func change_weapon_puppet(i_id, new_weapon):
if (int(i_id) == properties["internal_id"]):
change_weapon(new_weapon)