private void CreateGhost(string objectName = "BullDozer", SaveManager.SavableObject.WorldObject placing = null) { if (ghostObject != null) { //destroy the last ghostObject Destroy(ghostObject); } ghostObject = new GameObject($"{objectName}.GhostObject").AddComponent <GhostObject>(); ghostObject.ghostData.placing = placing; ghostObject.enabled = false; }
public void ClearObject() { Spawning = null; bullDozing = false; if (ghostObject != null) { Destroy(ghostObject.gameObject); } ghostObject = null; ghostSprite = null; GameHandler.gameState = GameHandler.GameState.Default; CursorManager.instance.UpdateCursor(); }
private void HandlePlacing() { GameHandler.gameState = GameHandler.GameState.Placing; bool okayToPlace = (GameHandler.instance.Money >= Spawning.pVal); if (ghostSprite == null) { ghostSprite = Modding.ModLoader.GetSprite(Spawning.spriteName); } if (ghostObject == null) { CreateGhost(Spawning.name, Spawning); SpriteRenderer spr = ghostObject.gameObject.AddComponent <SpriteRenderer>(); if (spr.sprite == null) { spr.sprite = ghostSprite; } } else { if (ghostObject.ghostData.okayToPlace == false) { okayToPlace = false; } ghostObject.enabled = true; } if (lastPos != MousePos) { ghostObject.transform.position = MousePos; lastPos = MousePos; ghostObject.CheckForCollisions(); if (start != Vector3.forward) { ghostObject.gameObject.SetActive(false); positions = PointGenerator.GetPoints(start, MousePos).ToArray(); foreach (var position in positions) { CreateGhost(position, Spawning.name, Spawning, ghostSprite); } } else { ghostObject.gameObject.SetActive(true); positions = new Vector3[] { MousePos }; } } if (Input.GetAxisRaw("Mouse ScrollWheel") != 0 && Spawning.rotatable) { int rounded = Mathf.RoundToInt(Input.GetAxisRaw("Mouse ScrollWheel") * 10); Mathf.Clamp(rounded, -1, 1); RotatePlacing(90 * rounded); } if (Input.GetButtonDown("Fire1") && okayToPlace) { start = MousePos; } if (Input.GetButtonUp("Fire1")) { if (positions != null) { if (Ghosts.Count > 0) { foreach (var ghost in Ghosts) { if (ghost != null) { GhostObject obj = ghost.GetComponent <GhostObject>(); obj.CheckForCollisions(); if (obj.ghostData.okayToPlace) { SpawnObject(ghost.transform.position); } } } AudioManager.instance.PlayBuildSound(ghostObject.transform.position); foreach (var item in Ghosts) { item.gameObject.SetActive(false); } Ghosts.Clear(); } else { if (ghostObject.ghostData.okayToPlace) { SpawnObject(ghostObject.transform.position); AudioManager.instance.PlayBuildSound(ghostObject.transform.position); } } if (Spawning.pVal > 0 && !Input.GetButton("Fire3")) { ClearObject(); } } else { Debug.Log("Positions array empty."); } start = Vector3.forward; } }