Skip to content

Commit 2f21c2e

Browse files
committed
despawn bullet + damage player
1 parent bd8491c commit 2f21c2e

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

client/assets/net/server_to_client.gd

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ func send_output_to_client_unreliable(states_udp_json_string: String):
8787
rigid_cube.position = Vector3(str_to_var("Vector3" + states_udp["rigid_cube"][id]["position"]))
8888
if states_udp.has("bullet"):
8989
for id in states_udp["bullet"]:
90-
var bullet = get_node("/root/main/bullets/").get_node(id)
91-
if bullet:
92-
if states_udp["bullet"][id].has("position"):
93-
bullet.position = Vector3(str_to_var("Vector3" + states_udp["bullet"][id]["position"]))
90+
if get_node("/root/main/bullets/").has_node(id):
91+
var bullet = get_node("/root/main/bullets/").get_node(id)
92+
if bullet:
93+
if states_udp["bullet"][id].has("position"):
94+
bullet.position = Vector3(str_to_var("Vector3" + states_udp["bullet"][id]["position"]))
9495

9596

9697
@rpc("call_remote", "reliable")
@@ -114,3 +115,9 @@ func spawn_bullet_on_client(data):
114115
b.position = data["position"]
115116
b.from_player = data["from_player"]
116117
$"/root/main/bullets".add_child(b)
118+
119+
@rpc("call_remote", "reliable")
120+
func free_bullet_on_client(id):
121+
if get_node("/root/main/bullets").has_node(str(id)):
122+
print("free_bullet(" + str(id) + ")")
123+
get_node("/root/main/bullets/").remove_child(get_node("/root/main/bullets/" + str(id)))

server/assets/bullet/bullet.gd

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ func _physics_process(delta):
4444

4545
func _on_body_entered(body):
4646
print(str(name) + " collided with " + str(body.name))
47-
# if body is CharacterBody3D:
48-
# body.damage(10)
49-
50-
queue_free()
47+
if body is CharacterBody3D:
48+
body.damage(10)
49+
destroy()
5150

5251
func destroy():
53-
# get_node("/root/Main/").global_state["bullet"].erase(name)
54-
queue_free()
55-
56-
#TODO: free_bullet_on_client + server func in net.gd
52+
#maybe do some explosion animation and sound or something
53+
54+
#remove bullet on server and clients
55+
get_node("/root/main/net").free_bullet(str(name))

server/assets/bullet/bullet.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rings = 4
1313
radius = 0.1
1414

1515
[node name="Bullet" type="Area3D"]
16+
collision_layer = 15
1617
script = ExtResource("1_q075n")
1718

1819
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]

server/assets/net/net.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@ func spawn_bullet(from_player: int):
214214
data["position"] = get_node("/root/main/players/" + str(from_player) + "/" + str(from_player) + "/shoot_from").global_position
215215
$server_to_client.spawn_bullet_on_client.rpc_id(int(str(p.get_name())), data)
216216

217+
func free_bullet(id):
218+
if get_node("/root/main/bullets").has_node(str(id)):
219+
if states_udp.has("bullet"):
220+
if states_udp["bullet"].has(name):
221+
states_udp["bullet"].remove(name)
222+
get_node("/root/main/bullets/" + str(id)).queue_free()
223+
$server_to_client.free_bullet_on_client.rpc(id)
224+
217225

218226
func print_net_info():
219227
print("****************************************")

server/assets/net/server_to_client.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ func send_output_to_client_reliable():
3030
func spawn_bullet_on_client():
3131
pass
3232

33+
@rpc("reliable")
34+
func free_bullet_on_client():
35+
pass

server/assets/player/player.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var old_camera_arm_rotation
2121
#Zoom
2222
var old_camera_arm_scale
2323

24+
var health: int = 100
25+
var mana: int = 100
26+
2427
@onready var camera_arm = $"SpringArm3D"
2528
@onready var camera = camera_arm.get_node("Camera3D")
2629
#@onready var b_res := preload("res://../assets/bullet/bullet.tscn")
@@ -31,6 +34,15 @@ func _ready():
3134
material.albedo_color = color
3235
mesh_instance.set_surface_override_material(0, material)
3336

37+
func damage(damage: int):
38+
health -= damage
39+
print(str(name) + ": health " + str(health))
40+
#respawn if health is zero
41+
if health <= 0:
42+
print(str(name) + "died")
43+
position = respawn_position
44+
health = 100
45+
3446
func _physics_process(delta):
3547

3648
if client_ready == true:

0 commit comments

Comments
 (0)