Esempio n. 1
0
        //Leave goal null to get random goal
        public VehicleSpawner(CityNode node, int timeToSpawn, List <CityNode> goals)
        {
            this.node        = node;
            this.timeToSpawn = timeToSpawn;

            this.goals = goals;
        }
Esempio n. 2
0
 public IEnumerable <CityNode> FindAll()
 {
     return(File.ReadAllLines(addressFile)
            .Skip(1)
            .Select(v => CityNode.FromCsv(v))
            .ToList());
 }
Esempio n. 3
0
        public override void Build(City c)
        {
            double xChange = 40 * Math.Cos(dir);
            double yChange = 40 * Math.Sin(dir);

            enter = new CityNode(x + (int)xChange, y + (int)yChange);
            exit  = new CityNode(x - (int)xChange, y - (int)yChange);

            xChange = 60 * Math.Cos(dir);
            yChange = 60 * Math.Sin(dir);

            p1 = new CityNode(x - (int)xChange, y - (int)yChange);
            p2 = new CityNode(x + (int)xChange, y + (int)yChange);

            xChange = 60 * Math.Cos(dir - Math.PI / 2);
            yChange = 60 * Math.Sin(dir - Math.PI / 2);

            pMid = new CityNode(x + (int)xChange, y + (int)yChange);

            enter.Connect(exit, CityPathType.privates, 40);

            p1.Connect(pMid, CityPathType.pedestrians);
            p2.Connect(pMid, CityPathType.pedestrians);
            pMid.Connect(p1, CityPathType.pedestrians);
            pMid.Connect(p2, CityPathType.pedestrians);

            c.nodes.Add(enter);
            c.nodes.Add(exit);
            c.nodes.Add(p1);
            c.nodes.Add(p2);
            c.nodes.Add(pMid);
        }
Esempio n. 4
0
 public void Connect(CityBuilderPath p, out CityNode start, out CityNode end, out CityNode p1, out CityNode p2)
 {
     start = exit;
     end   = enter;
     p1    = this.p1;
     p2    = this.p2;
 }
 public void Connect(CityBuilderPath p, out CityNode enter, out CityNode exit, out CityNode p1, out CityNode p2)
 {
     if (right == p)
     {
         enter = eRight;
         exit  = wRight;
         p1    = pBottomRight;
         p2    = pTopRight;
     }
     else if (left == p)
     {
         enter = eLeft;
         exit  = wLeft;
         p1    = pTopLeft;
         p2    = pBottomLeft;
     }
     else if (bottom == p)
     {
         enter = eBottom;
         exit  = wBottom;
         p1    = pBottomLeft;
         p2    = pBottomRight;
     }
     else
     {
         enter = eTop;
         exit  = wTop;
         p1    = pTopRight;
         p2    = pTopLeft;
     }
 }
Esempio n. 6
0
    public IEnumerator onTreatDisease(int senderPlayer, Colour c)
    {
        CityNode city = LevelManager.instance.players[senderPlayer].getLocation();

        GetComponent <PhotonView>().RPC("treatDisease", PhotonTargets.All, new object[] { city, c });
        currentActionsRemaining -= 1;
        switch ((Colour)c)
        {
        case Colour.BLUE:
            blueRem += 1;
            break;

        case Colour.BLACK:
            blackRem += 1;
            break;

        case Colour.PURPLE:
            purpleRem += 1;
            break;

        case Colour.RED:
            redRem += 1;
            break;

        case Colour.YELLOW:
            yellowRem += 1;
            break;
        }
        while (EventTransferManager.instance.waitingForPlayer)
        {
            yield return(new WaitForEndOfFrame());
        }
    }
Esempio n. 7
0
        public override double Compare(Node node)
        {
            var bdn = node as BibliographicDescriptionNode;

            if (bdn == null)
            {
                throw new Exception("Not Bibliographic DescriptionNode");
            }

            double difference = 0;

            difference += TitleNode.Compare(bdn.TitleNode);

            if (difference < 0.05 || difference > 0.25)
            {
                return(difference);
            }

            difference += AutorRangeNode.Compare(bdn.AutorRangeNode);
            difference += PublisherNode.Compare(bdn.PublisherNode);
            difference += CityNode.Compare(bdn.CityNode);
            difference += YearNode.Compare(bdn.YearNode);
            difference += PageCountNode.Compare(bdn.PageCountNode);

            return(difference);
        }
