Ejemplo n.º 1
0
 private void SaveMap()
 {
     MapEditorSaving.MapInfo saveMapData = new MapEditorSaving.MapInfo();
     saveMapData.id        = mapID;
     saveMapData.mapName   = mapName;
     saveMapData.mapWidth  = Mathf.RoundToInt(mapDimensions.x);
     saveMapData.mapheight = Mathf.RoundToInt(mapDimensions.y);
     for (int y = 0; y < mapData.GetLength(1); y++)
     {
         for (int x = 0; x < mapData.GetLength(0); x++)
         {
             MapTile targetTile = mapData[x, y];
             if (targetTile != null && !targetTile.isTheMirrorVersion)
             {
                 MapEditorSaving.TileInfo newTile = new MapEditorSaving.TileInfo();
                 newTile.xPos     = x;
                 newTile.yPos     = y;
                 newTile.rotation = Mathf.RoundToInt(targetTile.transform.eulerAngles.z);
                 newTile.type     = targetTile.typeIndex;
                 saveMapData.tiles.Add(newTile);
             }
         }
     }
     MapEditorSaving.SaveFile(saveMapData, mapID.ToString());
 }
Ejemplo n.º 2
0
        public static MapTile[,] createTiles(int GridSize)
        {
            if (GridSize == 1)
            {
                return(new MapTile[, ] {
                    { new MapTile()
                      {
                          Type = MapTile.MapTileType.Start, Exits = MapTileFactory.GenerateFourExits
                      } }
                });
            }
            else
            {
                MapTile[,] map = gererateEmptyMap(GridSize);

                GenerateRandomTile(GridSize, map, MapTile.MapTileType.Start);
                GenerateRandomTile(GridSize, map, MapTile.MapTileType.Key);
                GenerateRandomTile(GridSize, map, MapTile.MapTileType.Exit);

                for (int j = 0; j < map.GetLength(0); j++)     //Vertical
                {
                    for (int k = 0; k < map.GetLength(1); k++) //Horizontal
                    {
                        if (map[j, k].Type == MapTile.MapTileType.Empty)
                        {
                            GenerateTile(GridSize, map, MapTile.MapTileType.Regular, j, k);
                        }
                    }
                }


                return(map);
            }
        }
Ejemplo n.º 3
0
    public Location getTileLocation(MapTile[,] level, TileType tile)
    {
        Location output = null;
        int      x = level.GetLength(0), y = level.GetLength(1);

        for (int zx = 0; zx < x; zx++)
        {
            for (int zy = 0; zy < x; zy++)
            {
                if (level[zy, zx] != null)
                {
                    if (level[zy, zx].type == tile)
                    {
                        output = new Location {
                            X = zx, Y = zy
                        };
                    }
                }
            }
        }
        if (output == null)
        {
            Debug.Log("no " + tile + " found");
        }
        else
        {
            Debug.Log(tile + " found at " + output.X + "," + output.Y);
        }
        return(output);
    }
Ejemplo n.º 4
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Dirty = false;
            MapData data = new MapData();
            // Tilemap
            string result = "";

            for (int i = 0; i < Tiles.GetLength(0); i++)
            {
                for (int j = 0; j < Tiles.GetLength(1); j++)
                {
                    result += Tiles[i, j].TileID + "|";
                }
                result  = result.Substring(0, result.Length - 1);
                result += ";";
            }
            result     = result.Substring(0, result.Length - 1);
            data.Tiles = result;
            // Everything else
            data.Units       = Units;
            data.MapEvents   = melMapEvents.Datas;
            data.Tileset     = cmbTileSets.Text;
            data.LevelNumber = (int)nudLevelNumber.Value;
            data.Objective   = ObjectiveToString();
            data.Name        = txtLevelName.Text;
            CurrentDirectory.SaveFile(txtLevelName.Text, data.ToJson());
            flbFiles.UpdateList();
            VoiceAssist.Say("Save");
        }
