2016-07-27 14:25:01 +02:00
|
|
|
package armory.trait;
|
|
|
|
|
2016-07-28 13:21:27 +02:00
|
|
|
import iron.Trait;
|
2016-12-07 02:01:42 +01:00
|
|
|
import iron.math.Vec4;
|
2016-12-07 14:33:18 +01:00
|
|
|
import iron.math.Quat;
|
2016-12-07 02:01:42 +01:00
|
|
|
import iron.system.Tween;
|
2016-07-28 13:21:27 +02:00
|
|
|
|
2016-07-27 14:25:01 +02:00
|
|
|
class NavAgent extends Trait {
|
|
|
|
|
2018-03-07 11:07:23 +01:00
|
|
|
@prop
|
2021-09-03 21:00:58 +02:00
|
|
|
public var speed: Float = 5;
|
2020-02-22 23:08:33 +01:00
|
|
|
@prop
|
2021-09-03 21:00:58 +02:00
|
|
|
public var turnDuration: Float = 0.4;
|
2018-03-07 11:07:23 +01:00
|
|
|
|
2019-12-19 23:54:08 +01:00
|
|
|
var path: Array<Vec4> = null;
|
2016-12-07 02:01:42 +01:00
|
|
|
var index = 0;
|
2016-07-27 14:25:01 +02:00
|
|
|
|
2019-12-19 23:54:08 +01:00
|
|
|
var rotAnim: TAnim = null;
|
|
|
|
var locAnim: TAnim = null;
|
2017-09-27 00:04:47 +02:00
|
|
|
|
2016-10-15 20:19:09 +02:00
|
|
|
public function new() {
|
|
|
|
super();
|
2016-12-07 02:01:42 +01:00
|
|
|
}
|
2016-07-27 14:25:01 +02:00
|
|
|
|
2019-12-19 23:54:08 +01:00
|
|
|
public function setPath(path: Array<Vec4>) {
|
2018-05-15 23:30:47 +02:00
|
|
|
stopTween();
|
2016-12-07 14:33:18 +01:00
|
|
|
|
2016-12-07 02:01:42 +01:00
|
|
|
this.path = path;
|
|
|
|
index = 1;
|
2016-10-15 20:19:09 +02:00
|
|
|
notifyOnUpdate(update);
|
2016-07-27 14:25:01 +02:00
|
|
|
|
2016-12-07 02:01:42 +01:00
|
|
|
go();
|
2016-07-27 14:25:01 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 23:30:47 +02:00
|
|
|
function stopTween() {
|
|
|
|
if (rotAnim != null) Tween.stop(rotAnim);
|
|
|
|
if (locAnim != null) Tween.stop(locAnim);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stop() {
|
|
|
|
stopTween();
|
|
|
|
path = null;
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:54:08 +01:00
|
|
|
function shortAngle(from: Float, to: Float): Float {
|
2016-12-07 14:33:18 +01:00
|
|
|
if (from < 0) from += Math.PI * 2;
|
|
|
|
if (to < 0) to += Math.PI * 2;
|
|
|
|
var delta = Math.abs(from - to);
|
|
|
|
if (delta > Math.PI) to = Math.PI * 2 - delta;
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
var orient = new Vec4();
|
2016-12-07 02:01:42 +01:00
|
|
|
function go() {
|
|
|
|
if (path == null || index >= path.length) return;
|
2016-12-07 14:33:18 +01:00
|
|
|
|
2016-12-07 02:01:42 +01:00
|
|
|
var p = path[index];
|
2017-12-10 12:01:58 +01:00
|
|
|
var dist = Vec4.distance(object.transform.loc, p);
|
2016-12-07 14:33:18 +01:00
|
|
|
|
|
|
|
orient.subvecs(p, object.transform.loc).normalize;
|
|
|
|
var targetAngle = Math.atan2(orient.y, orient.x) + Math.PI / 2;
|
2019-12-19 23:54:08 +01:00
|
|
|
locAnim = Tween.to({ target: object.transform.loc, props: { x: p.x, y: p.y, z: p.z }, duration: dist / speed, done: function() {
|
2016-12-07 02:01:42 +01:00
|
|
|
index++;
|
|
|
|
if (index < path.length) go();
|
2016-12-07 14:33:18 +01:00
|
|
|
else removeUpdate(update);
|
2017-06-29 21:17:07 +02:00
|
|
|
}});
|
2017-08-29 14:29:02 +02:00
|
|
|
|
|
|
|
var q = new Quat();
|
2020-02-22 23:57:16 +01:00
|
|
|
rotAnim = Tween.to({ target: object.transform, props: { rot: q.fromEuler(0, 0, targetAngle) }, duration: turnDuration});
|
2016-10-15 20:19:09 +02:00
|
|
|
}
|
2016-07-27 14:25:01 +02:00
|
|
|
|
2016-10-15 20:19:09 +02:00
|
|
|
function update() {
|
2016-12-07 14:33:18 +01:00
|
|
|
object.transform.buildMatrix();
|
2016-10-15 20:19:09 +02:00
|
|
|
}
|
2016-07-27 14:25:01 +02:00
|
|
|
}
|