//Creates the world from the resource asset at path levelName
    public void CreateWorld(string levelName)
    {
        string[] lines = ReadLines(levelName);
        RoadStub[,] roadstubs         = new RoadStub[lines.Length, lines[0].Length];
        BuildingStub[,] buildingstubs = new BuildingStub[lines.Length, lines[0].Length];
        world.terraingrid             = new Tile[lines.Length, lines[0].Length];

        //Read file line by line and construct the map
        world.WorldSize = new Pair <int, int>(lines[0].Length, lines.GetLength(0));

        for (int y = 0; y < lines.GetLength(0); y++)
        {
            for (int x = 0; x < lines[0].Length; ++x)
            {
                switch (lines[y][x])
                {
                case '1': {
                    roadstubs[y, x] = new RoadStub(x, y);
                    break;
                }

                case '2': {
                    roadstubs[y, x]      = new RoadStub(x, y);
                    roadstubs[y, x].type = RoadStub.RoadType.ChargingSpot;
                    break;
                }

                case 'P': {
                    roadstubs[y, x]      = new RoadStub(x, y);
                    roadstubs[y, x].type = RoadStub.RoadType.ParkingSpot;
                    break;
                }

                case 'E': {
                    buildingstubs[y, x] = new BuildingStub(x, y, BuildingStub.BuildingType.PowerPlant);
                    world.BuildPowerplant(world.GetRealWorldCoordinates(x, y));
                    break;
                }

                case 'B': {
                    buildingstubs[y, x] = new BuildingStub(x, y, BuildingStub.BuildingType.Decoration);
                    break;
                }
                }
            }
        }
        world.roadmap = new RoadMap();
        world.roadmap.CreateMap(roadstubs);

        world.buildings = new BuildingManager();
        world.buildings.Build(buildingstubs);
    }
Beispiel #2
0
    private Road ChooseSprite(int x, int y, RoadStub[,] roads, int mapX, int mapY)
    {
        //check surrounding

        bool down      = false;
        bool up        = false;
        bool left      = false;
        bool right     = false;
        int  numAround = 0;

        try { if (roads[y, x + 1] != null)
              {
                  numAround++; right = true;
              }
        }
        catch (System.IndexOutOfRangeException) {}
        try { if (roads[y, x - 1] != null)
              {
                  numAround++; left = true;
              }
        }
        catch (System.IndexOutOfRangeException) {}
        try { if (roads[y + 1, x] != null)
              {
                  numAround++; down = true;
              }
        }
        catch (System.IndexOutOfRangeException) {}
        try { if (roads[y - 1, x] != null)
              {
                  numAround++; up = true;
              }
        }
        catch (System.IndexOutOfRangeException) {}

        Road     result = null;
        RoadStub stub   = roads[y, x];

        //Choose correct road
        //Could just use RoadFactory.CreateGeneralRoad, but at the time of writing this,
        //that function did not exist
        switch (numAround)
        {
        case 0: {
            throw new System.ArgumentException();
        }

        case 1: {
            if (stub.type == RoadStub.RoadType.ChargingSpot)
            {
                result = RoadFactory.CreateChargeSpot(mapX, mapY, up, down, left, right);
            }
            else if (stub.type == RoadStub.RoadType.Regular)
            {
                result = RoadFactory.CreateEnd(mapX, mapY, up, down, left, right);
            }
            else if (stub.type == RoadStub.RoadType.ParkingSpot)
            {
                result = RoadFactory.CreateParkingSpot(mapX, mapY, up, down, left, right);
            }
            break;
        }

        case 2: {
            result = RoadFactory.CreateTwoSided(mapX, mapY, up, down, left, right);
            break;
        }

        case 3: {
            result = RoadFactory.CreateT(mapX, mapY, up, down, left, right);
            break;
        }

        case 4: {
            result = RoadFactory.CreateQCrossroad(mapX, mapY);
            break;
        }
        }
        return(result);
    }