Guns and Reviving added - Other minor Bugfixes.

This commit is contained in:
Patrick 2024-12-19 17:41:41 +01:00
parent 8c90c4e744
commit 139e9a5134
11 changed files with 317 additions and 47 deletions

View file

@ -111,6 +111,16 @@ descend={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
interact={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null)
]
}
shoot={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}
[rendering]

View file

@ -0,0 +1,23 @@
[gd_scene load_steps=4 format=3 uid="uid://bdxw7gc28nv6a"]
[ext_resource type="Script" path="res://scripts/entities/bullet.gd" id="1_di2ph"]
[sub_resource type="BoxMesh" id="BoxMesh_gbwh0"]
size = Vector3(0.1, 0.1, 0.2)
[sub_resource type="BoxShape3D" id="BoxShape3D_26n4t"]
size = Vector3(0.1, 0.1, 0.2)
[node name="Bullet" type="CharacterBody3D"]
script = ExtResource("1_di2ph")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("BoxMesh_gbwh0")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("BoxShape3D_26n4t")
[node name="Timer" type="Timer" parent="."]
wait_time = 5.0
one_shot = true
autostart = true

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=4 format=3 uid="uid://dpdxe3muj2o26"]
[gd_scene load_steps=6 format=3 uid="uid://dpdxe3muj2o26"]
[ext_resource type="Script" path="res://scripts/entities/player.gd" id="1_r7f2r"]
@ -6,6 +6,12 @@
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_7frjr"]
[sub_resource type="BoxShape3D" id="BoxShape3D_flq6p"]
size = Vector3(3, 3, 3)
[sub_resource type="CapsuleMesh" id="CapsuleMesh_1sd1e"]
radius = 0.125
[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_r7f2r")
@ -18,6 +24,16 @@ shape = SubResource("CapsuleShape3D_7frjr")
[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0)
[node name="InteractDialog" type="Label" parent="."]
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -20.0
offset_right = 20.0
offset_bottom = 23.0
grow_horizontal = 2
size_flags_horizontal = 4
[node name="Label" type="Label" parent="."]
offset_right = 40.0
offset_bottom = 23.0
@ -28,3 +44,13 @@ pixel_size = 0.01
billboard = 1
no_depth_test = true
text = "SamplePlayer"
[node name="Area3D" type="Area3D" parent="."]
[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"]
shape = SubResource("BoxShape3D_flq6p")
[node name="Gun" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0.625, 0, -0.5)
visible = false
mesh = SubResource("CapsuleMesh_1sd1e")

View file

@ -0,0 +1,31 @@
[gd_scene load_steps=6 format=3 uid="uid://ctpubhh2gsccb"]
[ext_resource type="Script" path="res://scripts/maps/guncase.gd" id="1_dh5qf"]
[sub_resource type="BoxShape3D" id="BoxShape3D_g6om3"]
size = Vector3(2, 0.5, 0.5)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_wgwuf"]
albedo_color = Color(0, 0.501961, 0, 1)
[sub_resource type="BoxMesh" id="BoxMesh_qtnys"]
material = SubResource("StandardMaterial3D_wgwuf")
size = Vector3(2, 0.5, 0.5)
[sub_resource type="BoxShape3D" id="BoxShape3D_emap1"]
size = Vector3(3, 1, 2)
[node name="Guncase" type="StaticBody3D"]
script = ExtResource("1_dh5qf")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("BoxShape3D_g6om3")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("BoxMesh_qtnys")
[node name="Area3D" type="Area3D" parent="."]
[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.25, 0)
shape = SubResource("BoxShape3D_emap1")

View file

@ -1,9 +1,10 @@
[gd_scene load_steps=9 format=3 uid="uid://cbyee7drds7qu"]
[gd_scene load_steps=10 format=3 uid="uid://cbyee7drds7qu"]
[ext_resource type="Script" path="res://scripts/maps/map.gd" id="1_4npcs"]
[ext_resource type="Script" path="res://scripts/maps/objectives/hungry.gd" id="2_qrp84"]
[ext_resource type="PackedScene" uid="uid://bsghm187n6ykx" path="res://scenes/objects/closet.tscn" id="2_yvpvm"]
[ext_resource type="PackedScene" uid="uid://cvnjpnvchvakj" path="res://scenes/entities/npc.tscn" id="3_x3gyc"]
[ext_resource type="PackedScene" uid="uid://ctpubhh2gsccb" path="res://scenes/maps/elements/guncase.tscn" id="5_k7k8h"]
[sub_resource type="Environment" id="Environment_pq0iv"]
@ -71,3 +72,6 @@ skeleton = NodePath("../..")
shape = SubResource("BoxShape3D_rnmx0")
[node name="Timer" type="Timer" parent="."]
[node name="Guncase" parent="." instance=ExtResource("5_k7k8h")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 24, 1, 0)

View file

@ -0,0 +1,26 @@
class_name Bullet
extends CharacterBody3D
const SPEED: int = 20
func _ready() -> void:
if ($Timer as Timer).timeout.connect(_on_timeout):
pass
func _on_timeout() -> void:
queue_free()
func _physics_process(delta: float) -> void:
var direction: Vector3 = (transform.basis * Vector3(0, 0, -1)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
var collision: KinematicCollision3D = move_and_collide(velocity * delta)
if collision != null:
if collision.get_collider() is NPC and (collision.get_collider() as NPC).carried_object != null and Networking.isManager:
Networking.set_down_sync_call((collision.get_collider() as NPC), (collision.get_collider() as NPC).carried_object)
queue_free()
else:
queue_free()

View file

@ -7,6 +7,7 @@ extends CharacterBody3D
var movement_speed: float = 3.0
var angry_meter: int = 0
var target: Node3D
var carried_object: Carryable
var carrying: bool = false
# Legend:
@ -43,6 +44,7 @@ func set_text(text: String) -> void:
($Label3D as Label3D).text = text
# determines the closest resting place and navigates to it.
#TODO: Desynchronizes the path if there is an obstacle.
func find_objective() -> void:
var lowest_distance: float = 99999999999999
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/NPCRestPlaces").get_children():
@ -52,6 +54,7 @@ func find_objective() -> void:
navigation_agent.set_target_position(target.position)
# determines the closest resting place and navigates to it.
#TODO: Desynchronizes the path if the box is moved.
func find_carryable() -> void:
var lowest_distance: float = 99999999999999
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/NPCCarryables").get_children():
@ -63,13 +66,14 @@ func find_carryable() -> void:
# determines the closest player and navigates to them.
func find_player(lowest_distance: float) -> void:
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/").get_children():
if child is Player and position.distance_to((child as Player).position) < lowest_distance:
if child is Player and position.distance_to((child as Player).position) < lowest_distance and !(child as Player).incapacitated:
target = child
navigation_agent.set_target_position(target.position)
func pick_up(node: Carryable) -> void:
carrying = true
carried_object = node
node.get_parent().remove_child(node)
node.carry()
node.position = Vector3(0, 1, -1)
@ -80,6 +84,7 @@ func pick_up(node: Carryable) -> void:
func set_down(node: Carryable) -> void:
carrying = false
carried_object = null
node.get_parent().remove_child(node)
node.uncarry()
node.rotation = rotation

View file

@ -10,11 +10,17 @@ const JUMP_VELOCITY: float = 4.5
var activated: bool = false
var freecam: bool = false
var incapacitated: bool = false
var interaction: String
var revival_target: Player
var carrying_gun: bool = false
@onready var camera: Camera3D = $Camera3D
var camera_rotation: Vector2 = Vector2(0,0)
func _ready() -> void:
if ($Area3D as Area3D).body_entered.connect(_on_body_entered):
pass
($Label3D as Label3D).text = name
if name == Game.username:
activated = true
@ -22,43 +28,78 @@ func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
($Label3D as Label3D).hide()
func _on_body_entered(body: Node3D) -> void:
if body is Player and incapacitated:
(body as Player).revive_chance(self)
func _physics_process(delta: float) -> void:
if activated:
if !freecam:
if not is_on_floor():
velocity += get_gravity() * delta
if !freecam:
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
if !incapacitated:
if !freecam:
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
if freecam:
if Input.is_action_pressed("jump"):
velocity.y = JUMP_VELOCITY
elif Input.is_action_pressed("descend"):
velocity.y = -JUMP_VELOCITY
if freecam:
if Input.is_action_pressed("jump"):
velocity.y = JUMP_VELOCITY
elif Input.is_action_pressed("descend"):
velocity.y = -JUMP_VELOCITY
else:
velocity.y = 0
if Input.is_action_just_pressed("freecam"):
freecam = !freecam
($CollisionShape3D as CollisionShape3D).disabled = !($CollisionShape3D as CollisionShape3D).disabled
if Input.is_action_just_pressed("interact") and interaction != null:
if interaction == "revive" and revival_target != null:
Networking.revive_sync_call(revival_target)
($InteractDialog as Label).text = ""
revival_target = null
if interaction == "gun":
var guncase: Guncase = get_node("/root/%s/Guncase" % Game.mapname)
if !guncase.held:
carrying_gun = true
($InteractDialog as Label).text = ""
Networking.guncase_sync_call(true, self)
else:
carrying_gun = false
($InteractDialog as Label).text = ""
Networking.guncase_sync_call(false, self)
if Input.is_action_just_pressed("shoot") and carrying_gun:
Networking.bullet_sync_call(position + (transform.basis * Vector3(0, 0, -1)).normalized(), rotation)
var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forwards", "move_backwards")
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
#TODO: Hacky solution -> Also can be exploited to revive everyone in the game constantly.
if revival_target != null and position.distance_to(revival_target.position) > 3:
($InteractDialog as Label).text = ""
interaction = ""
revival_target = null
if interaction == "gun":
if position.distance_to((get_node("/root/%s/Guncase" % Game.mapname) as Guncase).position) > 3:
($InteractDialog as Label).text = ""
interaction = ""
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.y = 0
if Input.is_action_just_pressed("freecam"):
freecam = !freecam
($CollisionShape3D as CollisionShape3D).disabled = !($CollisionShape3D as CollisionShape3D).disabled
var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forwards", "move_backwards")
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
if move_and_slide():
pass
var fps_counter: Label = $Label
fps_counter.text = str(Engine.get_frames_per_second())
fps_counter.text = str(Engine.get_frames_per_second()) + " FPS"
Networking.player_sync_call(position, rotation)
@ -77,4 +118,32 @@ func _input(event: InputEvent) -> void:
rotate(Vector3.DOWN, camera_rotation.x)
func incapacitate() -> void:
pass
rotation.x = deg_to_rad(90)
incapacitated = true
if carrying_gun:
carrying_gun = false
Networking.guncase_sync_call(false, self)
func revive_chance(body: Player) -> void:
if activated:
revival_target = body
interaction = "revive"
($InteractDialog as Label).text = "Press E to revive %s." % revival_target.name
func revive() -> void:
rotation.x = 0
incapacitated = false
func gun_chance() -> void:
if activated:
interaction = "gun"
if carrying_gun:
($InteractDialog as Label).text = "Press E to set down gun."
else:
($InteractDialog as Label).text = "Press E to pick up gun."
func show_gun() -> void:
($Gun as MeshInstance3D).show()
func hide_gun() -> void:
($Gun as MeshInstance3D).hide()

View file

@ -25,8 +25,7 @@ func _on_body_entered(body: Node) -> void:
Networking.pick_up_sync_call(carried_by, self)
if body is Player and carried:
Networking.set_down_sync_call(carried_by, self)
(body as Player).incapacitate()
Networking.incapacitate_sync_call(body as Player)
func _physics_process(_delta: float) -> void:
if Networking.isManager and sleeping and !carried:

16
scripts/maps/guncase.gd Normal file
View file

@ -0,0 +1,16 @@
class_name Guncase
extends StaticBody3D
var held: bool = false
func _ready() -> void:
if ($Area3D as Area3D).body_entered.connect(_on_body_entered):
pass
func _on_body_entered(body: Node3D) -> void:
if body is Player:
if held:
if (body as Player).carrying_gun:
(body as Player).gun_chance()
else:
(body as Player).gun_chance()

View file

@ -1,10 +1,18 @@
## SPDX-License-Identifier: GPL-3.0-or-later
## Copyright (c) 2024 interstellardevelopment.org
# Some info about this script:
# The game manager is always absolutely trusted, if the game manager goes crazy, this is not checked yet.
#TODO: For the future, maybe make some client side anti-cheat that checks if the manager is playing tricks.
# The peers are untrusted. They may cheat, or try something stupid. If at all possible, Direct peer to peer communication should be prevented.
# Rather, such a thing should be done through the game manager if possible.
extends Node
signal playerlist_changed()
var bullet: PackedScene = preload("res://scenes/entities/bullet.tscn")
## Connection Code
# Connect the signals.
@ -278,6 +286,7 @@ func player_sync_call(position: Vector3, rotation: Vector3) -> void:
# Synchronizes the player state.
@rpc("any_peer", "call_remote", "unreliable")
func player_sync(position: Vector3, rotation: Vector3) -> void:
#TODO: Anti-Cheat -> Check for realistic movement, and prevent flying.
if verify_id(multiplayer.get_remote_sender_id()):
for peer: String in peers:
if peers[peer] == multiplayer.get_remote_sender_id() and has_node("/root/"+Game.mapname+"/"+peer):
@ -309,7 +318,7 @@ func object_sync_call(position: Vector3, rotation: Vector3, node_name: String) -
if rpc_id(peer, "object_sync", position, rotation, node_name) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the npc state.
# Synchronizes the object position.
@rpc("any_peer", "call_remote", "unreliable")
func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> void:
if managerID == multiplayer.get_remote_sender_id():
@ -317,14 +326,7 @@ func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> voi
carryable.position = position
carryable.rotation = rotation
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_sync")
func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void:
var npc_path: NodePath = npc.get_path()
var carryable_path: NodePath = carryable.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "pick_up_sync", npc_path, carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
Log.warning("Non-manager peer tried to send a manager only request: object_sync")
# Synchronizes the text above the npc.
@rpc("any_peer", "call_local", "reliable")
@ -335,9 +337,15 @@ func npc_text_sync(text: String, npc: NodePath) -> void:
Log.warning("Non-manager peer tried to send a manager only request: npc_text_sync")
func npc_text_sync_call(text: String, npc: NPC) -> void:
var npc_path: NodePath = npc.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "npc_text_sync", text, npc_path) != OK:
if rpc_id(peer, "npc_text_sync", text, npc.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void:
# We need to get the path before we send out sync calls. This is because the value changes depending on whether or not it already ran locally.
var carryable_path: NodePath = carryable.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "pick_up_sync", npc.get_path(), carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the object that is picked up by the npc.
@ -349,16 +357,69 @@ func pick_up_sync(npc: NodePath, carryable: NodePath) -> void:
Log.warning("Non-manager peer tried to send a manager only request: pick_up_sync")
func set_down_sync_call(npc: NPC, carryable: Carryable) -> void:
var npc_path: NodePath = npc.get_path()
# We need to get the path before we send out sync calls. This is because the value changes depending on whether or not it already ran locally.
var carryable_path: NodePath = carryable.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "set_down_sync", npc_path, carryable_path) != OK:
if rpc_id(peer, "set_down_sync", npc.get_path(), carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the object that is picked up by the npc.
# Synchronizes the object that is set down by the npc.
@rpc("any_peer", "call_local", "reliable")
func set_down_sync(npc: NodePath, carryable: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id():
(get_node(npc) as NPC).set_down(get_node(carryable) as Carryable)
else:
Log.warning("Non-manager peer tried to send a manager only request: set_down_sync")
func incapacitate_sync_call(player: Player) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "incapacitate_sync", player.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the player being incapacitated by an npc.
@rpc("any_peer", "call_local", "reliable")
func incapacitate_sync(player: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id():
(get_node(player) as Player).incapacitate()
else:
Log.warning("Non-manager peer tried to send a manager only request: incapacitate_sync")
func revive_sync_call(player: Player) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "revive_sync", player.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the player being revived by another.
@rpc("any_peer", "call_local", "reliable")
func revive_sync(player: NodePath) -> void:
#TODO: Anti-Cheat -> Check if such a thing should even be possible from the players position!
if verify_id(multiplayer.get_remote_sender_id()):
(get_node(player) as Player).revive()
func guncase_sync_call(status: bool, player: Player) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "guncase_sync", status, player.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the guncase state.
@rpc("any_peer", "call_local", "reliable")
func guncase_sync(status: bool, player_path: NodePath) -> void:
#TODO: Anti-Cheat -> Check if the gun is even available!
if verify_id(multiplayer.get_remote_sender_id()):
(get_node("/root/%s/Guncase" % Game.mapname) as Guncase).held = status
(get_node(player_path) as Player).show_gun()
func bullet_sync_call(position: Vector3, rotation: Vector3) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "bullet_sync", position, rotation) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the bullet spawn.
@rpc("any_peer", "call_local", "reliable")
func bullet_sync(position: Vector3, rotation: Vector3) -> void:
#TODO: Anti-Cheat -> Check if the gun is even equiped!
if verify_id(multiplayer.get_remote_sender_id()):
var bullet_instance: Bullet = bullet.instantiate()
bullet_instance.position = position
bullet_instance.rotation = rotation
get_node("/root/"+Game.mapname).add_child(bullet_instance)