Added reloading timing

This commit is contained in:
leca 2024-02-21 03:22:33 +03:00
parent 71f9cdec66
commit 9bdeb1d1e4
2 changed files with 42 additions and 17 deletions

View File

@ -168,6 +168,8 @@ func shot(client_id):
if (time_since_last_shot < current_weapon_settings["fireRate"] and client["last_shot"] > 0):
return
print(client["current_weapon"]["magazine"])
if (client["current_weapon"]["magazine"] == 0):
return
client["last_shot"] = Time.get_ticks_msec()

View File

@ -50,6 +50,7 @@ var HUD
var healthBar
var ammoLabel
var magazineLabel
var statusLabel
func set_properties(props):
properties = props
$"..".name = "player" + str(properties["internal_id"])
@ -60,6 +61,10 @@ func update_hud():
healthBar.value = properties["HP"]
ammoLabel.text = "Ammo: " + str(properties["current_weapon"]["ammo"])
magazineLabel.text = "Magazine: " + str(properties["current_weapon"]["magazine"])
if (properties["reloading"]):
statusLabel.text = "Reloading..."
else:
statusLabel.text = "Reloaded"
func change_weapon(new_weapon_number):
properties["reloading"] = false
@ -76,6 +81,14 @@ func find_weapon_by_number(number):
break
return found_weapon
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 _ready():
$"Head/Nickname".text = properties["nickname"]
if (!properties["is_playable"]): return
@ -87,43 +100,59 @@ func _ready():
healthBar = HUD.get_node("HealthBar")
ammoLabel = HUD.get_node("AmmoDisplay/Ammo")
magazineLabel = HUD.get_node("AmmoDisplay/Magazine")
statusLabel = HUD.get_node("Status")
playerCharacterBody.up_direction = Vector3.UP;
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func try_shoot():
print("aaaaa " + str((properties["current_weapon"])))
var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"]))
var current_weapon_settings = game_settings["weapons"][current_weapon]
#print("Current weapon: " + str(current_weapon) + ", it's settings: " + str(current_weapon_settings))
if (properties["current_weapon"]["magazine"] == 0):
return
if Input.is_action_pressed("shoot") and current_weapon_settings["fireRate"] >= 0.5 and not Input.is_action_just_pressed("shoot"):
return
properties["reloading"] = false
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())
if (properties["current_weapon"]["magazine"] > 0):
properties["current_weapon"]["magazine"] -= 1
properties["last_shot"] = Time.get_ticks_msec()
#print("I am shooting")
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"]):
print("Succsessfully reloaded")
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()
func _unhandled_input(event):
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())
properties["current_weapon"] = find_weapon_by_number(weapon_number)
#properties["current_weapon"] = find_weapon_by_number(weapon_number)
properties["current_weapon"] = find_current_weapon_by_number(weapon_number)
Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
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())
properties["current_weapon"] = find_weapon_by_number(weapon_number)
properties["current_weapon"] = find_current_weapon_by_number(weapon_number)
Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
change_weapon(properties["current_weapon"]["number"])
change_weapon(properties["current_weapon"]["number"])
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSETIVITY)
@ -144,16 +173,10 @@ func _physics_process(delta):
if Input.is_action_just_pressed("reload"):
var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"]))
var current_weapon_settings = game_settings["weapons"][current_weapon]
var to_reload = current_weapon_settings["magazine"] - properties["current_weapon"]["magazine"]
if (to_reload <= properties["current_weapon"]["ammo"]):
properties["current_weapon"]["magazine"] += to_reload
properties["current_weapon"]["ammo"] -= to_reload
else:
properties["current_weapon"]["magazine"] += properties["current_weapon"]["ammo"]
properties["current_weapon"]["ammo"] = 0
Networking.client_reloading.rpc_id(1, multiplayer.get_unique_id())
properties["reloading"] = true
get_tree().create_timer(game_settings["weapons"][current_weapon]["reload"]).connect("timeout", reload)
var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()