Esempio n. 1
0
    //Check for world objects that would prohibit the construction
    public bool CheckWorldObstacles()
    {
        Vector2 pos = gameObject.transform.position.ToVector2();
        //array coordinates
        Vector2 gridpos = world.GetImagWorldCoordinates((int)pos.x, (int)pos.y);

        //Allow building on sidewalks and empty tiles
        //Debug.Log(gridpos);
        if (!((int)gridpos.y < 0 || (int)gridpos.y >= world.terraingrid.GetLength(0) ||
              (int)gridpos.x < 0 || (int)gridpos.x >= world.terraingrid.GetLength(1)))
        {
            if (world.terraingrid[(int)gridpos.y, (int)gridpos.x] != Tile.Empty &&
                world.terraingrid[(int)gridpos.y, (int)gridpos.x] != Tile.Sidewalk)
            {
                return(false);
            }
        }

        //On a road (there is a functional road right beneath this object)
        Road r = world.FindNearestRoad(pos);

        if (r.AsVector().AlmostEqual(pos))
        {
            return(false);
        }
        return(true);
    }
Esempio n. 2
0
    public static List <Waypoint> CreateCrossroadWaypoint(Vector2 comingfrom, Vector2 goingto, Road road)
    {
        Waypoint w = null;

        /**
         *  #  |  #
         *  _1_|_2_
         *   4 | 3
         *  #  |  #
         */
        //waypoint 1
        if (comingfrom.AlmostEqual(-Vector2.right) && goingto.AlmostEqual(-Vector2.up) ||
            comingfrom.AlmostEqual(-Vector2.right) && goingto.AlmostEqual(-Vector2.right) ||
            comingfrom.AlmostEqual(-Vector2.up) && goingto.AlmostEqual(-Vector2.right))
        {
            w = new Waypoint(road.AsVector() + new Vector2(-offset.x, offset.y));
        }
        //waypoint 2
        else if (comingfrom.AlmostEqual(Vector2.up) && goingto.AlmostEqual(-Vector2.right) ||
                 comingfrom.AlmostEqual(Vector2.up) && goingto.AlmostEqual(Vector2.up) ||
                 comingfrom.AlmostEqual(-Vector2.right) && goingto.AlmostEqual(Vector2.up))
        {
            w = new Waypoint(road.AsVector() + new Vector2(offset.x, offset.y));
        }
        //waypoint 3
        else if (comingfrom.AlmostEqual(-Vector2.up) && goingto.AlmostEqual(-Vector2.up) ||
                 comingfrom.AlmostEqual(-Vector2.up) && goingto.AlmostEqual(Vector2.right) ||
                 comingfrom.AlmostEqual(Vector2.right) && goingto.AlmostEqual(-Vector2.up))
        {
            w = new Waypoint(road.AsVector() + new Vector2(-offset.x, -offset.y));
        }
        //waypoint 4
        else if (comingfrom.AlmostEqual(Vector2.right) && goingto.AlmostEqual(Vector2.right) ||
                 comingfrom.AlmostEqual(Vector2.up) && goingto.AlmostEqual(Vector2.right) ||
                 comingfrom.AlmostEqual(Vector2.right) && goingto.AlmostEqual(Vector2.up))
        {
            w = new Waypoint(road.AsVector() + new Vector2(offset.x, -offset.y));
        }
        else
        {
            Debug.LogError("Uncaught case! " + comingfrom + " " + goingto);
        }

        w.onRoad = road;
        return(new List <Waypoint>(new Waypoint[] { w }));
    }
Esempio n. 3
0
    //removing road from the outside, this is called when the user decides to remove an already placed
    //powerstation (has to be removed from the roadmap)
    public void RemoveRoad(Road r)
    {
        for (int i = 0; i < roads.Count; ++i)
        {
            if (roads[i].AsVector().AlmostEqual(r.AsVector()))
            {
                roads.Remove(roads[i]);
                break;
            }
        }
        Vector2 imag = World.FindObjectOfType <World>().GetImagWorldCoordinates(r.xPos, r.yPos);

        roadGrid[(int)imag.y, (int)imag.x] = null;
    }
Esempio n. 4
0
    //Check whether there is a valid road next to the gameObject (to check whether the station can be built here)
    //This is called every frame, but it shouldnt be too bad, cause we are only building one station at a time
    bool TryWeaving()
    {
        GetWorld();
        Vector2 weave = gameObject.transform.position.ToVector2() + offset;
        Road    r     = world.FindNearestRoad(weave);

        if (r.AsVector().AlmostEqual(weave))
        {
            GameObject go  = CreateWeavedRoad(r);
            bool       ret = go != null;
            Destroy(go);
            return(ret);
        }
        return(false);
    }