Esempio n. 8
0
        /// <summary>
        /// Scan if the final destination is within stopping distance. If it is, stop and park there.
        /// </summary>
        public void CheckaArrivalAndParking()
        {
            if (Util.Distance(x, y, dest.x, dest.y) <= Util.StopDistance(speed, DEACCELERATION))
            {
                limits.RemoveAll(l => l.name == "arrival");
                limits.Add(new SpeedLimiter(0, "arrival"));
            }
            else
            {
                limits.RemoveAll(l => l.name == "arrival");
            }

            //Snap to the parking node, and enter "parking" state.
            if (limits.Where(l => l.name == "arrival").Count() > 0 && speed <= 0.2)
            {
                x      = dest.x;
                y      = dest.y;
                speed  = 0;
                parked = true;
                limits.RemoveAll(l => l.name == "arrival");
                lastVisited = dest;
                (lastVisited as ParkingNode).vehicle = this;
                Console.WriteLine(this + " has arrived and parked at its destination");
            }

            if (parked)
            {
                limits.RemoveAll(l => l.name == "parked");
                limits.Add(new SpeedLimiter(0, "parked"));
            }
            else
            {
                limits.RemoveAll(l => l.name == "parked");
            }
        }
Esempio n. 9
0
    public List <CityNode> FindPathCalm(CityNode start, CityNode destination)
    {
        Queue <CityNode> frontier = new Queue <CityNode>();
        Dictionary <CityNode, CityNode> visited = new Dictionary <CityNode, CityNode>();

        frontier.Enqueue(start);

        int MAX     = 1000;
        int control = 0;

        while (frontier.Count > 0 || control < MAX)
        {
            CityNode tmp = frontier.Dequeue();
            if (tmp == destination)
            {
                break;
            }

            foreach (CityNode node in tmp.neighbors)
            {
                if (!visited.ContainsKey(node))
                {
                    if (node.type == NodeType.PedestrianEntrance || node.type == NodeType.Pavement || node.type == NodeType.CarEntrance)
                    {
                        frontier.Enqueue(node);
                        visited[node] = tmp;
                    }
                }
            }
            if (frontier.Count == 0)
            {
                return(path);
            }
            control++;
        }

/*         foreach (KeyValuePair<CityNode, CityNode> kvp in visited)
 *      {
 *          if (kvp.Key != null && kvp.Value != null)
 *              print("Key =" + kvp.Key.transform.position +"Value =" + kvp.Value.transform.position);
 *          else
 *              print(kvp.Key.transform.position);
 *      }
 */     CityNode path_temp = destination;

        path.Add(destination);
        while (true)
        {
            path_temp = visited[path_temp];
            path.Add(path_temp);
            if (visited[path_temp] == currentNode)
            {
                break;
            }
        }
        path.Add(currentNode);
        path.Reverse();

        return(path);
    }
Esempio n. 10
0
 public IEnumerator highlightCity(int senderPlayer, bool turnOn, CityNode c)
 {
     //highlight ...
     while (EventTransferManager.instance.waitingForPlayer)
     {
         yield return(new WaitForEndOfFrame());
     }
 }
Esempio n. 11
0
 public IEnumerator onShuttleFlight(int senderPlayer, CityNode dest)
 {
     GetComponent <PhotonView>().RPC("MoveSelfPawn", PhotonTargets.All, new object[] { senderPlayer, dest });
     currentActionsRemaining -= 1;
     while (EventTransferManager.instance.waitingForPlayer)
     {
         yield return(new WaitForEndOfFrame());
     }
 }
Esempio n. 12
0
 public BibliographicDescriptionNode(string title, IEnumerable <string> autors, string publisher, string city, string year, string pageCount)
 {
     TitleNode      = new TitleNode(title);
     AutorRangeNode = new AutorRangeNode(autors);
     PublisherNode  = new PublisherNode(publisher);
     CityNode       = new CityNode(city);
     YearNode       = new YearNode(year);
     PageCountNode  = new PageCountNode(pageCount);
 }
