project_teddy/objects/Bullet/Bullet.gd
2023-03-19 16:28:17 -06:00

52 lines
1.5 KiB
GDScript

extends Area3D
signal exploded
@export var muzzle_velocity = 100 # How fast the bullets are
@export var g = Vector3.DOWN * 5
@onready var bulletAuthority = get_multiplayer_authority()
var velocity = Vector3.ZERO
func _ready():
name = str("bullet ", bulletAuthority, "-0")
func _physics_process(delta):
velocity += g * delta # Bullet gravity
look_at(transform.origin + velocity.normalized(), Vector3.UP)
transform.origin += velocity * delta
if is_multiplayer_authority():
for body in get_overlapping_bodies():
if body.get_path() == Global.selfTeddy:
pass
elif body.is_in_group("human"):
var playerShot = body.get_multiplayer_authority()
print("Bullet hit ", playerShot)
rpc("damage_player")
rpc_id(playerShot,"take_damage", Global.playerName)
self.queue_free()
elif body.is_in_group("AI"):
print("Bullet hit an AI player")
Global.AIHit = true
self.queue_free()
else:
print("Bullet hit something else")
self.queue_free()
await get_tree().create_timer(2).timeout
self.queue_free()
func _on_Shell_body_entered(body):
emit_signal("exploded", transform.origin)
queue_free()
@rpc("any_peer", "call_remote", "reliable")
func damage_player(): # This can be used later to make sure the server and all other peers know the health of all other players
self.queue_free()
@rpc("any_peer", "call_remote")
func take_damage(damager):
var damage = RandomNumberGenerator.new().randi_range(7, 16)
Global.playerHealth = Global.playerHealth - damage
Global.lastPersonToHitMe = damager