~saurabhs/bomberman

9eed13fd269f39b377f81566c00e21a04a6433e9 — Saurabh S 4 years ago f225c69
Moved mapdata assignment back to MapGeneration
M Assets/Editor/MapGeneration.cs => Assets/Editor/MapGeneration.cs +93 -5
@@ 1,4 1,5 @@
using System.Collections.Generic;
using System.Xml;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;


@@ 17,7 18,7 @@ namespace Bomberman
                DestroyImmediate( oldWorld );
            }

            var mapData = Common.GetMapData( SceneManager.GetActiveScene().name );
            var mapData = GetMapData( SceneManager.GetActiveScene().name );
            var worldParent = new GameObject( "_World" );
            worldParent.tag = "World";



@@ 29,7 30,7 @@ namespace Bomberman
            {
                for ( var i = 0; i < mapData.width; i++ )
                {
                    var id = mapData.data[i, j];
                    var id = mapData.GetValue( i, j );
                    if ( id != 0 )
                    {
                        var go = GetAsset( id );


@@ 78,9 79,11 @@ namespace Bomberman
                var playerGO = GetAsset( Constants.PLAYER1_ID + i );
                var player = Instantiate( playerGO, new Vector3( point.x, 0, -point.y ), Quaternion.identity ) as GameObject;
                player.name = $"_{playerGO.name}_{index}";
                player.GetComponent<PlayerController>().SetWallBlocks( wallBlocksMapper );
                var playerController = player.GetComponent<PlayerController>();
                playerController.SetWallBlocks( wallBlocksMapper );
                playerController.SetMapData( mapData );

                gameController.GetComponent<GameController>().AddPlayerController(player.GetComponent<PlayerController>());
                gameController.GetComponent<GameController>().AddPlayerController( player.GetComponent<PlayerController>() );
            }

            //delete old enemies


@@ 99,7 102,9 @@ namespace Bomberman
                var enemyGO = GetAsset( Constants.ENEMY_ID );
                var enemy = Instantiate( enemyGO, new Vector3( point.x, 0, -point.y ), Quaternion.identity ) as GameObject;
                enemy.name = $"_{enemyGO.name}_{index}";
                enemy.GetComponent<AIController>().SetWallBlocks( wallBlocksMapper );
                var aiController = enemy.GetComponent<AIController>();
                aiController.SetWallBlocks( wallBlocksMapper );
                aiController.SetMapData( mapData );
            }

            //save scene 


@@ 135,5 140,88 @@ namespace Bomberman
        {
            PlayerPrefs.SetInt( Constants.GAME_TYPE, Constants.COOP_ID );
        }

        public static MapData GetMapData( string filePath )
        {
            return ReadMapDataFromTMXFile( GetXMLDocument( filePath ) );
        }

        private static MapData ReadMapDataFromTMXFile( XmlDocument xmlDocument )
        {
            var mapData = new MapData();

            //read metadata
            var xmlData = xmlDocument.DocumentElement.SelectSingleNode( "/map" );
            if ( xmlData == null )
                throw new System.Exception( "Invalid XML Data..." );

            if ( !int.TryParse( xmlData.Attributes["width"].Value.Trim(), out mapData.width ) )
                throw new System.Exception( "Invalid Width value..." );

            if ( !int.TryParse( xmlData.Attributes["height"].Value.Trim(), out mapData.height ) )
                throw new System.Exception( "Invalid Height value..." );

            //read tile map data
            var dataNode = xmlDocument.DocumentElement.SelectSingleNode( "/map/layer[@name='Map']/data" );
            var tiles = dataNode.InnerText.Split( ',' );
            var index = 0;

            mapData.tiledata = new List<TileDataMapper>();
            for ( var j = 0; j < mapData.height; j++ )
            {
                for ( var i = 0; i < mapData.width; i++ )
                {
                    mapData.tiledata.Add( new TileDataMapper( i, j, int.Parse( tiles[index++].Trim() ) ) );
                }
            }

            //players layer
            dataNode = xmlDocument.DocumentElement.SelectSingleNode( "/map/layer[@name='Players']/data" );
            tiles = dataNode.InnerText.Split( ',' );
            index = 0;

            mapData.players = new List<Point>
            {
                new Point(),
                new Point()
            };

            mapData.enemies = new List<Point>();

            for ( var j = 0; j < mapData.height; j++ )
            {
                for ( var i = 0; i < mapData.width; i++ )
                {
                    if ( int.Parse( tiles[index].Trim() ) == Constants.PLAYER1_ID )
                    {
                        mapData.players[0] = (new Point( i, j ));
                    }
                    else if ( int.Parse( tiles[index].Trim() ) == Constants.PLAYER2_ID )
                    {
                        mapData.players[1] = new Point( i, j );
                    }
                    else if ( int.Parse( tiles[index].Trim() ) == Constants.ENEMY_ID )
                    {
                        mapData.enemies.Add( new Point( i, j ) );
                    }

                    index++;
                }
            }

            return mapData;
        }

        private static XmlDocument GetXMLDocument( string filename )
        {
            var filePath = Application.dataPath + @"/Resources/Tilemaps/" + filename + @".xml";
            if ( !System.IO.File.Exists( filePath ) )
                return null;

            var xmlDocument = new XmlDocument();
            xmlDocument.Load( filePath );

            return xmlDocument;
        }
    }
}
\ No newline at end of file

