コード例 #1
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);
            }
        }
コード例 #2
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;
        }
コード例 #3
0
        public void followPath(List <int> path, WARGrid grid)
        {
            // make sure the source isn't highlighted
            grid.GetCell(path[0]).highlighted.Value = false;

            // if we are in the middle of walking a path
            if (pathRoutine != null)
            {
                // stop the previous routine
                StopCoroutine(pathRoutine);
            }

            // start walking the path
            pathRoutine = walkPath(path, grid);
            StartCoroutine(pathRoutine);
        }