public GridObject AddGridObject(SpriteData sprite, int x, int y) { if (!CanAddGridObject(sprite, x, y)) { return(null); } // Instantiate object GridObject clone = Instantiate(gridObjectPrefab, sprite.Functional ? gridObjectParentFunctional : gridObjectParentDecorative); clone.SetSprite(sprite); clone.SetPosition(x, y); gridObjects.Add(clone); // Add references to object in grid for (int i = x; i < x + sprite.Width; i++) { for (int j = y; j < y + sprite.Height; j++) { if (sprite.Functional) { gridFunctional[i, j] = clone; } else { gridDecorative[i, j] = clone; } } } return(clone); }
public GridObject AddGridObject(SpriteData sprite, int x, int y, bool writeLog) { if (!CanAddGridObject(sprite, x, y)) { return(null); } if (ContainsGridObject(sprite.Functional, x, y)) { return(null); } if (writeLog) { LogHandler.Instance.WriteLine("Added " + sprite.Name + " at " + x + ", " + y + ": time = " + Time.time); } // Instantiate object GridObject clone = Instantiate(gridObjectPrefab, sprite.Functional ? gridObjectParentFunctional : gridObjectParentDecorative); clone.SetSprite(sprite); clone.SetPosition(x, y); gridObjects.Add(clone); // Add references to object in grid for (int i = x; i < x + sprite.Width; i++) { for (int j = y; j < y + sprite.Height; j++) { if (sprite.Functional) { gridFunctional[i, j] = clone; } else { gridDecorative[i, j] = clone; } } } return(clone); }
private void Update() { // Calculate sprite coordinates for the current mouse position Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (previousMousePosition == null) { previousMousePosition = mousePosition; } #region Toggle Pathfinding Mode if (Input.GetKeyDown(KeyCode.P)) { if (mode != PlacementMode.Pathfinding) { Debug.Log("Enabling Pathfinding Mode"); mode = PlacementMode.Pathfinding; } else { Debug.Log("Disabling Pathfinding Mode"); mode = PlacementMode.Level; } pathEndPoints = 0; pathVis.Clear(); } #endregion if (!IsLocked) { // Interpolate between previous and current mouse position int spriteX = 0, spriteY = 0; for (float i = 0.25f; i <= 1; i += 0.25f) { spriteX = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.x, mousePosition.x, i) - (float)CurrentSprite.Width / 2); spriteY = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.y, mousePosition.y, i) - (float)CurrentSprite.Height / 2); if (mode == PlacementMode.Level) { if (Input.GetMouseButton(1)) { int mouseX = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.x, mousePosition.x, i) - 0.5f); int mouseY = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.y, mousePosition.y, i) - 0.5f); // Set deletion layer if not set, prioritizing the functional layer if (deletionLayer == null) { if (GridManager.Instance.ContainsGridObject(true, mouseX, mouseY)) { deletionLayer = true; } else if (GridManager.Instance.ContainsGridObject(false, mouseX, mouseY)) { deletionLayer = false; } } // Remove existing grid object based on deletion layer if (deletionLayer != null) { if (GridManager.Instance.ContainsGridObject(deletionLayer.Value, mouseX, mouseY)) { GridManager.Instance.RemoveGridObject(deletionLayer.Value, mouseX, mouseY); } } } else if (Input.GetMouseButton(0) && CurrentSprite.HoldToPlace && GridManager.Instance.CanAddGridObject(CurrentSprite, spriteX, spriteY)) { pathVis.Clear(); // Place new grid object (if hold-to-place) GridManager.Instance.AddGridObject(CurrentSprite, spriteX, spriteY, true); } } } //Pathfinding if (mode == PlacementMode.Pathfinding) { //Simple Line if (pathEndPoints == 1) { pathVis.SetDrawLine(pathStart, mousePosition); } //Clicking if (Input.GetMouseButton(0) && pathEndPoints == 0) { pathStart = new Vector2(spriteX, spriteY); pathEndPoints = 1; } else if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0) && pathEndPoints == 1) { Vector2 pathEnd = new Vector2(spriteX, spriteY);; if (pathStart != pathEnd) { // Actually calculates path! List <Vector2> path = pathFinding.FindPath(pathStart, pathEnd); pathEndPoints = 2; pathVis.SetDrawLine(path); Map.pathForBots = path; Map.pathForBots.Reverse(); } } //Exit Condition if (Input.GetMouseButtonUp(0) && pathEndPoints == 2) { pathEndPoints = 0; mode = PlacementMode.Level; } } else if (mode == PlacementMode.Level) { // Remove deletion layer if (Input.GetMouseButtonUp(1)) { deletionLayer = null; } } // Update preview object if (CurrentSprite.Sprite != previewObject.Data.Sprite) { previewObject.SetSprite(CurrentSprite); } previewObject.SetPosition(spriteX, spriteY); previewObject.gameObject.SetActive(GridManager.Instance.CanAddGridObject(CurrentSprite, spriteX, spriteY)); } // Store mouse position previousMousePosition = mousePosition; }
private void Update() { // Calculate sprite coordinates for the current mouse position Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (previousMousePosition == null) { previousMousePosition = mousePosition; } if (!IsLocked()) { // Interpolate between previous and current mouse position int spriteX = 0, spriteY = 0; for (float i = 0.25f; i <= 1; i += 0.25f) { spriteX = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.x, mousePosition.x, i) - (float)CurrentSprite.Width / 2); spriteY = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.y, mousePosition.y, i) - (float)CurrentSprite.Height / 2); if (Input.GetMouseButton(1)) { int mouseX = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.x, mousePosition.x, i) - 0.5f); int mouseY = Mathf.RoundToInt(Mathf.Lerp(previousMousePosition.Value.y, mousePosition.y, i) - 0.5f); // Set deletion layer if not set, prioritizing the functional layer if (deletionLayer == null) { if (GridManager.Instance.ContainsGridObject(true, mouseX, mouseY)) { deletionLayer = true; } else if (GridManager.Instance.ContainsGridObject(false, mouseX, mouseY)) { deletionLayer = false; } } // Remove existing grid object based on deletion layer if (deletionLayer != null) { if (GridManager.Instance.ContainsGridObject(deletionLayer.Value, mouseX, mouseY)) { GridManager.Instance.RemoveGridObject(deletionLayer.Value, mouseX, mouseY); } } } else if (Input.GetMouseButton(0) && CurrentSprite.HoldToPlace && GridManager.Instance.CanAddGridObject(CurrentSprite, spriteX, spriteY)) { // Place new grid object (if hold-to-place) GridManager.Instance.AddGridObject(CurrentSprite, spriteX, spriteY); } } // Update preview object if (CurrentSprite.Sprite != previewObject.Data.Sprite) { previewObject.SetSprite(CurrentSprite); } previewObject.SetPosition(spriteX, spriteY); previewObject.SetAlpha(GridManager.Instance.CanAddGridObject(CurrentSprite, spriteX, spriteY) ? 0.5f : 0); // Place new grid object (if not hold-to-place) if (Input.GetMouseButtonDown(0) && !Input.GetMouseButton(1) && GridManager.Instance.CanAddGridObject(CurrentSprite, spriteX, spriteY)) { GridManager.Instance.AddGridObject(CurrentSprite, spriteX, spriteY); } // Remove deletion layer if (Input.GetMouseButtonUp(1)) { deletionLayer = null; } } // Store mouse position previousMousePosition = mousePosition; }