Ejemplo n.º 5
0
    public void UpdateTile(MapTile[,] mapTile, MapTile tileToUpdate)
    {
        isGenerating = true;

        int width  = mapTile.GetLength(0);
        int height = mapTile.GetLength(1);

        BoundsInt bounds = new BoundsInt(-1, -1, 0, 3, 3, 1);

        foreach (Vector3Int b in bounds.allPositionsWithin)
        {
            if (tileToUpdate.position.x + b.x >= 0 && tileToUpdate.position.x + b.x < width && tileToUpdate.position.y + b.y >= 0 && tileToUpdate.position.y + b.y < height)
            {
                MapTile tile = mapTile[tileToUpdate.position.x + b.x, tileToUpdate.position.y + b.y];
                tile.tile = debugTiles[1];

                foreach (SO_RuleTile rule in rulesForTiles)
                {
                    if (rule.IsThisTile(tile, mapTile))
                    {
                        tile.tile = rule.tile;
                        break;
                    }
                }
            }
        }

        isGenerating = false;
    }
Ejemplo n.º 6
0
        public MapEnvironment(Tileset tileset, IEnumerable <TerrainType> terrainTypes, MapTile[,] mapTiles)
            : this()
        {
            _tileset      = tileset;
            _terrainTypes = new List <TerrainType>(terrainTypes);
            _cliffTypes   = GetDefaultCliffTypes().ToList();

            if (_terrainTypes.Count == 0)
            {
                _terrainTypes.AddRange(GetDefaultTerrainTypes());
            }
            else if (_terrainTypes.Count > TerrainTypeLimit)
            {
                throw new ArgumentException($"Cannot store more than {TerrainTypeLimit} terraintypes.", nameof(terrainTypes));
            }
            else
            {
                foreach (var terrainType in terrainTypes)
                {
                    if (!Enum.IsDefined(typeof(TerrainType), terrainType))
                    {
                        throw new ArgumentException($"Unknown terraintype: {terrainType}", nameof(terrainTypes));
                    }
                }
            }

            var width  = mapTiles.GetLength(0);
            var height = mapTiles.GetLength(1);
Ejemplo n.º 7
0
        partial void InitProjectSpecific()
        {
            OnLocationChanged += LocationChanged;

            borders = new Rectangle(
                (int)Locations.Min(l => l.MapPosition.X),
                (int)Locations.Min(l => l.MapPosition.Y),
                (int)Locations.Max(l => l.MapPosition.X),
                (int)Locations.Max(l => l.MapPosition.Y));
            borders.Width  = borders.Width - borders.X;
            borders.Height = borders.Height - borders.Y;

            mapTiles = new MapTile[
                (int)Math.Ceiling(size * BackgroundScale / generationParams.TileSpriteSpacing.X),
                (int)Math.Ceiling(size * BackgroundScale / generationParams.TileSpriteSpacing.Y)];

            for (int x = 0; x < mapTiles.GetLength(0); x++)
            {
                for (int y = 0; y < mapTiles.GetLength(1); y++)
                {
                    mapTiles[x, y] = new MapTile(
                        generationParams.BackgroundTileSprites[Rand.Int(generationParams.BackgroundTileSprites.Count)], Rand.Range(0.0f, 1.0f) < 0.5f ?
                        SpriteEffects.FlipHorizontally : SpriteEffects.None);
                }
            }

            drawOffset = -CurrentLocation.MapPosition;
        }
Ejemplo n.º 8
0
        public MinimapDisplay(Transform parent)
        {
            for (int row = 0; row < tiles.GetLength(0); row++)
            {
                for (int col = 0; col < tiles.GetLength(1); col++)
                {
                    var pos = new Vector2(row * 32, col * 16);

                    var     s = new Sprite(HUDSpriteFactory.Instance.CreateMinimapBlack());
                    MapTile t = new MapTile(s, pos);
                    tiles[row, col] = t;
                    Scene.Add(t);
                    parent.AddChild(t);
                }
            }

            // link
            linkPosition = new HUDItem(HUDSpriteFactory.Instance.CreateMapGreenSquare(), new Vector2(0, 0));
            SetLinkPosition();
            Constants.SetLayerDepth(linkPosition, Constants.LayerDepth.Debug);
            Scene.Add(linkPosition);
            parent.AddChild(linkPosition);

            // boss
            bossRoom = new HUDItem(HUDSpriteFactory.Instance.CreateMapBossLocation(), new Vector2(0, 0));
            var bossSprite = bossRoom.GetComponent <Sprite>();

            bossSprite.SetVisible(false);
            bossSprite.SetAnimate(true);
            bossSprite.SetUpdateFrameSpeed(10);
            SetBossPosition();
            Constants.SetLayerDepth(bossRoom, Constants.LayerDepth.Debug);
            Scene.Add(bossRoom);
            parent.AddChild(bossRoom);
        }
Ejemplo n.º 9
0
/// <summary>
    /// Instantiate new MapTiles into the scene (inactive - to be activated and animated later).
    /// </summary>
    /// <param name="newGeneratedMapArray">Unparsed generated map data.</param>
    /// <returns></returns>
    ///
    private GameObject[,] InstantiateNewMapTiles(MapTile[,] newGeneratedMapArray)
    {
        if (Verbose >= 2)
        {
            Debug.Log("InstantiateNewMapTiles");
        }

        GameObject[,] newMapObjectArray = new GameObject[newGeneratedMapArray.GetLength(0), newGeneratedMapArray.GetLength(1)];

        // iterate through array and instantiate all objects
        for (int j = 0; j < newGeneratedMapArray.GetLength(0); j++)
        {
            for (int k = 0; k < newGeneratedMapArray.GetLength(1); k++)
            {
                if (newGeneratedMapArray[j, k] == null)
                {
                    continue;
                }
                if (newGeneratedMapArray[j, k].type == null)
                {
                    continue;
                }

                newMapObjectArray[j, k] = CreateMapTile(newGeneratedMapArray[j, k].type); // TODO: This variable now shouldn't be lowercase
            }
        }

        return(newMapObjectArray);
    }
Ejemplo n.º 10
0
    public string[] levelToString(MapTile[,] level)
    {
        int x = level.GetLength(0), y = level.GetLength(1);

        string[] output = new string[x];
        for (int zx = 79; zx >= 0; zx--)
        {
            for (int zy = 0; zy < y; zy++)
            {
                if (level[zx, zy] == null)
                {
                    output[zx] += "X";
                }
                else if (level[zx, zy].type == TileType.Exit)
                {
                    output[zx] += "B";
                }
                else if (level[zx, zy].type == TileType.Entry)
                {
                    output[zx] += "A";
                }
                else
                {
                    output[zx] += "_";
                }
            }
        }
        return(output);
    }
Ejemplo n.º 11
0
    private void createMap()
    {
        Vector3 actulalTilePosition = new Vector3();

        for (int x = 0; x < map.GetLength(0); x++)
        {
            actulalTilePosition.x = x * (tileSize + tileDistance);
            for (int y = 0; y < map.GetLength(1); y++)
            {
                Vector3 pos = new Vector2(actulalTilePosition.x, actulalTilePosition.y + y * (tileSize + tileDistance));
                map[x, y]          = new MapTile();
                map[x, y].position = pos + pivot;
            }
        }

        for (int x = 0; x < map.GetLength(0); x++)
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                GameObject     o = (GameObject)Instantiate(tile, map[x, y].position, Quaternion.identity);
                TileProperties p = o.GetComponent <TileProperties>();
                //o.GetComponent<TileProperties>().seed = getSeed(x,y);
                p.Seed           = getSeed(x, y);
                p.PositionOnGrid = new Vector2(x, y);
            }
        }
    }
