コード例 #1
0
        /// <summary>
        /// Selectable selection logic
        /// </summary>
        private void CheckLocationSelection()
        {
            if (_currentTarget == null)
            {
                onUndefinedSelection?.Invoke();
                return;
            }

            // If we already have a selection => MoveAction
            if (HasOneSelected() && !_lastSelected.IsEmpty && _currentTarget.IsEmpty)
            {
                TransferAction(_currentTarget);
                return;
            }

            // Check if we can select it..
            if (!_currentTarget.IsEmpty)
            {
                if (HasOneSelected())
                {
                    _lastSelected.IsSelected = false;
                }

                _lastSelected            = _currentTarget;
                _lastSelected.IsSelected = true;
                onSelect?.Invoke();
            }
            else
            {
                onUndefinedSelection?.Invoke();
            }
        }
コード例 #2
0
 /// <summary>
 /// Cancel current movement action
 /// </summary>
 public void CancelAction()
 {
     if (_lastSelected != null)
     {
         _lastSelected.IsSelected = false;
         _lastSelected            = null;
         onCancel?.Invoke();
     }
 }
コード例 #3
0
        /// <summary>
        /// Mouse movement check for target.
        /// Used for location highlighting and potential tile information gathering
        /// </summary>
        private void CheckCurrentTarget(Vector3 mousePos)
        {
            RaycastHit hit;
            Ray        ray = _cam.ScreenPointToRay(mousePos);

            if (Physics.Raycast(ray, out hit, Mathf.Infinity, selectableLayerMask))
            {
                // Make sure the selectable has associated script
                SnapLocation newRef = hit.transform.GetComponent <SnapLocation>();

                // Change state of older one
                if (_currentTarget != null && _currentTarget != newRef)
                {
                    _currentTarget.IsLit = false;
                }

                _currentTarget = newRef;

                // Make sure the new reference was indeed a SnapLocation.. maybe not
                if (_currentTarget == null)
                {
                    return;
                }

                // Here the current target is a valid SnapLocation
                _currentTarget.IsLit = true;
            }
            else
            {
                // We didn't hit this layer
                if (_currentTarget != null)
                {
                    _currentTarget.IsLit = false;
                    _currentTarget       = null;
                }
            }

            UpdateInBetween();
        }
コード例 #4
0
        /// <summary>
        /// Moving objects from one tile to another
        /// </summary>
        /// <param name="moveToTarget"></param>
        private void TransferAction(SnapLocation moveToTarget)
        {
            int towersOnGround = _totalTowerCount - _inventory.GetSize();

            // Check if player have already placed all his towers
            // Still allow transfers inside the inventory itself.
            // Still allow movement inside grid itself.
            // Player can place level + 2 amount of towers.
            if (FromInventoryToGrid(_lastSelected, moveToTarget) &&
                towersOnGround >= _player.GetPlayerLevel() + 2)
            {
                CancelAction();
                return;
            }

            _currentTarget.ReplaceObject(_lastSelected.GetObject());

            // Simply for good practice... this will trigger some event as well
            _lastSelected.Clear();

            _lastSelected.IsSelected = false;
            _lastSelected            = null;
            onTransfer?.Invoke();
        }
コード例 #5
0
 private bool FromInventoryToGrid(SnapLocation from, SnapLocation to)
 {
     return(from.transform.GetComponentInParent <Inventory>() != null && to.transform.GetComponentInParent <Inventory>() == null);
 }