From 695470bf7a24fd61040a394a04bac7460a96cc2c Mon Sep 17 00:00:00 2001 From: chenyulin Date: Thu, 27 Jun 2024 17:52:05 +0800 Subject: [PATCH] feat. add task system --- Assets/Script/DT/RouteGenerator.cs | 5 +- Assets/Script/ObjectSpaceManager.cs | 3 +- Assets/Script/TaskManager.cs | 98 +++++++++++++++++++++++++++++ Assets/Script/TaskManager.cs.meta | 11 ++++ Assets/Script/UI/ObjectFrame.cs | 18 ++++++ 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 Assets/Script/TaskManager.cs create mode 100644 Assets/Script/TaskManager.cs.meta diff --git a/Assets/Script/DT/RouteGenerator.cs b/Assets/Script/DT/RouteGenerator.cs index e6cf91e..a9087ed 100644 --- a/Assets/Script/DT/RouteGenerator.cs +++ b/Assets/Script/DT/RouteGenerator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UnityEngine; +[System.Serializable] public class KeyPoint { public Vector3 pos = Vector3.zero; @@ -14,7 +15,7 @@ public KeyPoint(Vector3 pos, bool grab = false, bool wait = false) this.wait = wait; } } - +[System.Serializable] public class Route { public List routes = new List(); @@ -26,8 +27,10 @@ public Route(List turningPoints) } for (int i = 1; i < turningPoints.Count; i++) { + KeyPoint start = turningPoints[i-1]; KeyPoint end = turningPoints[i]; + Debug.Log(start.pos.ToString() + ' ' + end.pos.ToString()); float dist = (start.pos - end.pos).magnitude; int sigment = (int)(dist * 20f); for (int j = 0; j <= sigment; j++) diff --git a/Assets/Script/ObjectSpaceManager.cs b/Assets/Script/ObjectSpaceManager.cs index 3edf526..0411967 100644 --- a/Assets/Script/ObjectSpaceManager.cs +++ b/Assets/Script/ObjectSpaceManager.cs @@ -63,8 +63,9 @@ public void UpdateObject(string cat, Vector3 pos, Vector3 rot, Vector3 size) } if (!pre_found) { - CreateObject(id, cat, pos, rot, size); id++; + CreateObject(id, cat, pos, rot, size); + } } diff --git a/Assets/Script/TaskManager.cs b/Assets/Script/TaskManager.cs new file mode 100644 index 0000000..f27afa5 --- /dev/null +++ b/Assets/Script/TaskManager.cs @@ -0,0 +1,98 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +public class ArmTask +{ + public enum Type + { + Finished, + MoveObject + } + public Type type = Type.Finished; + + // moveObject + public Vector3 move_start; + public Vector3 move_end; + + public void InitAsMoveObject(Vector3 start, Vector3 end) + { + type = Type.MoveObject; + move_start = start; + move_end = end; + } +} +public class TaskManager : MonoBehaviour +{ + public ArmTask task = new ArmTask(); + + public Transform ArmEnd; + public IKController IKSolver; + public RouteGenerator routeGenerator; + + Coroutine taskRoutine; + + GameObject taskSouce; + + public void GetTask(ArmTask t, GameObject sender) + { + + if (task.type == ArmTask.Type.Finished) + { + task = t; + taskSouce = sender; + Debug.Log("New " + task.type.ToString() + " task assigned."); + + if (taskRoutine != null) + { + } + else + { + taskRoutine = StartCoroutine(ExecuteMoveObject()); + } + } + else + { + Debug.Log("Current task not finished."); + } + + } + + IEnumerator ExecuteMoveObject() + { + Debug.Log("Start move object coroutine."); + // generate the action sequence + List turningPoint = new List(); + turningPoint.Add(new KeyPoint(ArmEnd.position, false)); + turningPoint.Add(new KeyPoint(task.move_start, true)); + turningPoint.Add(new KeyPoint(task.move_end, false)); + routeGenerator.SetRoute(turningPoint); + + + + yield return new WaitForSeconds(3); + taskRoutine = null; + task = new ArmTask(); + try + { + taskSouce.GetComponent().CancelAim(); + } + catch { } + + Debug.Log("Task Finished"); + yield break; + } + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Script/TaskManager.cs.meta b/Assets/Script/TaskManager.cs.meta new file mode 100644 index 0000000..63b152b --- /dev/null +++ b/Assets/Script/TaskManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 83a34a6d2f0e5c74ea4b197c0ea81ae3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Script/UI/ObjectFrame.cs b/Assets/Script/UI/ObjectFrame.cs index 77acc85..dac39e8 100644 --- a/Assets/Script/UI/ObjectFrame.cs +++ b/Assets/Script/UI/ObjectFrame.cs @@ -28,11 +28,26 @@ public enum Type ObjectFrame source; [SerializeField] Material aimMat; [SerializeField] LineRenderer Line; + [SerializeField] TaskManager taskManager; // for detect type; ObjectFrame dist; [SerializeField] Material objectMat; + public void SendTask() + { + if (taskManager) + { + ArmTask t = new ArmTask(); + t.InitAsMoveObject(source.Frame.transform.position, Frame.transform.position); + taskManager.GetTask(t, gameObject); + } + else + { + Debug.Log("TaskManager not found."); + } + } + public void InitFrame(Type t, Vector3 pos, Vector3 rot, Vector3 scale) { type = t; @@ -53,6 +68,9 @@ public void UpdateFrame(string cat, Vector3 pos, Vector3 rot, Vector3 scale) public void CancelAim() { Debug.Log("Cancel Aim"); + + + if (type == Type.Aim) { source.transform.Find("Frame").gameObject.GetComponent().ManipulationType = Microsoft.MixedReality.Toolkit.Utilities.ManipulationHandFlags.OneHanded;