2014-11-19 15:33:15 +01:00
|
|
|
|
|
|
|
extends Navigation
|
|
|
|
|
2015-11-21 13:31:25 +01:00
|
|
|
# Member variables
|
2015-12-09 08:38:23 +01:00
|
|
|
const SPEED = 4.0
|
2014-11-19 15:33:15 +01:00
|
|
|
|
2015-12-09 08:38:23 +01:00
|
|
|
var camrot = 0.0
|
2014-11-19 15:33:15 +01:00
|
|
|
|
2015-12-09 08:38:23 +01:00
|
|
|
var begin = Vector3()
|
|
|
|
var end = Vector3()
|
2014-11-19 15:33:15 +01:00
|
|
|
var m = FixedMaterial.new()
|
|
|
|
|
2015-12-09 08:38:23 +01:00
|
|
|
var path = []
|
2015-11-21 13:31:25 +01:00
|
|
|
var draw_path = false
|
2014-11-19 15:33:15 +01:00
|
|
|
|
|
|
|
|
2015-12-09 08:38:23 +01:00
|
|
|
func _process(delta):
|
|
|
|
if (path.size() > 1):
|
2014-11-19 15:33:15 +01:00
|
|
|
var to_walk = delta*SPEED
|
2015-12-09 08:38:23 +01:00
|
|
|
var to_watch = Vector3(0, 1, 0)
|
|
|
|
while(to_walk > 0 and path.size() >= 2):
|
|
|
|
var pfrom = path[path.size() - 1]
|
|
|
|
var pto = path[path.size() - 2]
|
2014-11-19 15:33:15 +01:00
|
|
|
to_watch = (pto - pfrom).normalized()
|
|
|
|
var d = pfrom.distance_to(pto)
|
2015-12-09 08:38:23 +01:00
|
|
|
if (d <= to_walk):
|
|
|
|
path.remove(path.size() - 1)
|
|
|
|
to_walk -= d
|
2014-11-19 15:33:15 +01:00
|
|
|
else:
|
2015-12-09 08:38:23 +01:00
|
|
|
path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk/d)
|
|
|
|
to_walk = 0
|
|
|
|
|
|
|
|
var atpos = path[path.size() - 1]
|
2014-11-19 15:33:15 +01:00
|
|
|
var atdir = to_watch
|
2015-12-09 08:38:23 +01:00
|
|
|
atdir.y = 0
|
2014-11-19 15:33:15 +01:00
|
|
|
|
|
|
|
var t = Transform()
|
2015-12-09 08:38:23 +01:00
|
|
|
t.origin = atpos
|
|
|
|
t=t.looking_at(atpos + atdir, Vector3(0, 1, 0))
|
2014-11-19 15:33:15 +01:00
|
|
|
get_node("robot_base").set_transform(t)
|
|
|
|
|
2015-12-09 08:38:23 +01:00
|
|
|
if (path.size() < 2):
|
|
|
|
path = []
|
2014-11-19 15:33:15 +01:00
|
|
|
set_process(false)
|
|
|
|
else:
|
|
|
|
set_process(false)
|
|
|
|
|
|
|
|
|
|
|
|
func _update_path():
|
2015-12-09 08:38:23 +01:00
|
|
|
var p = get_simple_path(begin, end, true)
|
|
|
|
path = Array(p) # Vector3array too complex to use, convert to regular array
|
2014-11-19 15:33:15 +01:00
|
|
|
path.invert()
|
|
|
|
set_process(true)
|
|
|
|
|
|
|
|
if (draw_path):
|
|
|
|
var im = get_node("draw")
|
|
|
|
im.set_material_override(m)
|
|
|
|
im.clear()
|
2015-12-09 08:38:23 +01:00
|
|
|
im.begin(Mesh.PRIMITIVE_POINTS, null)
|
2014-11-19 15:33:15 +01:00
|
|
|
im.add_vertex(begin)
|
|
|
|
im.add_vertex(end)
|
|
|
|
im.end()
|
2015-12-09 08:38:23 +01:00
|
|
|
im.begin(Mesh.PRIMITIVE_LINE_STRIP, null)
|
2014-11-19 15:33:15 +01:00
|
|
|
for x in p:
|
|
|
|
im.add_vertex(x)
|
|
|
|
im.end()
|
|
|
|
|
|
|
|
|
2015-12-09 08:38:23 +01:00
|
|
|
func _input(event):
|
|
|
|
if (event.type == InputEvent.MOUSE_BUTTON and event.button_index == BUTTON_LEFT and event.pressed):
|
|
|
|
var from = get_node("cambase/Camera").project_ray_origin(event.pos)
|
|
|
|
var to = from + get_node("cambase/Camera").project_ray_normal(event.pos)*100
|
|
|
|
var p = get_closest_point_to_segment(from, to)
|
|
|
|
|
|
|
|
begin = get_closest_point(get_node("robot_base").get_translation())
|
|
|
|
end = p
|
2014-11-19 15:33:15 +01:00
|
|
|
|
|
|
|
_update_path()
|
2015-12-09 08:38:23 +01:00
|
|
|
|
|
|
|
if (event.type == InputEvent.MOUSE_MOTION):
|
|
|
|
if (event.button_mask&BUTTON_MASK_MIDDLE):
|
|
|
|
camrot += event.relative_x*0.005
|
|
|
|
get_node("cambase").set_rotation(Vector3(0, camrot, 0))
|
2014-11-19 15:33:15 +01:00
|
|
|
print("camrot ", camrot)
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
set_process_input(true)
|
|
|
|
m.set_line_width(3)
|
|
|
|
m.set_point_size(3)
|
2015-12-09 08:38:23 +01:00
|
|
|
m.set_fixed_flag(FixedMaterial.FLAG_USE_POINT_SIZE, true)
|
|
|
|
m.set_flag(Material.FLAG_UNSHADED, true)
|
2014-11-19 15:33:15 +01:00
|
|
|
#begin = get_closest_point(get_node("start").get_translation())
|
|
|
|
#end = get_closest_point(get_node("end").get_translation())
|
|
|
|
#call_deferred("_update_path")
|