cs-os/scripts/Player.gd

286 lines
9.7 KiB
GDScript3
Raw Normal View History

2024-02-07 23:25:08 +03:00
extends CharacterBody3D
const SENSETIVITY = 0.009;
2024-02-08 21:16:31 +03:00
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
2024-02-08 21:16:31 +03:00
var properties = {
HP = 100,
AP = 100,
HACK = 1.0,
class_type = 0,
is_playable = false,
2024-02-15 15:14:21 +03:00
internal_id = 0,
2024-02-15 18:12:48 +03:00
nickname = "Unnamed",
ready = false,
current_weapon = null, #game_settings["weapons"]["knife"].duplicate(),
current_weapon_number = 0,
2024-02-21 02:23:02 +03:00
last_shot = 0,
2024-02-21 04:37:40 +03:00
reloading = false,
offline_mode = false,
position = Vector3(0, 5, 0)
2024-02-08 21:16:31 +03:00
}
2024-02-21 04:37:40 +03:00
var speed = 12
var walk = 12
var run = 24
var jump = 16
var gravity = 9.8
2024-02-28 09:23:20 +03:00
var acceleration = 2.5
var inertia = 20
2024-03-02 03:46:17 +03:00
var climbing = false
2024-02-21 04:37:40 +03:00
var game_settings
var client_settings
2024-02-18 02:42:31 +03:00
var can_shoot = true
var time_since_last_shot = 0
2024-02-29 23:13:30 +03:00
2024-02-07 23:25:08 +03:00
@onready var head = $Head
@onready var camera: Camera3D = $Head/Camera
2024-02-07 23:25:08 +03:00
@onready var playerCharacterBody = $"."
2024-02-29 23:13:30 +03:00
2024-02-17 01:57:08 +03:00
var HUD
2024-02-29 23:13:30 +03:00
var healthLabel
var armorLabel
var ammoLabel
var magazineLabel
2024-02-21 03:22:33 +03:00
var statusLabel
func set_properties(props):
properties = props
$"Head/Nickname".text = properties["nickname"]
name = "player" + str(properties["internal_id"])
position = properties["position"]
2024-02-21 02:23:02 +03:00
if (game_settings != null):
change_weapon(properties["current_weapon"]["number"])
func set_property(key, value):
properties[key] = value
update_state()
func update_hud():
2024-02-29 23:13:30 +03:00
healthLabel.text = str(properties["HP"])
armorLabel.text = str(properties["AP"])
ammoLabel.text = str(properties["current_weapon"]["ammo"])
magazineLabel.text = str(properties["current_weapon"]["magazine"])
2024-02-21 03:22:33 +03:00
if (properties["reloading"]):
statusLabel.text = "Reloading..."
else:
statusLabel.text = "Reloaded"
func change_weapon(new_weapon_number):
2024-02-21 02:23:02 +03:00
properties["reloading"] = false
var weapons = $"Head/Camera/Hand".get_children()
for weapon in weapons:
weapon.visible = false
weapons[new_weapon_number].visible = true
func find_weapon_by_number(number):
var found_weapon
for weapon in game_settings["weapons"].keys():
if (game_settings["weapons"][weapon]["number"] == number):
found_weapon = game_settings["weapons"][weapon].duplicate()
break
return found_weapon
2024-02-07 23:25:08 +03:00
2024-02-21 03:22:33 +03:00
func find_current_weapon_by_number(number):
var found_weapon
for weapon in Weapons.keys():
if (Weapons[weapon]["number"] == number):
found_weapon = Weapons[weapon]
break
return found_weapon
func update_state():
camera.set_current(false)
2024-02-15 15:14:21 +03:00
$"Head/Nickname".text = properties["nickname"]
2024-02-08 21:16:31 +03:00
if (!properties["is_playable"]): return
camera.set_current(true)
if (HUD == null):
var hud = load("res://scenes/HUD/hud.tscn").instantiate()
add_child(hud)
2024-02-17 01:57:08 +03:00
HUD = $"HUD"
2024-02-29 23:13:30 +03:00
var healthAndAmmoDisplay = HUD.get_node("health_and_ammo_display")
healthLabel = healthAndAmmoDisplay.get_node("healthdisplay/text")
armorLabel = healthAndAmmoDisplay.get_node("armordisplay/text")
ammoLabel = healthAndAmmoDisplay.get_node("ammodisplay/text")
magazineLabel = healthAndAmmoDisplay.get_node("magazinedisplay/text")
2024-02-21 03:22:33 +03:00
statusLabel = HUD.get_node("Status")
2024-02-07 23:25:08 +03:00
playerCharacterBody.up_direction = Vector3.UP;
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
2024-02-15 15:14:21 +03:00
func _ready():
update_state()
func update_weapon_raycast():
var current_weapon_name = Weapons.find_key(properties["current_weapon"])
if(current_weapon_name == null): current_weapon_name = "knife"
var raycast:RayCast3D = get_node("Head/Camera/viewRaycast")
var weapon_raycast:RayCast3D = get_node("Head/Camera/Hand/" + str(current_weapon_name) + "/raycast")
weapon_raycast.target_position = weapon_raycast.to_local(raycast.to_global(raycast.target_position) - weapon_raycast.position)
2024-02-07 23:25:08 +03:00
2024-02-18 02:42:31 +03:00
func try_shoot():
var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"]))
2024-02-18 02:42:31 +03:00
var current_weapon_settings = game_settings["weapons"][current_weapon]
properties["reloading"] = false
if (properties["current_weapon"]["magazine"] == 0):
return
2024-02-18 02:42:31 +03:00
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.
if (time_since_last_shot > current_weapon_settings["fireRate"]):
2024-02-21 04:37:40 +03:00
if (!properties["offline_mode"]): Networking.shot.rpc_id(1, multiplayer.get_unique_id())
if (properties["current_weapon"]["magazine"] > 0):
properties["current_weapon"]["magazine"] -= 1
2024-02-18 02:42:31 +03:00
properties["last_shot"] = Time.get_ticks_msec()
2024-02-21 03:22:33 +03:00
func reload():
var current_weapon_instance = properties["current_weapon"]
var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(current_weapon_instance["number"]))
var current_weapon_settings = game_settings["weapons"][current_weapon]
var to_reload = current_weapon_settings["magazine"] - current_weapon_instance["magazine"]
if (properties["reloading"]):
if (to_reload <= current_weapon_instance["ammo"]):
current_weapon_instance["magazine"] += to_reload
current_weapon_instance["ammo"] -= to_reload
else:
current_weapon_instance["magazine"] += current_weapon_instance["ammo"]
current_weapon_instance["ammo"] = 0
properties["reloading"] = false
update_hud()
2024-02-18 02:42:31 +03:00
2024-02-07 23:25:08 +03:00
func _unhandled_input(event):
2024-02-08 21:16:31 +03:00
if (!properties["is_playable"]): return
if Input.is_action_pressed("MWU"):
var weapon_number = wrap(properties["current_weapon"]["number"] + 1, 0, game_settings["weapons"].size())
2024-02-21 03:22:33 +03:00
properties["current_weapon"] = find_current_weapon_by_number(weapon_number)
2024-02-21 04:37:40 +03:00
if (!properties["offline_mode"]): Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
2024-02-21 03:22:33 +03:00
change_weapon(properties["current_weapon"]["number"])
if Input.is_action_pressed("MWD"):
var weapon_number = wrap(properties["current_weapon"]["number"] - 1, 0, game_settings["weapons"].size())
2024-02-21 03:22:33 +03:00
properties["current_weapon"] = find_current_weapon_by_number(weapon_number)
2024-02-21 04:37:40 +03:00
if (!properties["offline_mode"]): Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
2024-02-21 03:22:33 +03:00
change_weapon(properties["current_weapon"]["number"])
2024-02-17 01:57:08 +03:00
2024-02-07 23:25:08 +03:00
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))
2024-02-14 22:18:05 +03:00
2024-02-07 23:25:08 +03:00
func _physics_process(delta):
if (!properties["is_playable"]): return
if (game_settings == null): return
2024-02-14 22:18:05 +03:00
if (multiplayer.multiplayer_peer == null):
properties["is_playable"] = false
2024-02-17 01:57:08 +03:00
2024-02-18 02:42:31 +03:00
if Input.is_action_pressed("shoot"):
try_shoot()
update_hud()
update_weapon_raycast()
if Input.is_action_just_pressed("reload"):
var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"]))
2024-02-21 04:37:40 +03:00
if (!properties["offline_mode"]): Networking.client_reloading.rpc_id(1, multiplayer.get_unique_id())
2024-02-21 03:22:33 +03:00
properties["reloading"] = true
get_tree().create_timer(game_settings["weapons"][current_weapon]["reload"]).connect("timeout", reload)
2024-02-07 23:25:08 +03:00
var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
2024-02-26 23:06:15 +03:00
if direction:
2024-02-28 09:23:20 +03:00
velocity.x = lerp(velocity.x, direction.x * speed, delta * acceleration)
velocity.z = lerp(velocity.z, direction.z * speed, delta * acceleration)
2024-03-02 03:46:17 +03:00
if (climbing):
print(str(input_dir))
velocity.y = -input_dir.y * jump / 2 if input_dir.x == 0 else abs(input_dir.x) * jump / 2
2024-02-26 23:06:15 +03:00
else:
if is_on_floor():
2024-02-28 09:23:20 +03:00
velocity.x = lerp(velocity.x, direction.x * speed * 0.5, delta * inertia)
velocity.z = lerp(velocity.z, direction.z * speed * 0.5, delta * inertia)
2024-03-02 03:46:17 +03:00
elif not is_on_floor() and not climbing:
2024-02-26 23:06:15 +03:00
velocity.x = lerp(velocity.x, direction.x * speed * 0.5, delta)
velocity.z = lerp(velocity.z, direction.z * speed * 0.5, delta)
2024-02-07 23:25:08 +03:00
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = jump
2024-02-07 23:25:08 +03:00
if Input.is_action_pressed("run"):
speed = run
2024-02-07 23:25:08 +03:00
else:
speed = walk
2024-02-07 23:25:08 +03:00
else:
2024-02-26 23:06:15 +03:00
velocity.y -= 4 * gravity * delta
2024-02-07 23:25:08 +03:00
velocity.x = lerp(velocity.x, direction.x * speed, delta)
velocity.z = lerp(velocity.z, direction.z * speed, delta)
move_and_slide()
2024-03-02 03:46:17 +03:00
for index in get_slide_collision_count():
var collision := get_slide_collision(index)
var body := collision.get_collider()
if ("ladder" in body.name):
climbing = true
else:
climbing = false
#print("Collided with: " + body.name)
2024-02-07 23:25:08 +03:00
if (!multiplayer.is_server()):
2024-03-02 03:46:17 +03:00
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"]))
2024-02-07 23:25:08 +03:00
@rpc ("authority", "call_remote", "unreliable")
2024-02-15 13:10:01 +03:00
func sync_puppet(i_id, p, rot):
2024-02-15 18:12:48 +03:00
if(!properties["ready"]): return
if(properties["is_playable"]): return
2024-02-17 20:23:18 +03:00
if (int(i_id) == properties["internal_id"]):
2024-02-07 23:25:08 +03:00
playerCharacterBody.position = p
2024-02-15 13:10:01 +03:00
head.rotation.y = rot.y
camera.rotation.x = rot.x
2024-02-17 01:57:08 +03:00
@rpc ("authority", "call_remote", "reliable")
func set_hp(hp):
if (!properties["is_playable"]): return
properties["HP"] = hp
@rpc ("authority", "call_remote", "reliable")
func set_game_settings(s):
game_settings = s
gravity = game_settings["moving"]["gravity"]
walk = game_settings["moving"]["walk"]
run = game_settings["moving"]["run"]
jump = game_settings["moving"]["jump"]
2024-02-28 09:23:20 +03:00
acceleration = game_settings["moving"]["acceleration"]
inertia = game_settings["moving"]["inertia"]
change_weapon(properties["current_weapon"]["number"])
2024-02-17 01:57:08 +03:00
@rpc ("authority", "call_remote", "reliable")
func teleport(pos):
2024-03-02 03:46:17 +03:00
position = pos
@rpc ("authority", "call_remote", "reliable")
func change_weapon_puppet(i_id, new_weapon_number):
if (int(i_id) == properties["internal_id"]):
change_weapon(new_weapon_number)
2024-03-05 02:46:07 +03:00
@rpc("authority", "call_remote", "reliable")
func end_round(result):
print("Result: %s" % str(result))