public void RefreshSprite() { // get local index int x = Mathf.RoundToInt(transform.position.x); int y = Mathf.RoundToInt(transform.position.y); int connectionCount = 0; // to identify which road type it is List <Vector3> directions = new List <Vector3>(); // to identify orientation of roads #region Errors if (world == null) { throw new Exception("WorldManager not set"); } ; if (world.Tiles == null) { throw new Exception("WorldManager.Tiles is null, Not Generated world yet?"); } ; #endregion foreach (Vector2Int v2i in world.GetNeighbours(true, new Vector2Int(x, y))) { if (!world.ValidateTile(v2i, false)) { connectionCount++; directions.Add(new Vector3(v2i.x - x, v2i.y - y)); } else if (world.Tiles[v2i.x, v2i.y] != null) { if (world.Tiles[v2i.x, v2i.y].BuiltObject.Category == BuildableObject.BuildableCategory.Road) { connectionCount++; directions.Add(new Vector3(v2i.x - x, v2i.y - y)); } } } int rotation = 0; SpriteRenderer renderer = GetComponent <SpriteRenderer>(); switch (connectionCount) { case 0: renderer.sprite = roadSprites[0]; break; case 1: renderer.sprite = roadSprites[1]; rotation = GetRotationFromDirection(directions[0]); break; case 2: Vector3 avgV3 = MathsStuff.AverageVector3s(directions[0], directions[1]); if (avgV3 == Vector3.zero) { rotation = GetRotationFromDirection(directions[0]); renderer.sprite = roadSprites[2]; } else { renderer.sprite = roadSprites[3]; if (avgV3 == new Vector3(-0.5f, -0.5f)) { rotation = 3; } else if (avgV3 == new Vector3(-0.5f, 0.5f)) { rotation = 2; } else if (avgV3 == new Vector3(0.5f, 0.5f)) { rotation = 1; } else if (avgV3 == new Vector3(0.5f, -0.5f)) { rotation = 0; } else { throw new System.Exception("Cannot find the matching 90 degree bend rotation"); }; } break; case 3: Vector3 avgDir = MathsStuff.AverageVector3s(directions.ToArray()); avgDir = Vector3.Normalize(avgDir); rotation = GetRotationFromDirection(avgDir); rotation = 3 - rotation; renderer.sprite = roadSprites[4]; break; case 4: renderer.sprite = roadSprites[5]; break; } transform.rotation = Quaternion.Euler(0, 0, rotation * 90f); //Debug.Log(transform.position.ToString() + " | Refreshed Sprite " + connectionCount + " | " + rotation); }
// Update is called once per frame void Update() { bool moving = false; #region cam movement if (Input.GetKey(KeyCode.A)) { transform.position += Vector3.left * Time.deltaTime * cameraSpeed; moving = true; } if (Input.GetKey(KeyCode.D)) { transform.position += Vector3.right * Time.deltaTime * cameraSpeed; moving = true; } if (Input.GetKey(KeyCode.W)) { transform.position += Vector3.up * Time.deltaTime * cameraSpeed; moving = true; } if (Input.GetKey(KeyCode.S)) { transform.position += Vector3.down * Time.deltaTime * cameraSpeed; moving = true; } #endregion #region boundaries Vector3 pos = transform.position; if (pos.x < 0) { pos.x = 0; } else if (pos.x > CamBounds.x) { pos.x = CamBounds.x; } ; if (pos.y < 0) { pos.y = 0; } else if (pos.y > CamBounds.y) { pos.y = CamBounds.y; } ; transform.position = pos; #endregion #region Positioning grid if (moving) { Vector3 v3 = transform.position; gridObject.transform.position = new Vector3(Mathf.Floor(v3.x), Mathf.Floor(v3.y), gridObject.transform.position.z); } #endregion if (Input.GetKeyDown(KeyCode.Escape)) { if (IsBuilding) { IsBuilding = false; } // else for other things } if (Input.GetKeyDown(KeyCode.M)) { world.Money += 200; } if (Input.GetKeyDown(KeyCode.B)) { IsBuilding = false; IsDestroying = !IsDestroying; } #region Clicking if (Input.GetKeyDown(KeyCode.Mouse0)) { if (!GUIClick.IsOverGUI()) { // Debug.Log("Clicked on world"); if (IsBuilding) { // build tool enabled // when building remember to ignore the z axis, put it on a particular one // Debug.Log($"Attempting to build {currentBuildableObject.Name} at {MathsStuff.RoundVector3(Camera.main.ScreenToWorldPoint(Input.mousePosition))}"); world.AttemptBuildAt(MathsStuff.RoundVector3(Camera.main.ScreenToWorldPoint(Input.mousePosition)), currentBuildableObject); } else if (IsDestroying) { world.AttemptRemoveAt(MathsStuff.RoundVector3(Camera.main.ScreenToWorldPoint(Input.mousePosition))); } } else { // Debug.Log("Clicked on GUI"); } } #endregion }