Ejemplo n.º 1
0
    public static WaypointTrigger DeserializeTrigger(JSONNode data)
    {
        if (data == null)
        {
            return(null);
        }
        var effectorsNode = data["effectors"].AsArray;
        var trigger       = new WaypointTrigger();

        for (var i = 0; i < effectorsNode.Count; i++)
        {
            var typeName = effectorsNode[i]["typeName"];
            if (typeName == null)
            {
                typeName = effectorsNode[i]["type_name"];
            }
            var newEffector = TriggersManager.GetEffectorOfType(typeName);
            if (newEffector == null)
            {
                Debug.LogWarning($"Could not find a trigger effector with type name: {typeName}.");
                continue;
            }

            newEffector.DeserializeProperties(effectorsNode[i]["parameters"]);
            trigger.AddEffector(newEffector);
        }

        return(trigger);
    }
Ejemplo n.º 2
0
        public void Execute(JSONNode args)
        {
            var uid       = args["uid"].Value;
            var waypoints = args["waypoints"].AsArray;
            var loop      = args["loop"];
            var api       = ApiManager.Instance;

            if (waypoints.Count == 0)
            {
                api.SendError(this, $"Waypoint list is empty");
                return;
            }

            if (api.Agents.TryGetValue(uid, out GameObject obj))
            {
                var npc = obj.GetComponent <NPCController>();
                if (npc == null)
                {
                    api.SendError(this, $"Agent '{uid}' is not a NPC agent");
                    return;
                }

                var wp = new List <DriveWaypoint>();
                for (int i = 0; i < waypoints.Count; i++)
                {
                    var deactivate = waypoints[i]["deactivate"];
                    var ts         = waypoints[i]["timestamp"];

                    wp.Add(new DriveWaypoint()
                    {
                        Position        = waypoints[i]["position"].ReadVector3(),
                        Speed           = waypoints[i]["speed"].AsFloat,
                        Angle           = waypoints[i]["angle"].ReadVector3(),
                        Idle            = waypoints[i]["idle"].AsFloat,
                        Deactivate      = deactivate.IsBoolean ? deactivate.AsBool : false,
                        TriggerDistance = waypoints[i]["trigger_distance"].AsFloat,
                        TimeStamp       = (ts == null) ? -1 : waypoints[i]["timestamp"].AsFloat,
                        Trigger         = WaypointTrigger.DeserializeTrigger(waypoints[i]["trigger"])
                    });;
                }

                var loopValue      = loop.IsBoolean ? loop.AsBool : false;
                var waypointFollow = npc.SetBehaviour <NPCWaypointBehaviour>();
                waypointFollow.SetFollowWaypoints(wp, loop); // TODO use NPCController to init waypoint data
                api.RegisterAgentWithWaypoints(npc.gameObject);
                api.SendResult(this);
            }
            else
            {
                api.SendError(this, $"Agent '{uid}' not found");
            }
        }
Ejemplo n.º 3
0
    public void CreateWayPoint(int index, int nbWaypoint, Vector2 position)
    {
        GameObject gm = Instantiate(waypointPref);

        gm.name = "waypoint" + index;

        WaypointTrigger w = gm.GetComponent <WaypointTrigger>();

        w.Index                    = index;
        w.NbWaypoint               = nbWaypoint;
        w.ParentID                 = transform;
        gm.transform.parent        = transform;
        gm.transform.localPosition = position;

        NetworkServer.Spawn(gm);
    }
