AI works again, bullets run as an RPC to peer

This commit is contained in:
Paul Black 2023-02-23 10:19:37 -07:00
parent 25a876f71a
commit 53ca76657f
5 changed files with 52 additions and 34 deletions

View file

@ -10,13 +10,14 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@export var Bullet = preload("res://objects/Bullet/Bullet.tscn") @export var Bullet = preload("res://objects/Bullet/Bullet.tscn")
@onready var neck := $CollisionShape3D/Neck @onready var neck := $CollisionShape3D/Neck
@onready var camera := $CollisionShape3D/Neck/Camera3D @onready var camera := $CollisionShape3D/Neck/Camera3D
@onready var teddyAuthority = get_multiplayer_authority()
var selfTeddy var selfTeddy
var teddyParent var teddyParent
var globalscene var globalscene
func _ready(): func _ready():
name = str(get_multiplayer_authority()) name = str(teddyAuthority)
Global.playingGame = true Global.playingGame = true
Global.selfTeddy = selfTeddy Global.selfTeddy = selfTeddy
selfTeddy = self.get_path() selfTeddy = self.get_path()
@ -56,9 +57,10 @@ func _physics_process(delta):
# This variable gets set my the death scene, so we know the player needs to be respawned # This variable gets set my the death scene, so we know the player needs to be respawned
if Global.playerPleaseRespawn == true: if Global.playerPleaseRespawn == true:
position.x = 0 #Set player X position.x = 0 #Set player X
position.y = 1 #Set player Y position.y = 10 #Set player Y
position.z = 0 #Set player Z position.z = 0 #Set player Z
Global.reset_variables() Global.reset_variables()
self.visible = false
## If player falls off the map, kill them! ## If player falls off the map, kill them!
if velocity.y < Global.playerYDeath: if velocity.y < Global.playerYDeath:
@ -122,6 +124,7 @@ func remote_set_position(authority_position, bulletgenerator_position, bulletgen
@rpc("any_peer", "reliable") @rpc("any_peer", "reliable")
func shoot_bullet(sound): func shoot_bullet(sound):
var b = Bullet.instantiate() var b = Bullet.instantiate()
b.set_multiplayer_authority(teddyAuthority)
teddyParent.add_child(b) teddyParent.add_child(b)
b.transform = $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_transform b.transform = $CollisionShape3D/Neck/Camera3D/BulletGenerator.global_transform
b.velocity = -b.global_transform.basis.z * b.muzzle_velocity b.velocity = -b.global_transform.basis.z * b.muzzle_velocity

View file

@ -4,32 +4,33 @@ signal exploded
@export var muzzle_velocity = 100 # How fast the bullets are @export var muzzle_velocity = 100 # How fast the bullets are
@export var g = Vector3.DOWN * 5 @export var g = Vector3.DOWN * 5
@onready var bulletAuthority = get_multiplayer_authority()
var velocity = Vector3.ZERO var velocity = Vector3.ZERO
###### CHEATS ###### CHEATS
var godmode = 0 var godmode = 0
func _ready():
name = str("bullet ", bulletAuthority, "-0")
func _physics_process(delta): func _physics_process(delta):
velocity += g * delta # Bullet gravity velocity += g * delta # Bullet gravity
look_at(transform.origin + velocity.normalized(), Vector3.UP) look_at(transform.origin + velocity.normalized(), Vector3.UP)
transform.origin += velocity * delta transform.origin += velocity * delta
for body in get_overlapping_bodies(): if is_multiplayer_authority():
if body.get_path() == Global.selfTeddy: # We need to modify this to make the player not invinsible for body in get_overlapping_bodies():
pass if body.get_path() == Global.selfTeddy: # We need to modify this to make the player not invinsible
elif body.is_in_group("human"): pass
print("Bullet hit a human player") elif body.is_in_group("human"):
Global.playerHealth = Global.playerHealth - 10 rpc("damage_player")
self.queue_free() elif body.is_in_group("AI"):
elif body.is_in_group("AI"): print("Bullet hit an AI player")
print("Bullet hit an AI player") body.queue_free()
body.queue_free() self.queue_free()
self.queue_free() else:
else: print("Bullet hit something else")
print("Bullet hit something else") self.queue_free()
self.queue_free()
await get_tree().create_timer(2).timeout await get_tree().create_timer(2).timeout
self.queue_free() self.queue_free()
@ -37,3 +38,9 @@ func _physics_process(delta):
func _on_Shell_body_entered(body): func _on_Shell_body_entered(body):
emit_signal("exploded", transform.origin) emit_signal("exploded", transform.origin)
queue_free() queue_free()
@rpc("any_peer", "call_remote", "unreliable")
func damage_player():
print("A bullet hit me!")
Global.playerHealth = Global.playerHealth - 10
self.queue_free()

View file

@ -11,10 +11,12 @@ var bulletrandom
# Get the gravity from the project settings to be synced with RigidBody nodes. # Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var player = get_node(^"/root/tutorial/Teddy") var player
@export var Bullet = preload("res://objects/Bullet/Bullet.tscn") @export var Bullet = preload("res://objects/Bullet/Bullet.tscn")
func _physics_process(delta): func _physics_process(delta):
if Global.selfTeddy:
player = get_node(Global.selfTeddy)
# Add the gravity. # Add the gravity.
if not is_on_floor(): if not is_on_floor():
velocity.y -= gravity * delta velocity.y -= gravity * delta

View file

@ -1,13 +1,16 @@
extends Node extends Node
var deathTimer = 0 var deathTimer = 0
var teddyNode
func _ready(): func _ready():
pass pass
func _process(delta): func _process(delta):
#teddyNode = get_node(Global.selfTeddy)
Global.playerAlive = false Global.playerAlive = false
#teddyNode.visible = false
if deathTimer < 100: if deathTimer < 100:
deathTimer = deathTimer + 10 * delta deathTimer = deathTimer + 10 * delta
if deathTimer > 100: if deathTimer > 100:

View file

@ -26,23 +26,26 @@ func _on_start_button_pressed():
var intMAX = int(MAX_PLAYERS) var intMAX = int(MAX_PLAYERS)
if PORT: if PORT:
prints("Player chose port:", intPORT) prints("Player chose port:", intPORT)
if SCENE: if intPORT < 1024:
print("Player chose map: ", SCENE) $mapSelected.text = "PORT CANNOT BE BELOW 1024"
if MAX_PLAYERS:
if intMAX < 1:
$mapSelected.text = "MAX PLAYERS CANNOT BE NEGATIVE"
elif intMAX > 4095:
$mapSelected.text = "MAX PLAYERS CANNOT BE ABOVE 4095"
else:
print("Player chose player limit: ", intMAX)
Networking.create_server(intPORT, intMAX)
Networking.load_map(SCENE)
Networking.add_player_node(1)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
$mapSelected.text = "NO MAX PLAYERS SELECTED"
else: else:
$mapSelected.text = "NO MAP SELECTED" if SCENE:
print("Player chose map: ", SCENE)
if MAX_PLAYERS:
if intMAX < 1:
$mapSelected.text = "MAX PLAYERS CANNOT BE NEGATIVE"
elif intMAX > 4095:
$mapSelected.text = "MAX PLAYERS CANNOT BE ABOVE 4095"
else:
print("Player chose player limit: ", intMAX)
Networking.create_server(intPORT, intMAX)
Networking.load_map(SCENE)
Networking.add_player_node(1)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
$mapSelected.text = "NO MAX PLAYERS SELECTED"
else:
$mapSelected.text = "NO MAP SELECTED"
else: else:
$mapSelected.text = "NO PORT SPECIFIED" $mapSelected.text = "NO PORT SPECIFIED"