/// <summary> /// See if the new entity can be added at the target coordinate (Using top left of entity as index) /// </summary> /// <param name="newEntity">new entity to be added</param> /// <param name="coordiante">target index coordinate</param> /// <returns>True if the entity can be added</returns> public bool CanAddEntity(GridEntity newEntity, GridCoordinate coordiante) { var neededCoordinates = _getNeededCoordinates(coordiante, newEntity.Size); GridEntityContainer c; return(_canAddEntity(newEntity, neededCoordinates, out c)); }
/// <summary> /// Removes whatever this entity is holding /// </summary> public void RemoveContent() { if (this.CurrentlyHolding != null) { Destroy(this.CurrentlyHolding.gameObject); } this.CurrentlyHolding = null; }
public GridEntity GetEntityAtPosition(GridCoordinate coordinate) { GridEntity result = null; if (this._map.TryGetValue(coordinate, out result)) { return(result); } return(null); }
/// <summary> /// Try to add a new entity /// </summary> /// <param name="newEntity">New entity to be added</param> /// <returns>True if operation succeed</returns> public override bool TryAddEntity(GridEntity newEntity) { if (!this.CanAddEntity(newEntity)) { return(false); } this.CurrentlyHolding = newEntity; newEntity.transform.parent = this.RotatorBase.transform; newEntity.transform.localPosition = Vector3.zero; return(true); }
/// <summary> /// Try to add a new entity /// </summary> /// <param name="newEntity">New entity to be added</param> /// <returns>True if operation succed</returns> public bool CanAddEntity(GridEntity newEntity) { // New entity must be 1 x 1 if (newEntity.Size.ExtrudeX > 0 || newEntity.Size.ExtrudeY > 0) { return(false); } if (this.CurrentlyHolding != null) { return(false); } return(true); }
/// <summary> /// Rotates the target entity /// </summary> /// <param name="targetEntity">Entity to be rotated</param> /// <param name="isClockWise">If the rotation is clockwise or not</param> public void RotateEntity(GridEntity targetEntity, bool isClockWise) { GridCoordinate index; if (!this._entities.TryGetValue(targetEntity, out index)) { return; } var neededCoordinates = this._getNeededCoordinates(index, targetEntity.Size.Rotate(isClockWise)); GridEntityContainer container; if (!this._canAddEntity(targetEntity, neededCoordinates, out container)) { return; } targetEntity.Rotate(isClockWise); this.TryAddEntity(targetEntity, index); }
/// <summary> /// Removes the target entity from the grid. Does nothing if the item is not inside the grid /// </summary> /// <param name="targetEntity">Target entity</param> public void RemoveEntity(GridEntity targetEntity) { GridCoordinate indexCoor; if (!this._entities.TryGetValue(targetEntity, out indexCoor)) { return; } var refundedAmount = SaveManager.CurrentInstance.SellItem(targetEntity); this._entities.Remove(targetEntity); var occupiedCoors = this._getNeededCoordinates(indexCoor, targetEntity.Size); foreach (var occupied in occupiedCoors) { this._map.Remove(occupied); } this.OnStateChange(); }
/// <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); } }
/// <summary> /// Calculates if a new entity can be added /// </summary> /// <param name="newEntity"></param> /// <param name="neededCoordinates">All of the coordinates that the new entity will take up</param> /// <returns>True if possible</returns> private bool _canAddEntity(GridEntity newEntity, List <GridCoordinate> neededCoordinates, out GridEntityContainer container) { container = null; foreach (var checkCoor in neededCoordinates) { // If a space that needs to be available is outside the map if (checkCoor.X < 0 || checkCoor.Y < 0 || checkCoor.X >= this.SizeX || checkCoor.Y >= this.SizeY) { return(false); } GridEntity occupied; if (this._map.TryGetValue(checkCoor, out occupied) && occupied != newEntity) { var checkContainer = occupied as GridEntityContainer; if (checkContainer) { if (checkContainer.CanAddEntity(newEntity)) { container = checkContainer; return(true); } return(false); } else { return(false); } } } return(true); }
/// <summary> /// Try to add a new entity /// </summary> /// <param name="newEntity">New entity to be added</param> /// <returns>True if operation succeed</returns> public abstract bool TryAddEntity(GridEntity 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); }
/// <summary> /// Try to add the given entity /// </summary> /// <param name="newEntity">New entity to be added</param> /// <param name="mousePos">The mouse position in world space</param> /// <param name="addToUndoStack">If the action should be added to the save state</param> /// <returns>True if the entity was added</returns> public bool TryAddEntity(GridEntity newEntity, Vector2 mousePos, bool addToUndoStack = true) { return(this.TryAddEntity(newEntity, this.GetMouseHoveringCoordinate(mousePos), addToUndoStack)); }