project_teddy/objects/Bullet/Bullet.gd

61 lines
1.7 KiB
GDScript3
Raw Normal View History

2023-01-27 12:47:13 -07:00
extends Area3D
signal exploded
@export var muzzle_velocity = 100 # How fast the bullets are
2023-02-07 13:25:06 -07:00
@export var g = Vector3.DOWN * 5
@onready var bulletAuthority = get_multiplayer_authority()
2023-01-27 12:47:13 -07:00
var velocity = Vector3.ZERO
2023-09-10 11:50:37 -06:00
var audio_stream_player = AudioStreamPlayer.new()
2023-01-27 12:47:13 -07:00
func _ready():
name = str("bullet ", bulletAuthority, "-0")
2023-01-27 12:47:13 -07:00
func _physics_process(delta):
2023-02-07 13:25:06 -07:00
velocity += g * delta # Bullet gravity
2023-01-27 12:47:13 -07:00
look_at(transform.origin + velocity.normalized(), Vector3.UP)
transform.origin += velocity * delta
if is_multiplayer_authority():
for body in get_overlapping_bodies():
2023-03-19 16:28:17 -06:00
if body.get_path() == Global.selfTeddy:
pass
elif body.is_in_group("human"):
var playerShot = body.get_multiplayer_authority()
print("Bullet hit ", playerShot)
2023-03-02 12:52:49 -07:00
rpc("damage_player")
2023-03-06 00:35:22 -07:00
rpc_id(playerShot,"take_damage", Global.playerName)
2023-02-28 09:46:35 -07:00
self.queue_free()
elif body.is_in_group("AI"):
print("Bullet hit an AI player")
2023-03-19 01:56:03 -06:00
Global.AIHit = true
self.queue_free()
else:
print("Bullet hit something else")
self.queue_free()
2023-02-21 12:53:13 -07:00
await get_tree().create_timer(2).timeout
self.queue_free()
2023-01-27 12:47:13 -07:00
func _on_Shell_body_entered(body):
emit_signal("exploded", transform.origin)
queue_free()
2023-02-24 13:43:51 -07:00
@rpc("any_peer", "call_remote", "reliable")
2023-03-02 12:52:49 -07:00
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")
2023-03-06 00:35:22 -07:00
func take_damage(damager):
var damage = RandomNumberGenerator.new().randi_range(7, 16)
2023-03-02 12:52:49 -07:00
Global.playerHealth = Global.playerHealth - damage
2023-03-06 00:35:22 -07:00
Global.lastPersonToHitMe = damager
2023-04-06 13:27:41 -06:00
Global.spawnCotton = true
2023-09-10 11:50:37 -06:00
play_audio(load("res://sounds/hit.mp3"))
func play_audio(audio):
audio_stream_player.set_stream(audio)
add_child(audio_stream_player)
audio_stream_player.play()