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

@ -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 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()