Ejemplo n.º 1
0
        private void Update()
        {
            if (!_isActive)
            {
                return;
            }
            if (grid == null || grid.Value == null)
            {
                return;
            }
            if (uiTilemap == null)
            {
                return;
            }

            // todo(chris) remove nasty hack for "is paused"
            if (Time.timeScale < float.Epsilon)
            {
                return;
            }

            var worldPoint = _camera.ScreenToWorldPoint(Input.mousePosition);
            var node       = new Grid.Grid.Node();

            if (grid.Value.TryGetNodeAtWorldPosition(worldPoint, ref node))
            {
                if (_node?.Equals(node) != true)
                {
                    _node = node;
                    OnNewNode(node);
                }
            }
        }
Ejemplo n.º 2
0
        private IEnumerator CoMove(Unit.Unit unit, Grid.Grid.Node destination, float speed, bool twoWayCollisions, Transform toMove)
        {
            Vector2 endPoint = destination.WorldPosition;

            var startNode = unit.Occupant.CurrentNode;

            if (startNode == null)
            {
                yield break;
            }

            var lastNode = startNode.Value;

            var startPoint = (Vector2)toMove.position;
            var start      = Time.time;
            var distance   = (endPoint - startPoint).magnitude;
            var duration   = distance / speed;
            var end        = start + duration;

            _collider2D = null;

            onStartMove.Invoke();

            while (Time.time < end && _collider2D == null)
            {
                var t = (Time.time - start) / duration;
                var x = Tween.Lerp(startPoint.x, endPoint.x, t);
                var y = Tween.Lerp(startPoint.y, endPoint.y, t);
                toMove.position = new Vector3(x, y, toMove.position.z);

                if (moveToLastNodeOnCollision)
                {
                    var node = new Grid.Grid.Node();
                    if (!unit.Occupant.Grid.TryGetNodeAtWorldPosition(toMove.position, ref node))
                    {
                        toMove.position = lastNode.WorldPosition;
                        yield break;
                    }

                    if (!node.Equals(lastNode) && !node.Equals(destination))
                    {
                        if (Vector2.Distance(toMove.position, node.WorldPosition) < .4f)
                        {
                            lastNode = node;
                        }
                    }
                }

                yield return(null);
            }

            OnStopMove.Invoke();

            if (_collider2D == null && _myCollider != null)
            {
                var cached = _myCollider.enabled;
                _myCollider.enabled = false;
                _collider2D         = Physics2D.OverlapCircle(toMove.position, .3f, layers);

                _myCollider.enabled = cached;
            }

            if (_collider2D == null)
            {
                toMove.position = new Vector3(endPoint.x, endPoint.y, toMove.position.z);
            }
            else
            {
                yield return(StartCoroutine(OnCollision(unit, startNode.Value, lastNode, speed, twoWayCollisions, toMove)));
            }
        }