/// <summary> /// Disables movement on the grid. /// </summary> /// <param name="enableCondition">When this condition is true, movement will be enabled.</param> /// <param name="waitForEndOfMovement">If the object is moving, its movement will be disabled once its reached /// its destination. If false, movement is stopped immediately.</param> /// <param name="overridesMoveCondition">This condition to enable movement will override the move condition from an earlier call.</param> public void DisableMovement(Condition enableCondition, bool waitForEndOfMovement = true, bool overridesMoveCondition = false) { //Used to see what object disabled movement //StackFrame stackFrame = new StackFrame(1); //if (stackFrame != null) //{ // MethodBase method = stackFrame.GetMethod(); // if (method != null) // UnityEngine.Debug.Log(method.DeclaringType.Name); //} if (!_canMove && !overridesMoveCondition) { return; } if (IsMoving && waitForEndOfMovement) { AddOnMoveEndTempAction(() => { _canMove = false; StopAllCoroutines(); _isMoving = false; }); } else { _canMove = false; StopAllCoroutines(); _isMoving = false; } _movementEnableCheck = enableCondition; _moveDisabledEventListener.Invoke(gameObject); _tempAlignment = Alignment; }
public void AddItem(ILayoutable item, int rowIndex, int columnIndex, HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left, VerticalAlignment verticalAlignment = VerticalAlignment.Top) { if (columnIndex < 0) { throw new InvalidOperationException("Item cannot be placed in a column index less than 0"); } if (rowIndex < 0) { throw new InvalidOperationException("Item cannot be placed in a row index less than 0"); } // Ignore null items if (item == null) { return; } // If this item is already tracked, remove it from the current position // and place the item in the new position if (_items.Contains(item)) { _items.Remove(item); } // Add the item to the grid collection if (_items[rowIndex, columnIndex] != null) { throw new InvalidOperationException( string.Format("An item already exists for this grid at row {0} column {1}", rowIndex, columnIndex)); } var alignment = new GridAlignment { HorizontalAlignment = horizontalAlignment, VerticalAlignment = verticalAlignment }; _items.Add(item, alignment, rowIndex, columnIndex); // Attach the item to the background sprite item.AttachTo(_backgroundSprite, true); item.RelativeZ = 0.1f; item.Alpha = _alpha; item.ParentLayout = this; if (item.OnAddedToLayout != null) { item.OnAddedToLayout(this); } _recalculateLayout = true; item.OnSizeChangeHandler = sender => _recalculateLayout = true; }
/// <summary> /// Creates a new instance of the entity and places it on the grid /// </summary> /// <param name="entity">The entity to create a new instance of</param> /// <param name="position">The position in world space to spawn the entity</param> /// <param name="gridAlignment">The side of the grid this entity will belong to</param> public static void SpawnEntity(GameObject entity, Vector2 position, GridAlignment gridAlignment = GridAlignment.ANY) { //Try to get the move script attached GridMovementBehaviour moveScript = entity.GetComponent <GridMovementBehaviour>(); if (!moveScript) { Debug.LogError("You can't spawn a game object that doesn't have a grid movement script. Game object was " + entity.name); } //Set spawn point and create instance moveScript.Position = position; Instantiate(moveScript.gameObject, null); moveScript.Alignment = gridAlignment; }
// Always keep the prefab preview up-to-date private void Update() { gridSettings = settings.GridState; gridAlignment = gridSettings.gridAlignment; gridlineCount = gridSettings.gridlineCount; gridSize = gridSettings.gridSize; gridPos = gridSettings.gridPos; gridShiftAxis1 = gridSettings.gridShiftAxis1; gridShiftAxis2 = gridSettings.gridShiftAxis2; if (settings.Prefab != null && prefabPreview != null) { prefabPreview.eulerAngles = settings.PrefabState.rotation; SceneView.RepaintAll(); } }
// Always keep the prefab preview up-to-date private void Update() { gridSettings = settings.GridState; gridAlignment = gridSettings.gridAlignment; gridlineCount = gridSettings.gridlineCount; gridSize = gridSettings.gridSize; gridPos = gridSettings.gridPos; gridShiftAxis1 = gridSettings.gridShiftAxis1; gridShiftAxis2 = gridSettings.gridShiftAxis2; if (settings.Prefab && prefabPreview) { prefabPreview.localEulerAngles = settings.PrefabState.rotation; SceneView.RepaintAll(); } Tools.hidden = isEnabled; }
internal static ElementClassSet Class(GridAlignment alignment) { switch (alignment) { case GridAlignment.Left: return(leftClass); case GridAlignment.Right: return(rightClass); case GridAlignment.Center: return(centerClass); case GridAlignment.Stretch: return(stretchClass); default: return(ElementClassSet.Empty); } }
/// <summary> /// Gradually moves the gameObject from its current position to the position given. /// </summary> /// <param name="newPosition"></param> /// <returns></returns> private IEnumerator LerpPosition(Vector3 newPosition) { float lerpVal = 0; Vector3 startPosition = transform.position; while (transform.position != newPosition) { //Sets the current position to be the current position in the interpolation transform.position = Vector3.Lerp(startPosition, newPosition, lerpVal += Time.deltaTime * _speed); //Waits until the next fixed update before resuming to be in line with any physics calls yield return(new WaitForFixedUpdate()); } if (CurrentPanel) { CurrentPanel.Occupied = !_canBeWalkedThrough; } MoveDirection = Vector2.zero; _tempAlignment = Alignment; }
private void Start() { //Set the starting panel to be occupied if (BlackBoardBehaviour.Instance.Grid.GetPanel(_position, out _currentPanel, true, Alignment)) { _currentPanel.Occupied = true; if (MoveOnStart) { MoveToPanel(_currentPanel, true); } } //else // Debug.LogError(name + " could not find starting panel"); if (_knockbackBehaviour) { _knockbackBehaviour.AddOnKnockBackAction(() => SetIsMoving(false)); } _tempAlignment = _defaultAlignment; }
/// <summary> /// Disables movement on the grid. /// </summary> /// <param name="moveEvent">When this event is raised, movement will be enabled.</param> /// <param name="intendedSender">Thhe game object that will send the event. If null, movement will be /// enabled regardless of what raises the event.</param> /// <param name="waitForEndOfMovement">If the object is moving, its movement will be disabled once its reached /// its destination. If false, movement is stopped immediately.</param> /// <param name="overridesMoveCondition">This condition to enable movement will override the move condition from an earlier call.</param> public void DisableMovement(GridGame.Event moveEvent, GameObject intendedSender = null, bool waitForEndOfMovement = true, bool overridesMoveCondition = false) { if (!_canMove) { return; } if (IsMoving && waitForEndOfMovement) { AddOnMoveEndTempAction(() => { _canMove = false; StopAllCoroutines(); }); } else { _canMove = false; StopAllCoroutines(); } _moveEnabledEventListener.Event = moveEvent; _moveEnabledEventListener.IntendedSender = intendedSender; _moveEnabledEventListener.AddAction(() => { _canMove = true; }); _moveDisabledEventListener.Invoke(gameObject); _tempAlignment = Alignment; }
/// <summary> /// Uses A* to find a path from the starting panel to the end panel /// </summary> /// <param name="startPanel">The panel where the path will start</param> /// <param name="endPanel">The panel where the path will end</param> /// <param name="allowOccupiedPanels">Whether or not the path should avoid panels that are occupied</param> /// <param name="alignment">The grid alignment this path can go through</param> /// <returns>A list containing the constructed path</returns> public List <PanelBehaviour> GetPath(PanelBehaviour startPanel, PanelBehaviour endPanel, bool allowOccupiedPanels = false, GridAlignment alignment = GridAlignment.ANY) { PanelNode panelNode; List <PanelNode> openList = new List <PanelNode>(); PanelNode start = new PanelNode { panel = startPanel }; PanelNode end = new PanelNode { panel = endPanel }; openList.Add(start); List <PanelNode> closedList = new List <PanelNode>(); start.fScore = CustomHeuristic(startPanel, endPanel); while (openList.Count > 0) { openList = SortNodes(openList); panelNode = openList[0]; if (panelNode.panel == end.panel) { return(ReconstructPath(start, panelNode)); } openList.Remove(panelNode); closedList.Add(panelNode); foreach (PanelBehaviour neighbor in BlackBoardBehaviour.Instance.Grid.GetPanelNeighbors(panelNode.panel.Position)) { if (ContainsPanel(closedList, neighbor) || ContainsPanel(openList, neighbor)) { continue; } else if (neighbor.Occupied && !allowOccupiedPanels) { continue; } else { PanelNode newNode = new PanelNode { panel = neighbor }; newNode.gScore += panelNode.gScore; newNode.fScore = newNode.gScore + CustomHeuristic(neighbor, endPanel); newNode.parent = panelNode; openList.Add(newNode); } } } return(new List <PanelBehaviour>()); }
public static int IndicesToSkip(int totalSelectedShapes, int rowLength, GridAlignment alignment) { var numOfShapesInLastRow = totalSelectedShapes%rowLength; if (alignment == GridAlignment.AlignLeft || alignment == GridAlignment.None || numOfShapesInLastRow == 0) { return 0; } if (alignment == GridAlignment.AlignRight) { return rowLength - numOfShapesInLastRow; } if (alignment == GridAlignment.AlignCenter) { var difference = rowLength - numOfShapesInLastRow; return difference/2; } return 0; }
public static void SetDistributeGridAlignment(GridAlignment alignment) { DistributeGridAlignment = alignment; }
/// <summary> /// Moves the gameObject from its current panel to the panel at the given position. /// </summary> /// <param name="targetPanel">The panel on the grid that the gameObject will travel to.</param> /// <param name="snapPosition">If true, teh gameObject will immediately teleport to its destination without a smooth transition.</param> /// <returns>Returns false if the panel is occupied or not in the grids array of panels.</returns> public bool MoveToPanel(PanelBehaviour targetPanel, bool snapPosition = false, GridAlignment tempAlignment = GridAlignment.NONE) { if (tempAlignment == GridAlignment.NONE) { tempAlignment = _defaultAlignment; } else { _tempAlignment = tempAlignment; } if (!targetPanel) { return(false); } if (IsMoving && !canCancelMovement || targetPanel.Alignment != tempAlignment && tempAlignment != GridAlignment.ANY || !_canMove) { return(false); } _previousPanel = _currentPanel; _targetPanel = targetPanel; //Sets the new position to be the position of the panel added to half the gameOgjects height. //Adding the height ensures the gameObject is not placed inside the panel. float offset = 0; if (!_meshFilter) { offset = transform.localScale.y / 2; } else { offset = (_meshFilter.mesh.bounds.size.y * transform.localScale.y) / 2; } Vector3 newPosition = _targetPanel.transform.position + new Vector3(0, offset, 0); _targetPosition = newPosition; MoveDirection = targetPanel.Position - _position; SetIsMoving(true); //If snap position is true, hard set the position to the destination. Otherwise smoothly slide to destination. if (snapPosition) { transform.position = newPosition; SetIsMoving(false); } else { _MoveRoutine = StartCoroutine(LerpPosition(newPosition)); } //Sets the current panel to be unoccupied if it isn't null if (_currentPanel) { _currentPanel.Occupied = false; } //Updates the current panel _currentPanel = _targetPanel; _currentPanel.Occupied = !_canBeWalkedThrough; _position = _currentPanel.Position; return(true); }
/// <summary> /// Moves the gameObject from its current panel to the panel at the given position. /// </summary> /// <param name="panelPosition">The position of the panel on the grid that the gameObject will travel to.</param> /// <param name="snapPosition">If true, the gameObject will immediately teleport to its destination without a smooth transition.</param> /// <returns>Returns false if the panel is occupied or not in the grids array of panels.</returns> public bool MoveToPanel(Vector2 panelPosition, bool snapPosition = false, GridAlignment tempAlignment = GridAlignment.NONE, bool canBeOccupied = false, bool reservePanel = true) { if (tempAlignment == GridAlignment.NONE) { tempAlignment = _defaultAlignment; } else { _tempAlignment = tempAlignment; } if (IsMoving && !canCancelMovement || !_canMove) { return(false); } else if (canCancelMovement && IsMoving) { StopAllCoroutines(); } //If it's not possible to move to the panel at the given position, return false. if (!BlackBoardBehaviour.Instance.Grid.GetPanel(panelPosition, out _targetPanel, _position == panelPosition || canBeOccupied, tempAlignment)) { return(false); } _previousPanel = _currentPanel; //Sets the new position to be the position of the panel added to half the gameObjects height. //Adding the height ensures the gameObject is not placed inside the panel. float offset = 0; if (!_meshFilter) { offset = transform.localScale.y / 2; } else { offset = (_meshFilter.mesh.bounds.size.y * transform.localScale.y) / 2; } Vector3 newPosition = _targetPanel.transform.position + new Vector3(0, offset, 0); _targetPosition = newPosition; MoveDirection = panelPosition - _position; SetIsMoving(true); //If snap position is true, hard set the position to the destination. Otherwise smoothly slide to destination. if (snapPosition) { transform.position = newPosition; SetIsMoving(false); } else { _MoveRoutine = StartCoroutine(LerpPosition(newPosition)); } //Sets the current panel to be unoccupied if it isn't null if (_currentPanel) { _currentPanel.Occupied = false; } //Updates the current panel _currentPanel = _targetPanel; if (reservePanel) { _currentPanel.Occupied = !_canBeWalkedThrough; } _position = _currentPanel.Position; return(true); }
public void AddItem(ILayoutable item, int rowIndex, int columnIndex, HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left, VerticalAlignment verticalAlignment = VerticalAlignment.Top) { if (columnIndex < 0) throw new InvalidOperationException("Item cannot be placed in a column index less than 0"); if (rowIndex < 0) throw new InvalidOperationException("Item cannot be placed in a row index less than 0"); // Ignore null items if (item == null) return; // If this item is already tracked, remove it from the current position // and place the item in the new position if (_items.Contains(item)) _items.Remove(item); // Add the item to the grid collection if (_items[rowIndex, columnIndex] != null) throw new InvalidOperationException( string.Format("An item already exists for this grid at row {0} column {1}", rowIndex, columnIndex)); var alignment = new GridAlignment { HorizontalAlignment = horizontalAlignment, VerticalAlignment = verticalAlignment }; _items.Add(item, alignment, rowIndex, columnIndex); // Attach the item to the background sprite item.AttachTo(_backgroundSprite, true); item.RelativeZ = 0.1f; item.Alpha = _alpha; item.ParentLayout = this; if (item.OnAddedToLayout != null) item.OnAddedToLayout(this); _recalculateLayout = true; item.OnSizeChangeHandler = sender => _recalculateLayout = true; }