private void Update() { if (!MapData.CanSpawnZombies) { return; } if (Mouse.current.leftButton.wasPressedThisFrame) { var startMousePos = Mouse.current.position.ReadValue(); _startRay = _camera.ScreenPointToRay(startMousePos); if (Physics.Raycast(_startRay, out var hitInfo, 500f, _rayLayerMask)) { startWorldPos = hitInfo.point; _canSpawn = true; } } if (Mouse.current.leftButton.isPressed && _canSpawn) { var endMousePos = Mouse.current.position.ReadValue(); var endRay = _camera.ScreenPointToRay(endMousePos); if (Physics.Raycast(endRay, out var hitInfo, 500f, _rayLayerMask)) { endWorldPos = hitInfo.point; var startGridPos = PathfindingManager.NodeFromWorldPoint(startWorldPos).gridPosition; var endGridPos = PathfindingManager.NodeFromWorldPoint(endWorldPos).gridPosition; if (startGridPos.x > endGridPos.x) { var temp = startGridPos.x; startGridPos.x = endGridPos.x; endGridPos.x = temp; } if (startGridPos.y > endGridPos.y) { var temp = startGridPos.y; startGridPos.y = endGridPos.y; endGridPos.y = temp; } _spawnPreviews.ForEach(preview => preview.hasJustSpawned = false); _currentCost = 0; for (var x = startGridPos.x; x <= endGridPos.x; x++) { for (var y = startGridPos.y; y <= endGridPos.y; y++) { if (!MapData.Map[x, y].canWalk) { continue; } if (_currentCost > MapData.FingerAmount - MapData.ZombieToSpawn.totalCost || MapData.FingerAmount == 0) { break; } var evalPreview = _spawnPreviews.FirstOrDefault(preview => preview.gridPosition == MapData.Map[x, y].gridPosition); if (evalPreview != null) { evalPreview.hasJustSpawned = true; _currentCost += _zombieToSpawn.spawnCost; continue; } var objToInstantiate = Instantiate(zombiePreview, MapData.Map[x, y].worldPosition, Quaternion.identity); objToInstantiate.IsError = !MapData.Map[x, y].canSpawn; objToInstantiate.gridPosition = MapData.Map[x, y].gridPosition; _spawnPreviews.Add(objToInstantiate); _currentCost += _zombieToSpawn.spawnCost; } } foreach (var preview in _spawnPreviews.ToArray()) { //if it hasn't spawned this fixed frame, it's not part of the preview and can be destroyed if (!preview.hasJustSpawned) { preview.gameObject.Destroy(); _spawnPreviews.Remove(preview); } } //todo:fix flaoting away text on resolutions other than 1920x1080 costText.gameObject.SetActive(true); costText.GetComponent <RectTransform>().anchoredPosition = endMousePos * Screen.width / Screen.height; costText.text = _currentCost.RoundToInt().ToString(); } if (Mouse.current.rightButton.wasPressedThisFrame) { _canSpawn = false; foreach (var preview in _spawnPreviews) { preview.gameObject.Destroy(); } _spawnPreviews.Clear(); costText.gameObject.SetActive(false); } } if (Mouse.current.leftButton.wasReleasedThisFrame && _canSpawn) { _spawnPreviews.RemoveAll(preview => preview == null); var zombieBehaviours = new List <ZombieBehaviour>(); foreach (var preview in _spawnPreviews.ToArray()) { var spawnedZombie = Instantiate(_zombieToSpawn, MapData.Map[preview.gridPosition.x, preview.gridPosition.y].worldPosition + Vector3.up * 0.5f, Quaternion.identity); if (preview.IsError) { _currentCost -= _zombieToSpawn.spawnCost; } spawnedZombie.gameObject.SetActive(true); zombieBehaviours.Add(spawnedZombie); preview.gameObject.Destroy(); } MapData.ZombieList.AddRange(zombieBehaviours); _spawnPreviews.Clear(); MapData.FingerAmount -= _currentCost.RoundToInt(); costText.gameObject.SetActive(false); } }