Ejemplo n.º 12
0
    public MapTile GetTile(Vector2Int pos)
    {
        if (pos.x >= 0 && pos.x < tiles.GetLength(0) && pos.y >= 0 && pos.y < tiles.GetLength(1))
        {
            return(tiles[pos.x, pos.y]);
        }

        return(null);
    }
Ejemplo n.º 13
0
    public void LoadMap(string name, bool setSpawns)
    {
        floorMap.ClearAllTiles();
        wallMap.ClearAllTiles();
        boxMap.ClearAllTiles();
        triggerMap.ClearAllTiles();
        if (previewMap != null)
        {
            previewMap.ClearAllTiles();
        }
        iceMap.ClearAllTiles();

        Tiles = new MapTile[maxX + 2, maxY + 2];
        for (int mx = 0; mx < Tiles.GetLength(0); mx++)
        {
            for (int my = 0; my < Tiles.GetLength(1); my++)
            {
                Tiles[mx, my] = new MapTile(TileType.Empty, mx, my);
            }
        }

        Dictionary <int, MapTile> dicSpawns = new Dictionary <int, MapTile>();

        string[] allLines = GetMapFile(name);
        int      y        = 1; //because outer layer be empty

        for (int i = allLines.Length - 1; i >= 2; i--)
        {
            string line = allLines[i];
            int    x    = 1; //because outer layer be empty
            foreach (char c in line)
            {
                Tiles[x, y] = new MapTile(MapTile.ConvertChar(c), x, y);
                if (Tiles[x, y].Type == TileType.Spawn)
                {
                    dicSpawns[int.Parse("" + c)] = Tiles[x, y];
                }
                ListOfTiles.Add(Tiles[x, y]);
                x++;
            }
            y++;
        }
        Spawns = new MapTile[dicSpawns.Count];
        foreach (int i in dicSpawns.Keys)
        {
            Spawns[i] = dicSpawns[i];
        }
        if (setSpawns)
        {
            PlayerController.Instance.SetSpawnPosition(new List <MapTile>(Spawns));
        }
        SetTileMaps();
    }
