Esempio n. 1
0
        /// <summary>
        /// Constructs a list of needed coordinates
        /// </summary>
        /// <param name="targetEntity">Target entity</param>
        /// <param name="index">Index coordinate</param>
        /// <returns>A list of coordinates that the target entity will take up</returns>
        private List <GridCoordinate> _getNeededCoordinates(GridCoordinate index, GridEntitySize entitySize)
        {
            List <GridCoordinate> result = new List <GridCoordinate>();

            int signX = entitySize.ExtrudeX > 0 ? 1 : -1;
            int signY = entitySize.ExtrudeY > 0 ? 1 : -1;

            for (int x = 0; x <= Math.Abs(entitySize.ExtrudeX); x++)
            {
                for (int y = 0; y <= Math.Abs(entitySize.ExtrudeY); y++)
                {
                    result.Add(index + new GridCoordinate(x * signX, y * signY));
                }
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Try to add the given entity onto the map
        /// </summary>
        /// <param name="newEntity">The new entity to be added</param>
        /// <param name="coordinate">Target coordinate index</param>
        /// <param name="addToUndoStack">If the action should be added to the save state</param>
        /// <returns>True if operation succeed</returns>
        public bool TryAddEntity(GridEntity newEntity, GridCoordinate coordinate, bool addToUndoStack = true)
        {
            if (addToUndoStack && !SaveManager.CurrentInstance.TrySpendResource(newEntity.ManufactureCost))
            {
                return(false);
            }

            var neededCoordinates = _getNeededCoordinates(coordinate, newEntity.Size);
            GridEntityContainer container;

            if (!this._canAddEntity(newEntity, neededCoordinates, out container))
            {
                return(false);
            }

            if (container != null)
            {
                return(container.TryAddEntity(newEntity));
            }
            else
            {
                foreach (var coor in neededCoordinates)
                {
                    this._map[coor] = newEntity;
                }

                newEntity.transform.position = this.GetCellWorldPosition(coordinate);
                this._entities[newEntity]    = coordinate;

                if (addToUndoStack)
                {
                    this.OnStateChange(newEntity.ManufactureCost);
                }

                return(true);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Try to load the grid from the given state
        /// </summary>
        /// <param name="state">Target state</param>
        /// <returns>True if operation succeed</returns>
        private bool _tryLoadFromState(MapGridState state, bool shouldSave = false)
        {
            if (state == null)
            {
                return(false);
            }

            this.prefabManager = PrefabManager.CurrentInstance;

            if (state.SizeX != this.SizeX || state.SizeY != this.SizeY)
            {
                return(false);
            }

            var stateToObject = new Dictionary <GridEntityState, GridEntity>();

            // First place all  of the entities
            foreach (var entityState in state.GridEntities)
            {
                // Try to instantiate the entities
                var entityObject = this.InstantiateEntityFromState(entityState);
                if (entityObject == null)
                {
                    Debug.Log("Failed to load entity at " + entityState.PosX + ',' + entityState.PosY);
                    return(false);
                }
                else
                {
                    // Try to place the entity on board
                    var newCoor = new GridCoordinate(entityState.PosX, entityState.PosY);
                    if (!this.TryAddEntity(entityObject, newCoor, false))
                    {
                        Debug.Log("Failed to add entity at " + newCoor);
                        this.ResetBoard();
                        return(false);
                    }
                }

                // Hash result for the connecting part
                stateToObject[entityState] = entityObject;
            }

            // Then connect all of the outputs to input
            foreach (var entity in state.GridEntities)
            {
                var emitter = stateToObject[entity] as IEmitter;
                for (int i = 0; i < entity.Outputs.Count; i++)
                {
                    var outputState = entity.Outputs[i];
                    var outputObj   = emitter.GetOutputSocket(i);

                    foreach (var connection in outputState.Connections)
                    {
                        GridEntity connectedEntity;
                        var        targetCoordinate = new GridCoordinate(connection.ConnectedX, connection.ConnectedY);
                        if (!this._map.TryGetValue(targetCoordinate, out connectedEntity))
                        {
                            Debug.Log("No entity at coordinate to connect to: " + targetCoordinate);
                            this.ResetBoard();
                            return(false);
                        }

                        var receiver = connectedEntity as IReceiver;
                        if (receiver == null)
                        {
                            Debug.Log("Entity at coordinate is not a receiver: " + targetCoordinate);
                            this.ResetBoard();
                            return(false);
                        }

                        var targetInput = receiver.GetInputSocket(connection.InputSocketIndex);
                        if (!outputObj.TryAddInputSocket(targetInput))
                        {
                            Debug.Log("Failed to connect output socket to" + targetCoordinate);
                            this.ResetBoard();
                            return(false);
                        }
                    }
                }
            }

            if (shouldSave)
            {
                this.OnStateChange();
            }
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Saves the target entity into a state
        /// </summary>
        /// <param name="targetEntity">Target entity</param>
        /// <returns>Static state of the entity, null if unavailable</returns>
        private GridEntityState SaveEntityToState(GridEntity targetEntity)
        {
            GridCoordinate position;

            this._entities.TryGetValue(targetEntity, out position);
            if (position == null)
            {
                position = new GridCoordinate(-1, -1);
            }

            // Check if the target entity is a container
            var             containerEntity = targetEntity as GridEntityContainer;
            GridEntityState entityState     = null;

            if (containerEntity == null)
            {
                entityState = new GridEntityState();
            }
            else
            {
                var containerState = new GridEntityContainerState();

                if (containerEntity.CurrentlyHolding != null)
                {
                    var holdingEntityState = this.SaveEntityToState(containerEntity.CurrentlyHolding);
                    containerState.HoldingEntity = holdingEntityState;
                }

                entityState = containerState;
            }

            // Construct information about the entity

            entityState.EntityID = targetEntity.ID;
            entityState.PosX     = position.X;
            entityState.PosY     = position.Y;
            entityState.Rotation = targetEntity.Rotation;

            // Construct the connected outputs
            IEmitter emitter = targetEntity as IEmitter;

            if (emitter != null)
            {
                foreach (var output in emitter.Outputs)
                {
                    var outputState = new OutputSocketState();
                    foreach (var connection in output.ConnectedInputs)
                    {
                        var            input           = connection.Key;
                        var            connectionState = new OutputConnectionState();
                        GridCoordinate pos             = this.GetMouseHoveringCoordinate(input.transform.position);
                        GridEntity     entityAtPos;

                        if (!this._map.TryGetValue(pos, out entityAtPos))
                        {
                            return(null);
                        }

                        var receiver = entityAtPos as IReceiver;
                        if (receiver == null)
                        {
                            Debug.LogError("Item at " + pos.ToString() + " is not a receiver");
                            return(null);
                        }

                        connectionState.ConnectedX = pos.X;
                        connectionState.ConnectedY = pos.Y;


                        connectionState.InputSocketIndex = receiver.IndexOf(input);

                        outputState.Connections.Add(connectionState);
                    }

                    entityState.Outputs.Add(outputState);
                }
            }

            return(entityState);
        }
Esempio n. 5
0
 /// <summary>
 /// Gets the world position of the given cell
 /// </summary>
 /// <param name="coordinate">coordinate of the given cell</param>
 /// <returns>The world position of the cell</returns>
 public Vector2 GetCellWorldPosition(GridCoordinate coordinate)
 {
     return((Vector2)(this.transform.position) + coordinate.ToVector2() * GeneralSettings.GridSize);
 }