Esempio n. 13
0
 public IEnumerator onCharterFlight(int senderPlayer, CityNode dest, CityCard cc)
 {
     CityCard[] ccs = new CityCard[1];
     ccs[0] = cc;
     GetComponent <PhotonView>().RPC("discardFromPlayerHand", PhotonTargets.All, new object[] { senderPlayer, ccs });
     GetComponent <PhotonView>().RPC("MoveSelfPawn", PhotonTargets.All, new object[] { senderPlayer, dest });
     currentActionsRemaining -= 1;
     while (EventTransferManager.instance.waitingForPlayer)
     {
         yield return(new WaitForEndOfFrame());
     }
 }
Esempio n. 14
0
    public void PickDestination()
    {
        int temp = Random.Range(0, cityGraph.pedestrianEntranceNodes.Count);

        if (cityGraph.pedestrianEntranceNodes[temp] != currentNode)
        {
            testingDestination = cityGraph.pedestrianEntranceNodes[temp];
        }
        else
        {
            PickDestination();
        }
    }
Esempio n. 15
0
 public void MoveToNextNode()
 {
     if ((new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(path[index + 1].transform.position.x, 0, path[index + 1].transform.position.z)).magnitude < 0.05f)
     {
         if (state == WalkerState.Rage || state == WalkerState.Fear)
         {
             index++;
             currentNode = path[index];
             gameObject.transform.position = new Vector3(path[index].transform.position.x, 0, path[index].transform.position.z);
         }
         else
         {
             if (path.Count - index > 2)
             {
                 if (path[index + 2].type == NodeType.Street)
                 {
                     waiting = true;
                 }
                 else
                 {
                     waiting = false;
                     index++;
                     currentNode = path[index];
                     gameObject.transform.position = new Vector3(path[index].transform.position.x, 0, path[index].transform.position.z);
                 }
             }
             else
             {
                 waiting = false;
                 index++;
                 currentNode = path[index];
                 gameObject.transform.position = new Vector3(path[index].transform.position.x, 0, path[index].transform.position.z);
             }
         }
     }
     else
     {
         if (state == WalkerState.Calm)
         {
             gameObject.transform.position += (new Vector3(path[index + 1].transform.position.x, 0, path[index + 1].transform.position.z) - new Vector3(transform.position.x, 0, transform.position.z)).normalized * calmSpeed * Time.deltaTime;
         }
         else if (state == WalkerState.Rage)
         {
             gameObject.transform.position += (new Vector3(path[index + 1].transform.position.x, 0, path[index + 1].transform.position.z) - new Vector3(transform.position.x, 0, transform.position.z)).normalized * rageSpeed * Time.deltaTime;
         }
         else if (state == WalkerState.Fear)
         {
             gameObject.transform.position += (new Vector3(path[index + 1].transform.position.x, 0, path[index + 1].transform.position.z) - new Vector3(transform.position.x, 0, transform.position.z)).normalized * fearSpeed * Time.deltaTime;
         }
     }
 }
Esempio n. 16
0
    public List <CityNode> FindPathToClosestSafeSpot(CityNode start, CityNode destination)
    {
        Queue <CityNode> frontier = new Queue <CityNode>();
        Dictionary <CityNode, CityNode> visited = new Dictionary <CityNode, CityNode>();

        frontier.Enqueue(start);

        int MAX     = 1000;
        int control = 0;

        while (frontier.Count > 0 || control < MAX)
        {
            CityNode tmp = frontier.Dequeue();
            if (tmp == destination)
            {
                break;
            }
            foreach (CityNode node in tmp.neighbors)
            {
                if (!visited.ContainsKey(node))
                {
                    frontier.Enqueue(node);
                    visited[node] = tmp;
                }
            }
            if (frontier.Count == 0)
            {
                return(path);
            }
            control++;
        }
        CityNode path_temp = destination;

        //Debug.Log(destination);
        path.Add(destination);
        while (true)
        {
            path_temp = visited[path_temp];
            path.Add(path_temp);
            if (visited[path_temp] == currentNode)
            {
                break;
            }
        }
        path.Add(currentNode);
        path.Reverse();

        return(path);
    }
