Example #1
0
        /// <summary>
        ///     Tries to spawn a stack of a certain type.
        ///     See <see cref="StackTypeSpawnEvent"/>
        /// </summary>
        private void OnStackTypeSpawn(StackTypeSpawnEvent args)
        {
            // Can't spawn a stack for an invalid type.
            if (args.StackType == null)
            {
                return;
            }

            // Set the output result parameter to the new stack entity...
            args.Result = EntityManager.SpawnEntity(args.StackType.Spawn, args.SpawnPosition);
            var stack = args.Result.GetComponent <StackComponent>();

            // And finally, set the correct amount!
            RaiseLocalEvent(args.Result.Uid, new StackChangeCountEvent(args.Amount), false);
        }
        public void CreateBoardAndStockParts()
        {
            // Entity might not be initialized yet.
            var boardContainer = Owner.EnsureContainer <Container>(MachineFrameComponent.BoardContainer, out var existedBoard);
            var partContainer  = Owner.EnsureContainer <Container>(MachineFrameComponent.PartContainer, out var existedParts);

            if (string.IsNullOrEmpty(BoardPrototype))
            {
                return;
            }

            var entityManager = Owner.EntityManager;

            if (existedBoard || existedParts)
            {
                // We're done here, let's suppose all containers are correct just so we don't screw SaveLoadSave.
                if (boardContainer.ContainedEntities.Count > 0)
                {
                    return;
                }
            }

            var board = entityManager.SpawnEntity(BoardPrototype, Owner.Transform.Coordinates);

            if (!_boardContainer.Insert(board))
            {
                throw new Exception($"Couldn't insert board with prototype {BoardPrototype} to machine with prototype {Owner.Prototype?.ID ?? "N/A"}!");
            }

            if (!board.TryGetComponent <MachineBoardComponent>(out var machineBoard))
            {
                throw new Exception($"Entity with prototype {BoardPrototype} doesn't have a {nameof(MachineBoardComponent)}!");
            }

            foreach (var(part, amount) in machineBoard.Requirements)
            {
                for (var i = 0; i < amount; i++)
                {
                    var p = entityManager.SpawnEntity(MachinePartComponent.Prototypes[part], Owner.Transform.Coordinates);

                    if (!partContainer.Insert(p))
                    {
                        throw new Exception($"Couldn't insert machine part of type {part} to machine with prototype {Owner.Prototype?.ID ?? "N/A"}!");
                    }
                }
            }

            foreach (var(stackType, amount) in machineBoard.MaterialRequirements)
            {
                var stackSpawn = new StackTypeSpawnEvent()
                {
                    Amount = amount, StackType = stackType, SpawnPosition = Owner.Transform.Coordinates
                };
                Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, stackSpawn);

                var s = stackSpawn.Result;

                if (s == null)
                {
                    throw new Exception($"Couldn't spawn stack of type {stackType}!");
                }

                if (!partContainer.Insert(s))
                {
                    throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {Owner.Prototype?.ID ?? "N/A"}");
                }
            }

            foreach (var(compName, info) in machineBoard.ComponentRequirements)
            {
                for (var i = 0; i < info.Amount; i++)
                {
                    var c = entityManager.SpawnEntity(info.DefaultPrototype, Owner.Transform.Coordinates);

                    if (!partContainer.Insert(c))
                    {
                        throw new Exception($"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {Owner.Prototype?.ID ?? "N/A"}");
                    }
                }
            }

            foreach (var(tagName, info) in machineBoard.TagRequirements)
            {
                for (var i = 0; i < info.Amount; i++)
                {
                    var c = entityManager.SpawnEntity(info.DefaultPrototype, Owner.Transform.Coordinates);

                    if (!partContainer.Insert(c))
                    {
                        throw new Exception($"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {Owner.Prototype?.ID ?? "N/A"}");
                    }
                }
            }
        }