Esempio n. 5
0
    //Remove the road connections and change back the road next to this one;
    public void UndoChanges()
    {
        //the road next to this is the only one connected
        Road adjacent = neighbourRoads[0];

        List <Road> connections = adjacent.neighbourRoads;

        connections.Remove(this);
        Vector2 position = adjacent.AsVector();

        bool up = adjacent.up, down = adjacent.down, left = adjacent.left, right = adjacent.right;

        if (this.up)
        {
            down = false;
        }
        if (this.down)
        {
            up = false;
        }
        if (this.left)
        {
            right = false;
        }
        if (this.right)
        {
            left = false;
        }

        Road restored = RoadFactory.CreateGeneralRoad((int)position.x, (int)position.y, up, down, left, right);

        restored.neighbourRoads = connections;
        foreach (Road r in connections)
        {
            r.neighbourRoads.Remove(adjacent);
            r.neighbourRoads.Add(restored);
        }

        //remove itself from roadmap
        FindObjectOfType <World>().roadmap.RemoveRoad(this);
        FindObjectOfType <World>().roadmap.RemoveRoad(adjacent);
        FindObjectOfType <World>().roadmap.RegisterRoad(restored);
        //remove old one
        Destroy(adjacent.gameObject);
    }
Esempio n. 6
0
    void Weave()
    {
        GetWorld();
        weavingPosition = gameObject.transform.position.ToVector2() + offset;
        Road r = world.FindNearestRoad(weavingPosition);

        StopWeaving();
        if (r.AsVector().AlmostEqual(weavingPosition))
        {
            //create temporary road in place of r
            weaved = CreateWeavedRoad(r);
            if (weaved == null)
            {
                return;
            }
            weaved.transform.parent = gameObject.transform;
        }
    }
Esempio n. 7
0
    //Creates waypoints for roads in the path. Probles: path too short,
    //undecideablity for starting point (the weird little path when the car chooses a new path)
    public static List <Waypoint> CreateWaypoints(List <Road> path, Car car)
    {
        List <Waypoint> points = new List <Waypoint>();

        if (path.Count < 3)
        {
            Vector2 last = path[0].neighbourRoads[0].transform.position.ToVector2();
            foreach (Road r in path)
            {
                Vector2 dir = r.AsVector() - last;
                if (dir.AlmostEqual(Vector2.zero))
                {
                    last = r.AsVector();
                    continue;
                }
                points.AddRange(r.CreateWaypoints(r.AsVector() - last, r.AsVector() - last));
                last = r.AsVector();
            }
            return(points);
        }

        Road comingfrom = path[0];
        Road current    = path[1];
        Road goingto    = path[2];

        bool skipfirst = false;

        if (comingfrom is ParkingSpotRoad)
        {
            skipfirst = true;
        }

        if (!skipfirst)
        {
            Vector2 first = (comingfrom.AsVector() - car.transform.position.ToVector2()).normalized;
            Vector2 best  = Vector2.up;
            float   dist  = first.Distance(Vector2.up);
            if (first.Distance(-Vector2.up) < dist)
            {
                dist = first.Distance(-Vector2.up);
                best = -Vector2.up;
            }
            if (first.Distance(-Vector2.right) < dist)
            {
                dist = first.Distance(-Vector2.right);
                best = -Vector2.right;
            }
            if (first.Distance(Vector2.right) < dist)
            {
                dist = first.Distance(Vector2.right);
                best = Vector2.right;
            }
            Vector2 other = current.AsVector() - comingfrom.AsVector();
            if (!other.AlmostEqual(-best))
            {
                points.AddRange(CreateWaypoints(best, other, comingfrom));
            }
        }

        for (int i = 1; i < path.Count; ++i)
        {
            //Debug.Log("Looking for waypoint from " + comingfrom + " to " + current + " going to " + goingto);
            Vector2 dircomingfrom = current.AsVector() - comingfrom.AsVector();
            Vector2 dirgoingto    = goingto.AsVector() - current.AsVector();

            List <Waypoint> w = CreateWaypoints(dircomingfrom, dirgoingto, current);

            //move a step
            points.AddRange(w);

            comingfrom = current;
            current    = goingto;
            try {
                goingto = path[i + 2];
            } catch (System.ArgumentOutOfRangeException) {
                for (int j = 0; j < current.neighbourRoads.Count; ++j)
                {
                    goingto = current.neighbourRoads[j];
                    if (goingto != comingfrom && goingto != current)
                    {
                        break;
                    }
                }
            }
        }
        return(points);
    }