示例#1
0
        // Event handlers.

        private void UpdateNode(UnitPresentation unit)
        {
            if (unitSystemPresentation.Contains(unit))
            {
                var position = unitSystemPresentation[unit];
                if (unitsToNodes.TryGetValue(unit, out var node))
                {
                    node.Position = position;
                }
                else
                {
                    node = NodeController.BuildNode(map, position);
                    node.transform.parent = transform;
                    unitsToNodes[unit]    = node;
                }
            }
            else
            {
                if (unitsToNodes.TryGetValue(unit, out var node))
                {
                    Destroy(node.gameObject);
                    unitsToNodes.Remove(unit);
                }
            }
        }
        public void MoveUnit(UnitPresentation unitPresentation, Vector2Int position)
        {
            if (unitPresentation is null)
            {
                throw new ArgumentNullException(nameof(unitPresentation));
            }
            if (!Contains(unitPresentation))
            {
                throw new InvalidOperationException($"There is no unit {unitPresentation}!");
            }
            var onPosition = this[position];

            if (onPosition != null)
            {
                if (onPosition != unitPresentation)
                {
                    throw new InvalidOperationException($"Position {position} is already occupied by {onPosition}.");
                }
            }
            else
            {
                UnitCore unit        = presentationsToUnits[unitPresentation];
                var      oldPosition = unitsToPositions[unit];
                positionsToUnits.Remove(oldPosition);
                unitsToPositions[unit]     = position;
                positionsToUnits[position] = unit;
                state = state.BuildTransition(unitPresentation);
            }
        }
示例#3
0
        private string Format(Summary summary, ImmutableConfig config, Statistics statistics, SummaryStyle style)
        {
            if (statistics == null)
            {
                return("NA");
            }

            int    precision = summary.DisplayPrecisionManager.GetPrecision(style, this, parentColumn);
            string format    = "N" + precision;

            double value = calc(statistics);

            if (double.IsNaN(value))
            {
                return("NA");
            }
            return(UnitType == UnitType.Time
                ? TimeInterval.FromNanoseconds(value)
                   .ToString(
                       style.TimeUnit,
                       style.CultureInfo,
                       format,
                       UnitPresentation.FromVisibility(style.PrintUnitsInContent))
                : value.ToString(format, style.CultureInfo));
        }
示例#4
0
        // Fabric.

        public static UnitController BuildUnit(UnitPresentation presentation)
        {
            GameObject     unit       = Instantiate(Prefab);
            UnitController controller = unit.GetComponent <UnitController>();

            controller.Build(presentation);
            return(controller);
        }
        // Informational methods.

        public bool Contains(UnitPresentation unitPresentation)
        {
            if (unitPresentation is null)
            {
                throw new ArgumentNullException(nameof(unitPresentation));
            }
            return(presentationsToUnits.ContainsKey(unitPresentation));
        }
        // Properties.

        public Vector2Int this[UnitPresentation unitPresentation] {
            get {
                if (!presentationsToUnits.TryGetValue(unitPresentation, out UnitCore unit))
                {
                    throw new KeyNotFoundException($"No such unit ({unitPresentation}) in unit system!");
                }
                return(unitsToPositions[unit]);
            }
        }
示例#7
0
        // Usage.

        public bool CheckIfUsable(UnitPresentation target)
        {
            if (target is null || Context is null || target.Context != Context)
            {
                return(false);
            }
            Vector3 ownerPosition  = Owner.Body.Position;
            Vector3 targetPosition = Owner.Body.Position;

            return((ownerPosition - targetPosition).magnitude <= attackRange);
        }
示例#8
0
        // Building.

        public void Build(UnitPresentation presentation)
        {
            if (presentation is null)
            {
                throw new ArgumentNullException(nameof(presentation));
            }
            Start();
            ValidateNotStartedBuilding();

            // Build stuff.
            SetStartedBuilding();
            this.presentation = presentation;
            name = presentation.Name;
            SetFinishedBuilding();
        }
示例#9
0
        public void Use(UnitPresentation target)
        {
            if (!CheckIfUsable(target))
            {
                return;
            }
            var condition = new DirectAttackCondition(
                target: target,
                singleAttackDamage: singleAttackDamage,
                attackDuration: attackDuration,
                attackMoment: attackMoment,
                attackRange: attackRange,
                id: conditionId
                );

            Owner.PlanAction(new ConditionChangingAction(condition, forceChange: false));
        }
        public DirectAttackCondition(DirectAttackCondition other)
        {
            // Meta.
            id = other.id;

            // Properties.
            target             = other.target;
            singleAttackDamage = other.singleAttackDamage;
            attackDuration     = other.attackDuration;
            attackMoment       = other.attackMoment;
            attackRange        = other.attackRange;

            // Progress.
            accumulatedTime = other.accumulatedTime;
            attacked        = other.attacked;
            atStart         = other.atStart;
        }
        // Constructors.

        public DirectAttackCondition(
            UnitPresentation target, Damage singleAttackDamage,
            float attackDuration, float attackMoment, float attackRange,
            NamedId id)
        {
            // Meta.
            this.id = id;

            // Attack properties.
            this.target             = target ?? throw new ArgumentNullException(nameof(target));
            this.singleAttackDamage = singleAttackDamage;
            this.attackDuration     = Floats.SetPositive(attackDuration);
            this.attackMoment       = Floats.LimitPositive(attackMoment, this.attackDuration);
            this.attackRange        = Floats.SetPositive(attackRange);

            // Progress.
            accumulatedTime = 0f;
            attacked        = false;
            atStart         = true;
        }
        public UnitCore RemoveUnit(UnitPresentation unitPresentation)
        {
            if (unitPresentation is null)
            {
                throw new ArgumentNullException(nameof(unitPresentation));
            }
            if (!Contains(unitPresentation))
            {
                throw new InvalidOperationException($"There is no unit {unitPresentation}!");
            }
            UnitCore unit = presentationsToUnits[unitPresentation];

            unit.Disconnect();
            presentationsToUnits.Remove(unitPresentation);
            var position = unitsToPositions[unit];

            positionsToUnits.Remove(position);
            unitsToPositions.Remove(unit);
            state = state.BuildTransition(unitPresentation);
            return(unit);
        }
示例#13
0
        // Constructor.

        public OffensiveGoal(UnitPresentation target)
        {
            this.target = target ?? throw new ArgumentNullException(nameof(target));
        }
        // Methods.

        public bool Contains(UnitPresentation unitPresentation) => Presented.Contains(unitPresentation);
示例#15
0
        // Methods.

        public bool CheckIfUsable(UnitPresentation target) => Presented.CheckIfUsable(target);
示例#16
0
        // Constructor.

        public AttackUsageAction(IAttackAbilityPresentation ability, UnitPresentation target) : base(ability)
        {
            this.target = target ?? throw new ArgumentNullException(nameof(target));
        }
        // Constructor.

        public UnitMotionAction(UnitPresentation unit, Vector2Int position)
        {
            this.unit     = unit ?? throw new System.ArgumentNullException(nameof(unit));
            this.position = position;
        }
示例#18
0
        // Constructor.

        public UnitRemovalAction(UnitPresentation unit)
        {
            this.unit = unit ?? throw new System.ArgumentNullException(nameof(unit));
        }
示例#19
0
        // Support.

        private void UpdateInfo()
        {
            UnitPresentation presentation = null;

            if (selectedUnit != null)
            {
                presentation = selectedUnit.Presentation;
            }

            // Update name.
            unitNameText.text = presentation?.Name ?? "";

            // Update description.
            if (presentation != null)
            {
                var stringBuilder = new StringBuilder();

                // Describe goal.
                var currentGoal    = presentation.Intelligence.CurrentGoal;
                var goalDescriptor = Descriptors.ForGoals[currentGoal.Id];
                stringBuilder.AppendLine($"Current goal: {goalDescriptor.ComposeDescription(currentGoal)}");
                stringBuilder.AppendLine();

                // Describe condition.
                var conditionDescriptor = Descriptors.ForConditions[presentation.Condition.Id];
                stringBuilder.AppendLine($"Current condition: {conditionDescriptor.ComposeDescription(presentation.Condition)}");
                stringBuilder.AppendLine();

                // Describe abilities.
                var abilities = presentation.AbilityCollection.Abilities;
                if (abilities.Count == 0)
                {
                    stringBuilder.AppendLine("No abilities.");
                }
                else
                {
                    stringBuilder.AppendLine($"Abilities ({abilities.Count}):");
                    foreach (var ability in abilities)
                    {
                        var abilityDescriptor = Descriptors.ForAbilities[ability.Id];
                        stringBuilder.AppendLine(abilityDescriptor.Name);
                    }
                }

                unitDescriptionText.text = stringBuilder.ToString();
            }
            else
            {
                unitDescriptionText.text = "Click on unit to get its description.";
            }

            // Update durability info.
            if (presentation != null)
            {
                var durabilityModule = presentation.Durability;
                durabilitySection.gameObject.SetActive(true);
                durabilitySection.SetDurability(durabilityModule);
            }
            else
            {
                durabilitySection.gameObject.SetActive(false);
                durabilitySection.SetDurability(null);
            }
        }
        // Properties.

        public Vector2Int this[UnitPresentation unitPresentation] => Presented[unitPresentation];