// Constructors.

        public BattlefieldCore(MapCore map, LayoutCore layout, UnitSystemCore unitSystem)
        {
            this.map = map?.Clone() ?? throw new ArgumentNullException(nameof(map));
            this.map.Connect(Presentation);
            this.layout = layout?.Clone() ?? throw new ArgumentNullException(nameof(layout));
            this.layout.Connect(Presentation);
            this.unitSystem = unitSystem?.Clone() ?? throw new ArgumentNullException(nameof(unitSystem));
            this.unitSystem.Connect(Presentation);
        }
 public BattlefieldCore(BattlefieldCore other)
 {
     map = other.map.Clone();
     map.Connect(Presentation);
     layout = other.layout.Clone();
     layout.Connect(Presentation);
     unitSystem = other.unitSystem.Clone();
     unitSystem.Connect(Presentation);
 }
Пример #3
0
        // Generation.

        public override UnitSystemCore Generate(int seed, CancellationToken?cancellationToken = null)
        {
            var unitSystem = new UnitSystemCore();
            int added      = 0;

            cancellationToken?.ThrowIfCancellationRequested();

            // Initialize random.
            var random = new System.Random(seed);

            // Count free nodes.
            int freeNodes = 0;

            foreach (var position in Enumerables.InRange2(Map.Size))
            {
                if (Map[position].Type == MapNode.NodeType.Common)
                {
                    ++freeNodes;
                }
            }
            if (freeNodes <= UnitCount)
            {
                foreach (var position in Enumerables.InRange2(Map.Size))
                {
                    if (Map[position].Type == MapNode.NodeType.Common)
                    {
                        unitSystem.AddUnit(BuildUnit(random, position, Map, ref added), position);
                    }
                    cancellationToken?.ThrowIfCancellationRequested();
                }
                return(unitSystem);
            }
            cancellationToken?.ThrowIfCancellationRequested();

            // Place units at free nodes.
            int toAdd = UnitCount;

            while (added < UnitCount)
            {
                Vector2Int position;
                do
                {
                    position = new Vector2Int(random.Next(Map.Size.x), random.Next(Map.Size.y));
                    cancellationToken?.ThrowIfCancellationRequested();
                } while (Map[position].Type != MapNode.NodeType.Common || unitSystem[position] != null);
                unitSystem.AddUnit(BuildUnit(random, position, Map, ref added), position);
                cancellationToken?.ThrowIfCancellationRequested();
            }
            return(unitSystem);
        }