Ejemplo n.º 14
0
 public void UpdatePlants()
 {
     for (int i = 0; i < worldMap.GetLength(0); i++)
     {
         for (int j = 0; j < worldMap.GetLength(1); j++)
         {
             worldMap[i, j].UpdatePlants(updateInterval);
         }
     }
     updateInterval++;
     completedUpdateHour = Program.TimeHandler.CurrentTime.Hour;
 }
Ejemplo n.º 15
0
 //TODO: probably not the most efficient way of doing it
 //better way might be to have each tile analyze and set the tiles to the north and west of them
 private void UpdateOverlays()
 {
     for (int x = 0; x < _tiles.GetLength(0); x++)
     {
         for (int y = 0; y < _tiles.GetLength(1); y++)
         {
             if (_tiles[x, y] != null)
             {
                 _tiles[x, y].updateConnectedDoors(getTileDoors(x, y - 1), getTileDoors(x, y + 1), getTileDoors(x + 1, y), getTileDoors(x - 1, y));
             }
         }
     }
 }
Ejemplo n.º 16
0
    //I have no clue why I went with "Items" here..
    //So here, I am spawning mapTiles based on the string given above.
    //It then places the cursor in the center, and  makes 2 summoners (I may make the 2 summoners thing a part of the map text)
    void SpawnItems(string[] n)
    {
        for (int i = 0; i < n.Length; i++)
        {
            for (int j = 0; j < n[0].Length; j++)
            {
                //make map tiles
                GameObject m = null;
                if (map[i, j] == "M")
                {
                    m = (GameObject)Instantiate(Resources.Load("Prefab/Tiles/Mountain"));
                    m.transform.position = new Vector3(HUB.SPACER * j, HUB.SPACER * i, 0);
                }
                else if (map[i, j] == "G")
                {
                    m = (GameObject)Instantiate(Resources.Load("Prefab/Tiles/Grass"));
                    m.transform.position = new Vector3(HUB.SPACER * j, HUB.SPACER * i, 0);
                }
                else if (map[i, j] == "W")
                {
                    m = (GameObject)Instantiate(Resources.Load("Prefab/Tiles/Water"));
                    m.transform.position = new Vector3(HUB.SPACER * j, HUB.SPACER * i, 0);
                }
                newMapTiles[i, j] = m.GetComponent <MapTile>();
            }
        }
        setAdjacentTiles();
        //add summoners
        hub.moveCharacter(sum1, newMapTiles[newMapTiles.GetLength(0) / 2, 0]);
        hub.moveCharacter(sum2, newMapTiles[newMapTiles.GetLength(0) / 2, newMapTiles.GetLength(1) - 1]);

        hub.CURSOR.setOnMapTile(newMapTiles[newMapTiles.GetLength(0) / 2, 0]);
    }
