예제 #1
0
        private void PreviewPlacement(bool active = false)
        {
            if (!active)
            {
                _gridPlacement.DisablePreview();
                return;
            }
            Ray ray = _mainCamera.ScreenPointToRay(Input.mousePosition);

            Physics.Raycast(ray, out var hit, 100f);
            if (hit.collider == null)
            {
                return;
            }

            GridChunk gridChunk = hit.collider.GetComponent <GridChunk>();

            if (gridChunk != null)
            {
                gridChunk.PreviewPlacement(_gridPlacement, hit.point);
            }
            else
            {
                _gridPlacement.DisablePreview();
            }
        }
예제 #2
0
    public bool CreateMap(int xx, int zz)
    {
        if (xx <= 0 || xx % Metrics.chunkSizeX != 0 ||
            zz <= 0 || zz % Metrics.chunkSizeZ != 0)
        {
            Debug.LogError("Unsupported map size " + xx + zz);
            return(false);
        }

        cellCountX  = xx;
        cellCountZ  = zz;
        chunkCountX = xx / Metrics.chunkSizeX;
        chunkCountZ = zz / Metrics.chunkSizeZ;

        chunks = new GridChunk[chunkCountZ * chunkCountX];
        for (int z = 0, i = 0; z < chunkCountZ; z++)
        {
            for (int x = 0; x < chunkCountX; x++)
            {
                GridChunk chunk = chunks[i++] = Instantiate(chunkPrefab);
                chunk.transform.SetParent(transform);
            }
        }

        cells = new Cell[cellCountZ * cellCountX];
        for (int z = 0, i = 0; z < cellCountZ; z++)
        {
            for (int x = 0; x < cellCountX; x++)
            {
                CreateCell(x, z, i++);
            }
        }

        return(true);
    }
예제 #3
0
        public void TestValueEquality()
        {
            var chunk1 = new GridChunk <int>();
            var chunk2 = new GridChunk <int>();

            chunk1[0, 13] = -42;
            chunk2[0, 13] = -42;

            Assert.AreEqual(chunk1, chunk2, "The chunks should be equal.");
        }
예제 #4
0
        public void TestContains()
        {
            var chunk = new GridChunk <int>();

            chunk[32, 32] = 42;

            Assert.AreEqual(true, chunk.Contains(42));
            Assert.AreEqual(true, chunk.Contains(0));
            Assert.AreEqual(false, chunk.Contains(33));
        }
예제 #5
0
    /// <summary>
    /// Assigns a cell to a chunk Dependant on its location and the chunk size.
    /// </summary>
    /// <param name="x"> int x location of cell </param>
    /// <param name="y"> int y location of cell </param>
    /// <param name="cell"> Cell cell being assigned</param>
    void AddCellToChunk(int x, int y, Cell cell)
    {
        int       chunkX = x / GlobalDataBase.chunckSizeX;
        int       chunkZ = y / GlobalDataBase.chunckSizeY;
        GridChunk chunk  = chunks[chunkX + chunkZ * chunkCountX];

        int localX = x - chunkX * GlobalDataBase.chunckSizeX; //localX coordinate based on chunk size
        int localY = y - chunkZ * GlobalDataBase.chunckSizeY; //localY coordinate based on chunk size

        chunk.AddCell(localX + localY * GlobalDataBase.chunckSizeX, cell);
    }
예제 #6
0
    void AddCellToChunk(int x, int z, GridCell cell)
    {
        int       chunkX = x / GridMetrics.chunkSizeX;
        int       chunkZ = z / GridMetrics.chunkSizeZ;
        GridChunk chunk  = chunks[chunkX + chunkZ * chunkCountX];

        int localX = x - chunkX * GridMetrics.chunkSizeX;
        int localZ = z - chunkZ * GridMetrics.chunkSizeZ;

        chunk.AddCell(localX + localZ * GridMetrics.chunkSizeX, cell);
    }
예제 #7
0
        public void TestValueEquality2()
        {
            var refthing = new ReferenceTypeTestThing(5);

            var chunk1 = new GridChunk <ReferenceTypeTestThing>();
            var chunk2 = new GridChunk <ReferenceTypeTestThing>();

            chunk1[12, 4] = refthing;
            chunk2[12, 4] = refthing;

            Assert.AreEqual(chunk1, chunk2, "The chunks should be equal.");
        }
예제 #8
0
        public void TestIsEmpty()
        {
            boolchunk = new GridChunk <bool>();

            boolchunk[0, 0] = true;

            Assert.IsFalse(boolchunk.IsEmpty);

            boolchunk[0, 0] = false;

            Assert.IsFalse(!boolchunk.IsEmpty);
        }
예제 #9
0
 void CreateChunks()
 {
     chunks = new GridChunk[chunkCountX * chunkCountZ];
     for (int z = 0, i = 0; z < chunkCountZ; z++)
     {
         for (int x = 0; x < chunkCountX; x++)
         {
             GridChunk chunk = chunks[i++] = Instantiate(chunkPrefab);
             chunk.transform.SetParent(transform);
             chunk.transform.position = new Vector3(x * GridMetrics.chunkSizeX, 0, z * GridMetrics.chunkSizeZ);
             chunk.chunkTypes         = GridMetrics.GenerateChunkType();
         }
     }
 }
예제 #10
0
    void CreateChunks()
    {
        chunks = new GridChunk[Metrics.chunkCount()];

        for (int z = 0, i = 0;  z < Metrics.chunkCountZ; z++)
        {
            for (int x = 0; x < Metrics.chunkCountX; x++)
            {
                //GridChunk chunk = chunks[i++] = Instantiate(chunkPrefab, new Vector3(Metrics.chunkSizeX*Metrics.scale,0,Metrics.chunkSizeZ*z*Metrics.scale), Quaternion.identity);
                //chunks[i] = Instantiate(chunkPrefab, new Vector3(Metrics.chunkSizeX * Metrics.scale * x, 0, Metrics.chunkSizeZ * z * Metrics.scale), Quaternion.identity);
                GridChunk chunk = chunks[i++] = Instantiate(chunkPrefab);
                chunk.transform.SetParent(transform);
                //chunks[i].setHeights(x,z, map);
                //i++;
            }
        }
    }
예제 #11
0
    /// <summary>
    /// Creates empty game objects for Columns and Chunks for the cells to be assigned to.
    /// Creates as many chunks as needed in the X and Y direction to fit all cells in.
    /// </summary>
    void CreateChunks()
    {
        columns = new Transform[chunkCountX];

        for (int x = 0; x < chunkCountX; x++)
        {
            columns[x] = new GameObject("Column").transform;
            columns[x].SetParent(transform, false);
        }

        chunks = new GridChunk[chunkCountX * chunkCountY];
        for (int z = 0, i = 0; z < chunkCountY; z++)
        {
            for (int x = 0; x < chunkCountX; x++)
            {
                GridChunk chunk = chunks[i++] = Instantiate(chunkPrefab);
                chunk.transform.SetParent(columns[x], false);
            }
        }
    }
예제 #12
0
        public void TestClearReferenceType()
        {
            var chunk = new GridChunk <ReferenceTypeTestThing>();

            for (int x = 1; x < GridChunk <int> .ChunkSize; x++)
            {
                for (int y = 1; y < GridChunk <int> .ChunkSize; y++)
                {
                    chunk[x - 1, y - 1] = new ReferenceTypeTestThing(1);
                }
            }


            chunk.Clear();

            foreach (var value in chunk)
            {
                Assert.AreEqual(null, value);
            }
        }
예제 #13
0
        public void TestClearValueType()
        {
            var chunk = new GridChunk <int>();

            for (int x = 1; x < GridChunk <int> .ChunkSize; x++)
            {
                for (int y = 1; y < GridChunk <int> .ChunkSize; y++)
                {
                    chunk[x - 1, y - 1] = x * y;
                }
            }


            chunk.Clear();

            foreach (var value in chunk)
            {
                Assert.AreEqual(0, value);
            }
        }
예제 #14
0
    void CreateCell(int x, int z, int i)
    {
        Vector3 position = new Vector3(x * Metrics.radius * 2f, 0f, z * Metrics.radius * 2f);
        Cell    cell     = Instantiate(cellPrefab);

        // cell.transform.SetParent(transform, false);
        cell.transform.localPosition = position;
        cell.coordinates             = Coordinates.FromOffsetCoordinates(x, z);

        if (x > 0)
        {
            cell.SetNeighbor(Direction.W, cells[i - 1]);
        }
        if (z > 0)
        {
            cell.SetNeighbor(Direction.S, cells[i - cellCountX]);
        }

        cells[i] = cell;

        Text label = Instantiate <Text>(cellLabelPrefab);

        // label.rectTransform.SetParent(gridCanvas.transform, false);
        label.rectTransform.anchoredPosition = new Vector2(position.x, position.z);
        label.text  = cell.coordinates.ToString();
        cell.uiRect = label.rectTransform;

        // add cell to chunk
        int       chunkX = x / Metrics.chunkSizeX;
        int       chunkZ = z / Metrics.chunkSizeZ;
        GridChunk chunk  = chunks[chunkX + chunkZ * chunkCountX];
        int       localX = x - chunkX * Metrics.chunkSizeX;
        int       localZ = z - chunkZ * Metrics.chunkSizeZ;

        chunk.AddCell(localX, localZ, cell);

        cell.Elevation = 0; // Refresh
    }
예제 #15
0
        private void LeftClickRaycast()
        {
            Ray ray = _mainCamera.ScreenPointToRay(Input.mousePosition);

            Physics.Raycast(ray, out var hit, 100f);
            if (hit.collider == null)
            {
                return;
            }

            GridChunk gridChunk = hit.collider.GetComponent <GridChunk>();

            if (gridChunk != null)
            {
                gridChunk.PlaceObject(_gridPlacement, hit.point);
                AudioManager.Instance.PlayBuildSound();
            }
            GridObject gridObject = hit.collider.GetComponent <GridObject>();

            if (gridObject != null)
            {
                gridObject.OnSelect();
            }
        }
예제 #16
0
    // Use this for initialization
    void Awake()
    {
        Grid = new GridElement[GridSize, GridSize];
        InitializeGrid();

        GridChunk Chunk = new GridChunk(GridSize, 4);

        DetectPoint = new DetectPointOnGrid();
        DetectPoint.SetGridSize(GridSize);

        Vector2 Point = Camera.main.ViewportToWorldPoint(StartPosition.position);

        int Count = ( (5 * GridSize) * 2);
        Line.SetVertexCount(Count);

        int i = 0;
           // for (int i = 0; i < Count;)
           // {
            for(int j = 0; j< GridSize; j++)
            {
                Line.SetPosition(i, new Vector3(Grid[0, j].getA().x, Grid[0, j].getA().y, Grid[0, j].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[GridSize - 1, j].getB().x, Grid[GridSize - 1, j].getB().y, Grid[GridSize - 1, j].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[GridSize - 1, j].getC().x, Grid[GridSize - 1, j].getC().y, Grid[GridSize - 1, j].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[0, j].getD().x, Grid[0, j].getD().y, Grid[0, j].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[0, j].getA().x, Grid[0, j].getA().y, Grid[0, j].getZ()));
                i++;
            }
            int index = GridSize - 1;
            for (int l = 0; l < GridSize; l++)
            {
                Line.SetPosition(i, new Vector3(Grid[l, index].getD().x, Grid[l, index].getD().y, Grid[l, index].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[l, 0].getA().x, Grid[l, 0].getA().y, Grid[l, 0].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[l, 0].getB().x, Grid[l, 0].getB().y, Grid[l, 0].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[l, index].getC().x, Grid[l, index].getC().y, Grid[l, index].getZ()));
                i++;
                Line.SetPosition(i, new Vector3(Grid[l, index].getD().x, Grid[l, index].getD().y, Grid[l, index].getZ()));
                i++;
            }

           // }
    }