Example #1
0
    // Use this for initialization
    void Awake()
    {
        instance = this;

        worldState = GetComponent <WorldState>();

        wayPointSystem = GetComponent <WayPointSystem>();


        wayPointSystem.CreateWaypointGrid();
        Debug.Log("Creating obstacles");
        for (int i = 0; i < numOfObstacles; i++)
        {
            obstacles.Add(Instantiate(ObstaclePrefab, GetObstacleCoordinate(), new Quaternion()));
            obstacles[i].transform.SetParent(gameObject.transform);
            obstacles[i].GetComponent <Obstacle>().GenerateObstacle2(Random.Range(4, 17));
            worldState.obstacles.Add(obstacles[i].GetComponent <Obstacle>());
        }
        timer                     = 0;
        doorInt                   = 0;
        currentNumOfWanders       = 0;
        currentNumOfSocials       = 0;
        currentNumberOfTravellers = 0;
        canStart                  = true;
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        if (!t && Time.time - timer > 1)
        {
            worked = UseWayPoint();
            t      = true;
            //    t2 = true;
            timer = Time.time;
        }
        if (!worked && Time.time - timer > 1)
        {
            ClearLevel();

            wayPointSystem.CreateWaypointGrid();
            for (int i = 0; i < numOfObstacles; i++)
            {
                obstacles.Add(Instantiate(ObstaclePrefab, GetObstacleCoordinate(), new Quaternion()));
                obstacles[i].transform.SetParent(gameObject.transform);
                obstacles[i].GetComponent <Obstacle>().GenerateObstacle2(Random.Range(4, 17));
                worldState.obstacles.Add(obstacles[i].GetComponent <Obstacle>());
            }
            timer = Time.time;
            t     = false;
        }
        if (worked && canStart)
        {
            // will try to spawn agent in empty space unitl all are intialized
            if (currentNumOfWanders < numOfWanders)
            {
                Vector3 pt = GetAgentCoordinate();

                // This is to prevent crashes if you ask for too many
                //if (numOfWanders < 8) {
                //    do {
                //        Physics.SphereCast(pt + Vector3.up * 4, 0.3f, Vector3.down, out hit, 3f);
                //        if (hit.transform != null)
                //            Debug.Log("Hit:" + hit.transform.tag);
                //    } while (hit.transform != null);

                //}

                //do {
                Physics.SphereCast(pt + Vector3.up * 4, 0.3f, Vector3.down, out hit, 3f);
                //if (hit.transform != null)
                //    Debug.Log("Hit:" + hit.transform.tag);
                //} while (hit.transform != null);

                if (hit.transform == null)
                {
                    GameObject go = Instantiate(AgentPrefab, pt, new Quaternion());
                    WorldState.GetInstance().agents.Add(go.GetComponent <Agent>());
                    go.GetComponent <Agent>().maxVelocity = 1 + Random.value * 2;
                    go.GetComponent <Agent>().agentType   = Agent.AgentType.WANDERING;
                    currentNumOfWanders++;
                }
            }

            //for (int i = 0; i < numOfSocials; i++) {
            if (currentNumOfSocials < numOfSocials)
            {
                Vector3 pt2 = GetAgentCoordinate();

                //do {
                Physics.SphereCast(pt2 + Vector3.up * 4, 0.3f, Vector3.down, out hit, 3f);
                //    if (hit.transform != null)
                //        Debug.Log("Hit:" + hit.transform.tag);
                //} while (hit.transform != null);
                if (hit.transform == null)
                {
                    GameObject go = Instantiate(AgentPrefab, pt2, new Quaternion());
                    WorldState.GetInstance().agents.Add(go.GetComponent <Agent>());
                    go.GetComponent <Agent>().maxVelocity = 1 + Random.value * 2;
                    go.GetComponent <Agent>().agentType   = Agent.AgentType.SOCIAL;
                    currentNumOfSocials++;
                }
            }
            if (currentNumOfSocials == numOfSocials &&
                currentNumOfWanders == numOfWanders)
            {
                foreach (Obstacle O in worldState.obstacles)
                {
                    if (Random.value < 0.5)
                    {
                        O.GetComponent <MeshCollider>().convex = false;
                    }
                }
            }
            //timer = Time.time;
        }
        if (worked && Time.time - timer > 2.0f)
        {
            Debug.Log("Instatiating travelling agents");
            t = true;

            // //Instatiate travelling agents
            if (currentNumberOfTravellers < numOfTravelers)
            {
                GameObject go = Instantiate(AgentPrefab, worldState.startDoor.transform.position, new Quaternion());
                WorldState.GetInstance().agents.Add(go.GetComponent <Agent>());
                go.GetComponent <Agent>().maxVelocity = 1 + Random.value * 2;
                go.GetComponent <Agent>().agentType   = Agent.AgentType.TRAVELLING;

                Vector3 target = worldState.startDoor.GetComponentInChildren <WayPoint>().nextPoint[go.GetComponent <TravellingAgent>().door].transform.position;
                go.GetComponent <TravellingAgent>().SetTargetPosition(target);
                currentNumberOfTravellers++;
            }
            timer = Time.time;
            //t2 = false;
        }
    }