Ejemplo n.º 17
0
 public void refreshCollidersOnOuterTiles()
 {
     for (int x = 0; x < mapTiles.GetLength(0); x++)
     {
         for (int y = 0; y < mapTiles.GetLength(1); y++)
         {
             if (mapTiles[x, y] != null && mapTiles[x, y].IsOuterTile())
             {
                 //Enable Collider
                 mapTiles [x, y].gameObject.GetComponent <BoxCollider2D> ().enabled = true;
             }
         }
     }
 }
Ejemplo n.º 18
0
    /// <summary>
    /// 배치된 맵 타일의 정보를 디버그 로그로 출력합니다.
    /// </summary>
    private void DebugLog_MapTiles()
    {
        string alltemp = "";

        for (int i = 0; i < MapTiles.GetLength(1); i++)
        {
            string temp = "";
            for (int j = 0; j < MapTiles.GetLength(0); j++)
            {
                temp += (int)MapTiles[j, i].myTileType;
            }
            alltemp += temp + "\n";
        }
        Debug.Log(alltemp);
    }
Ejemplo n.º 19
0
    public void SetTiles(MapTile[,] mapTiles)
    {
        int width  = mapTiles.GetLength(0);
        int height = mapTiles.GetLength(1);

        tiles = new MapTile[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                tiles[x, y] = mapTiles[x, y];
            }
        }
    }