Ejemplo n.º 4
0
        public void Execute(JSONNode args)
        {
            var uid       = args["uid"].Value;
            var waypoints = args["waypoints"].AsArray;
            var loop      = args["loop"].AsBool;
            var api       = ApiManager.Instance;

            if (waypoints.Count == 0)
            {
                api.SendError(this, $"Waypoint list is empty");
                return;
            }

            if (api.Agents.TryGetValue(uid, out GameObject obj))
            {
                var ped = obj.GetComponent <PedestrianController>();
                if (ped == null)
                {
                    api.SendError(this, $"Agent '{uid}' is not a pedestrian agent");
                    return;
                }

                var wp = new List <WalkWaypoint>();
                for (int i = 0; i < waypoints.Count; i++)
                {
                    wp.Add(new WalkWaypoint()
                    {
                        Position        = waypoints[i]["position"].ReadVector3(),
                        Speed           = waypoints[i]["speed"].AsFloat,
                        Idle            = waypoints[i]["idle"].AsFloat,
                        TriggerDistance = waypoints[i]["trigger_distance"].AsFloat,
                        Trigger         = WaypointTrigger.DeserializeTrigger(waypoints[i]["trigger"])
                    });
                }

                ped.FollowWaypoints(wp, loop);
                api.RegisterAgentWithWaypoints(ped.gameObject);
                api.SendResult(this);
            }
            else
            {
                api.SendError(this, $"Agent '{uid}' not found");
            }
        }
        private WaypointTrigger DeserializeTrigger(JSONNode data)
        {
            if (data == null)
            {
                return(null);
            }
            var effectorsNode = data["effectors"].AsArray;
            var trigger       = new WaypointTrigger();

            trigger.Effectors = new List <TriggerEffector>();
            for (int i = 0; i < effectorsNode.Count; i++)
            {
                var typeName    = effectorsNode[i]["type_name"];
                var newEffector = TriggersManager.GetEffectorOfType(typeName);
                newEffector.DeserializeProperties(effectorsNode[i]["parameters"]);
                trigger.Effectors.Add(newEffector);
            }

            return(trigger);
        }
Ejemplo n.º 6
0
        public void Execute(JSONNode args)
        {
            var api       = ApiManager.Instance;
            var uid       = args["uid"].Value;
            var waypoints = args["waypoints"].AsArray;
            // Try parse the path type, set linear as default
            var pathTypeNode = args["waypoints_path_type"];
            WaypointsPathType waypointsPathType;

            if (pathTypeNode == null)
            {
                waypointsPathType = WaypointsPathType.Linear;
            }
            else if (!Enum.TryParse(pathTypeNode, true, out waypointsPathType))
            {
                waypointsPathType = WaypointsPathType.Linear;
                api.SendError(this, $"Could not parse the waypoints path type \"{waypointsPathType}\".");
            }
            var loop = args["loop"].AsBool;

            if (waypoints.Count == 0)
            {
                api.SendError(this, $"Waypoint list is empty");
                return;
            }

            if (api.Agents.TryGetValue(uid, out GameObject obj))
            {
                var ped = obj.GetComponent <PedestrianController>();
                if (ped == null)
                {
                    api.SendError(this, $"Agent '{uid}' is not a pedestrian agent");
                    return;
                }

                var wp = new List <WalkWaypoint>();
                var previousPosition = ped.transform.position;
                for (int i = 0; i < waypoints.Count; i++)
                {
                    var position = waypoints[i]["position"].ReadVector3();
                    var angle    = waypoints[i].HasKey("angle") ?
                                   waypoints[i]["angle"].ReadVector3() :
                                   Quaternion.LookRotation((position - previousPosition).normalized).eulerAngles;

                    wp.Add(new WalkWaypoint()
                    {
                        Position        = position,
                        Angle           = angle,
                        Speed           = waypoints[i]["speed"].AsFloat,
                        Idle            = waypoints[i]["idle"].AsFloat,
                        TriggerDistance = waypoints[i]["trigger_distance"].AsFloat,
                        Trigger         = WaypointTrigger.DeserializeTrigger(waypoints[i]["trigger"])
                    });
                    previousPosition = position;
                }

                var waypointBehaviour = ped.SetBehaviour <PedestrianWaypointBehaviour>();
                waypointBehaviour.SetFollowWaypoints(wp, loop, waypointsPathType);
                api.RegisterAgentWithWaypoints(ped.gameObject);
                api.SendResult(this);
            }
            else
            {
                api.SendError(this, $"Agent '{uid}' not found");
            }
        }
