void Dragging() { Ray cameraRay = _currentCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(cameraRay, out hit, float.PositiveInfinity, ~LayerMask.GetMask("Cube"))) { transform.position = hit.point + Vector3.up * 0.5f; _currentOnCell = BaseCellPlane.GetCell(BaseCellPlane.ToGridPosition(hit.point)); if (!_currentOnCell) { return; } _highLightObj.gameObject.SetActive(true); _highLightObj.transform.position = _currentOnCell.WorldPosition + Vector3.up * 0.001f; _highLightObj.material.color = _currentOnCell.CanBuildBuilding && _currentOnCell.Building == null ? _buildableHighLightColor : _unbuildableHighLightColor; } else { transform.position = _currentCamera.ScreenToWorldPoint(Input.mousePosition + Vector3.forward); _highLightObj.gameObject.SetActive(false); _currentOnCell = null; } }
void GetScores(Vector2Int position, Building building, int range, List <Component> checkedList, ref float score) // Position 是当前building所放置的位置,building是当前拖拽的building, { var cell = BaseCellPlane.GetCell(position); if (cell != null) { float value = 0; if (cell.Building != null && !checkedList.Contains(cell.Building)) { checkedList.Add(cell.Building); if (CheckBuilding(building, cell.Building, out value)) { PopMessage(value, cell.Building.transform.position); score += value; } } if (!checkedList.Contains(cell) && !isCellTypeChecked[(int)cell.CellType]) { checkedList.Add(cell); isCellTypeChecked[(int)cell.CellType] = true; if (CheckCell(building, cell, out value)) { PopMessage(value, building.transform.position); score += value; } } } range++; if (range > building.Range) { return; } Vector2Int[] nextPositions = { position + Vector2Int.right, position + Vector2Int.up, position + Vector2Int.left, position + Vector2Int.down }; foreach (var pos in nextPositions) { GetScores(pos, building, range, checkedList, ref score); } }
public static float Calculate(Building building) { float score = 0; Vector2Int position = BaseCellPlane.ToGridPosition(building.transform.position); List <Component> checkedList = new List <Component>(); checkedList.Add(building); checkedList.Add(BaseCellPlane.GetCell(position)); for (int i = 0; i < Instance.isCellTypeChecked.Length; i++) { Instance.isCellTypeChecked[i] = false; } Instance.GetScores(position, building, 0, checkedList, ref score); Instance.totalScore += score; Instance.scoreText.text = Instance.totalScore.ToString(); print(Instance.totalScore); return(score); }