M Assets/Scripts/Common/Constants.cs => Assets/Scripts/Common/Constants.cs +37 -88
@@ 37,6 37,21 @@ namespace Bomberman
    }

    [System.Serializable]
    public struct TileDataMapper
    {
        [SerializeField] public int i;
        [SerializeField] public int j;
        [SerializeField] public int value;

        public TileDataMapper( int i, int j, int value )
        {
            this.i = i;
            this.j = j;
            this.value = value;
        }
    }

    [System.Serializable]
    public struct TilesetData
    {
        public string prefix;


@@ 49,9 64,30 @@ namespace Bomberman
    {
        public int height;
        public int width;
        public int[,] data;
        public List<TileDataMapper> tiledata;
        public List<Point> players;
        public List<Point> enemies;

        //public void Set2DArrayFromTileDataMapper()
        //{
        //    data = new int[width, height];
        //    foreach ( var item in tiledata )
        //    {
        //        data[item.i, item.j] = item.value;
        //    }
        //}

        public void SetValue( int i, int j, int value )
        {
            var index = i * width + j;
            tiledata[index] = new TileDataMapper( i, j, value );
        }

        public int GetValue( int i, int j )
        {
            var index = i * width + j;
            return tiledata[index].value;
        }
    }

    /// <summary>


@@ 98,91 134,4 @@ namespace Bomberman
        public static LayerMask LAYER_EXPLOSION = LayerMask.NameToLayer( "Explosion" );
        public static LayerMask LAYER_POWERUP = LayerMask.NameToLayer( "Powerup" );
    }

    public class Common
    {
        public static MapData GetMapData( string filePath )
        {
            return ReadMapDataFromTMXFile( GetXMLDocument( filePath ) );
        }

        private static MapData ReadMapDataFromTMXFile( XmlDocument xmlDocument )
        {
            var mapData = new MapData();

            //read metadata
            var xmlData = xmlDocument.DocumentElement.SelectSingleNode( "/map" );
            if ( xmlData == null )
                throw new System.Exception( "Invalid XML Data..." );

            if ( !int.TryParse( xmlData.Attributes["width"].Value.Trim(), out mapData.width ) )
                throw new System.Exception( "Invalid Width value..." );

            if ( !int.TryParse( xmlData.Attributes["height"].Value.Trim(), out mapData.height ) )
                throw new System.Exception( "Invalid Height value..." );

            //read tile map data
            var dataNode = xmlDocument.DocumentElement.SelectSingleNode( "/map/layer[@name='Map']/data" );
            var tiles = dataNode.InnerText.Split( ',' );
            var index = 0;

            mapData.data = new int[mapData.width, mapData.height];
            for ( var j = 0; j < mapData.height; j++ )
            {
                for ( var i = 0; i < mapData.width; i++ )
                {
                    mapData.data[i, j] = int.Parse( tiles[index++].Trim() );
                }
            }

            //players layer
            dataNode = xmlDocument.DocumentElement.SelectSingleNode( "/map/layer[@name='Players']/data" );
            tiles = dataNode.InnerText.Split( ',' );
            index = 0;

            mapData.players = new List<Point>
            {
                new Point(),
                new Point()
            };

            mapData.enemies = new List<Point>();

            for ( var j = 0; j < mapData.height; j++ )
            {
                for ( var i = 0; i < mapData.width; i++ )
                {
                    if ( int.Parse( tiles[index].Trim() ) == Constants.PLAYER1_ID )
                    {
                        mapData.players[0] = (new Point( i, j ));
                    }
                    else if ( int.Parse( tiles[index].Trim() ) == Constants.PLAYER2_ID )
                    {
                        mapData.players[1] = new Point( i, j );
                    }
                    else if ( int.Parse( tiles[index].Trim() ) == Constants.ENEMY_ID )
                    {
                        mapData.enemies.Add( new Point( i, j ) );
                    }

                    index++;
                }
            }

            return mapData;
        }

        private static XmlDocument GetXMLDocument( string filename )
        {
            var filePath = Application.dataPath + @"/Resources/Tilemaps/" + filename + @".xml";

            if ( !System.IO.File.Exists( filePath ) )
                return null;

            var xmlDocument = new XmlDocument();
            xmlDocument.Load( filePath );

            return xmlDocument;
        }
    }
}
\ No newline at end of file