Ejemplo n.º 7
0
        public void Execute(JSONNode args)
        {
            var api       = ApiManager.Instance;
            var uid       = args["uid"].Value;
            var waypoints = args["waypoints"].AsArray;
            // Try parse the path type, set linear as default
            var pathTypeNode = args["waypoints_path_type"];
            WaypointsPathType waypointsPathType;

            if (pathTypeNode == null)
            {
                waypointsPathType = WaypointsPathType.Linear;
            }
            else if (!Enum.TryParse(pathTypeNode, true, out waypointsPathType))
            {
                waypointsPathType = WaypointsPathType.Linear;
                api.SendError(this, $"Could not parse the waypoints path type \"{waypointsPathType}\".");
            }
            var loop = args["loop"];

            if (waypoints.Count == 0)
            {
                api.SendError(this, $"Waypoint list is empty");
                return;
            }

            if (api.Agents.TryGetValue(uid, out GameObject obj))
            {
                var npc = obj.GetComponent <NPCController>();
                if (npc == null)
                {
                    api.SendError(this, $"Agent '{uid}' is not a NPC agent");
                    return;
                }

                var wp = new List <DriveWaypoint>();
                var previousPosition = npc.transform.position;
                for (int i = 0; i < waypoints.Count; i++)
                {
                    var deactivate = waypoints[i]["deactivate"];
                    var ts         = waypoints[i]["timestamp"];
                    var position   = waypoints[i]["position"].ReadVector3();
                    var angle      = waypoints[i].HasKey("angle") ?
                                     waypoints[i]["angle"].ReadVector3() :
                                     Quaternion.LookRotation((position - previousPosition).normalized).eulerAngles;

                    wp.Add(new DriveWaypoint()
                    {
                        Position        = position,
                        Speed           = waypoints[i]["speed"].AsFloat,
                        Acceleration    = waypoints[i]["acceleration"].AsFloat,
                        Angle           = angle,
                        Idle            = waypoints[i]["idle"].AsFloat,
                        Deactivate      = deactivate.IsBoolean ? deactivate.AsBool : false,
                        TriggerDistance = waypoints[i]["trigger_distance"].AsFloat,
                        TimeStamp       = (ts == null) ? -1 : waypoints[i]["timestamp"].AsFloat,
                        Trigger         = WaypointTrigger.DeserializeTrigger(waypoints[i]["trigger"])
                    });
                    previousPosition = position;
                }

                var loopValue      = loop.IsBoolean && loop.AsBool;
                var waypointFollow = npc.SetBehaviour <NPCWaypointBehaviour>();
                waypointFollow.SetFollowWaypoints(wp, loopValue, waypointsPathType); // TODO use NPCController to init waypoint data
                api.RegisterAgentWithWaypoints(npc.gameObject);
                api.SendResult(this);
            }
            else
            {
                api.SendError(this, $"Agent '{uid}' not found");
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Deserializes a trigger from the json data
 /// </summary>
 /// <param name="trigger">Trigger object to fill with effectors</param>
 /// <param name="triggerNode">Json data with a trigger</param>
 private static void DeserializeTrigger(ScenarioTrigger trigger, JSONNode triggerNode)
 {
     trigger.Trigger = WaypointTrigger.DeserializeTrigger(triggerNode);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Deserializes a trigger from the json data
 /// </summary>
 /// <param name="trigger">Trigger object to fill with effectors</param>
 /// <param name="triggerNode">Json data with a trigger</param>
 private void DeserializeTrigger(ScenarioTrigger trigger, JSONNode triggerNode)
 {
     trigger.Trigger         = WaypointTrigger.DeserializeTrigger(triggerNode);
     trigger.TargetAgentType = ParentAgent.Type;
 }