Ejemplo n.º 20
0
 public MapTile this[int x, int y] {
     get {
         if (0 <= x && x < tiles.GetLength(0) && 0 <= y && y < tiles.GetLength(1))
         {
             return(tiles[x, y]);
         }
         else
         {
             return(null);
         }
     }
     set {
         tiles[x, y] = value;
     }
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Moves the projectiles that are on the map, and removes them
        /// if they are no longer visible or if they collide with a wall.
        /// </summary>
        private void MoveProjectiles()
        {
            for (int i = _projectiles.Count - 1; i >= 0; i--)
            {
                Point p = _projectiles[i].Move();

                // Calculate which grid row/column the projectile is in
                int pColumn = p.X / TileSize;
                int pRow    = p.Y / TileSize;

                // remove the projectile if off-screen
                if (pColumn < 0 || pColumn >= map.GetLength(0) || pRow < 0 || pRow >= map.GetLength(1))
                {
                    _projectiles.Remove(_projectiles[i]);
                    return;
                }


                // Destroy walls and closed gates
                if (map[pRow, pColumn] == MapTile.Wall || map[pRow, pColumn] == MapTile.ClosedGate)
                {
                    // Destroy the projectile if it hits a wall
                    if (map[pRow, pColumn] == MapTile.Wall)
                    {
                        _projectiles.Remove(_projectiles[i]);
                    }

                    map[pRow, pColumn] = MapTile.Dirt;
                }
            }
        }
Ejemplo n.º 22
0
    /// <summary>
    /// Returns the tile at x and y real world coords
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    public MapTile getTile(float x, float y)
    {
        Vector2 index = worldPosToIndex(x, y);

        int i1 = (int)index.x;
        int i2 = (int)index.y;

        if (i1 >= 0 && i2 >= 0 && i1 < map.GetLength(0) && i2 < map.GetLength(1))
        {
            return(map[(int)index.x, (int)index.y]);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 23
0
        public void NoTileNullMap()
        {
            int nbTileNull = 0;

            for (int i = 0; i < _map.GetLength(0); i++)
            {
                for (int j = 0; j < _map.GetLength(1); j++)
                {
                    if (_map[i, j] == null)
                    {
                        nbTileNull++;
                    }
                }
            }
            Assert.AreEqual(0, nbTileNull);
        }
Ejemplo n.º 24
0
    List <MapTile> NodeNeighbours(MapTile node)
    {
        List <MapTile> neighbours = new List <MapTile>();

        for (int x = -1; x <= 1; ++x)
        {
            for (int y = -1; y <= 1; ++y)
            {
                if (x == 0 && y == 0)
                {
                    continue;
                }

                int checkX = node.gridLocation.x + x;
                int checkY = node.gridLocation.y + y;

                if (checkX >= 0 && checkX < mapTiles.GetLength(0) && checkY >= 0 && checkY < mapTiles.GetLength(1))
                {
                    neighbours.Add(mapTiles[checkX, checkY]);
                }
            }
        }

        return(neighbours);
    }
Ejemplo n.º 25
0
        public void NoImageNullMap()
        {
            MapTile[,] map = MapGenerator.LoadMapFromFile(CasinoUI.Properties.Resources.map);
            int nbImageNull = 0;

            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    if (map[i, j].Sprite == null)
                    {
                        nbImageNull++;
                    }
                }
            }
            Assert.AreEqual(0, nbImageNull);
        }
Ejemplo n.º 26
0
    // Start is called before the first frame update
    public void /*Fire emblem */ Initialize/*ning*/ ()
    {
        tilemapReal       = transform.Find("TilemapReal").gameObject.GetComponent <Tilemap>();
        tilemapReal.color = Color.clear;
        boundsFloor       = tilemapReal.cellBounds;
        TileBase[] tilesWorld = tilemapReal.GetTilesBlock(boundsFloor);
        Debug.Log(boundsFloor);


        mapTiles = new MapTile[boundsFloor.size.x / 2, boundsFloor.size.y / 2];
        for (int x = 0; x < boundsFloor.size.x / 2; x++)
        {
            for (int y = 0; y < boundsFloor.size.y / 2; y++)
            {
                mapTiles[x, y]      = new MapTile();
                mapTiles[x, y].tile = parseMapTile(tilesWorld[(y * boundsFloor.size.x + x) * 2]?.name);
            }
        }

        debugTiles  = transform.Find("TilemapDebug").GetComponent <DebugTiles>();
        pathfinding = GetComponent <Pathfinding>();
        int xMin = mapTiles.GetLength(0);
        int yMin = mapTiles.GetLength(0);
        int xMax = -1;
        int yMax = -1;

        for (int x = 0; x < mapTiles.GetLength(0); x++)
        {
            for (int y = 0; y < mapTiles.GetLength(1); y++)
            {
                if (mapTiles[x, y].tile != TileType.NULL)
                {
                    xMin = Math.Min(xMin, x);
                    yMin = Math.Min(yMin, y);
                    xMax = Math.Max(xMax, x);
                    yMax = Math.Max(yMax, y);
                }
            }
        }
        xMax          += 1;
        yMax          += 1;
        mapBounds.xMax = xMax;
        mapBounds.xMin = xMin;
        mapBounds.yMax = yMax;
        mapBounds.yMin = yMin;
    }
Ejemplo n.º 27
0
    public void checkWalkable()
    {
        terrain = Terrain.activeTerrain;

        for (int x = 0; x < tiles.GetLength(0); x++) // Loop through nodes
        {
            for (int y = 0; y < tiles.GetLength(1); y++)
            {
                Vector3 tileLocation = tiles[x, y].position;                                                               // Get the position

                Vector3[] points = new Vector3[4];                                                                         // Array of positions at four corners of tile
                points[0] = new Vector3(tileLocation.x - (tileSize / 2), tileLocation.y, tileLocation.z + (tileSize / 2)); // Top left
                points[1] = new Vector3(tileLocation.x + (tileSize / 2), tileLocation.y, tileLocation.z + (tileSize / 2)); // Top right
                points[2] = new Vector3(tileLocation.x + (tileSize / 2), tileLocation.y, tileLocation.z - (tileSize / 2)); // Bottom right
                points[3] = new Vector3(tileLocation.x - (tileSize / 2), tileLocation.y, tileLocation.z - (tileSize / 2)); // Bottom left

                float[] heightPoints = new float[4];                                                                       // Height of all corners

                for (int i = 0; i < 4; i++)
                {
                    heightPoints[i] = terrain.SampleHeight(points[i]);
                }


                bool walkable = true;

                if (tileLocation.y < seaLevel) // If tile is under sea, unwalkable
                {
                    walkable = false;
                }

                for (int i = 0; i < 4 && walkable; i++)                       // Loop through corners
                {
                    float diff = Mathf.Abs(tileLocation.y - heightPoints[i]); // Work out difference between corner and centre

                    if (diff > heightThreshold)                               // If corner too high, unwalkable
                    {
                        walkable = false;
                    }
                }

                tiles[x, y].walkable = walkable;
            }
        }
    }
Ejemplo n.º 28
0
    public MapTile GetTileBelow(Vector3 position)
    {
        //map[i, j].transform.position = new Vector3(i*MapTileMetrics.outerWidth, elevation, j*MapTileMetrics.outerWidth);
        float x = position.x;
        float z = position.z;

        int i = Mathf.FloorToInt(x / MapTileMetrics.outerWidth);
        int j = Mathf.FloorToInt(z / MapTileMetrics.outerWidth);

        if (i < map.GetLength(0) && j < map.GetLength(1) && i >= 0 && j >= 0)
        {
            return(map[i, j]);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 29
0
    /// <summary>
    /// Converts a vector 3 world position into a vector2 grid coordinate
    /// </summary>
    /// <param name="position">Vector 3 world position</param>
    /// <returns>Vector 2 grid coordinate</returns>
    public iVector2 getCoordFromPosition(Vector3 position)
    {
        position -= transform.position;

        Vector2 mapPercentage; // Convert the position to a percentage across the available map (clamped between 0% and 100%).

        mapPercentage.x = Mathf.Clamp01((position.x + gridWorldSize.x / 2) / gridWorldSize.x);
        mapPercentage.y = Mathf.Clamp01((position.z + gridWorldSize.y / 2) / gridWorldSize.y);

        // Advance across tiles array for percentage, then round to closest tile
        int xTile = Mathf.RoundToInt((tiles.GetLength(0) - 1) * mapPercentage.x);
        int yTile = Mathf.RoundToInt((tiles.GetLength(1) - 1) * mapPercentage.y);

        //        0%=================100%
        // Array [0] [1] [2] [3] [4] [5]

        return(new Vector2(xTile, yTile));
    }
Ejemplo n.º 30
0
    //==============
    // Initialize
    //==============
    public static void initialize()
    {
        parent_obj      = new GameObject();
        parent_obj.name = "TilemapManager";

        // sprites
        // -------
        all_sprites = Resources.LoadAll <Sprite>("Textures/t");
        if (all_sprites == null)
        {
            Debug.Log("TilemapManager Resources.LoadAll Error: all_sprites null");
        }

        // tilemaps
        // --------
        tilemap0 = GameObject.Find("tilemap0").GetComponent <Tilemap>();
        tilemap1 = GameObject.Find("tilemap1").GetComponent <Tilemap>();
        tilemap2 = GameObject.Find("tilemap2").GetComponent <Tilemap>();
        tilemap3 = GameObject.Find("tilemap3").GetComponent <Tilemap>();
        tilemap4 = GameObject.Find("tilemap4").GetComponent <Tilemap>();
        tilemaps = new Tilemap[] { tilemap0, tilemap1, tilemap2, tilemap3, tilemap4 };

        // raise tilemaps z placement
        // --------------------------
        int i = 0;

        foreach (Tilemap t in tilemaps)
        {
            t.gameObject.transform.position = new Vector3(0, 0, -i * LEVEL_HEIGHT);
            i++;
        }

        // Logic Maps
        // ----------
        map = new MapTile[CHUNK_WIDTH, CHUNK_HEIGHT];
        for (int y = 0; y < map.GetLength(1); y++)
        {
            for (int x = 0; x < map.GetLength(0); x++)
            {
                map[x, y] = new MapTile(MAX_LEVEL);
            }
        }
    }
Ejemplo n.º 31
0
    // TO BE USED IN GAME
    // also used to load a new level
    //public void CreateLoadedWorldMesh (Vector2 size) {
    public void CreateLoadedWorldMesh(bool createColliders)
    {
        // create a new array
        world = WorldController.singleton.world;
        // worldSize = size;
        worldSize.x = world.GetLength(0);
        worldSize.y = world.GetLength(1);
        WorldController.singleton.chunkSize = chunkSize;

        // create the world chunk array
        // get the sizes for the array
        //int chunkXSize = Mathf.CeilToInt(size.x/chunkSize);
        //int chunkYSize = Mathf.CeilToInt(size.y/chunkSize);
        int chunkXSize = Mathf.CeilToInt(worldSize.x/chunkSize);
        int chunkYSize = Mathf.CeilToInt(worldSize.y/chunkSize);
        // create a new array
        worldChunks = new WorldChunk[chunkXSize,chunkYSize];
        // determine the offset required so that the world starts at 0,0
        Vector3 extraPos = new Vector3(chunkSize/2,0,chunkSize/2);

        // fill in the array
        for(int i=0;i<worldChunks.GetLength(0);i++){
            for(int j=0;j<worldChunks.GetLength(1);j++){
                // instantiate a new chunk
                GameObject newChunk =  (GameObject)Instantiate(chunkPrefab,Vector3.zero,Quaternion.identity);
                // set the position
                worldChunks[i,j] = newChunk.GetComponent<WorldChunk>();
                worldChunks[i,j].SetChunk(new Vector3(i*chunkSize,0,j*chunkSize)+extraPos,i,j);
                if(worldContainer != null){
                    newChunk.transform.parent = worldContainer;
                }
            }
        }

        CreateNewWorldMesh();

        if(createColliders == true){
            AddCollidersToChunks();
        }
    }
Ejemplo n.º 32
0
    // ONLY USED BY THE MAP EDITOR
    public void CreateEmptyWorld(Vector2 size)
    {
        // create a new array
        world = WorldController.singleton.world;
        worldSize = size;
        WorldController.singleton.chunkSize = chunkSize;

        // create the world chunk array
        // get the sizes for the array
        int chunkXSize = Mathf.CeilToInt(size.x/chunkSize);
        int chunkYSize = Mathf.CeilToInt(size.y/chunkSize);
        // create the array
        worldChunks = new WorldChunk[chunkXSize,chunkYSize];
        // determine the offset required so that the world starts at 0,0
        Vector3 extraPos = new Vector3(chunkSize/2,0,chunkSize/2);

        // fill in the array
        for(int i=0;i<worldChunks.GetLength(0);i++){
            for(int j=0;j<worldChunks.GetLength(1);j++){
                // instantiate a new chunk
                GameObject newChunk =  (GameObject)Instantiate(chunkPrefab,Vector3.zero,Quaternion.identity);
                // set the position
                worldChunks[i,j] = newChunk.GetComponent<WorldChunk>();
                worldChunks[i,j].SetChunk(new Vector3(i*chunkSize,0,j*chunkSize)+extraPos,i,j);
                if(worldContainer != null){
                    newChunk.transform.parent = worldContainer;
                }
            }
        }

        // random thing here
        /*for(int n=0;n<ripple.Length;n++){
        if(n < (ripple.Length-1)){
            ripple[n] = ripple[n+1];
        }
        else{
            ripple[n] = ripple[0];
        }
        }*/

        // populate the array
        for(int i=0;i<world.GetLength(0);i++){
            for(int j=0;j<world.GetLength(1);j++){

                // for minecraft like world, do this
                //world[i,j] = new MapTile((byte)(j+i));

                // draw some borders
                if((i==0 || i==(size.x-1)) || (j==0 || j==(size.y-1))){
                    world[i,j] = new MapTile(10);
                }
                else{
                    world[i,j] = new MapTile(1);
                }

            }

        }
        // create the world mesh

        CreateNewWorldMesh();
        // TODO remove this
        MapEditor.CreateColliders();
        /*CreateWorldMesh(size);
        floorMeshFilter.mesh = floorMesh;
        wallMeshFilter.mesh = wallMesh;
        print ("Floor:");
        print ("Total Vertices: "+floorMesh.vertexCount);
        print ("Total Triangles: "+(floorMesh.triangles.Length/3));

        print ("Walls:");
        print ("Total Vertices: "+wallMesh.vertexCount);
        print ("Total Triangles: "+(wallMesh.triangles.Length/3));*/
    }