Godot-RTS-Concept/DroneMgr.gd

92 lines
2.4 KiB
GDScript3
Raw Normal View History

2023-11-16 15:00:37 -04:00
extends Node3D
@export var camera: Camera3D
@export var drone: PackedScene
@export var navreg: NavigationRegion3D
var targetLoc= Vector3()
var drones = Array()
var curDrone = 0
var ignoreNext = false
var clickPos
@export var indivRadius = 1
var spawnpoints = Array()
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var lol = 1
if Input.is_action_just_pressed("right_click"):
var newDrone = drone.instantiate()
self.add_child(newDrone)
targetLoc = _click_intersect()
drones.append(newDrone)
newDrone.position = targetLoc
newDrone.cam = camera
newDrone.navres = navreg
if Input.is_action_just_pressed("left_click") && (drones.size()>0):
for d in drones:
d.canMove = false
_click_intersect()
drones[0]._do_path(spawnpoints[0])
#newDrone.canMove = true
func _click_intersect():
var space_state = get_world_3d().direct_space_state
var mouse_position = get_viewport().get_mouse_position()
var rayOrigin = camera.project_ray_origin(mouse_position)
var rayEnd = rayOrigin + camera.project_ray_normal(mouse_position)*20000
var query = PhysicsRayQueryParameters3D.create(rayOrigin,rayEnd)
var intersection = space_state.intersect_ray(query)
if intersection.is_empty():
intersection = get_global_position()
print("nothing found")
else:
print (get_name())
clickPos = intersection.position
_circle_points(clickPos)
return clickPos
func _circle_points(center:Vector3):
spawnpoints.clear()
var count = drones.size()
var radius = 0
var angle = 1
if(count == 0): return
elif(count == 1): spawnpoints.append(center)
elif (count>2):
angle = _deg_rad(360/count)
radius = indivRadius/sin(angle)
for i in range(count):
spawnpoints.append(center + (Vector3.FORWARD*radius*1.5).rotated(Vector3.UP, angle*i))
func _deg_rad(f:float):
return(f*0.0174533)
func _on_navigation_region_3d_bake_finished():
if(ignoreNext == false):
curDrone = curDrone+1
if(curDrone<drones.size()):
await get_tree().create_timer(0.05).timeout
drones[curDrone]._do_path(spawnpoints[curDrone])
else:
for d in drones:
d.canMove = true
d.cleanUp()
await get_tree().create_timer(0.05).timeout
navreg.bake_navigation_mesh()
curDrone = 0
ignoreNext = true
else:
ignoreNext = false