M Assets/Scripts/Core/Bomb.cs => Assets/Scripts/Core/Bomb.cs +13 -13
@@ 106,10 106,10 @@ namespace Bomberman
            var currentGrid = new Point( Mathf.Abs( ( int )transform.position.x ), Mathf.Abs( ( int )transform.position.z ) );

            //check for grid extrems or if the next block is indestructible
            var canMoveUp = (currentGrid.y - 1) >= 0 && mapData.data[currentGrid.x, currentGrid.y - 1] != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveDown = (currentGrid.y + 1) < mapData.height && mapData.data[currentGrid.x, currentGrid.y + 1] != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveRight = (currentGrid.x + 1) < mapData.width && mapData.data[currentGrid.x + 1, currentGrid.y] != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveLeft = (currentGrid.x - 1) >= 0 && mapData.data[currentGrid.x - 1, currentGrid.y] != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveUp = (currentGrid.y - 1) >= 0 && mapData.GetValue( currentGrid.x, currentGrid.y - 1 ) != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveDown = (currentGrid.y + 1) < mapData.height && mapData.GetValue( currentGrid.x, currentGrid.y + 1 ) != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveRight = (currentGrid.x + 1) < mapData.width && mapData.GetValue( currentGrid.x + 1, currentGrid.y ) != Constants.INDESTRUCTABLE_WALL_ID;
            var canMoveLeft = (currentGrid.x - 1) >= 0 && mapData.GetValue( currentGrid.x - 1, currentGrid.y ) != Constants.INDESTRUCTABLE_WALL_ID;

            //add grids to be used for explosin and update for next block
            for ( var i = 1; i <= range; i++ )


@@ 119,53 119,53 @@ namespace Bomberman
                {
                    var point = new Point( ( int )transform.position.x, ( int )transform.position.z + i );

                    var isCurrentBlockDestructibleWall = mapData.data[point.x, point.AbsY] == Constants.DESTRUCTABLE_WALL_ID;
                    var isCurrentBlockDestructibleWall = mapData.GetValue( point.x, point.AbsY ) == Constants.DESTRUCTABLE_WALL_ID;
                    DestroyCurrentBlockAndUpdateMapData( isCurrentBlockDestructibleWall, point );

                    //check if next point is valid
                    canMoveUp = point.y + 1 <= 0 &&
                                !isCurrentBlockDestructibleWall &&
                                mapData.data[point.x, point.AbsY - 1] != Constants.INDESTRUCTABLE_WALL_ID;
                                mapData.GetValue( point.x, point.AbsY - 1 ) != Constants.INDESTRUCTABLE_WALL_ID;
                }
                //down
                if ( canMoveDown )
                {
                    var point = new Point( ( int )transform.position.x, ( int )transform.position.z - i );

                    var isCurrentBlockDestructibleWall = mapData.data[point.x, point.AbsY] == Constants.DESTRUCTABLE_WALL_ID;
                    var isCurrentBlockDestructibleWall = mapData.GetValue( point.x, point.AbsY ) == Constants.DESTRUCTABLE_WALL_ID;
                    DestroyCurrentBlockAndUpdateMapData( isCurrentBlockDestructibleWall, point );


                    //check if next point is valid
                    canMoveDown = point.AbsY + 1 < mapData.height &&
                                    !isCurrentBlockDestructibleWall &&
                                    mapData.data[point.x, point.AbsY + 1] != Constants.INDESTRUCTABLE_WALL_ID;
                                    mapData.GetValue( point.x, point.AbsY + 1 ) != Constants.INDESTRUCTABLE_WALL_ID;
                }
                //right
                if ( canMoveRight )
                {
                    var point = new Point( ( int )transform.position.x + i, ( int )transform.position.z );

                    var isCurrentBlockDestructibleWall = mapData.data[point.x, point.AbsY] == Constants.DESTRUCTABLE_WALL_ID;
                    var isCurrentBlockDestructibleWall = mapData.GetValue( point.x, point.AbsY ) == Constants.DESTRUCTABLE_WALL_ID;
                    DestroyCurrentBlockAndUpdateMapData( isCurrentBlockDestructibleWall, point );

                    //check if next point is valid
                    canMoveRight = point.x + 1 < mapData.width &&
                                    !isCurrentBlockDestructibleWall &&
                                    mapData.data[point.x + 1, point.AbsY] != Constants.INDESTRUCTABLE_WALL_ID;
                                    mapData.GetValue( point.x + 1, point.AbsY ) != Constants.INDESTRUCTABLE_WALL_ID;
                }
                //left
                if ( canMoveLeft )
                {
                    var point = new Point( ( int )transform.position.x - i, ( int )transform.position.z );

                    var isCurrentBlockDestructibleWall = mapData.data[point.x, point.AbsY] == Constants.DESTRUCTABLE_WALL_ID;
                    var isCurrentBlockDestructibleWall = mapData.GetValue( point.x, point.AbsY ) == Constants.DESTRUCTABLE_WALL_ID;
                    DestroyCurrentBlockAndUpdateMapData( isCurrentBlockDestructibleWall, point );

                    //check if next point is valid
                    canMoveLeft = point.x - 1 >= 0 &&
                                !isCurrentBlockDestructibleWall &&
                                mapData.data[point.x - 1, point.AbsY] != Constants.INDESTRUCTABLE_WALL_ID;
                                mapData.GetValue( point.x - 1, point.AbsY ) != Constants.INDESTRUCTABLE_WALL_ID;
                }

                yield return new WaitForSeconds( delay );


@@ 186,7 186,7 @@ namespace Bomberman

            if ( isCurrentBlockDesWall )
            {
                mapData.data[point.x, point.AbsY] = Constants.GROUND_ID;
                mapData.SetValue( point.x, point.AbsY, Constants.GROUND_ID );
                var index = point.x + (point.AbsY * mapData.width);

                var listIndex = wallBlocks.FindIndex( obj => obj.index == index );

M Assets/Scripts/Core/Controller.cs => Assets/Scripts/Core/Controller.cs +4 -4
@@ 24,7 24,7 @@ namespace Bomberman
        /// <summary>
        /// grid data
        /// </summary>
        protected MapData mapData;
        [SerializeField] protected MapData mapData;

        /// <summary>
        /// next move position


@@ 42,10 42,10 @@ namespace Bomberman
        /// <summary>
        /// cache grid data
        /// </summary>
        public void SetMapData()
        public void SetMapData( MapData mapData )
        {
            //cache grid data
            mapData = Common.GetMapData( UnityEngine.SceneManagement.SceneManager.GetActiveScene().name );
            this.mapData = mapData;
        }

        /// <summary>


@@ 93,7 93,7 @@ namespace Bomberman
        protected bool IsValidMove( Point point )
        {
            return (point.x >= 0 && point.x < mapData.width && point.y <= 0 && point.AbsY < mapData.height) &&
                (mapData.data[point.AbsX, point.AbsY] == 0 || (mapData.data[point.AbsX, point.AbsY]) > Constants.INDESTRUCTABLE_WALL_ID);
                (mapData.GetValue( point.AbsX, point.AbsY ) == 0 || (mapData.GetValue( point.AbsX, point.AbsY )) > Constants.INDESTRUCTABLE_WALL_ID);
        }

        /// <summary>

M Assets/Scripts/Core/GameController.cs => Assets/Scripts/Core/GameController.cs +15 -15
@@ 36,26 36,26 @@ namespace Bomberman
        #region unity lifecycle
        private void Start()
        {
            if (playerControllers == null || playerControllers.Count == 0)
                throw new System.Exception("Cannot find any playerController references.");
            if ( playerControllers == null || playerControllers.Count == 0 )
                throw new System.Exception( "Cannot find any playerController references." );

            //enable yang for coop match
            bool isSinglePlayer = false;
            if (isSinglePlayer = PlayerPrefs.GetInt(Constants.GAME_TYPE, Constants.COOP_ID) != Constants.COOP_ID)
            if ( isSinglePlayer = PlayerPrefs.GetInt( Constants.GAME_TYPE, Constants.COOP_ID ) != Constants.COOP_ID )
            {
                playerControllers[1].gameObject.SetActive(!isSinglePlayer);
                playersBombCountText[1].gameObject.SetActive(false);
                playersPowerText[1].gameObject.SetActive(false);
                playerControllers.RemoveAt(1);
                playerControllers[1].gameObject.SetActive( !isSinglePlayer );
                playersBombCountText[1].gameObject.SetActive( false );
                playersPowerText[1].gameObject.SetActive( false );
                playerControllers.RemoveAt( 1 );
            }
        }

        private void Update()
        {
            if (levelTime <= 0)
            if ( levelTime <= 0 )
            {
                PlayerPrefs.SetString(Constants.GAME_RESULT, Constants.GAME_DRAW);
                UnityEngine.SceneManagement.SceneManager.LoadScene("gameover");
                PlayerPrefs.SetString( Constants.GAME_RESULT, Constants.GAME_DRAW );
                UnityEngine.SceneManagement.SceneManager.LoadScene( "gameover" );
            }
            else
            {


@@ 72,8 72,8 @@ namespace Bomberman
        /// </summary>
        private void UpdateTimerText()
        {
            var min = ((int)levelTime / 60).ToString();
            var sec = (int)levelTime % 60;
            var min = (( int )levelTime / 60).ToString();
            var sec = ( int )levelTime % 60;

            var secText = sec < 10 ? "0" : "";
            timerText.text = $"{min} : {secText}{sec}";


@@ 81,7 81,7 @@ namespace Bomberman

        private void UpdateBombCountText()
        {
            for (var i = 0; i < playerControllers.Count; i++)
            for ( var i = 0; i < playerControllers.Count; i++ )
            {
                playersBombCountText[i].text = $"{playerControllers[i].bombsInHand}";
                playersPowerText[i].text = $"{playerControllers[i].powerupName}";


@@ 94,9 94,9 @@ namespace Bomberman
        /// <summary>
        /// setup refs for player controllers
        /// </summary>
        public void AddPlayerController(PlayerController player)
        public void AddPlayerController( PlayerController player )
        {
            playerControllers.Add(player);
            playerControllers.Add( player );
        }

        /// <summary>

M Assets/Scripts/Enemy/AIController.cs => Assets/Scripts/Enemy/AIController.cs +0 -1
@@ 20,7 20,6 @@ namespace Bomberman
        #region unity lifecycle
        private void Awake()
        {
            SetMapData();
        }

        private void Update()

M Assets/Scripts/Player/PlayerController.cs => Assets/Scripts/Player/PlayerController.cs +1 -1
@@ 59,7 59,7 @@ namespace Bomberman
        #region unity lifecycle
        private void Awake()
        {
            SetMapData();
            //mapData.Set2DArrayFromTileDataMapper();
            bombsInHand = 1;
        }