Ejemplo n.º 1
0
 private bool checkIfCompatible(selectAgent a)
 {
     if (taskType == "Construction")
     {
         return(a.agentType == "ConstructionAgent");
     }
     else
     {
         return(a.agentType == "TransportAgent");
     }
 }
        // Update is called once per frame
        void FixedUpdate()
        {
            OnRightClick rc = plane.GetComponent <OnRightClick>();

            if (rc.clicked_off)
            {
                foreach (GameObject task in construction_tasks.Values)
                {
                    SelectTask selections = task.GetComponent <SelectTask>();
                    selections.rightClickAndSelected = false;
                }

                foreach (GameObject task in transport_tasks.Values)
                {
                    SelectTask selections = task.GetComponent <SelectTask>();
                    selections.rightClickAndSelected = false;
                }

                if (selectAgent.currentlySelected.Count != 0)
                {
                    selectAgent.DeselectAll();
                }
                rc.clicked_off = false;
            }

            if (!start_recieved)
            {
                rosSocket.Publish(updateID, message);
            }

            // Update agents through the update function
            if (agent_message != null)
            {
                if (!agents.ContainsKey(agent_message.name))
                {
                    GameObject addingAgent = new GameObject();
                    try
                    {
                        if (agent_message.agent_type == "ConstructionAgent")
                        {
                            addingAgent = Instantiate(contructionAgentModel, getVectFromMessage(agent_message.pose), getQuadFromMsg(agent_message.pose));
                        }
                        else
                        {
                            addingAgent = Instantiate(transportAgentModel, getVectFromMessage(agent_message.pose), getQuadFromMsg(agent_message.pose));
                        }
                        selectAgent menu = addingAgent.AddComponent <selectAgent>();
                        menu.agentType = agent_message.agent_type;
                        BoxCollider box = addingAgent.AddComponent <BoxCollider>();
                        box.size = box.size * 1.3f;
                        NameDisplay name_display = addingAgent.AddComponent <NameDisplay>();
                        if (agent_message.agent_type == "ConstructionAgent")
                        {
                            name_display.colour = red;
                        }
                        else
                        {
                            name_display.colour = blue;
                        }
                        AgentInfo agent_info = addingAgent.AddComponent <AgentInfo>();
                        name_display.name      = agent_message.name;
                        menu.agentName         = agent_message.name;
                        menu.agentCurrentTask  = agent_message.current_task;
                        menu.currentAction     = agent_message.current_action;
                        menu.agentCapabilities = getStringCompatibilities(agent_message.capabilities);
                        menu.movementSpeed     = agent_message.movement_speed.ToString();
                        menu.isAtTask          = agent_message.is_at_task.ToString();
                        agents.Add(agent_message.name, addingAgent);
                    } catch (ArgumentException e)
                    {
                        print("Some reason, the agent has already been added... " + e);
                        Destroy(addingAgent);
                        agents.Remove(agent_message.name);
                    }
                }
                else
                {
                    GameObject currentAgent;
                    agents.TryGetValue(agent_message.name, out currentAgent);
                    currentAgent.transform.position      = getVectFromMessage(agent_message.pose);
                    currentAgent.transform.localRotation = getQuadFromMsg(agent_message.pose);
                    selectAgent menu = currentAgent.GetComponent <selectAgent>();
                    menu.agentCurrentTask  = agent_message.current_task;
                    menu.currentAction     = agent_message.current_action;
                    menu.agentCapabilities = getStringCompatibilities(agent_message.capabilities);
                    menu.movementSpeed     = agent_message.movement_speed.ToString();
                    menu.isAtTask          = agent_message.is_at_task.ToString();
                }
                agent_message = null;
            }

            if (construction_task_message != null)
            {
                string PERM_CONTRUCTION_NAME = String.Copy(construction_task_message.name);
                if ((!construction_tasks.ContainsKey(construction_task_message.name)))
                {
                    try
                    {
                        if (!construction_task_message.is_complete)
                        {
                            GameObject addingConstructionTask = Instantiate(contructionTaskModel, getVectFromMessage(construction_task_message.pose, new Vector3(0, 1f, 0)), getQuadFromMsg(construction_task_message.pose));
                            construction_tasks.Add(PERM_CONTRUCTION_NAME, addingConstructionTask);
                            NameDisplay name_display = addingConstructionTask.AddComponent <NameDisplay>();
                            name_display.name   = PERM_CONTRUCTION_NAME;
                            name_display.colour = offRed;
                            BoxCollider box = addingConstructionTask.AddComponent <BoxCollider>();
                            box.size = box.size * 1.3f;
                            SelectTask taskSelect = addingConstructionTask.AddComponent <SelectTask>();
                            taskSelect.taskName = PERM_CONTRUCTION_NAME;
                            taskSelect.taskType = "Construction";
                        }
                    }
                    catch (ArgumentException e)
                    {
                        print("Some reason, the contruction task has already been added... " + e);
                    }
                }
                else
                {
                    GameObject currentConstructionTask;
                    construction_tasks.TryGetValue(construction_task_message.name, out currentConstructionTask);
                    if (construction_task_message.is_complete)
                    {
                        construction_tasks.Remove(construction_task_message.name);
                        Destroy(currentConstructionTask);
                    }
                }
                construction_task_message = null;
            }

            if (transport_task_message != null)
            {
                if (!transport_tasks.ContainsKey(transport_task_message.name))
                {
                    if (!transport_task_message.is_complete)
                    {
                        GameObject addingTransportTask    = Instantiate(transportTaskModel, getVectFromMessage(transport_task_message.pose, new Vector3(0, 0.47f, 0)), getQuadFromMsg(transport_task_message.pose));
                        GameObject addingTransportTaskEnd = Instantiate(dartModel, getVectFromMessage(transport_task_message.end_pose, new Vector3(0, 1.252f, 0)), Quaternion.Euler(90, 0, 0));
                        addingTransportTaskEnd.transform.localScale = new Vector3(200, 200, 200);
                        // Makes the transport task end a child of the adding transport task.
                        // addingTransportTaskEnd.transform.parent = addingTransportTask.transform;
                        transport_tasks.Add(transport_task_message.name, addingTransportTask);
                        LineRenderer line = addingTransportTask.AddComponent <LineRenderer>();
                        line.material = lineMaterial;
                        NameDisplay name_display = addingTransportTask.AddComponent <NameDisplay>();
                        name_display.colour = offBlue;
                        line.SetPositions(new Vector3[2] {
                            new Vector3(addingTransportTask.transform.position.x, 0.01f, addingTransportTask.transform.position.z),
                            new Vector3(addingTransportTaskEnd.transform.position.x, 0.01f, addingTransportTaskEnd.transform.position.z)
                        });
                        line.startWidth   = 0.05f;
                        line.endWidth     = 0.5f;
                        name_display.name = transport_task_message.name;
                        BoxCollider box = addingTransportTask.AddComponent <BoxCollider>();
                        box.size = box.size * 1.3f;
                        SelectTask taskSelect = addingTransportTask.AddComponent <SelectTask>();
                        taskSelect.taskName = transport_task_message.name;
                        taskSelect.taskType = "Transport";
                    }
                }
                else
                {
                    GameObject currentTransportTask;
                    transport_tasks.TryGetValue(transport_task_message.name, out currentTransportTask);
                    try
                    {
                        currentTransportTask.transform.position = getVectFromMessage(transport_task_message.pose, new Vector3(0, 0.47f, 0));
                        LineRenderer line = currentTransportTask.GetComponent <LineRenderer>();
                        line.SetPosition(0, new Vector3(currentTransportTask.transform.position.x, 0.01f, currentTransportTask.transform.position.z));
                        if (transport_task_message.is_complete)
                        {
                            transport_tasks.Remove(transport_task_message.name);
                            Destroy(currentTransportTask);
                        }
                    } catch (MissingReferenceException e)
                    {
                        print("Transport task was destroyed, the task is still in the dictionary for some reasons... Deleting it again.");
                        transport_tasks.Remove(transport_task_message.name);
                    }
                }
                transport_task_message = null;
            }



            if (construction_tasks.Count != 0)
            {
                foreach (GameObject task in construction_tasks.Values)
                {
                    task.transform.localEulerAngles = new Vector3(45, task.transform.localEulerAngles.y + 1f, 90);
                }
            }
        }