private void Awake()
 {
     if (!instance)
     {
         instance = this;
     }
 }
        public static void SaveLevel(string saveName, GridBase gridBase)
        {
            if (string.IsNullOrEmpty(saveName))
            {
                saveName = "level1";
            }

            SaveLevelFile saveFile = new SaveLevelFile();

            saveFile.sizeX = gridBase.sizeX;
            saveFile.sizeY = gridBase.sizeY;
            saveFile.sizeZ = gridBase.sizeZ;

            saveFile.scaleXZ    = gridBase.scaleXZ;
            saveFile.scaleY     = gridBase.scaleY;
            saveFile.scaleXZ    = gridBase.scaleXZ;
            saveFile.savedNodes = NodeToSaveable(gridBase);

            string saveLocation = SaveLocation();

            saveLocation += saveName;

            IFormatter formatter = new BinaryFormatter();
            Stream     stream    = new FileStream(saveLocation, FileMode.Create, FileAccess.Write, FileShare.None);

            formatter.Serialize(stream, saveFile);
            stream.Close();
        }
Пример #3
0
        public void LoadObstacles(GridBase gridBase, bool isInEdit = false)
        {
            Obstacle[] allObstacle = GameObject.FindObjectsOfType <Obstacle>();
            obstacles.AddRange(allObstacle);

            foreach (Obstacle obstacle in obstacles)
            {
                BoxCollider bx = null;
                if (obstacle.mainRenderer)
                {
                    bx = obstacle.mainRenderer.gameObject.AddComponent <BoxCollider>();
                }
                if (bx)
                {
                    float halfX = bx.size.x * 0.5f;
                    float halfY = bx.size.y * 0.5f;
                    float halfZ = bx.size.z * 0.5f;

                    Vector3 center = obstacle.mainRenderer.bounds.center;
                    Vector3 from   = obstacle.mainRenderer.bounds.min;

                    from.y = 0;

                    Vector3 to = obstacle.mainRenderer.bounds.max;
                    to.y = 0;

                    int stepX = Mathf.RoundToInt(Mathf.Abs(from.x - to.x) / gridBase.scaleXZ);
                    int stepZ = Mathf.RoundToInt(Mathf.Abs(from.z - to.z) / gridBase.scaleXZ);

                    for (int x = 0; x < stepX; x++)
                    {
                        for (int z = 0; z < stepZ; z++)
                        {
                            Vector3 tp = from;
                            tp.x += gridBase.scaleXZ * x;
                            tp.z += gridBase.scaleXZ * z;
                            //tp.y = obstacle.mainRenderer.transform.position.y;

                            Vector3 p = obstacle.mainRenderer.transform.InverseTransformPoint(tp) - bx.center;
                            tp.y = 0;

                            if (p.x < halfX && p.y < halfY && p.z < halfZ && p.x > -halfX && p.y > -halfY && p.z > -halfZ)
                            {
                                Node n = gridBase.GetNodeFromWorldPosition(tp);
                                n.ChangeNodeStatus(false, gridBase);
                            }
                        }
                    }
                }
                if (isInEdit)
                {
                    DestroyImmediate(bx);
                }
                else
                {
                    Destroy(bx);
                }
            }
        }
        public static List <SaveableNode> NodeToSaveable(GridBase grid)
        {
            List <SaveableNode> returnValue = new List <SaveableNode>();

            for (int x = 0; x < grid.sizeX; x++)
            {
                for (int y = 0; y < grid.sizeY; y++)
                {
                    for (int z = 0; z < grid.sizeZ; z++)
                    {
                        SaveableNode sn = new SaveableNode();
                        Node         n  = grid.grid[x, y, z];
                        sn.x          = n.X;
                        sn.y          = n.Y;
                        sn.z          = n.Z;
                        sn.isWalkable = n.IsWalkable;
                        returnValue.Add(sn);
                    }
                }
            }

            return(returnValue);
        }
        public void Init()
        {
            grid      = GridBase.instance;
            uIManager = UIManager.instance;
            uIManager.Init();


            GameObject blue = new GameObject();

            blue.name                 = "lvl visual blue";
            pathBlueVisual            = blue.AddComponent <LineRenderer>();
            pathBlueVisual.startWidth = 0.2f;
            pathBlueVisual.endWidth   = 0.2f;
            pathBlueVisual.material   = this.blue;

            GameObject red = new GameObject();

            red.name                 = "lvl visual red";
            pathRedVisual            = red.AddComponent <LineRenderer>();
            pathRedVisual.startWidth = 0.2f;
            pathRedVisual.endWidth   = 0.2f;
            pathRedVisual.material   = this.red;
            InitUnit();
        }