Guns and Reviving added - Other minor Bugfixes.
This commit is contained in:
parent
8c90c4e744
commit
139e9a5134
11 changed files with 317 additions and 47 deletions
26
scripts/entities/bullet.gd
Normal file
26
scripts/entities/bullet.gd
Normal 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()
|
|
@ -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
|
||||
|
|
|
@ -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 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 !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
|
||||
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.x = move_toward(velocity.x, 0, SPEED)
|
||||
velocity.z = move_toward(velocity.z, 0, SPEED)
|
||||
|
||||
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)
|
||||
|
||||
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()
|
||||
|
|
|
@ -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
16
scripts/maps/guncase.gd
Normal 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()
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue