Ejemplo n.º 1
0
        public override void removeObjectsFromCell(int cellId, List <WARGridObject> objects)
        {
            // find the designated cell
            WARActorCell cell = null;

            foreach (var c in cells)
            {
                if (c.id == cellId)
                {
                    cell = c;
                    break;
                }
            }

            // if we found the cell
            if (cell != null)
            {
                // remove the given objects
                foreach (var obj in objects)
                {
                    cell.objects.Remove(obj);
                }
            }
            // we were told to add objects to a cell we couldn't find
            else
            {
                Debug.LogError("Could not remove objects from cell with id " + cellId);
            }
        }
Ejemplo n.º 2
0
        // coroutine to move object between cells
        private IEnumerator moveBetweenCells(WARActorCell source, WARActorCell target, WARGrid grid)
        {
            // the moment in time along the path between the two cells
            var i = 0.0f;
            // the amount to move per tick
            var rate = 1.0f / speed;

            // if the cell and the target are the same then we're done
            if (source == target)
            {
                yield break;
            }
            // make sure the current cell highlighted
            lastCell = grid.GetCell(grid.findCellsUnderObject(this)[0]);
            lastCell.highlighted.Value = true;

            // until we reach the end
            while (i < 1.0)
            {
                // increment the distance counter to the next tick
                i += Time.deltaTime * rate;

                // raycast to find the cell under us
                var currentCell = grid.GetCell(grid.findCellsUnderObject(this)[0]);

                // if the cell is different from the last one we saw
                if (lastCell.id != currentCell.id)
                {
                    // unhighlight the last cell
                    lastCell.highlighted.Value = false;
                    // and highlilght the one we are under
                    currentCell.highlighted.Value = true;
                    // add ourself to the new cell
                    grid.addObjectsToCell(currentCell.id, new List <WARGridObject> {
                        this
                    });
                    // remove ourself from the old cell
                    grid.removeObjectsFromCell(lastCell.id, new List <WARGridObject> {
                        this
                    });

                    // update the cell tracker
                    lastCell = currentCell;
                }

                // update our position to the appropriate part of the lerp
                gameObject.transform.position = Vector3.Lerp(
                    source.transform.position,
                    target.transform.position,
                    i
                    );
                // we're done for this tick
                yield return(null);
            }
        }
Ejemplo n.º 3
0
        private IEnumerator walkPath(List <int> path, WARGrid grid)
        {
            // the last cell we saw
            var lastCell = path[0];

            foreach (var cell in path.Skip(1))
            {
                // the source and target cells
                var source = grid.GetCell(lastCell);
                var target = grid.GetCell(cell);

                gameObject.transform.LookAt(target.transform.position);

                // move between this cell and the next
                yield return(moveBetweenCells(source, target, grid));

                // make sure we move between this cell next time
                lastCell = cell;
            }

            // we're done with our coroutine
            pathRoutine = null;
        }
Ejemplo n.º 4
0
        public override void createGrid()
        {
            // position and rotate our table actor container to match plane.center and rotation
            transform.position = plane.center;

            // create a grid of hexagons given our plane.extent (x,z) for (width,height)
            // TODO, figure out which direction we dragged so we can always start the grid
            // at bottom left of the created plane
            Vector3 origin = transform.position - plane.extent * 0.5f;

            // outter radius is from center to vertex
            float outterRadius = transform.localScale.z * globalGridScale;
            // inner radius is from center to edge
            float innerRadius = outterRadius * Mathf.Sqrt(3) / 2f;            //magic hexagon math

            // how many columns and rows of hexagon tiles will fit in our desired plane
            int numberOfColumns = Mathf.CeilToInt(plane.extent.x / (3f * outterRadius));
            int numberOfRows    = Mathf.CeilToInt(plane.extent.z / innerRadius);

            // grid cannot exist without atleast two rows
            numberOfRows = numberOfRows > 1 ? numberOfRows : 2;

            // start the id counter
            var id = 0;

            // loop through our number of columns and rows
            for (int x = 0; x < numberOfColumns; x++)
            {
                for (int y = 0; y < numberOfRows; y++)
                {
                    // each column is 1.5 outter radii away from eachother and each row is 1 inner radius away
                    Vector3 offset = new Vector3(
                        x * 3f * outterRadius,
                        0f,
                        y * innerRadius
                        );

                    // we create double the number of cells in a column and offset half to fill the gaps
                    if (y % 2 == 0)
                    {
                        offset += new Vector3(1.5f * outterRadius, 0f, 0f);
                    }
                    // TODO we probably want to use a WARActorGrid class to hold the cells themselves
                    // and give us some control functions (distance, grid coords to cell obj, etc.)
                    // we create our hex cell as a child of our game object container, go
                    GameObject hex = GameObject.Instantiate(hexPrefab, origin + offset, hexPrefab.transform.rotation, transform);

                    // and scale the grid based on our global scale factor
                    hex.transform.localScale = globalGridScale * hex.transform.localScale;
                    WARActorCell cell = hex.GetComponent <WARActorCell>().Init();
                    if (cell)
                    {
                        // make sure we get a unique id next time
                        cell.id = id;
                        // and add it to the list
                        cells.Add(cell);
                        cell.GetComponentInChildren <TextMesh>().text = id.ToString();
                        cell.neighbors = FindCellNeighborIDs(cell.id, numberOfColumns, numberOfRows);

                        // increment the id counter
                        id++;
                    }
                }
            }

            // align the table to match the designated plane
            transform.rotation = plane.rotation;
        }