Esempio n. 17
0
 public void MoveSquad(CityNode position)
 {
     if (!moving || position == origin)
     {
         if (position == origin)
         {
             origin = destination;
         }
         destination = position;
         moving      = true;
     }
     if (squadSpeed == null)
     {
         SetSquadSpeed();
     }
 }
Esempio n. 18
0
        public double DistanceToNodeOnPath(CityNode n)
        {
            double totalDist = Util.Distance(x, y, CurrentCityPath.e.x, CurrentCityPath.e.y);

            foreach (CityPath p in path)
            {
                if (p != CurrentCityPath)
                {
                    totalDist += Util.Distance(p.s.x, p.s.y, p.e.x, p.e.y);
                }
                if (p.e == n)
                {
                    return(totalDist);
                }
            }
            return(totalDist);
        }
Esempio n. 19
0
        public void DriveTo(CityNode dest)
        {
            Console.WriteLine(this + " is driving to " + dest + "...");
            Console.WriteLine("its owner's goal is " + owner.goal);

            if (owner.currentRide != this)
            {
                throw new Exception("Car trying to drive without owner? (bug)");
            }

            (lastVisited as ParkingNode).vehicle = null;
            this.dest     = dest;
            path          = City.GetPath(lastVisited, dest, CityPathType.privates);
            currentDestId = 0;
            CurrentCityPath.vehicles.Insert(0, this);
            parked = false;
        }
Esempio n. 20
0
        public void Save(CityNode citynode)
        {
            var addresses = FindAll().ToList();

            if (!addresses.Exists(a => a.CityNodeId == citynode.CityNodeId))
            {
                addresses.Add(citynode);
            }

            var text = new StringBuilder();

            foreach (var a in addresses)
            {
                text.AppendLine(a.ToString());
            }

            File.WriteAllText(addressFile, text.ToString());
        }
Esempio n. 21
0
    public void PickDestinationInFear()
    {
        Debug.Log(fearedNode);
        Vector3  fearDirection     = (currentNode.transform.position - fearedNode.transform.position).normalized * fearRange;
        Vector3  fearedDestination = fearDirection + fearedNode.transform.position;
        float    championMagnitude = (fearedDestination - fearedNode.transform.position).magnitude;
        CityNode champion          = fearedNode;

        foreach (CityNode node in cityGraph.allNodes)
        {
            if ((node.transform.position - fearedDestination).magnitude <= championMagnitude)
            {
                champion          = node;
                championMagnitude = (node.transform.position - fearedDestination).magnitude;
            }
        }
        testingDestination = champion;
    }
Esempio n. 22
0
        public GasolineCar(CityNode start)
        {
            CARS_COUNT++;

            this.x = start.x;
            this.y = start.y;

            this.capacity = 5;

            this.lastVisited = start;

            this.source = start;

            this.dir = Util.GetLookatDir(x, y, start.paths[0].e.x, start.paths[0].e.y);
            limits.Add(new SpeedLimiter(MAX_SPEED, "max speed"));

            parked = true;
            (lastVisited as ParkingNode).vehicle = this;
        }
Esempio n. 23
0
        public override void Update()
        {
            time++;

            if (time >= timeToSpawn && GasolineCar.CARS_COUNT < MAX_VEHICLE_AMOUNT)
            {
                time = 0;

                CityNode goal = goals[rand.Next(goals.Count)];

                if (lastSpawn == null || Util.Distance(lastSpawn.x, lastSpawn.y, node.x, node.y) >= Util.ToPixels(10))
                {
                    T t = (T)Activator.CreateInstance(typeof(T), node, goal);
                    Console.WriteLine("Spawning " + t);
                    Sim.instance.vehicles.Add(t);

                    lastSpawn = t;
                }
            }
        }
