public TDTile Dequeue() { TDTile temp = innerList [0]; innerList.RemoveAt(0); return(temp); }
public void SetSelectedTile(int x, int z) { TGMap tempMap = (TGMap)FindObjectOfType(typeof(TGMap)); Component selectedTile = transform.FindChild("SelectedTile"); if (!showMarker) { selectedTile.GetComponent <Renderer>().enabled = false; return; } else { selectedTile.GetComponent <Renderer>().enabled = true; } prevSelectedTileIndex = selectedTileIndex; selectedTileIndex = (int)(x + (z * tileSizeX)); selectedTileData = tempMap.tileDataMap.GetTile(selectedTileIndex); selectedTile.transform.position = new Vector3(x + 0.5f, tempMap.tileDataMap.GetTile(selectedTileIndex).Pos.y + 0.0001f, z + 0.5f); if (selectedTileData.IsTraversable) { selectedTile.GetComponent <Renderer>().material.color = Color.white; } else { selectedTile.GetComponent <Renderer>().material.color = Color.red; } }
void DragDropRefresh(Vector2 currentTileCoords, TDTile selectedTile, int newTileType) { if (visualizeAlgorithms) { // full visualization // we clear map but don't draw new visualization yet. we do that after mouse gets released ClearMap(); // set last tile SetLastTile(currentTileCoords); // set new tile SetTileTexture(selectedTile, newTileType, (int)currentTileCoords.x, (int)currentTileCoords.y); // set current map RefreshMap(); } else { // only path // clear map, draw new path ClearMap(); // set last tile SetLastTile(currentTileCoords); // set new tile SetTileTexture(selectedTile, newTileType, (int)currentTileCoords.x, (int)currentTileCoords.y); // refresh map and draw new path RefreshMap(); RefreshAlgorithm(); } }
public List <TDTile> GetSolution(/*Character.FRIENDFOE friendFoe*/) { List <TDTile> solution = new List <TDTile>(); if (goalTile.ParentTDTile == null) { return(solution); } TDTile currTile = goalTile; //if (friendFoe == Character.FRIENDFOE.FOE) // currTile = currTile.ParentTDTile; while (currTile != null) { if (currTile.IsTraversable) { solution.Add(currTile); } currTile = currTile.ParentTDTile; } solution.Reverse(); return(solution); }
void BuildTexture() { map = new TDMap(gridSizeX, gridSizeZ); int texWidth = gridSizeX * tileResolution; int texHeight = gridSizeZ * tileResolution; texture = new Texture2D(texWidth, texHeight); oldTexture = new Texture2D(texWidth, texHeight); for (int y = 0; y < gridSizeZ; y++) { for (int x = 0; x < gridSizeX; x++) { TDTile tile = map.GetTile(x, y); if (tile == null) { continue; } //texture.SetPixels(x * tileResolution, y * tileResolution, tileResolution, tileResolution, tilePixels[tile.GetTileType()]); texture.SetPixels(x * tileResolution, y * tileResolution, tileResolution, tileResolution, spriteArray[tile.GetTileType()]); } } texture.filterMode = FilterMode.Point; texture.wrapMode = TextureWrapMode.Clamp; texture.Apply(); MeshRenderer meshRenderer = GetComponent <MeshRenderer>(); meshRenderer.sharedMaterials[0].mainTexture = texture; Debug.Log("Done Texture!"); }
private void SetLastTile(Vector2 currentTileCoords) { int x = (int)lastTileCoords.x; int z = (int)lastTileCoords.y; TDTile lastTileObject = map.GetTile(x, z); if (lastTileObject == null) { return; } // set last tile to old type // if last tile is start or endpoint --> assign ground int newTileType = lastTileObject.GetOldTileType(); if (newTileType == (int)TILE_TYPE.STARTPOINT || newTileType == (int)TILE_TYPE.ENDPOINT || newTileType == -1) { newTileType = (int)TILE_TYPE.GROUND; } // set last tile to current tile lastTileCoords = currentTileCoords; SetTileTexture(lastTileObject, newTileType, x, z); }
public TDMap(int width, int height) { _width = width; _height = height; _tiles = new TDTile[_width * _height]; rooms = new List <DRoom>(); for (int x = 0; x < _width * _height; x++) { _tiles[x] = new TDTile(TDTile.TILES.UNEXPLORED); } for (int i = 0; i < 10; i++) { int roomSizeX = Random.Range(4, 8); int roomSizeY = Random.Range(4, 8); DRoom r = new DRoom(); r.left = Random.Range(0, width - roomSizeX); r.top = Random.Range(0, height - roomSizeY); r.width = roomSizeX; r.height = roomSizeY; if (!RoomCollides(r)) { rooms.Add(r); MakeRoom(r); } } }
public void Engueue(TDTile node) { if (innerList.Count == 0) { innerList.Add(node); } else { for (int i = 0; i < innerList.Count + 1; ++i) { if (innerList.Count == i) { innerList.Add(node); break; } else if (node.FValue >= innerList[i].FValue) { continue; } else { innerList.Insert(i, node); break; } } } }
public void GenerateNature(natureType type, TDTile Tile) { Transform child = Object.Instantiate(plant_prefab).transform; child.SetParent(MapTransform); SpriteRenderer sr = child.GetComponent <SpriteRenderer>(); if (nature[type].Count == 0) { return; } sr.sprite = nature[type][Random.Range(0, nature[type].Count)]; child.localPosition = new Vector3((-size_x / 2 + Tile.x) * tile_size + tile_size * 0.5f, (-size_y / 2 + Tile.y) * tile_size + tile_size * 0.5f, 0); if (type != natureType.leaves) { child.GetComponent <LayerSorter>().SortDown = 30; float scale = Random.Range(0.25f, 0.75f); child.localScale = Vector3.one * scale; Vector3 p = Vector2.zero; p.x = Random.Range(-0.4f, 0.4f); p.y = Random.Range(-0.4f, 0.4f); child.localPosition += p * tile_size; } else { child.GetComponent <LayerSorter>().SortDown = 30; child.localScale = Vector3.one * 0.7f; } }
public override void StartAlgorithm(TDTile start, TDTile end, TGMap map) { algoSteps.Clear(); SimplePriorityQueue <TDTile> frontier = new SimplePriorityQueue <TDTile>(); frontier.Enqueue(start, 0); Dictionary <TDTile, TDTile> cameFrom = new Dictionary <TDTile, TDTile>(); cameFrom.Add(start, null); Dictionary <TDTile, float> costSoFar = new Dictionary <TDTile, float>(); costSoFar.Add(start, 0); float priority = 0.0f; while (frontier.Count > 0) { TDTile currentTile = frontier.Dequeue(); if (currentTile.GetTileType() == (int)TILE_TYPE.ENDPOINT) { break; } AlgorithmStep algoStep = new AlgorithmStep(currentTile); algoSteps.Add(algoStep); foreach (TDTile nextTile in currentTile.neighbours) { if (nextTile == null || nextTile.GetTileType() == (int)TILE_TYPE.WATER || nextTile.GetTileType() == (int)TILE_TYPE.WALL) { continue; } // diagonal step check: if neighbour is diagonal but diagonal step not allowed --> we skip if (IsDiagonalNeighbour(currentTile, nextTile) && !IsDiagonalStepAllowed()) { continue; } algoTiles.Add(nextTile); float newCost = costSoFar[currentTile] + map.GetCostByTileType(nextTile.GetTileType()); if (!costSoFar.ContainsKey(nextTile) || newCost < costSoFar[nextTile]) { costSoFar[nextTile] = newCost; priority = newCost; frontier.Enqueue(nextTile, priority); cameFrom.Add(nextTile, currentTile); algoStep.NeighbourTiles.Add(nextTile); } } } GeneratePath(end, cameFrom); }
private void MakePoints() { startPoint = MakeRandomPoint(); endPoint = MakeRandomPoint(); startPoint.SetTileType((int)TILE_TYPE.STARTPOINT); endPoint.SetTileType((int)TILE_TYPE.ENDPOINT); }
public TDTile GetNeigbour(int x, int y) { TDTile r = map.GetTile(this.x + x, this.y + y); if (r == null) { return(this); } return(r); }
void IncrementDestinationIfOnCurrentDestinationTile() { TDTile currentTile = map.GetTileForWorldPosition(transform.position); TDTile destinationTile = map.GetTileForWorldPosition(destination); if (currentTile.Equals(destinationTile)) { SetDestination(GetNextDestination()); } }
public node(Vector2 _pos, bool _walkable, bool _diagonal_move, int _GridX, int _GridY, int _penalty) { GridX = _GridX; GridY = _GridY; pos = _pos; walkable = _walkable; diagonal_move = _diagonal_move; MovementPenalty = _penalty; tile = TDMap.GlobalMap.GetTile(GridX, GridY); }
public override void StartAlgorithm(TDTile start, TDTile end, TGMap map) { algoSteps.Clear(); SimplePriorityQueue <TDTile> frontier = new SimplePriorityQueue <TDTile>(); frontier.Enqueue(start, 0); Dictionary <TDTile, TDTile> cameFrom = new Dictionary <TDTile, TDTile>(); cameFrom.Add(start, null); float priority = 0.0f; while (frontier.Count > 0) { TDTile currentTile = frontier.Dequeue(); Debug.Log("Dequeue Tile: " + currentTile.GetHashCode()); if (currentTile.GetTileType() == (int)TILE_TYPE.ENDPOINT) { break; } AlgorithmStep algoStep = new AlgorithmStep(currentTile); algoSteps.Add(algoStep); foreach (TDTile nextTile in currentTile.neighbours) { if (nextTile == null || nextTile.GetTileType() == (int)TILE_TYPE.WATER || nextTile.GetTileType() == (int)TILE_TYPE.WALL) { continue; } // diagonal step check: if neighbour is diagonal but diagonal step not allowed --> we skip if (IsDiagonalNeighbour(currentTile, nextTile) && !IsDiagonalStepAllowed()) { continue; } algoTiles.Add(nextTile); if (!cameFrom.ContainsKey(nextTile)) { priority = Heuristic(end, nextTile) + ComputeVectorCrossProduct(start, end, nextTile); // TODO: add priority field to TDTile? to debug and or display priority in game frontier.Enqueue(nextTile, priority); //Debug.Log("Enqueue Tile: " + nextTile.GetHashCode() + " - priority: " + priority); cameFrom.Add(nextTile, currentTile); algoStep.NeighbourTiles.Add(nextTile); // WRONG! WE NEED TO LOOK AT THE TILE WITH LOWEST HEURISTIC FIRST } } } GeneratePath(end, cameFrom); }
Vector3 GetNextDestination() { TDStep step = _firetruck.PopPathStep(); TDStep afterStep = _firetruck.PeekPathStep(); Vector3 nextPosition = new Vector3(); Vector3 afterPosition = new Vector3(); if (step != null) { TDTile nextTile = step.tile; TDTile afterTile = null; if (afterStep != null) { afterTile = afterStep.tile; afterPosition = map.GetPositionForTile(afterTile.GetX(), afterTile.GetY()); afterPosition.x += 0.5f; afterPosition.z -= 0.5f; } nextPosition = map.GetPositionForTile(nextTile.GetX(), nextTile.GetY()); nextPosition.x += 0.5f; nextPosition.z -= 0.5f; //If nextPosition is to the right then trucks drive in the lower //of the horizontal lanes if (nextPosition.x > transform.position.x || (afterStep != null && afterPosition.x > transform.position.x)) { nextPosition.z -= 0.25f; //If it's to the left then trucks drive in the upper lane } else if (nextPosition.x < transform.position.x || (afterStep != null && afterPosition.x < transform.position.x)) { nextPosition.z += 0.25f; } //If nextPosition is up the trucks drive in the right lane if (nextPosition.z > transform.position.z || (afterStep != null && afterPosition.z > transform.position.z)) { nextPosition.x += 0.25f; //If it's down then trucks drive in the left lane } else if (nextPosition.z < transform.position.z || (afterStep != null && afterPosition.z < transform.position.z)) { nextPosition.x -= 0.25f; } } return(nextPosition); }
public bool equals(TDTile other) { if (this.position == other.position) { return true; } else { return false; } }
// Use this for initialization IEnumerator Start() { ms = GetComponent <SpriteRenderer>(); while (ms.sprite == null) { yield return(new WaitForSeconds(Time.deltaTime * 5)); } try { Child = transform.GetChild(0); } catch { Child = this.transform; } while (TGMap.instance == null) { yield return(null); } TDTile t = TGMap.instance.TileFromWorldPoint(Child.position); ms.sortingOrder = 32767 - Mathf.Abs((t.x * -20) + (t.y * 30 * TGMap.instance.size_x)) - SortDown; //StartCoroutine(WaitAndPrint()); if (SortDown <= 20) { int sortd = (int)(transform.position.y % TGMap.instance.tileSize); ms.sortingOrder += sortd; if (!transform.name.Contains("tree")) { ms.sortingLayerName = "all"; } else { //transform.GetComponentInChildren<SpriteRenderer>().sortingOrder = ms.sortingOrder; } } else { ms.sortingLayerName = "ground"; } if (Application.isPlaying) { Destroy(this); } else { DestroyImmediate(this); } }
void DragDropStartEndTiles() { if (Input.GetMouseButtonDown(0)) { int x = 0, z = 0; TDTile draggedTile = GetSelectedTile(ref x, ref z); if (draggedTile == null) { return; } if (draggedTile.GetTileType() != (int)TILE_TYPE.STARTPOINT && draggedTile.GetTileType() != (int)TILE_TYPE.ENDPOINT) { return; } isDragged = true; draggedTileType = draggedTile.GetTileType(); lastTileCoords = new Vector2(x, z); } if (Input.GetMouseButtonUp(0)) { if (isDragged) { RefreshAlgorithm(); } isDragged = false; } if (isDragged) { int x = 0, z = 0; TDTile selectedTile = GetSelectedTile(ref x, ref z); if (selectedTile == null || selectedTile.GetTileType() == (int)TILE_TYPE.WATER || selectedTile.GetTileType() == (int)TILE_TYPE.WALL || selectedTile.GetTileType() == (int)TILE_TYPE.STARTPOINT || selectedTile.GetTileType() == (int)TILE_TYPE.ENDPOINT) { return; } int newTileType = draggedTileType; // set old tile Vector2 currentTileCoords = new Vector2(x, z); if (currentTileCoords != lastTileCoords) { DragDropRefresh(currentTileCoords, selectedTile, newTileType); } } }
private void SetTileTextureSelfMapCreation(TDTile tile, int tileType, int x, int z) { // start & stop tile can not be overwritten if (tile.GetTileType() == (int)TILE_TYPE.STARTPOINT || tile.GetTileType() == (int)TILE_TYPE.ENDPOINT) { return; } tile.SetTileType(tileType); texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, spriteArray[tileType]); //texture.SetPixels(x * tileResolution, z * tileResolution, tileResolution, tileResolution, tilePixels[tileType]); texture.Apply(); }
// Update is called once per frame void Update() { if (!run) { return; } if (Input.GetMouseButtonDown(0)) { node n = Grid_creator.instance.NodeFromWorldPoint(Vector3.zero); TDTile t = TGMap.instance.TileFromWorldPoint(Vector3.zero); print(n.GridX + " ," + n.GridY + " // " + t.x + " ," + t.y); } }
public void GenerateTree(TDTile Tile) { Transform child = Object.Instantiate(trees[Random.Range(0, trees.Length)]).transform; child.SetParent(MapTransform); child.localPosition = new Vector3((-size_x / 2 + Tile.x) * tile_size, (-size_y / 2 + Tile.y) * tile_size + (tile_size * 1.5f), 0); //child.localScale = new Vector3(0.5f, 0.5f, 1); if (Grid_creator.instance != null && Grid_creator.instance.Loaded) { Grid_creator.instance.GetNode(Tile.x, Tile.y).walkable = false; } return; }
protected float ComputeVectorCrossProduct(TDTile start, TDTile goal, TDTile current) { TGMap tgm = GameObject.Find("TileMap").GetComponent <TGMap>(); if (tgm == null || !tgm.computeVectorCrossProduct) { return(0.0f); } float dx1 = current.GetX() - goal.GetX(); float dy1 = current.GetY() - goal.GetY(); float dx2 = start.GetX() - goal.GetX(); float dy2 = start.GetY() - goal.GetY(); return(System.Math.Abs(dx1 * dy2 - dx2 * dy1) * 0.001f); }
private TDTile GetSelectedTile(ref int x, ref int z) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (GetComponent <Collider>().Raycast(ray, out hitInfo, Mathf.Infinity)) { x = Mathf.FloorToInt(hitInfo.point.x / tileSize); z = Mathf.FloorToInt(hitInfo.point.z / tileSize); z += gridSizeZ; TDTile selectedTile = map.GetTile(x, z); return(selectedTile); } return(null); }
public TDTile(TDTile.TYPE type, int index, UnityEngine.Vector3 pos) { _numTilesPerRow = 4; _numRows = 2; PopulateUVsFromAtlas(); TileType = type; _index = index; _tileUVs = UVs[(int)type]; _nHValue = -1; _nGValue = 10; _parentTDTile = null; _pos = pos; }
void UpdateSpread() { float delta = Time.deltaTime; Vector3 scale = transform.root.localScale; if (scale.x >= maxSize || tile.durability <= 0) { timeSinceSpreadAttempt += delta; if (timeSinceSpreadAttempt >= spreadInterval) { int rand = Random.Range(0, spreadChance); if (rand % spreadChance == 0) { List <TDTile> houses = map.Map.FindAdjacentFlammableTiles(tile); if (houses.Count > 0) { rand = Random.Range(0, houses.Count); TDTile tileToIgnite = houses[rand]; if (!tileToIgnite.OnFire && tile.durability > 0) { GameObject flame = (GameObject)Instantiate(spreadPrefab); Vector3 flamePos = map.GetPositionForTile(tileToIgnite.GetX(), tileToIgnite.GetY()); flamePos.x += 0.5f; flamePos.z -= 0.5f; flame.transform.position = flamePos; EGFlame egFlame = flame.GetComponent <EGFlame>(); egFlame.SetTile(tileToIgnite); egFlame.SetMap(map); egFlame.SetSpreadPrefab(spreadPrefab); PopUpUIManager.Instance.ShowFireSpreadNPCMessage(); tileToIgnite.OnFire = true; } } timeSinceSpreadAttempt = 0; } } } }
// Update is called once per frame void Update() { //Sort through trucks so that they are kept in the right sets, active and idle List <EGFiretruck> toMoveToIdle = new List <EGFiretruck> (); for (int i = 0; i < _dispatcher.GetActiveTrucks().Count; i++) { EGFiretruck truck = _dispatcher.GetActiveTruckAtIndex(i); if (!truck.IsActive() && !truck.IsPuttingOutFire()) { //Don't remove them while iterating, remove afterwards toMoveToIdle.Add(truck); } } //Set the trucks that need setting for (int i = 0; i < toMoveToIdle.Count; i++) { EGFiretruck truck = toMoveToIdle[i]; SetTruckIdle(truck); } //Identify trucks that need to be removed List <EGFiretruck> toDestroy = new List <EGFiretruck> (); for (int i = 0; i < _dispatcher.GetIdleTrucks().Count; i++) { EGFiretruck truck = _dispatcher.GetIdleTruckAtIndex(i); TDTile truckTile = _map.GetTileForWorldPosition(truck.transform.position); //Trucks that are idle at the firehouse need to be removed if (truckTile.type == TDTile.Type.FIREHOUSE) { toDestroy.Add(truck); } } //Remove trucks that need to be removed for (int i = 0; i < toDestroy.Count; i++) { EGFiretruck truck = toDestroy[i]; _dispatcher.RemoveIdleTruck(truck); _firehouse.DecreaseTruckCount(); Destroy(truck.gameObject); } }
public override void StartAlgorithm(TDTile start, TDTile end, TGMap map = null) { algoSteps.Clear(); Queue <TDTile> frontier = new Queue <TDTile>(); frontier.Enqueue(start); Dictionary <TDTile, TDTile> cameFrom = new Dictionary <TDTile, TDTile>(); cameFrom.Add(start, null); while (frontier.Count > 0) { TDTile currentTile = frontier.Dequeue(); if (currentTile.GetTileType() == (int)TILE_TYPE.ENDPOINT) { break; } AlgorithmStep algoStep = new AlgorithmStep(currentTile); algoSteps.Add(algoStep); foreach (TDTile nextTile in currentTile.neighbours) { if (nextTile == null || nextTile.GetTileType() == (int)TILE_TYPE.WATER || nextTile.GetTileType() == (int)TILE_TYPE.WALL) { continue; // || == START TYLE !? } // diagonal step check: if neighbour is diagonal but diagonal step not allowed --> we skip if (IsDiagonalNeighbour(currentTile, nextTile) && !IsDiagonalStepAllowed()) { continue; } if (!cameFrom.ContainsKey(nextTile)) { frontier.Enqueue(nextTile); cameFrom.Add(nextTile, currentTile); algoStep.NeighbourTiles.Add(nextTile); } } } GeneratePath(end, cameFrom); }
protected bool IsDiagonalNeighbour(TDTile current, TDTile neighbour) { if (current == null || neighbour == null) { return(false); } if (current.neighbours[(int)TILE_NEIGHBOUR.NORTHEAST] == neighbour || current.neighbours[(int)TILE_NEIGHBOUR.NORTHWEST] == neighbour || current.neighbours[(int)TILE_NEIGHBOUR.SOUTHEAST] == neighbour || current.neighbours[(int)TILE_NEIGHBOUR.SOUTHWEST] == neighbour) { return(true); } return(false); }
private void ApproachTargetFlame() { if (targetFlame != null) { TDTile currentTile = map.GetTileForWorldPosition(transform.position); TDTile flameTile = map.GetTileForWorldPosition(targetFlame.transform.position); if (currentTile.IsOtherAdjacent(flameTile)) { destination = currentTile.FindPointAdjacentToTileWithBuffer(flameTile, FIRE_BUFFER, transform.position); } else { EGDispatcher.Instance.SendTruckToTile(this, flameTile.GetX(), flameTile.GetY()); } } }
public TDTile PopStep() { if (pathTimes.Count > 0) { pathTimes.RemoveAt(0); } TDTile popped = null; if (pathSteps.Count > 0) { popped = pathSteps[0]; pathSteps.RemoveAt(0); } return(popped); }
public void SetTDTile(TDTile tile) { _tileType = tile._tileType; _tileTrait = tile._tileTrait; }
public TDPath checkPath(TDMap map, TDTile start, int maxStamina) { TDTile selected = new TDTile(); TDTile finalSelected = new TDTile(); TDTile pathRun = new TDTile(); ArrayList finalPath = new ArrayList(); ArrayList unvisited = new ArrayList(); for (int i = start.position - maxStamina - maxStamina * map.getWidth(); i < start.position + maxStamina + maxStamina * map.getWidth(); i++) { if (i > 0 && i < map.getWidth() * map.getHeight()) { map.getTile(i).distance = 20000; map.getTile(i).previousTile = null; unvisited.Add(map.getTile(i)); } Debug.Log("On position " + i); if (i % map.getWidth() == start.x + maxStamina) { i += map.getWidth() - maxStamina * 2 - 1; Debug.Log("Switched Rows " + i); } } map.getTile(start.position).distance = 0; int looped = 0; int finalDistance = -1; while (unvisited.Count > 0 && !selected.equals(map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z)) && selected.distance < maxStamina) { looped++; selected.distance = 200000; for (int i = 0; i < unvisited.Count; i++) { TDTile comparing = (TDTile)unvisited[i]; if (comparing.distance < selected.distance) { selected = comparing; if (selected.equals(map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z))) { if (selected.distance > maxStamina) { return null; } finalDistance = selected.distance; finalSelected = selected; pathRun = finalSelected; while (pathRun.previousTile != null) { finalPath.Add(pathRun); pathRun = pathRun.previousTile; } return new TDPath(finalPath, finalDistance); } Debug.Log(" Selected: " + selected + " " + selected.distance + "looped: " + looped); } unvisited.Remove(selected); for (int j = 0; j < selected.findNeighbors().Length; j++) { TDTile neighbor = map.getTile(selected.findNeighbors()[j]); if (neighbor != null) { if (unvisited.Contains(neighbor)) { int alternate = selected.distance + 1; if (alternate < neighbor.distance) { neighbor.distance = alternate; neighbor.previousTile = selected; } } } } } } return null; }
// Update is called once per frame void Update() { bool firstRun = true; if (firstRun) { map = _TGMap.getMapData(); firstRun = false; } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (_TGMap.GetComponent<Collider>().Raycast(ray, out hitInfo, Mathf.Infinity)) { int x = Mathf.FloorToInt(hitInfo.point.x / _TGMap.tileSize); int z = Mathf.FloorToInt(hitInfo.point.z / _TGMap.tileSize); currentTileCoord.x = x; currentTileCoord.z = z; selectionCube.transform.position = currentTileCoord * _TGMap.tileSize; } else { } if (Input.GetMouseButtonDown(0)) { occupy = map.getTile((int)playerSelectedCoord.x, (int)playerSelectedCoord.z); if (map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z).getOccupied() == 1) { playerSelected = true; Debug.Log("Player Selected" + map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z)); playerSelectedCoord = new Vector3(currentTileCoord.x, 0, currentTileCoord.z); } else if (map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z).getOccupied() == 0 && playerSelected == true) { playerSelected = false; Debug.Log("Player Moved New coord: " + map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z)); Debug.Log("Player Moved Old coord: " + map.getTile((int)playerSelectedCoord.x, (int)playerSelectedCoord.z)); path = checkPath(map, map.getTile((int)playerSelectedCoord.x, (int)playerSelectedCoord.z), 7); if (path != null) { Debug.Log(path); Debug.Log(path.path[0]); moving = path.path.Count - 1; } } } if(Input.GetMouseButtonDown(1)) { if (map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z).getOccupied() == 2) { opponent = map.getTile((int)currentTileCoord.x, (int)currentTileCoord.z); attacker = map.getTile((int)playerSelectedCoord.x, (int)playerSelectedCoord.z); float hitRoll = Random.Range(0, 10); float cover = 0; //cover calculations Debug.Log(opponent.x + "And" + attacker.x + "ys = " + opponent.y + "And " + attacker.y); Debug.Log("Neighbors: " + map.getTile(opponent.findNeighbors()[0]).getType() + ", " + map.getTile(opponent.findNeighbors()[1]).getType() + ", " + map.getTile(opponent.findNeighbors()[2]).getType() + ", " + map.getTile(opponent.findNeighbors()[3]).getType()); //Left cover if ((opponent.x > attacker.x && map.getTile(opponent.findNeighbors()[1]).getType() == 3)&& !((Mathf.Abs(opponent.x-attacker.x) == 1 && (map.getTile(attacker.findNeighbors()[0]).getType() == 3)) || map.getTile(attacker.findNeighbors()[3]).getType() == 3)) { Debug.Log("Covered! Left"); cover = 2; } //Right Cover else if (opponent.x < attacker.x && map.getTile(opponent.findNeighbors()[2]).getType() == 3 && !((Mathf.Abs(opponent.x - attacker.x) == 1 && (map.getTile(attacker.findNeighbors()[0]).getType() == 3)) || map.getTile(attacker.findNeighbors()[3]).getType() == 3)) { Debug.Log("Covered! Right"); cover = 2; } //Down Cover else if (opponent.y > attacker.y && map.getTile(opponent.findNeighbors()[0]).getType() == 3 && !((Mathf.Abs(opponent.y - attacker.y) == 1 && (map.getTile(attacker.findNeighbors()[1]).getType() == 3)) || map.getTile(attacker.findNeighbors()[2]).getType() == 3)) { Debug.Log("Covered! Down"); cover = 2; } //Up Cover else if (opponent.y < attacker.y && map.getTile(opponent.findNeighbors()[3]).getType() == 3 && !((Mathf.Abs(opponent.y - attacker.y) == 1 && (map.getTile(attacker.findNeighbors()[1]).getType() == 3)) || map.getTile(attacker.findNeighbors()[2]).getType() == 3)) { Debug.Log("Covered! Up"); cover = 2; } Debug.Log("Cover Bonus! " + cover); //hit Debug.Log("HitRoll = " + hitRoll); if((hitRoll - (cover * 3)) > 1) //90% base, -30 for half, -60 for fullcover { opponent.getEnemy().setHealth(opponent.getEnemy().getHealth() - 3); Debug.Log("Hit! Health: " + opponent.getEnemy().getHealth()); } else { } if(opponent.getEnemy().getHealth() <= 0) { opponent.setOccupied(0); } _TGMap.UpdateGraphics(); } } if (moving >= 0) { TDTile next = (TDTile)path.getPath()[moving]; Debug.Log(moving); occupy.removePlayer(); map.setTile(next.position, 1, 1); map.setTile(occupy.position, 1, 0); occupy.switchPlayers(next); occupy = next; _TGMap.UpdateGraphics(); moving -= 1; } }
public void setTile(int pos, int type, int occupied) { tiles[pos] = new TDTile(type, occupied, pos % _width, pos / _width, _width, _height, pos); }
public void setTile(int pos, int type) { tiles[pos] = new TDTile(type); }
public void switchPlayers(TDTile other) { Player switchPlayer = this.getPlayer(); this.setPlayer(other.getPlayer()); other.setPlayer(switchPlayer); }
public void add(TDTile tile, int staminaCost) { path.Add(tile); staminaUsed += staminaCost; }
public TDPath(TDTile tile, int staminaCost) { path = new ArrayList(); path.Add(tile); staminaUsed = staminaCost; }