private void useUpgradeTool() { //Upgrade a turret when the player clicks on it //Enable upgrade icon Vector3 pos = MainCamera.ScreenToViewportPoint(Input.mousePosition); pos.x = (pos.x - 0.5f) * hudCanvas.sizeDelta.x; pos.y = (pos.y - 0.5f) * hudCanvas.sizeDelta.y; upgradeIcon.localPosition = pos; upgradeIcon.gameObject.SetActive(true); //Get the turret on the highlighted tile Turret turret = currentTile.GetComponent <Turret>(); if (turret != null) { //Check if turret can be upgraded if (turret.turretUpgrade != null) { //Highlight green and get cost currentTile.GetComponent <ColorSwapper>().swapColour(Color.green); int cost = turret.upgradeCost; setBuyText(cost); //If left mouse button clicked if (Input.GetMouseButtonDown(0)) { //If player can afford it if (cost <= money) { //Update money and replace turret with new one buyItem(cost); turrets.Remove(turret); int oldCost = currentTile.costToRemove; currentTile = mMap.swapTile(currentTile, turret.turretUpgrade, true, false); //Add upgrade costs together so sell price can be calculated currentTile.costToRemove = oldCost + cost; turret = currentTile.GetComponent <Turret>(); turret.setSpawners(spawners); turret.setGame(this); turrets.Add(turret); showRange(turret, false); } else { sendMessage("Can't afford"); } } } else if (Input.GetMouseButtonDown(0)) { //Send message if turret already max level sendMessage("Max level"); } } }
// Replace world tile with a new one public EnvironmentTile ReplaceEnviromentTile(EnvironmentTile current, EnvironmentTile replacment) { Vector3 newPosition = GetRawPosition(current.PositionTile.x, current.PositionTile.y); EnvironmentTile tile = Instantiate(replacment, new Vector3(), Quaternion.identity, transform); tile.Position = current.Position; tile.PositionTile = current.PositionTile; SetTileRotation(ref tile, current.Rotation); if (mCharacter.CurrentPosition == current) { mCharacter.CurrentPosition = tile; } // Attach the tint shader to all the tile blocks foreach (Material m in tile.GetComponent <MeshRenderer>().materials) { m.shader = TintShader; m.enableInstancing = true; } // Add the tile to the global map mMap[current.PositionTile.x][current.PositionTile.y] = tile; // Setup connections between the new tile and its local tiles SetupConnections(current.PositionTile.x, current.PositionTile.y); if (current.PositionTile.x > 0) { SetupConnections(current.PositionTile.x - 1, current.PositionTile.y); } if (current.PositionTile.x < mMapGenerationPayload.size.x - 1) { SetupConnections(current.PositionTile.x + 1, current.PositionTile.y); } if (current.PositionTile.y > 0) { SetupConnections(current.PositionTile.x, current.PositionTile.y - 1); } if (current.PositionTile.y < mMapGenerationPayload.size.y - 1) { SetupConnections(current.PositionTile.x, current.PositionTile.y + 1); } // Remove the old tile and add the new one mAll.Remove(current); mAll.Add(tile); Destroy(current.gameObject); return(tile); }
private void PlaceTile(EnvironmentTile tile) { state = State.Open; if (moneyManager.TrySpend(towerDragging.cost)) { EnvironmentTile towerPlaced = environment.SwapTile(towerDragging.tile, tile.position2D, false); towerPlaced.GetComponent <Tower>().Activate(); } ResetTowerDrag(); }
private IEnumerator DoUpgrade(EnvironmentTile tile) { for (int i = 0; i < 60; i++) { yield return(null); } ManagePlacement script = tile.GetComponent <ManagePlacement>(); script.Upgrade(); Debug.Log("Built"); tile.IsAccessible = true; busy = false; yield return(null); }
// Func for reverting a tile to be accessible public void RevertTile(int x, int y) { // Get transform position finalPosition = mMap[x][y].transform.position; // Create new prefab for new tile (set to flat grass plane) EnvironmentTile prefabA = AccessibleTiles[0]; // Make a tile in reference to the map pos tile var t = mMap[x][y]; // Change mesh to be flat grass t.GetComponent <MeshFilter>().sharedMesh = prefabA.GetComponent <MeshFilter>().sharedMesh; t.GetComponent <MeshRenderer>().materials = prefabA.GetComponent <MeshRenderer>().sharedMaterials; // Make tile accessible and define as type 'ground' t.IsAccessible = true; t.Type = string.Format("ground"); // Delete extraneous objects (e.g. a rock on-top of the tile) foreach (Transform child in t.transform) { GameObject.Destroy(child.gameObject); } // Replace tile in map tile list if (mAll.FindIndex(ind => ind.Equals(mMap[x][y])) != -1) { mAll[mAll.FindIndex(ind => ind.Equals(mMap[x][y]))] = t; } // Remove this tile from the forager check list foragerTilesTBC.Remove(t); // Change this tile to be the new accessible tile mMap[x][y] = t; }
void GatherActionList() { ActionSelector.actions.Clear(); if (mCurrentHoveredTile != null) { EnvironmentTile tile = mCurrentHoveredTile.transform.GetComponent <EnvironmentTile>(); foreach (Material m in tile.GetComponent <MeshRenderer>().materials) { m.SetColor("_Tint", new Color(0.8f, 0.8f, 0.8f)); } if (tile != null) { Entity[] entitiesOnTile = Environment.instance.GetEntitiesAt(tile.PositionTile); foreach (Entity e in entitiesOnTile) { AttachTileActionsToSelector(e.GetComponents <TileAction>(), tile); } AttachTileActionsToSelector(tile.GetComponents <TileAction>(), tile); Item item = mCharacter.GetHandItem(); if (item != null) { foreach (var action in item.GetComponents <TileAction>()) { action.environmentTile = tile; if (action.Valid(mCharacter)) { // We must instantiate a instance of the current action. // The reason for this is a edge case when you have a item action that has reference to a tile, // then you select the option multiple times. Only the last one will be recognized and the // tile highlighting will go crazy as it is pointing to unknown tiles TileAction actionInstance = TileAction.Instantiate(action); actionInstance.environmentTile = tile; ActionSelector.actions.Add(actionInstance); } } } } } }
void placePathways(EnvironmentTile tile) { //Create a path at the given tile, mark as a path if (Input.GetMouseButtonDown(0) && !tile.isPaddock && !tile.isPath && tile.IsAccessible) { if (currency.sufficientFunds(pathCost)) { tile.GetComponent <Renderer>().materials[1].color = new Color32(196, 136, 75, 1); tile.isPath = true; vis.addPath(tile); currency.takeIncome(pathCost); for (int i = 0; i < shops.Count; i++) { shops[i].GetComponent <ConcessionStand>().checkTiles(); } level.addExp(10); } } }
// Finish off a tile and initialize the environment tile private void FinalizeTile(ref EnvironmentTile tile, int x, int y, Vector3 position) { // Attach the tint shader to all the tile blocks foreach (Material m in tile.GetComponent <MeshRenderer>().materials) { m.shader = TintShader; } tile.PositionTile = new Vector2Int(x, y); tile.Position = new Vector3(position.x + (TileSize / 2), TileHeight, position.z + (TileSize / 2)); tile.gameObject.name = string.Format("Tile({0},{1})", x, y); if (mMap[x][y] != null) { Destroy(mMap[x][y].gameObject); } mMap[x][y] = tile; mAll.Add(tile); }
private void Generate() { // Setup the map of the environment tiles according to the specified width and height // Generate tiles from the list of accessible and inaccessible prefabs using a random // and the specified accessible percentage mMap = new EnvironmentTile[Size.x][]; properties = new Properties[Size.x][]; int halfWidth = Size.x / 2; int halfHeight = Size.y / 2; Vector3 position = new Vector3(-(halfWidth * TileSize), 0.0f, -(halfHeight * TileSize)); bool start = false; // Initial map value setup for (int x = 0; x < Size.x; ++x) { mMap[x] = new EnvironmentTile[Size.y]; properties[x] = new Properties[Size.y]; for (int y = 0; y < Size.y; ++y) { if (x == Size.x / 2 && y == Size.y / 2) { start = true; } bool isAccessible = start || Random.value < AccessiblePercentage; List <EnvironmentTile> tiles = isAccessible ? AccessibleTiles : InaccessibleTiles; if (tiles == AccessibleTiles) { properties[x][y].listType = 0; } else { properties[x][y].listType = 1; } properties[x][y].tileType = Random.Range(0, tiles.Count); if (properties[x][y].listType == 1 && properties[x][y].tileType > 20) { // Is it unbreakabable? properties[x][y].tileType -= 21; properties[x][y].listType = 2; tiles = UnbreakableTiles; } else if (properties[x][y].listType == 1 && properties[x][y].tileType == 20) { // Grass tile should just be picked up, not broken down properties[x][y].listType = 3; } EnvironmentTile prefab = tiles[properties[x][y].tileType]; EnvironmentTile tile = Instantiate(prefab, position, Quaternion.identity, transform); // Instantiate it // Give it all its properties tile.Position = new Vector3(position.x + (TileSize / 2), TileHeight, position.z + (TileSize / 2)); // Give it a position if (properties[x][y].listType == 0) { tile.IsAccessible = true; // Make the tile something you can move on if (properties[x][y].tileType > 0) { Transform[] grass = tile.GetComponentsInChildren <Transform>(); for (int i = 1; i < grass.Length; ++i) { grass[i].Rotate(0.0f, Random.Range(0.0f, 360.0f), 0.0f); } } } else { tile.IsAccessible = false; // Or not if (properties[x][y].listType == 1 || properties[x][y].listType == 2) { tile.child.Rotate(0.0f, Random.Range(0.0f, 360.0f), 0.0f, Space.World); } } tile.gameObject.name = string.Format("Tile({0},{1})", x, y); // Name it properties[x][y].pos.x = tile.Position.x; properties[x][y].pos.y = tile.Position.z; tile.xID = x; tile.yID = y; mMap[x][y] = tile; mAll.Add(tile); if (start) { playerTile = tile.GetComponent <EnvironmentTile>(); Start = tile; mMap[x][y].occupant = GameObject.FindGameObjectWithTag("Player"); } position.z += TileSize; start = false; } position.x += TileSize; position.z = -(halfHeight * TileSize); } GetComponentInParent <Game>().GetComponentInChildren <DoomsdayTimer>().enabled = true; //----------------------------------------------------------------- // NPC setup //------- for (int x = 0; x < Size.x; x++) { // Prepare for chickens for (int y = 0; y < Size.y; y++) { if (Random.value > 0.999f) { // It has to be really low because otherwise it gets super laggy GameObject chicken; // This is all just the general creation process chicken = Instantiate(NPC[0], GetComponentInParent <Game>().transform); chicken.transform.position = new Vector3(mMap[x][y].Position.x, 2.0f, mMap[x][y].Position.z); chicken.GetComponent <CharacterManager>().currentTile = mMap[x][y]; chicken.GetComponent <Character>().CurrentPosition = mMap[x][y]; mMap[x][y].occupant = chicken; // Give it a tile too, so I can keep track of it for later chicken.GetComponent <CharacterManager>().currentTile.xID = x; chicken.GetComponent <CharacterManager>().currentTile.yID = y; } } } }
private void doGameUpdate() { //If a tile was selected in the last frame then restore its colour if (currentTile != null && currentTile.canBeDestroyed && !currentTile.IsAccessible) { currentTile.GetComponent <ColorSwapper>().restoreColour(); } //If a turret was selected in the last frame then hide its range if (currentTurret != null) { showRange(currentTurret, false); } //Get the current tile that the mouse is hovering over tileIsHighlighted = getMouseTile(); //If the place tool is being used if (objectToPlace != null) { //disable the object objectToPlace.SetActive(false); //Rotate the object if rotate key pressed if (Input.GetKeyDown(KeyCode.R)) { tileRotation = (tileRotation + 1) % 4; objectToPlace.transform.transform.Rotate(new Vector3(0, 1, 0), 90); } } //Disable mouse icons buyPrice.gameObject.SetActive(false); destroyIcon.gameObject.SetActive(false); rotateIcon.gameObject.SetActive(false); upgradeIcon.gameObject.SetActive(false); //If mouse is hovering over a tile if (tileIsHighlighted) { currentTurret = currentTile.GetComponent <Turret>(); //If place tool is being used if (isUsingPlaceTool) { if (!startPlaced) { placeStartPoint(); } else { usePlaceTool(); } } //If destroy tool is being used else if (isUsingDestroyTool) { useDestroyTool(); } //If a turret is highlighted show its range else if (currentTurret != null) { showRange(currentTurret, true); } //If upgrade tool is being used if (isUsingUpgradeTool) { useUpgradeTool(); } } if (Input.GetKeyDown(KeyCode.C)) { cancelTool(); } if (!isDoingWave && startPlaced) { //Start wave when timer reaches 0 timeUntilWave -= Time.deltaTime; if (timeUntilWave <= 0) { menu.startWave(false); } else { //Update timer text nextWaveCountdown.text = "Next wave in " + (int)timeUntilWave; nextWaveBonus.text = "<< Start now for bonus $" + calculateNextWaveBonus(); } } }
// Preform the pause, inventory manipulation and tile change public virtual IEnumerator DoCollection(Entity entity, EnvironmentTile tile) { // Turn towards the tile entity.transform.rotation = Quaternion.LookRotation(tile.Position - entity.CurrentPosition.Position, Vector3.up); entity.ChangeAnimation(AnimationState); yield return(new WaitForSeconds(ActionTime)); ItemInstance[] items = FindValidItemGroup(entity); if (items != null) { // Handle all item removals first as we may be in a case where a item gets removed only for just enough space to be made // for the newly inserted items foreach (ItemInstance itemInstance in items) { switch (itemInstance.mode) { case ItemMode.Take: case ItemMode.HandItem: // If the item is marked to not be consumed on use, continue if (!itemInstance.item.consumeOnUse) { continue; } float chanceRandom = Random.Range(0.0f, 1.0f); // Check to see if the random chance of the item being used comes true // Set to less then or equals as the random.range is set to min inclusive and it could be 0.0f if (chanceRandom <= itemInstance.chance) { entity.RemoveFromInventory(itemInstance.item, itemInstance.count); } break; } } // Now preform all item insertions foreach (ItemInstance itemInstance in items) { switch (itemInstance.mode) { case ItemMode.Give: float chanceRandom = Random.Range(0.0f, 1.0f); // Check to see if the random chance of the item being dropped comes true // Set to less then or equals as the random.range is set to min inclusive and it could be 0.0f if (chanceRandom <= itemInstance.chance) { if (!entity.AddToInventory(itemInstance.item, itemInstance.count)) { // Drop on the floor? } } break; } } } // Do we have a tile to replace the current one with? if (ReplacmentTiles.Length > 0) { // Replace the tile, choosing a random new tile to go in its place EnvironmentTile newTile = Environment.instance.ReplaceEnviromentTile(tile, ReplacmentTiles[Random.Range(0, ReplacmentTiles.Length)]); // If the tile is set to be recursive then call the tile action collection on the new instance if (Recursive && newTile.GetComponent <TileActionCollection>() != null) { TileActionCollection newAction = newTile.GetComponent <TileActionCollection>(); newAction.environmentTile = newTile; //newAction.environmentTile = tile; // Verify that there is a new action is valid if (newAction != null && newAction.Valid(entity)) { entity.SetCurrentAction(newAction); yield return(newAction.DoCollection(entity, newTile)); } } } }
// Make a specific tile inaccessible (at point x,y and of specific type) // Types (more for possible expansion): // 0 = rocks // 1 = stone // 2 = trees // 3 = logs // 4 = water // 5 = player base // 6 = enemy base public void MakeTileInaccessible(int x, int y, int type) { // Get transform pos of where to make inaccessible finalPosition = mMap[x][y].transform.position; // Create new tile initially to the first inaccessible tile type in list EnvironmentTile prefabNa = InaccessibleTiles[0]; // Define what type of inaccessible tile this will be switch (type) { case 0: prefabNa = InaccessibleTiles[Random.Range(4, 10)]; break; // Rocks case 1: prefabNa = InaccessibleTiles[Random.Range(10, 16)]; break; // Stone case 2: prefabNa = InaccessibleTiles[Random.Range(2, 4)]; break; // Trees case 3: prefabNa = InaccessibleTiles[Random.Range(0, 2)]; break; // Logs case 4: prefabNa = InaccessibleTiles[16]; break; // Water case 5: prefabNa = InaccessibleTiles[17]; break; // Player Base case 6: prefabNa = InaccessibleTiles[18]; break; // Enemy Base } // Get reference to tile on map var t = mMap[x][y]; // Change mesh of tile to be the type of inaccessible tile t.GetComponent <MeshFilter>().sharedMesh = prefabNa.GetComponent <MeshFilter>().sharedMesh; t.GetComponent <MeshRenderer>().materials = prefabNa.GetComponent <MeshRenderer>().sharedMaterials; // Using rocks and player/enemy base objects, requires instantiation of extra object (e.g the actual rock) if (type == 0 || type == 5 || type == 6) { Instantiate(prefabNa.GetComponentInChildren <MeshFilter>(), finalPosition, mMap[x][y].transform.rotation, t.transform); } // Make this tile not accessible t.IsAccessible = false; // Define tile type string and health (if appropriate) switch (type) { case 0: t.Type = "rock"; t.Health = 100; break; case 1: t.Type = "stone"; t.Health = 100; break; case 2: t.Type = "tree"; t.Health = 100; break; case 3: t.Type = "logs"; t.Health = 100; break; case 4: t.Type = "water"; break; case 5: t.Type = "player base"; break; case 6: t.Type = "enemy base"; break; default: t.Type = "error"; break; } // Replace tile in the map tile list if (mAll.FindIndex(ind => ind.Equals(mMap[x][y])) != -1) { mAll[mAll.FindIndex(ind => ind.Equals(mMap[x][y]))] = t; } // Change this tile to be the new inaccessible tile mMap[x][y] = t; }
void clearDebris(EnvironmentTile tile) { //Only change the tile is it is appropriate if (Input.GetMouseButtonDown(0)) { Ray screenClick = MainCamera.ScreenPointToRay(Input.mousePosition); if (tile != null) { if (!tile.IsAccessible && !tile.isPaddock) { //Remove the object on top of the tile and set it to accessible if (tile.gameObject.transform.childCount > 0) { //SHOPS if (tile.gameObject.transform.GetChild(0).GetComponent <ConcessionStand>()) { shops.Remove(tile.gameObject.transform.GetChild(0).gameObject); //Give back shop money currency.addIncome(shopCost); //Delete the object Destroy(tile.gameObject.transform.GetChild(0).gameObject); tile.IsAccessible = true; } //DECORATIONS else if (tile.gameObject.transform.GetChild(0).tag == "Decoration") { //Give back the money for destoying decorations currency.addIncome(decorationCost); //Delete the object Destroy(tile.gameObject.transform.GetChild(0).gameObject); tile.IsAccessible = true; } //DEBRIS else if (currency.sufficientFunds(5)) { //clearing debris costs money currency.takeIncome(5); //Delete the object Destroy(tile.gameObject.transform.GetChild(0).gameObject); tile.IsAccessible = true; } } } //PATHS else if (tile.isPath) { tile.isPath = false; tile.IsAccessible = true; int ran = Random.Range(0, grassColours.Count); tile.GetComponent <Renderer>().materials[1].color = grassColours[ran]; currency.addIncome(pathCost); } } else if (Physics.Raycast(screenClick, out mRaycastHits[0])) { if (mRaycastHits[0].transform.tag == "Paddock") { deletionScreen.SetActive(true); deletingObject = mRaycastHits[0].transform.gameObject; } } } }