// Update is called once per frame void Update() { if (_map != null && _isEnabled) { var point = Camera.main.ScreenToWorldPoint(Input.mousePosition); var x = Mathf.RoundToInt(point.x); var y = Mathf.RoundToInt(point.y); transform.position = new Vector3(Mathf.RoundToInt(x), Mathf.RoundToInt(y), 0); // If mouse is within game area if (x >= 0 && y >= 0 && x < _map._chunkHeightAndWidth && y < _map._chunkHeightAndWidth) { var chunk = _map.GetChunkAt(Mathf.RoundToInt(point.x), Mathf.RoundToInt(point.y)); // Check chunks type if (chunk._chunkType != Enums.MapChunkType.Empty_Buildable) { _validBuild = false; } else if (chunk._chunkType == Enums.MapChunkType.Empty_Buildable) { _validBuild = true; } // If can build and clicked if (Input.GetKeyDown(KeyCode.Mouse0) && _validBuild && chunk != null) { var go = _map.GetMapChunkControllerAt(x, y); go.GetComponent <SpriteRenderer>().sprite = _map._sLib.GetChunkSpriteByIndex((int)Enums.MapChunkType.Initial); go.GetComponent <MapChunkController>()._chunkData._chunkType = Enums.MapChunkType.Initial; go.tag = Constants.TAG_PLAYER_CHUNK; } } // Cursor color if (_validBuild) { transform.GetComponent <SpriteRenderer>().material.color = new Color(0, 225, 0, .7f); } else { transform.GetComponent <SpriteRenderer>().material.color = new Color(225, 0, 0, .7f); } } }
// Update is called once per frame void Update() { if (_map != null && _isEnabled) { var point = Camera.main.ScreenToWorldPoint(Input.mousePosition); var x = (int)_cam.transform.position.x; var y = (int)_cam.transform.position.y; var chunk = _map.GetChunkAt(x, y); // Check if cursor is within chunk if (point.x >= x - _HALF_CHUNK_SIZE + _HALF_MOUSE_SIZE && point.y >= y - _HALF_CHUNK_SIZE + _HALF_MOUSE_SIZE && point.x <= x + _HALF_CHUNK_SIZE - _HALF_MOUSE_SIZE && point.y <= y + _HALF_CHUNK_SIZE - _HALF_MOUSE_SIZE && chunk != null) { // Get the snapped position of the X and Y inside the chunk float snapToGridX = -1f; float snapToGridY = -1f; var locX = point.x - x + _HALF_CHUNK_SIZE; var locY = point.y - y + _HALF_CHUNK_SIZE; if (locX >= 0 && locX < _TILE_SIZE) { snapToGridX = 0; } else if (locX >= _TILE_SIZE && locX < _TILE_SIZE * 2) { snapToGridX = 1; } else if (locX >= _TILE_SIZE * 2 && locX < _TILE_SIZE * 3) { snapToGridX = 2; } if (locY >= 0 && locY < _TILE_SIZE) { snapToGridY = 0; } else if (locY >= _TILE_SIZE && locY < _TILE_SIZE * 2) { snapToGridY = 1; } else if (locY >= _TILE_SIZE * 2 && locY < _TILE_SIZE * 3) { snapToGridY = 2; } // If correct grid location was found if (snapToGridX > -1 && snapToGridY > -1) { // Set cursor to location transform.position = new Vector3(x + (snapToGridX * _TILE_SIZE) - _TILE_SIZE, y + (snapToGridY * _TILE_SIZE) - _TILE_SIZE, 0); _tile = _map.GetMapChunkControllerAt(x, y).GetComponent <MapChunkController>();//._tileData[(int)snapToGridX, (int)snapToGridY]; // If got tile and mouse left pressed if (_tile != null && Input.GetKeyDown(KeyCode.Mouse0)) { // Later buy these _tile.SetTileToStructureAt((int)snapToGridX, (int)snapToGridY, Enums.StructureType.Warfare); } } else { _tile = null; } } } }