private IEnumerator DoBuildTile(EnvironmentTile tile) { // Go to List <EnvironmentTile> route = map.Solve(CurrentPosition, tile); if (route != null && route.Count > 2) { Debug.Log("Searching for adjacent route"); List <EnvironmentTile> adjacent_route = map.SolveAdjacent(CurrentPosition, tile); if (adjacent_route != null) { Debug.Log("Adjacent route found"); yield return(DoGoTo(adjacent_route)); Debug.Log("Building"); yield return(DoBuild(tile)); } } // Build else { Debug.Log("Searching for adjacent route"); List <EnvironmentTile> adjacent_route = map.SolveAdjacent(CurrentPosition, tile); if (adjacent_route != null) { Debug.Log("Adjacent route found"); yield return(DoGoTo(adjacent_route)); Debug.Log("Upgrading"); yield return(DoUpgrade(tile)); } } }
private IEnumerator DoGoTo(List <EnvironmentTile> route, bool full) { // Move through each tile in the given route if (route != null) { Vector3 position = CurrentPosition.Position; for (int count = 0; count < route.Count; ++count) { if (!full && count == route.Count - 1) { break; } Vector3 next = route[count].Position; yield return(DoMove(position, next)); CurrentPosition = route[count]; position = next; GetComponent <CharacterManager>().currentTile = route[count]; GetComponent <CharacterManager>().characterState = 1; CurrentPosition.occupant = gameObject; } GetComponent <Animator>().SetBool("walking", false); GetComponent <CharacterManager>().characterState = 0; // Character is idle after walking } }
void placeDecoUpdate(EnvironmentTile tile) { if (tile != null) { //Shpw the shop at the mouse position in the centre of the tile spawnedDeco.transform.position = new Vector3(mRaycastHits[0].transform.position.x + 5, mRaycastHits[0].transform.position.y + 3, mRaycastHits[0].transform.position.z + 5); } //Rotate the shop using the scroll click or space if (Input.GetMouseButtonDown(2) || Input.GetKeyDown(KeyCode.Space)) { spawnedDeco.transform.Rotate(Vector3.up, 90.0f); } else if (Input.GetMouseButtonDown(0) && !tile.isPaddock && !tile.isPath) { if (tile.IsAccessible) { if (currency.sufficientFunds(decorationCost)) { decoClone = decorations.pickDecoration(decoSelected); decoClone.transform.rotation = spawnedDeco.transform.rotation; decoClone.transform.position = new Vector3(tile.transform.position.x + 5, tile.transform.position.y + 3, tile.transform.position.z + 5); tile.IsAccessible = false; currency.takeIncome(decorationCost); decoClone.transform.parent = tile.transform; level.addExp(standardExp); } } // buttons.setButtons(true); Destroy(spawnedDeco); placingDeco = false; } }
// Move the player to x tile on the map private void MovePlayerToStart(Character Character, EnvironmentTile tile) { Character.transform.position = tile.Position + new Vector3(0.0f, CharacterYOffset, 0.0f); Character.CurrentPosition = tile; Character.transform.rotation = Quaternion.identity; Character.transform.parent = this.transform; }
// Generate a new world using the save data public void GenerateWorld(Character Character, GenerationPayload generationPayload, Game.SaveDataPacket saveData) { mMapGenerationPayload = generationPayload; // Load the world save data GenerateWaterMap(saveData); Generate(saveData); SetupConnections(); // Move the player to there last position MovePlayerToStart(Character, mMap[saveData.PlayerX][saveData.PlayerY]); mCharacter = Character; // Load all entities for (int i = 0; i < saveData.Entities.Length; i++) { foreach (Entity e in EntityInstances) { if (saveData.Entities[i].N == e.entityName) { EnvironmentTile tile = mMap[saveData.Entities[i].X][saveData.Entities[i].Y]; Entity ent = GameObject.Instantiate(e, Environment.instance.transform); RegisterEntity(ent); ent.transform.position = tile.Position; ent.transform.rotation = Quaternion.identity; ent.CurrentPosition = tile; break; } } } }
// Generate the world using the save data private void Generate(Game.SaveDataPacket saveData) { Vector2Int Size = mMapGenerationPayload.size; mMap = new EnvironmentTile[Size.x][]; // Loop through for each tile coordinate for (int x = 0; x < Size.x; ++x) { mMap[x] = new EnvironmentTile[Size.y]; for (int y = 0; y < Size.y; ++y) { bool isWater = mWaterMap[x, y]; EnvironmentTile tile = null; Vector3 position = GetRawPosition(x, y); // Check to see if the tile should be a water tile or not if (isWater) { AddWaterTile(position, ref tile, mMapGenerationPayload.size, x, y); } else { Game.TileSaveData saveInstance = saveData.TileData[x + (y * Size.x)]; AddLandTile(position, ref tile, mMapGenerationPayload.size, x, y, saveInstance.N, saveInstance.R); } } } }
// Walk x range from its current location to a random free tile if available public void Walk(int range) { Vector2Int mapSize = Environment.instance.mMapGenerationPayload.size; // Calculate the mins and maxes for the possible touching tile coordinates int xMin = CurrentPosition.PositionTile.x - range < 0 ? 0 : CurrentPosition.PositionTile.x - range; int yMin = CurrentPosition.PositionTile.y - range < 0 ? 0 : CurrentPosition.PositionTile.y - range; int xMax = CurrentPosition.PositionTile.x + range < mapSize.x ? CurrentPosition.PositionTile.x + range : mapSize.x - 1; int yMax = CurrentPosition.PositionTile.y + range < mapSize.y ? CurrentPosition.PositionTile.y + range : mapSize.y - 1; int randomX = Random.Range(xMin, xMax); int randomY = Random.Range(yMin, yMax); EnvironmentTile destination = Environment.instance.GetTile(randomX, randomY); List <EnvironmentTile> route = Environment.instance.Solve(CurrentPosition, destination); // If the animal is already there, break if (route == null) { mPreformingAction = false; return; } if (route.Count > 0) { StopAllCoroutines(); StartCoroutine(DoWalk(route, destination)); } else { mPreformingAction = false; return; } }
// Make sure that at least one of the touching tiles is land public override bool Valid(Entity entity) { EnvironmentTile tile = environmentTile; // Is the current tile water if (PlaceEarth != Environment.instance.mWaterMap[tile.PositionTile.x, tile.PositionTile.y] || !base.Valid(entity)) { return(false); } if (!PlaceEarth && tile.Type != EnvironmentTile.TileType.Accessible) { return(false); } int xMin = (tile.PositionTile.x - 1 < 0) ? 0 : tile.PositionTile.x - 1; int yMin = (tile.PositionTile.y - 1 < 0) ? 0 : tile.PositionTile.y - 1; int xMax = (tile.PositionTile.x + 1 < Environment.instance.mMapGenerationPayload.size.x) ? (tile.PositionTile.x + 1) : (Environment.instance.mMapGenerationPayload.size.x - 1); int yMax = (tile.PositionTile.y + 1 < Environment.instance.mMapGenerationPayload.size.y) ? (tile.PositionTile.y + 1) : (Environment.instance.mMapGenerationPayload.size.x - 1); // Check the 4 edges of the water tile for land to see if its remotely possible to path to it later on if (!Environment.instance.mWaterMap[xMin, tile.PositionTile.y] || !Environment.instance.mWaterMap[xMax, tile.PositionTile.y] || !Environment.instance.mWaterMap[tile.PositionTile.x, yMin] || !Environment.instance.mWaterMap[tile.PositionTile.x, yMax]) { return(true); } return(false); }
private void RegulateNewTile(EnvironmentTile oldTile, bool onGeneration, int tileType) { if (oldTile.IsPickupable == true && mCharacter.CurrentPosition == oldTile) { oldTile.Random = 0; oldTile.IsPickupable = false; } newTile = mMap.GenerateTile(onGeneration, tileType, oldTile.Random, oldTile.Position, oldTile.Rotation, oldTile.X, oldTile.Y, oldTile.IsAccessible, oldTile.IsPickupable); if (mCharacter.CurrentPosition == oldTile) { mCharacter.CurrentPosition = newTile; } for (int j = 0; j < mMap.Enemies.Count; j++) { if (mMap.Enemies[j].CurrentPosition == oldTile) { mMap.Enemies[j].CurrentPosition = newTile; } } for (int x = 0; x < oldTile.Connections.Count; x++) { oldTile.Connections[x].Connections.Add(newTile); oldTile.Connections[x].Connections.Remove(oldTile); } Destroy(oldTile.gameObject); mMap.Connect(newTile, newTile.X, newTile.Y); }
// Helper func to find the closest tile around another tile (mainly for forager) public EnvironmentTile CheckAround(EnvironmentTile tile, EnvironmentTile objective) { EnvironmentTile temp = null; int dist = int.MaxValue; // Ensure parameters are valid if (tile != null && objective != null) { // Check through all tiles connected to initial tile foreach (EnvironmentTile e in tile.Connections) { // Ensure tile is accessible if (e.IsAccessible == true) { // Calculate length of route from the objective (forager tile pos) to the tile List <EnvironmentTile> route = mMap.Solve(objective, e, "Game"); // Ensure route calculated is possible if (route != null) { if (route.Count < dist) { // Found a shortest route to a surrounding tile dist = route.Count; temp = e; } } } } } return(temp); }
private int getTileClearCost(EnvironmentTile tile) { //Get the cost to remove the tile or the sell price of the turret if tile is a turret //Decrement turret cost of the tile to get the price that was paid for it incrementTurretCost(tile, -1); Turret turret = tile.GetComponentInChildren <Turret>(); int cost; if (turret is MachineGun) { cost = (int)(-(machineGunCost + tile.costToRemove) * sellMoneyBackPercent); } else if (turret is Mortar) { cost = (int)(-(mortarCost + tile.costToRemove) * sellMoneyBackPercent); } else { cost = tile.costToRemove; } //Increment cost again to ensure turret cost stays the same incrementTurretCost(tile, 1); return(cost); }
bool cycleThrough(RaycastHit[] hit) { //Check to see if the immediate tiles are in-fact paths for (int i = 0; i < hit.Length; i++) { RaycastHit hits = hit[i]; EnvironmentTile tl = hits.transform.GetComponent <EnvironmentTile>(); if (tl.isPath) { //If it is a path, the shop can be accessed by customers pathConnecting = true; incomeSprite.GetComponent <SpriteRenderer>().sprite = producing; return(true); } else { pathConnecting = false; incomeSprite.GetComponent <SpriteRenderer>().sprite = notProducing; return(false); } } return(false); }
private void SetupConnections() { // Currently we are only setting up connections between adjacnt nodes for (int x = 0; x < Size.x; ++x) { for (int y = 0; y < Size.y; ++y) { EnvironmentTile tile = mMap[x][y]; tile.Connections = new List <EnvironmentTile>(); if (x > 0) { tile.Connections.Add(mMap[x - 1][y]); } if (x < Size.x - 1) { tile.Connections.Add(mMap[x + 1][y]); } if (y > 0) { tile.Connections.Add(mMap[x][y - 1]); } if (y < Size.y - 1) { tile.Connections.Add(mMap[x][y + 1]); } } } }
public void Extract() { if (Extractor) { EnvironmentTile eTile = GameTiles.instance.environmentTiles[LocalPlace]; List <string> extractResources = GameTiles.instance.EnvironmentResourceNames; // if there are still resources in the ground if (GroundHasResources()) { // extract it foreach (string resourceName in extractResources) { eTile.Resources[resourceName] -= Resources[resourceName]; if (eTile.Resources[resourceName] < 0) { eTile.Resources[resourceName] = 0; } } // update health bar float ratio = GetResourcesRatio(); HealthBar.SetValue(ratio); } } }
public void StartSpawning(List <EnvironmentTile> tiles, EnvironmentTile target) { StopAllCoroutines(); spawningTiles = tiles; this.target = target; StartCoroutine(Spawner()); }
private float Heuristic(EnvironmentTile a, EnvironmentTile b) { // Use the locations of the node to estimate how close they are by line of sight // experiment here with better ways of estimating the distance. This is used to // calculate the global goal and work out the best order to prossess nodes in return(Vector3.Distance(a.Position, b.Position)); }
public float GetResourcesRatio() { EnvironmentTile ground = GameTiles.instance.environmentTiles[LocalPlace]; var groundTypeResources = GameTiles.instance.GetEnvironmentTypes().FindTypeByName(ground.Name).GenerateResourcesDictionary(); Dictionary <string, int> extractedResources = new Dictionary <string, int>(); // get ratio in function of ground resources // list the resources consumed by the facility and record their amount in the ground tile foreach (string resourceName in GameTiles.instance.EnvironmentResourceNames) { if (Resources[resourceName] > 0) { extractedResources[resourceName] = ground.Resources[resourceName]; } } // total in the ground among all resources extracted float groundTotal = 0; // total in the ground before any mining was performed float groundMax = 0; foreach (KeyValuePair <string, int> entry in extractedResources) { groundTotal += entry.Value; groundMax += groundTypeResources[entry.Key]; } return(groundTotal / groundMax); }
// Override the "TileActionCollection" DoCollection function // Remove the entity that this script is attached to and preform all the lower level inventory adding code public override IEnumerator DoCollection(Entity entity, EnvironmentTile tile) { yield return(base.DoCollection(entity, tile)); Environment.instance.RemoveEntity(GatherEntity); Destroy(GatherEntity.gameObject); }
// Randomly returns a neighbouring tile to the tile given public EnvironmentTile GetNeighbourTile(EnvironmentTile tile) { int numNeighbours = tile.Connections.Count; int x = Random.Range(0, numNeighbours); return(tile.Connections[x]); }
// Override the "TileActionCollection" DoCollection function // Pause and rotate the tile public override IEnumerator DoCollection(Entity entity, EnvironmentTile tile) { yield return(base.DoCollection(entity, tile)); // Rotate the tile 90 degrees clockwise Environment.instance.SetTileRotation(ref tile, (tile.Rotation + 1) % 4); }
// Make sure there is a pair of opposing tiles that we can jump between public override bool Valid(Entity entity) { Vector2Int targetPosition = environmentTile.PositionTile; int xMin = (targetPosition.x - 1 < 0) ? 0 : targetPosition.x - 1; int yMin = (targetPosition.y - 1 < 0) ? 0 : targetPosition.y - 1; int xMax = (targetPosition.x + 1 < Environment.instance.mMapGenerationPayload.size.x) ? (targetPosition.x + 1) : (Environment.instance.mMapGenerationPayload.size.x - 1); int yMax = (targetPosition.y + 1 < Environment.instance.mMapGenerationPayload.size.y) ? (targetPosition.y + 1) : (Environment.instance.mMapGenerationPayload.size.x - 1); // Make sure we are not going out of the map bounds if (xMin != targetPosition.x && xMax != targetPosition.x) { EnvironmentTile tile2 = Environment.instance.GetTile(targetPosition.x + 1, targetPosition.y); EnvironmentTile tile4 = Environment.instance.GetTile(targetPosition.x - 1, targetPosition.y); if (tile2 != null && tile2.Type == EnvironmentTile.TileType.Accessible && tile4 != null && tile4.Type == EnvironmentTile.TileType.Accessible) { return(true); } } else if (xMin != targetPosition.x && xMax != targetPosition.x)// Make sure we are not going out of the map bounds { EnvironmentTile tile1 = Environment.instance.GetTile(targetPosition.x, targetPosition.y + 1); EnvironmentTile tile3 = Environment.instance.GetTile(targetPosition.x, targetPosition.y - 1); if (tile1 != null && tile1.Type == EnvironmentTile.TileType.Accessible && tile3 != null && tile3.Type == EnvironmentTile.TileType.Accessible) { return(true); } } return(false); }
private List <EnvironmentTile> GetAbility2Tiles() { if (SpellTiles != null) { SpellTiles.Clear(); } EnvironmentTile CP = mCharacter.CurrentPosition; List <EnvironmentTile> C = CP.Connections; for (int i = 0; i < C.Count; i++) { SpellTiles.Add(C[i]); for (int j = 0; j < C[i].Connections.Count; j++) { if (C[i].Connections[j].X == CP.X + 2 || C[i].Connections[j].X == CP.X - 2 || C[i].Connections[j].Y == CP.Y + 2 || C[i].Connections[j].Y == CP.Y - 2) { if (!SpellTiles.Contains(C[i].Connections[j])) { SpellTiles.Add(C[i].Connections[j]); } } } } return(SpellTiles); }
public void releaseTheHound(EnvironmentTile tilePassedIn) { CancelInvoke(); List <EnvironmentTile> visRoute = mMap.Solve(this.CurrentPosition, tilePassedIn, 0); this.GoTo(visRoute); }
public EnvironmentTile GetRandom() { int x = Random.Range(0, Size.x - 1); int y = Random.Range(0, Size.y - 1); EnvironmentTile target = mMap[x][y]; return(target); }
public void HighlightTile(EnvironmentTile tile) { // turn tile green tile.TilemapMember.SetTileFlags(tile.LocalPlace, TileFlags.None); tile.TilemapMember.SetColor(tile.LocalPlace, Color.green); highlightedPositions.Add(tile.LocalPlace); }
protected InteractableObject(GameWorld owner, EnvironmentTile owningTile, string texture, string action) : base(owner) { OwningTile = owningTile; Texture = texture; Action = action; }
public void setTile(EnvironmentTile tile) { placedTile = tile; this.transform.position = placedTile.transform.position; this.transform.rotation = Quaternion.identity; this.CurrentPosition = placedTile; }
// Start is called before the first frame update void Start() { // Define health and references to game/environment Health = 100; game = GameObject.FindGameObjectWithTag("GameController"); mMap = game.GetComponentInChildren <Environment>(); baseTile = game.GetComponentInChildren <Environment>().baseTile; }
// Given an Vector2 containing two integers, return the tile at that location in the map public EnvironmentTile GetTileAtPosition(Vector2Int position) { int tile = position.y + position.x * Size.y; EnvironmentTile result = mAll[tile]; return(result); }
/// <summary> /// Starts construction of this building. /// </summary> public void StartBuilding() { _buildingState = BuildingStates.UnderConstruction; foreach (BuildingTile buildingTile in _buildingTiles) { EnvironmentTile targetTile = Owner.Environment[_position.X + buildingTile.Position.X, _position.Y + buildingTile.Position.Y]; targetTile.CanAccess = false; } }