From b4677e91b99b7adc8ccccd9abbc86c577811821c Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 27 Mar 2024 20:07:26 +0300 Subject: [PATCH] added tracers --- scenes/models/bullet_trace.tscn | 15 +++++++++ scenes/models/kill_log_entry.tscn | 2 +- scripts/BulletTraces.gd | 11 +++++++ scripts/GameData.gd | 6 ++++ .../{kill_log_entry.gd => KillLogEntry.gd} | 0 scripts/Networking.gd | 1 + scripts/Player.gd | 31 ++++++++++++++----- 7 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 scenes/models/bullet_trace.tscn create mode 100644 scripts/BulletTraces.gd rename scripts/{kill_log_entry.gd => KillLogEntry.gd} (100%) diff --git a/scenes/models/bullet_trace.tscn b/scenes/models/bullet_trace.tscn new file mode 100644 index 0000000..8f4a92f --- /dev/null +++ b/scenes/models/bullet_trace.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=4 format=3 uid="uid://cm4as5vv2k2di"] + +[ext_resource type="Script" path="res://scripts/BulletTraces.gd" id="1_trgm5"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_g4lay"] +albedo_color = Color(1, 0, 1, 1) +emission = Color(1, 0, 0.741176, 1) +emission_energy_multiplier = 9.21 + +[sub_resource type="ImmediateMesh" id="ImmediateMesh_djmsg"] + +[node name="MeshInstance3D" type="MeshInstance3D"] +material_override = SubResource("StandardMaterial3D_g4lay") +mesh = SubResource("ImmediateMesh_djmsg") +script = ExtResource("1_trgm5") diff --git a/scenes/models/kill_log_entry.tscn b/scenes/models/kill_log_entry.tscn index 74ee80e..babf167 100644 --- a/scenes/models/kill_log_entry.tscn +++ b/scenes/models/kill_log_entry.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=3 format=3 uid="uid://dvrv2bfmotivm"] [ext_resource type="Texture2D" uid="uid://bmuo2tbe3r8ih" path="res://textures/logo/ammo.svg" id="1_p3k0o"] -[ext_resource type="Script" path="res://scripts/kill_log_entry.gd" id="1_tki6n"] +[ext_resource type="Script" path="res://scripts/KillLogEntry.gd" id="1_tki6n"] [node name="kill_log_entry" type="HBoxContainer"] offset_right = 311.0 diff --git a/scripts/BulletTraces.gd b/scripts/BulletTraces.gd new file mode 100644 index 0000000..877692d --- /dev/null +++ b/scripts/BulletTraces.gd @@ -0,0 +1,11 @@ +extends MeshInstance3D + +func init(start, end): + var draw_mesh = ImmediateMesh.new() + mesh = draw_mesh + draw_mesh.surface_begin(Mesh.PRIMITIVE_LINES, material_override) + draw_mesh.surface_add_vertex(to_local(start)) + draw_mesh.surface_add_vertex(to_local(end)) + draw_mesh.surface_end() + await get_tree().create_timer(1).timeout + free() diff --git a/scripts/GameData.gd b/scripts/GameData.gd index 954f341..ed93999 100644 --- a/scripts/GameData.gd +++ b/scripts/GameData.gd @@ -38,6 +38,11 @@ var properties_example = { var client_settings var server_settings +func apply_weapon_settings(s: Dictionary): #game/weapons/weapon + for weapon in s.values(): + Weapons[s.find_key(weapon)] = weapon + print("New weapons: " + str(Weapons)) + func _ready(): read_settings() @@ -66,6 +71,7 @@ func read_settings(): else: server_settings_file = FileAccess.open("./settings/server-settings.json", FileAccess.READ) server_settings = JSON.parse_string(server_settings_file.get_as_text()) + apply_weapon_settings(server_settings["game"]["weapons"]) func save_client_settings(): var client_settings_file = FileAccess.open("./settings/client-settings.json", FileAccess.WRITE) diff --git a/scripts/kill_log_entry.gd b/scripts/KillLogEntry.gd similarity index 100% rename from scripts/kill_log_entry.gd rename to scripts/KillLogEntry.gd diff --git a/scripts/Networking.gd b/scripts/Networking.gd index b91e59f..b2ac707 100644 --- a/scripts/Networking.gd +++ b/scripts/Networking.gd @@ -125,6 +125,7 @@ func shot(client_id): var client_playermodel = ServerUtils.find_playermodel_by_internal_id(internal_id) var raycast:RayCast3D = client_playermodel.get_node("Head/Camera/viewRaycast") var weapon_raycast:RayCast3D = client_playermodel.get_node("Head/Camera/Hand/" + str(current_weapon) + "/raycast") + print("currect weapon: " + current_weapon) raycast.target_position.z = -current_weapon_settings["range"] raycast.force_raycast_update() raycast.force_update_transform() diff --git a/scripts/Player.gd b/scripts/Player.gd index fdb2f2c..12347e3 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -133,11 +133,7 @@ func change_weapon(new_weapon_number): func find_weapon_in_inventory(number): var found_weapon - print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa: " + str(properties["weapons"])) for weapon in properties["weapons"].values(): - #var a:Dictionary = properties["weapons"] - #a. - print("Checking: " + str(weapon)) if weapon["number"] == number: found_weapon = weapon break @@ -189,6 +185,7 @@ func try_shoot(): print(str(properties["current_weapon"])) #var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"])) var current_weapon = find_weapon_name_by_number(properties["current_weapon"]["number"])#game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"])) + print("currect weapon: " + current_weapon) var current_weapon_settings = game_settings["weapons"][current_weapon] properties["reloading"] = false @@ -202,6 +199,26 @@ func try_shoot(): Networking.shot.rpc_id(1, multiplayer.get_unique_id()) properties["current_weapon"]["magazine"] -= 1 properties["last_shot"] = Time.get_ticks_msec() + + var raycast:RayCast3D = get_node("Head/Camera/viewRaycast") + var weapon_raycast:RayCast3D = get_node("Head/Camera/Hand/" + str(current_weapon) + "/raycast") + raycast.target_position.z = -current_weapon_settings["range"] + raycast.force_raycast_update() + raycast.force_update_transform() + var target_point = raycast.get_collision_point() + + weapon_raycast.target_position = weapon_raycast.to_local(target_point) * 1.1 + weapon_raycast.rotation.y = atan((weapon_raycast.position.x - position.x) / ((-current_weapon_settings["range"]) - (abs(weapon_raycast.position.z - position.z)))) + + weapon_raycast.target_position.y += current_weapon_settings["spreading"] * randf() * sin(randf() * 2 * PI) + weapon_raycast.target_position.z += current_weapon_settings["spreading"] * randf() * cos(randf() * 2 * PI) + + weapon_raycast.force_raycast_update() + weapon_raycast.force_update_transform() + + var trace = preload("res://scenes/models/bullet_trace.tscn").instantiate() + get_node("..").add_child(trace) + trace.init((weapon_raycast.global_position), (weapon_raycast.get_collision_point())) func reload(): var current_weapon_instance = properties["current_weapon"] @@ -240,10 +257,6 @@ func _unhandled_input(event): camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-50), deg_to_rad(60)) func _physics_process(delta): - #for shape:CollisionShape3D in collision_shapes: - ##shape.rotation = self.rotation - #shape.rotate_y(head.rotation.y) - #shapee. if (!properties["is_playable"]): return if (game_settings == null): return if (multiplayer.multiplayer_peer == null): @@ -327,6 +340,8 @@ func set_game_settings(s): acceleration = game_settings["moving"]["acceleration"] inertia = game_settings["moving"]["inertia"] change_weapon(properties["current_weapon"]["number"]) + GameData.apply_weapon_settings(game_settings["weapons"]) + Weapons = GameData.Weapons @rpc ("authority", "call_remote", "reliable") func teleport(pos):