/// <summary> /// Used for deploying object in Editor /// </summary> public void DeployIfPossible(Ray ray, Deployable deployableObject) { RaycastHit hitInfo; Physics.Raycast(ray, out hitInfo, 100, 1 << gridPlaneLayer); if (hitInfo.collider) { Vector3 loc = hitInfo.point - planeBottomLeftPosition; IntVector2 index = new IntVector2(loc.x * Columns / boundX, loc.y * Rows / boundY); if (IsPlaceableWithOffset(deployableObject, index)) { Vector3 pos = IndexToWorldPosition(index); Vector2 wOffset = deployableObject.TileMap.GetWorldTransformOffset(GlobalCellWidth); pos.x += wOffset.x; pos.y += wOffset.y; Deployable newCell = (Deployable)Instantiate(deployableObject, pos, Quaternion.identity); newCell.transform.parent = deployableParentDictionary [deployableObject.GetLayer()]; MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()]; newCell.ParentMapGridCell = cells[CalculateIndex(index)]; newCell.gameObject.layer = (int)deployableObject.GetLayer(); newCell.GridIndex = index; UpdateTilesWithOffset(newCell, index, false); } } }
public void UpdateTilesWithOffset(Deployable deployableObject, IntVector2 cellIndex, bool isEmpty) { for (int i = 0; i < deployableObject.TileMap.TileSize.X; i++) { for (int j = 0; j < deployableObject.TileMap.TileSize.Y; j++) { int posX = cellIndex.X + i - deployableObject.TileMap.TileOffset.X; int posY = cellIndex.Y - j + deployableObject.TileMap.TileOffset.Y; if (posX < Columns && posX >= 0 && posY < Rows && posY >= 0) { int index = CalculateIndex(posX, posY); MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()]; cells [index].IsEmpty = isEmpty; if (isEmpty) { cells [index].InCellObject = null; } else { cells [index].InCellObject = deployableObject; } } } } }
/// <summary> /// 检查物体能否放入该格子 /// </summary> public bool IsPlaceableWithOffset(Deployable deployableObject, IntVector2 cellIndex) { TileMap tile = deployableObject.TileMap; for (int i = 0; i < tile.TileSize.X; i++) { for (int j = 0; j < tile.TileSize.Y; j++) { int posX = cellIndex.X + i - tile.TileOffset.X; int posY = cellIndex.Y - j + tile.TileOffset.Y; if (posX < Columns && posX >= 0 && posY < Rows && posY >= 0) { int index = CalculateIndex(posX, posY); MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()]; if (!cells [index].IsEmpty) { return(false); } } else { return(false); } } } return(true); }
// Use this for initialization void Start() { Instance = this; fileDirectory = Application.dataPath + "/Data/"; deployableTypeDictionary = new Dictionary <Deployable.DeployableType, Deployable> (); DeployableList.Reverse(); foreach (Deployable deployableObject in DeployableList) { deployableTypeDictionary.Add(deployableObject.GetDeployableType(), deployableObject); // dynamically create deployable buttons GameObject newButton = (GameObject)Instantiate(DeployableButton); newButton.transform.SetParent(DeployableButtonParent); newButton.GetComponentInChildren <Text> ().text = deployableObject.GetDeployableType().ToString(); Button button = newButton.GetComponent <Button> (); // this temp variable is useful in this foreach block Deployable deployableTemp = deployableObject; button.onClick.AddListener(() => OnDeployableButtonClick(deployableTemp, button)); } layerToSelectDictionary = new Dictionary <int, Deployable.DeployLayer> (); layerToSelectDictionary.Add(0, Deployable.DeployLayer._Null); layerToSelectDictionary.Add(1, Deployable.DeployLayer._WalkableLayer); layerToSelectDictionary.Add(2, Deployable.DeployLayer._BuildableLayer); layerToSelectDictionary.Add(3, Deployable.DeployLayer._GameObjectLayer); }
void OnDeployableButtonClick(Deployable deployable, Button button) { // check whether layer is active Transform layerTransform = GameMapGrid.deployableParentDictionary [deployable.GetLayer()]; if (!layerTransform.gameObject.activeSelf) { return; } // when clicking self if (button == lastDeployableButton) { return; } objectToDeploy = deployable; currentLayerToErase = deployable.GetLayer(); if (lastDeployableButton) { lastDeployableButton.image.color = button.image.color; } lastDeployableButtonColor = button.image.color; button.image.color = new Color(0.5f, 0.5f, 0.5f, button.image.color.a); lastDeployableButton = button; }
public Deployable[] GetAllChildren(Deployable.DeployLayer deployLayer) { Transform parentTransform = deployableParentDictionary [deployLayer]; var result = new Deployable[parentTransform.childCount]; for (int i = 0; i < parentTransform.childCount; i++) { result [i] = parentTransform.GetChild(i).GetComponent <Deployable> (); } return(result); }
private void ResetObjectToDeploy() { objectToDeploy = null; currentLayerToErase = Deployable.DeployLayer._Null; if (lastDeployableButton) { lastDeployableButton.image.color = lastDeployableButtonColor; lastDeployableButton = null; } }
public void EraseDeployableObject(Ray ray, Deployable.DeployLayer selectedLayer) { RaycastHit hitInfo; Physics.Raycast(ray, out hitInfo, 100, 1 << gridPlaneLayer); if (hitInfo.collider) { Vector3 loc = hitInfo.point - planeBottomLeftPosition; IntVector2 index = new IntVector2(loc.x * Columns / boundX, loc.y * Rows / boundY); MapGridCell[] cells = deployableCellDictionary [selectedLayer]; Deployable toDeleteObject = cells [CalculateIndex(index)].InCellObject; if (toDeleteObject) { UpdateTilesWithOffset(toDeleteObject, toDeleteObject.GridIndex, true); Destroy(toDeleteObject.gameObject); } } }
/// <summary> /// Used for Loading map file /// </summary> public Deployable DeployIfPossible(IntVector2 index, Deployable deployableObject) { if (IsPlaceableWithOffset(deployableObject, index)) { Vector3 pos = IndexToWorldPosition(index); Vector2 wOffset = deployableObject.TileMap.GetWorldTransformOffset(GlobalCellWidth); pos.x += wOffset.x; pos.y += wOffset.y; Deployable newCell = (Deployable)Instantiate(deployableObject, pos, Quaternion.identity); newCell.transform.parent = deployableParentDictionary [deployableObject.GetLayer()]; MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()]; newCell.ParentMapGridCell = cells[CalculateIndex(index)]; newCell.gameObject.layer = (int)deployableObject.GetLayer(); newCell.GridIndex = index; UpdateTilesWithOffset(newCell, index, false); return(newCell); } return(null); }