public void Fire(Unit.Unit unit, Grid.Grid.Node destination, Action onComplete)
        {
            _move.Move(unit, destination, () =>
            {
                // spawn item??
                // destroy
                if (pickupPrefab)
                {
                    unit.QueueAction(new SpawnPickupAction(pickupPrefab, transform.position, item));
                    unit.QueueAction(new DestroyGameObjectAction(gameObject));
                }
                else
                {
                    _unit.Occupant.UpdateGrid(transform.position);
                }

                onComplete();
            });
        }
 public void StartAction(Unit.Unit unit)
 {
     // if about to destroy ourselves and more actions to perform then push this onto the back of the actions list
     if (unit.gameObject == _gameObject && unit.ActionsLeft > 1)
     {
         unit.QueueAction(this);
     }
     else
     {
         UnityEngine.Object.Destroy(_gameObject);
     }
 }
        public void Damage(Unit.Unit unit, DamageEffect effect)
        {
            var inventory = GetComponent <IProvider <Inventory> >().Get();

            if (inventory != null && healthKey != null)
            {
                if (inventory.RetrieveSlot(healthKey, out AggregateSlot health))
                {
                    health.Total -= Mathf.Min(effect.Damage, health.Total);
                    inventory.UpdateSlot(healthKey, health);
                    onDamage.Invoke(health.Total);

                    if (health.Total > 0)
                    {
                        return;
                    }
                }
            }

            // if i get here i either have no health or have spent it all
            onDestroy.Invoke();

            unit.QueueAction(new DestroyGameObjectAction(gameObject));
        }
示例#4
0
        private bool QueueActions(Vector3 position)
        {
            Grid.Grid.Node clickedNode        = new Grid.Grid.Node();
            var            isClickedNodeValid = _unit.Occupant.Grid.TryGetNodeAtWorldPosition(position, ref clickedNode);


            Grid.Grid.Node myNode        = new Grid.Grid.Node();
            var            isMyNodeValid = _unit.Occupant.Grid.TryGetNodeAtWorldPosition(transform.position, ref myNode);

            if (isMyNodeValid && isClickedNodeValid)
            {
                if (_isUsingBomb)
                {
                    if (_inventory == null)
                    {
                        return(false);
                    }

                    var diff = clickedNode.Position - myNode.Position;
                    if (!diff.IsCardinal())
                    {
                        return(false);
                    }

                    AggregateSlot slot;
                    if (!_inventory.RetrieveSlot(bombKey, out slot))
                    {
                        return(false);
                    }

                    slot.Total -= 1;

                    _inventory.UpdateSlot(bombKey, slot);

                    _unit.QueueRange(bombChucker.Use(_unit.Occupant.Occupant, diff, _unit.Occupant.Grid));

                    return(true);
                }
                else if (_isUsingItem)
                {
                    if (_item != null)
                    {
                        var direction = clickedNode.Position - _unit.Occupant.Occupant.Position;
                        var actions   = _item.Use(_unit.Occupant.Occupant, direction, _unit.Occupant.Grid);

                        if (actions != null && actions.Count > 0)
                        {
                            _unit.QueueRange(actions);
                            return(true);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    // attack
                    var direction = clickedNode.Position - _unit.Occupant.Occupant.Position;
                    var targets   = mainWeapon.FindTargets(_unit.Occupant.Occupant, direction, _unit.Occupant.Grid);

                    if (Array.Exists(targets, it => it.Position == clickedNode.Position))
                    {
                        var actions = mainWeapon.Use(_unit.Occupant.Occupant, direction, _unit.Occupant.Grid);

                        if (actions != null && actions.Count > 0)
                        {
                            _unit.QueueRange(actions);
                            return(true);
                        }
                    }


                    // move
                    var path = _pathfinding.CalculatePath(transform.position, position);

                    if (path != null && path.Count > 0)
                    {
                        var first = path[0];

                        _unit.QueueAction(new MoveToPointAction(_unit, first));
                        return(true);
                    }
                }
            }

            return(false);
        }