Esempio n. 24
0
        /// <summary>
        /// Check if the car is very close to its current node destination (it will never be exactly on it due to nominal time calculations).
        /// If it is, snap to the node and start moving towards the next one.
        /// </summary>
        public void HandleNodeArrival()
        {
            //Find the width distance and length distance of the car from the destination node
            double angle = Util.GetLookatDir(x, y, CurrentCityPath.e.x, CurrentCityPath.e.y) % 360;

            if (angle < 0)
            {
                angle += Math.PI * 2;
            }
            double dirPositive = dir % 360;

            if (dirPositive < 0)
            {
                dirPositive += Math.PI * 2;
            }

            double d            = angle - MathHelper.ToRadians(270) - dirPositive;
            double distToTarget = Util.Distance(x, y, CurrentCityPath.e.x, CurrentCityPath.e.y);

            //Check if the distances are close enough
            if (distToTarget * Math.Cos(d) < 8 && distToTarget * Math.Sin(d) < 2)
            {
                lastVisited = CurrentCityPath.e;

                //If this car just exit an intersection, notify the intersection that it is now free.
                if (CurrentCityPath.e is IntersectionExitNode)
                {
                    (CurrentCityPath.e as IntersectionExitNode).intersection.Exit(this);
                }

                //Snap to the node
                x = CurrentCityPath.e.x;
                y = CurrentCityPath.e.y;

                //If this is not the final destination of the trip, recalculate route to make sure no changes were made
                if (CurrentCityPath.e != dest)
                {
                    UpdateParkingDest(goal);
                }
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Scan for upcoming stop signs, stop there, and then wait for the intersection to clear before proceeding.
        /// </summary>
        public void HandleStopSign()
        {
            foreach (CityPath p in PathsInView())
            {
                CityNode node = p.e;
                if (node is StopSignNode)
                {
                    StopSignNode n    = node as StopSignNode;
                    double       dist = DistanceToNodeOnPath(n);

                    if (!stopped)
                    {
                        if (dist <= Util.StopDistance(speed, DEACCELERATION) + 25)
                        {
                            limits.RemoveAll(l => l.name == "stop sign");
                            limits.Add(new SpeedLimiter(0, "stop sign"));
                        }
                        if (dist < 26)
                        {
                            if (speed <= 0.1)
                            {
                                stopped = true;
                                n.intersection.VehicleApproach(this);
                            }
                        }
                    }
                    else
                    {
                        if (n.intersection.current == this)
                        {
                            limits.RemoveAll(l => l.name == "stop sign");
                        }
                        else
                        {
                            limits.RemoveAll(l => l.name == "stop sign");
                            limits.Add(new SpeedLimiter(0, "stop sign"));
                        }
                    }
                }
            }
        }
Esempio n. 26
0
        public Commuter(CityNode start, CityNode home = null, CityNode work = null, GasolineCar car = null)
        {
            this.x = start.x;
            this.y = start.y;

            this.home   = home;
            this.work   = work;
            lastVisited = start;
            goal        = start;

            this.car = car;
            if (car != null)
            {
                car.owner = this;
            }

            int possibilites = (2 * 60 + 30) / Sim.MINUTES_PER_SECOND;

            homeLeaveTime = 5 * 60 + 30 + r.Next(possibilites) * Sim.MINUTES_PER_SECOND;
            workLeaveTime = 16 * 60 + 30 + r.Next(possibilites) * Sim.MINUTES_PER_SECOND;

            Console.WriteLine("Leave to work: " + Util.GetTimeString(homeLeaveTime) + ", leave back home: " + Util.GetTimeString(workLeaveTime));
        }
Esempio n. 27
0
 // Update is called once per frame
 void Update()
 {
     //Move speed is determined by this unit's Move Speed stat
     if (moving)
     {
         if (squadSpeed != 0.0f)
         {
             gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position,
                                                                 destination.transform.localPosition,
                                                                 squadSpeed * Time.deltaTime);
             distance = destination.transform.localPosition - gameObject.transform.position;
             if (Vector3.SqrMagnitude(distance) < sqDistance)
             {
                 origin = destination;
                 Debug.Log(destination);
                 moving = false;
             }
         }
         else
         {
             SetSquadSpeed();
         }
     }
 }
Esempio n. 28
0
        /// <summary>
        /// Initialize a City Node (the City and finds out the connections)
        /// </summary>
        /// <param name="p">where the city is on the city map</param>
        private CityNode init_cityNode(Point p)
        {
            //ineficient algorithm

            CityMap cm = GameState.CurrentState.citymap["gen"];

            CityNode cn = new CityNode();
            cn.city = cm.get(p.X, p.Y);
            cn.conls = new List<City>();
            cn.sidels=new List<City.CitySide>();

            //map for connection discovery
            /*
             * 0 blocking block (not road and not city)
             * 1 road
             * 2 city
             * 3 checked by the algorithm
             */
            int[,] nmap=new int[cm.NumX, cm.NumY];

            for(int i=0; i<cm.NumX; i++)
                for (int e = 0; e < cm.NumY; e++)
                {
                    if (tm.get(i, e).Type == Tile.TileType.ROADS)
                        nmap[i, e] = 1;
                    else if (tm.get(i, e).Type == Tile.TileType.CITY)
                        nmap[i, e] = 2;
                    else
                        nmap[i, e] = 0;
                }

            /*System.Console.Out.WriteLine(">>" + cn.city.Name);

            pnmap(nmap);*/

            //starting seed (put 3 near city where roads)
            nma_spread(nmap, p);

            //pnmap(nmap);

            //if the nmap was modified
            bool m = true;
            Point tp;
            City c;

            while (m)
            {
                m = false;

                 for(int i=0; i<cm.NumX; i++)
                     for (int e = 0; e < cm.NumY; e++)
                     {
                         tp = nmap_nearval(nmap, new Point(i, e), 2);

                         if (nmap[i, e] == 3 && tp != new Point(-1, -1))
                         {
                             //found a connected city
                             c = cm.get(tp.X, tp.Y);

                             if (c != null && c.Name != cn.city.Name && !contains(cn.conls, c))
                             {
                                 cn.conls.Add(c);

                                 cn.sidels.Add(City.move2side(new Point(i, e), tp));

                                 //System.Console.Out.WriteLine(cn.city.Name+" -> "+c.Name);
                             }
                         }

                         if(nmap[i, e] == 3)
                             if (nma_spread(nmap, new Point(i, e)))
                                m=true;
                     }

                 //pnmap(nmap);
            }

            return cn;
        }
Esempio n. 29
0
        /// <summary>
        /// returns the integer to use with cn.conls[X] and cn.sidels[X]. the integer represents a city which is an ally city in cn. or -1 if none found
        /// </summary>
        /// <param name="cn"></param>
        /// <returns></returns>
        private int border(CityNode cn)
        {
            for (int i = 0; i < cn.conls.Count; i++)
                if (cn.conls[i].Owner == "main")
                    return i;

            return -1;
        }
Esempio n. 30
0
        public override void Update()
        {
            //Go to work between 6:30 and 8:00
            if ((Sim.instance.time >= homeLeaveTime && Sim.instance.time < workLeaveTime) && lastVisited == home)
            {
                goal   = work;
                parked = false;
            }

            //Go home between 16:30 and 18:00
            if (Sim.instance.time >= workLeaveTime || Sim.instance.time < homeLeaveTime && lastVisited == work)
            {
                goal   = home;
                parked = false;
            }

            //If needs to go somewhere
            if (lastVisited != goal)
            {
                //If not currently on a ride, find your car
                if (!parked && currentRide == null)
                {
                    if (!car.parked)
                    {
                        //throw new Exception("Car is driving without owner, someone stole it? (bug)");
                        return;
                    }

                    List <CityPath> pathToCar = City.GetPath(lastVisited, car.lastVisited, CityPathType.pedestrians);
                    CityNode        subDest   = pathToCar[0].e;

                    double dir  = Util.GetLookatDir(x, y, subDest.x, subDest.y);
                    double dist = Util.Distance(x, y, subDest.x, subDest.y);

                    if (dist < 4)
                    {
                        if (subDest == car.lastVisited)
                        {
                            currentRide = car;
                            car.DriveToParking(goal);
                        }
                        else
                        {
                            x           = subDest.x;
                            y           = subDest.y;
                            lastVisited = subDest;
                        }
                    }

                    else
                    {
                        x += WALK_SPEED * Math.Cos(dir);
                        y += WALK_SPEED * Math.Sin(dir);
                    }
                }
                else if (!parked)
                {
                    x = currentRide.x;
                    y = currentRide.y;

                    CityNode driveDest = car.dest;
                    double   dist      = Util.Distance(x, y, driveDest.x, driveDest.y);

                    if (dist < 4)
                    {
                        x           = driveDest.x;
                        y           = driveDest.y;
                        parked      = true;
                        currentRide = null;
                        lastVisited = driveDest;
                    }
                }
                else
                {
                    List <CityPath> pathToGoal = City.GetPath(lastVisited, goal, CityPathType.pedestrians);
                    CityNode        subDest    = pathToGoal[0].e;

                    double dir  = Util.GetLookatDir(x, y, subDest.x, subDest.y);
                    double dist = Util.Distance(x, y, subDest.x, subDest.y);

                    if (dist < 4)
                    {
                        x           = subDest.x;
                        y           = subDest.y;
                        lastVisited = subDest;

                        if (subDest == goal)
                        {
                            Console.WriteLine(this + " arrived to his destination.");
                        }
                    }

                    else
                    {
                        x += WALK_SPEED * Math.Cos(dir);
                        y += WALK_SPEED * Math.Sin(dir);
                    }
                }
            }
        }
Esempio n. 31
0
    /*public void PickNode(CityNode node) {
     *  PickNode(node);
     * }*/

    public void MoveToNextNode()
    {
        bool hasArrived = false;

        if (state == CarState.Rage)
        {
            hasArrived = ((new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(next_node.transform.position.x, 0, next_node.transform.position.z)).magnitude < 0.1f);
            if (hasArrived)
            {
                if (currentNode.type == NodeType.CarExit)
                {
                    Destroy(gameObject);
                }
                currentNode.currentCarOnNode = null;
                currentNode = next_node;
                currentNode.currentCarOnNode = this;
                next_node = currentNode.possible_neighbors[Random.Range(0, currentNode.possible_neighbors.Count)];
            }
            else
            {
                if (next_node.currentCarOnNode == null)
                {
                    transform.LookAt(next_node.transform.position);
                    gameObject.transform.position += (new Vector3(next_node.transform.position.x, 0, next_node.transform.position.z) - new Vector3(transform.position.x, 0, transform.position.z)).normalized * calmSpeed * Time.deltaTime;
                }
                else
                {
                    next_node = currentNode.possible_neighbors[Random.Range(0, currentNode.possible_neighbors.Count)];
                }
            }
        }
        else if (state == CarState.Calm)
        {
            hasArrived = ((new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(next_node.transform.position.x, 0, next_node.transform.position.z)).magnitude < 0.1f);
            if (hasArrived)
            {
                if (currentNode.type == NodeType.CarExit)
                {
                    Destroy(gameObject);
                }
                currentNode.currentCarOnNode = null;
                currentNode = next_node;
                currentNode.currentCarOnNode = this;
                next_node = currentNode.possible_neighbors[Random.Range(0, currentNode.possible_neighbors.Count)];
            }
            else
            {
                if ((next_node.type == NodeType.CarEntrance || next_node.type == NodeType.Street) && next_node.currentCarOnNode == null)
                {
                    gameObject.transform.position += (new Vector3(next_node.transform.position.x, 0, next_node.transform.position.z) - new Vector3(transform.position.x, 0, transform.position.z)).normalized * calmSpeed * Time.deltaTime;
                    waiting       = false;
                    waitingTimer -= Time.deltaTime;
                    transform.LookAt(next_node.transform.position);
                }
                else
                {
                    waiting       = true;
                    waitingTimer += Time.deltaTime;
                    if (waitingCD <= waitingTimer)
                    {
                        state          = CarState.Rage;
                        ragePathPicked = false;
                    }
                }
            }
        }
    }
Esempio n. 32
0
        /// <summary>
        /// Initialize graphics settings (screen size etc'), and call SetupGame.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            graphics.PreferredBackBufferWidth  = GAME_WIDTH;
            graphics.PreferredBackBufferHeight = GAME_HEIGHT;
            graphics.ApplyChanges();

            IsFixedTimeStep   = true;
            TargetElapsedTime = TimeSpan.FromMilliseconds(1000 / Util.ToTicks(1));

            IsMouseVisible = true;

            sideMenu = new StackPanel(GAME_WIDTH - 350, 0, 350, GAME_HEIGHT);

            CityBuilder builder = new CityBuilder();

            BuilderTrafficCircleNode i1 = new BuilderTrafficCircleNode(430, 415);
            BuilderAllWayStopNode    i2 = new BuilderAllWayStopNode(940, 415);

            BuilderDeadEndNode e1top    = new BuilderDeadEndNode(430, 50, 0);
            BuilderDeadEndNode e1left   = new BuilderDeadEndNode(65, 415, MathHelper.ToRadians(-90));
            BuilderDeadEndNode e1bottom = new BuilderDeadEndNode(430, 780, MathHelper.ToRadians(180));

            BuilderDeadEndNode e2top    = new BuilderDeadEndNode(940, 50, MathHelper.ToRadians(0));
            BuilderDeadEndNode e2right  = new BuilderDeadEndNode(1310, 415, MathHelper.ToRadians(90));
            BuilderDeadEndNode e2bottom = new BuilderDeadEndNode(940, 780, MathHelper.ToRadians(180));

            CityBuilderParkingTwoLanePath connection = new CityBuilderParkingTwoLanePath(i1, i2);

            i1.right = connection;
            i2.left  = connection;

            CityBuilderParkingTwoLanePath i1top = new CityBuilderParkingTwoLanePath(i1, e1top);

            i1.top = i1top;
            CityBuilderParkingTwoLanePath i1left = new CityBuilderParkingTwoLanePath(i1, e1left);

            i1.left = i1left;
            CityBuilderParkingTwoLanePath i1bottom = new CityBuilderParkingTwoLanePath(i1, e1bottom);

            i1.bottom = i1bottom;

            CityBuilderParkingTwoLanePath i2right = new CityBuilderParkingTwoLanePath(i2, e2right);

            i2.right = i2right;
            CityBuilderParkingTwoLanePath i2top = new CityBuilderParkingTwoLanePath(i2, e2top);

            i2.top = i2top;
            CityBuilderParkingTwoLanePath i2bottom = new CityBuilderParkingTwoLanePath(i2, e2bottom);

            i2.bottom = i2bottom;

            builder.nodes.Add(i1);
            builder.nodes.Add(i2);
            builder.nodes.Add(e1top);
            builder.nodes.Add(e1left);
            builder.nodes.Add(e1bottom);
            builder.nodes.Add(e2right);
            builder.nodes.Add(e2top);
            builder.nodes.Add(e2bottom);
            builder.paths.Add(connection);
            builder.paths.Add(i1top);
            builder.paths.Add(i1bottom);
            builder.paths.Add(i1left);
            builder.paths.Add(i2right);
            builder.paths.Add(i2top);
            builder.paths.Add(i2bottom);
            city = builder.Build();

            Random r = new Random();

            foreach (CityNode n in city.nodes)
            {
                if (n is ParkingNode)
                {
                    //vehicles.Add(new GasolineCar(n));
                }
            }

            CityNode work1 = new CityNode(1200, 300);
            CityNode work2 = new CityNode(1100, 600);

            city.nodes.Add(work1);
            city.nodes.Add(work2);

            CityNode n1 = city.NearestNode(1200, 300, CityPathType.pedestrians);

            work1.Connect(n1, CityPathType.pedestrians);
            n1.Connect(work1, CityPathType.pedestrians);
            CityNode n2 = city.NearestNode(1100, 600, CityPathType.pedestrians);

            work2.Connect(n2, CityPathType.pedestrians);
            n2.Connect(work2, CityPathType.pedestrians);


            CityNode[] homes = new CityNode[]
            {
                new CityNode(250, 200),
                new CityNode(270, 300),
                new CityNode(120, 300),
                new CityNode(570, 100),
                new CityNode(570, 210),
                new CityNode(220, 560),
                new CityNode(550, 560),
                new CityNode(580, 700),
                new CityNode(750, 560),
                new CityNode(780, 720),
                new CityNode(780, 310),
                new CityNode(1080, 720),
                new CityNode(1080, 100),
            };

            foreach (CityNode home in homes)
            {
                city.nodes.Add(home);
                CityNode n = city.NearestNode((int)home.x, (int)home.y, CityPathType.pedestrians);
                home.Connect(n, CityPathType.pedestrians);
                n.Connect(home, CityPathType.pedestrians);

                CityNode park = City.ClosestParking(n);

                GasolineCar c = new GasolineCar(park);
                vehicles.Add(c);

                Commuter guy = new Commuter(home, home, r.Next(2) == 0 ? work1 : work2, c);
                debugObjects.Add(guy);
            }
        }