/// <summary> /// Try to load an entity from state /// </summary> /// <param name="entityState">Target entity state</param> /// <returns>The resulting entity object</returns> private GridEntity InstantiateEntityFromState(GridEntityState entityState) { var prefab = PrefabManager.CurrentInstance.GetEntityPrefab(entityState.EntityID); if (!prefab) { Debug.Log("Entity id not found: " + entityState.EntityID); this.ResetBoard(); return(null); } var newEntity = Instantiate(prefab); for (int i = 0; i < entityState.Rotation; i++) { newEntity.Rotate(true); } var containerState = entityState as GridEntityContainerState; if (containerState != null && containerState.HoldingEntity != null) { var containerdEntity = this.InstantiateEntityFromState(containerState.HoldingEntity); var newEntityContainer = newEntity as GridEntityContainer; if (newEntityContainer == null) { Debug.LogError("Instance of state is not a container"); return(null); } if (!newEntityContainer.TryAddEntity(containerdEntity)) { Debug.LogError("Failed to add contained entity to instantiated object"); return(null); } } return(newEntity); }
/// <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); }