void getMultipleTilesFromCoords(int width, int height) { try { width += 2; height += 2; GameObject tileAtMousePoint = null; Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); tileAtMousePoint = MapGenerator.me.getTile((int)mousePos.x, (int)mousePos.y).gameObject; //selectionRaycast (ref tileAtMousePoint);//have to change so that it doesnt hit the collider for the building placement TileMaster tm = tileAtMousePoint.GetComponent <TileMaster>(); Vector2 tileGridCoords = tm.getCoords(); if (isSelectionInGridRange(tileGridCoords, width, height) == true) { //Debug.Log("Enough Space"); Vector2 startPos = new Vector2(tileGridCoords.x - (width / 2), tileGridCoords.y - (height / 2)); Vector2 endPos = new Vector2(tileGridCoords.x + (width / 2), tileGridCoords.y + (height / 2)); setCurrent(MapGenerator.me.getTiles(startPos, endPos)); } else { clearCurrent(); //Debug.Log("Not enough space"); } } catch { //Debug.Log("No tile at mouse position"); } }
protected void Start() { //Kinematic variables control whether the tile is kinematic or dynamic and for how much time T_TileGoKinematic = true; T_TileGoKinematicTimer = 1.0f; T_Sabotaged = false; T_ContainsTileGen = false; if (T_TileMaster = GameObject.Find("Tile Master").GetComponent <TileMaster>()) { if (gameObject.tag.Equals("Tile1") || gameObject.tag.Equals("Tile1T") || gameObject.tag.Equals("Tile1S")) { TileMaster.TM_Tiles1.Add(this.gameObject); T_TileNumber = TileMaster.TM_Tiles1.Count; //Debug.Log("TileNumber1: " + T_TileNumber); } else if (gameObject.tag.Equals("Tile2") || gameObject.tag.Equals("Tile2T") || gameObject.tag.Equals("Tile2S")) { TileMaster.TM_Tiles2.Add(this.gameObject); T_TileNumber = TileMaster.TM_Tiles2.Count; //Debug.Log("TileNumber2: " + T_TileNumber); } } }
void createBuildingAtLocation(Vector3 cursorPos, int width, int height) { //gets the lowest and highest of each axis int xLowBound = (int)current[0].GetComponent <TileMaster>().getCoords().x; int xHighBound = (int)current[current.Count - 1].GetComponent <TileMaster>().getCoords().x; int yLowBound = (int)current[0].GetComponent <TileMaster>().getCoords().y; int yHighBound = (int)current[current.Count - 1].GetComponent <TileMaster>().getCoords().y; for (int x = 0; x < current.Count - 1; x++) { //goes through all the tiles and makes the ones not on the outside (where the actual building will be placed unwalkable GameObject tile = current[x]; TileMaster tm = tile.GetComponent <TileMaster>(); Vector2 curTileGrid = tm.getCoords(); if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound) { if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound) { //Debug.LogError ("Making " + tile.name + " Unwalkable "); //Debug.LogError("Keeping " + tile.name + " Walkable"); } } else if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound) { if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound) { //Debug.LogError ("Making " + tile.name + " Unwalkable "); //Debug.LogError("Keeping " + tile.name + " Walkable"); } } else { tm.setWalkable(false); } } //creating the building Vector3 spawnPos = current[(current.Count - 1) / 2].transform.position; spawnPos.z = -1; //Debug.Log(BuildingManager.me.getToBuild()); GameObject built = (GameObject)Instantiate(BuildingManager.me.getToBuild(), spawnPos, Quaternion.Euler(0, 0, 0)); //Debug.Log(built.name); /* * SpriteRenderer sr = built.gameObject.AddComponent<SpriteRenderer>(); * sr.sprite = built.GetComponent<Building>().buildingSprite; * sr.sortingOrder = 10;*/ built.AddComponent <BoxCollider2D>(); built.SetActive(true); built.tag = "Building"; built.name = "Main_keep"; //Debug.Log(built.tag); BuildingManager.me.buildingsInGame.Add(built.GetComponent <Building>()); clearCurrent(); }
int GetDistance(TileMaster nodeA, TileMaster nodeB) { int dstX = Mathf.Abs((int)nodeA.getCoords().x - (int)nodeB.getCoords().x); int dstY = Mathf.Abs((int)nodeA.getCoords().y - (int)nodeB.getCoords().y); if (dstX > dstY) { return(14 * dstY + 10 * (dstX - dstY)); } return(14 * dstX + 10 * (dstY - dstX)); }
bool isLocationValidForConstruction() { if (current.Count <= 0) { return(false); } foreach (GameObject tile in current) { TileMaster tm = tile.GetComponent <TileMaster>(); if (tm.isWalkable() == false) { return(false); } } int xLowBound = (int)current[0].GetComponent <TileMaster>().getCoords().x; //(int)cursorPos.x - ((width + 0)/2); int xHighBound = (int)current[current.Count - 1].GetComponent <TileMaster>().getCoords().x; //(int)cursorPos.x + ((width + 0)/2); int yLowBound = (int)current[0].GetComponent <TileMaster>().getCoords().y; //(int)cursorPos.y - ((height + 0) / 2); int yHighBound = (int)current[current.Count - 1].GetComponent <TileMaster>().getCoords().y; //(int)cursorPos.y + ((height + 0) / 2); for (int x = 0; x < current.Count - 1; x++) { GameObject tile = current[x]; TileMaster tm = tile.GetComponent <TileMaster>(); Vector2 curTileGrid = tm.getCoords(); if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound) { if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound) { if (tm.isWalkable() == false) { return(false); //should make sure all edge tiles are walkable } } } if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound) { if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound) { if (tm.isWalkable() == false) { return(false); //should make sure all edge tiles are walkable } } } } return(true); }
private void Start() { // external finds m_TileMaster = FindObjectOfType <TileMaster>(); m_GameManager = FindObjectOfType <GameManager>(); // things that belong to me m_Master = transform.root.GetComponent <CharacterMaster>(); m_Sound = transform.root.GetComponent <CharacterSoundManager>(); m_Renderer = GetComponent <Renderer>(); myTag = transform.root.tag; }
public void setTileNearMe(TileMaster tm) { int x = (int)tm.getCoords().x; int y = (int)tm.getCoords().y; int mod = (tilesHeight / 2) + 1; y -= mod; entrance = MapGenerator.me.getTile(x, y); entrance.onPathSelect(); //Debug.Log("TILE NEAREST : " + entrance.name); }
void RetracePath(TileMaster startNode, TileMaster targetNode, ref List <TileMaster> store) { List <TileMaster> path = new List <TileMaster>(); TileMaster currentNode = targetNode; while (currentNode != startNode) { //Debug.Log ("Retracing path " + currentNode.gameObject.name); path.Add(currentNode); //currentNode.OnSelect(); currentNode = currentNode.getParent(); } path.Reverse(); store = path; }
void commandToMove() { if (isUnitSelected()) { if (Input.GetMouseButton(1)) { /* * Vector3 mousePos = Input.mousePosition; * Vector3 mouseInWorld = Camera.main.ScreenToWorldPoint(mousePos); * mouseInWorld.z = 0;*/ Vector2 mousePosray = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); GameObject hitObject = null; string objectTag = ""; RaycastHit2D raycast = Physics2D.Raycast(mousePosray, Vector2.zero, 0f); try { hitObject = raycast.collider.gameObject; objectTag = hitObject.tag; } catch { //Debug.Log("Nothing Hit"); } if (objectTag == "Resource") { //Debug.Log("Hit a resource"); Vector3 mousePos = Input.mousePosition; Vector3 mouseInWorld = Camera.main.ScreenToWorldPoint(mousePos); TileMaster tm = MapGenerator.me.getTile((int)mouseInWorld.x, (int)mouseInWorld.y); if (tm != null) { foreach (GameObject g in SelectionManager.me.getCurrent()) { if (g.GetComponent <Unit>() != null) { Unit u = g.GetComponent <Unit>(); Action a = g.AddComponent <Action_ResourceGathering>(); if (u.canWePerformAction(a)) { a.initaliseLocation(mouseInWorld); //u.actions.Add(a); Action_ResourceGathering res = g.GetComponent <Action_ResourceGathering>(); res.resourceType = hitObject.GetComponent <Resource>().getType(); Debug.Log(res.resourceType + " ----- resource typeeeeeee"); u.addAction(a); a.enabled = false; } else { Destroy(a); moveUnitToLocation(mouseInWorld, g); } } } } } else { //Debug.Log("Did not hit a resource"); Debug.Log(objectTag); Vector3 mousePos = Input.mousePosition; Vector3 mouseInWorld = Camera.main.ScreenToWorldPoint(mousePos); mouseInWorld.z = 0; TileMaster tm = MapGenerator.me.getTile((int)mouseInWorld.x, (int)mouseInWorld.y); if (tm != null) { foreach (GameObject g in SelectionManager.me.getCurrent()) { moveUnitToLocation(mouseInWorld, g); } } } } } }
public void setParent(TileMaster val) { parent = val; }
void getPath(Vector3 startPos, Vector3 endPos, ref List <TileMaster> store) { Vector2 sPos = new Vector2((int)startPos.x, (int)startPos.y); Vector2 ePos = new Vector2((int)endPos.x, (int)endPos.y); TileMaster startNode = MapGenerator.me.getTile((int)sPos.x, (int)sPos.y); TileMaster targetNode = MapGenerator.me.getTile((int)ePos.x, (int)ePos.y); if (startNode == null || targetNode == null || targetNode.isWalkable() == false) { Debug.Log("not walkable"); return; } List <TileMaster> openSet = new List <TileMaster>(); //tiles to check List <TileMaster> closedSet = new List <TileMaster>(); //checked openSet.Add(startNode); while (openSet.Count > 0) { TileMaster node = openSet[0]; for (int i = 1; i < openSet.Count; ++i) { if (openSet[i].fCost < node.fCost || openSet[i].fCost == node.fCost) { if (openSet[i].getH() < node.getH()) { node = openSet[i]; } } } openSet.Remove(node); closedSet.Add(node); if (node == targetNode) { //Debug.LogError ("Finished Path " + startNode.name + " " + targetNode.name); RetracePath(startNode, targetNode, ref store); return; } foreach (TileMaster neighbour in MapGenerator.me.getTileNeighbours(node)) { if (!neighbour.isWalkable() || closedSet.Contains(neighbour) || neighbour == null || node == null) {//not valid neighbour continue; } int newCostToNeighbour = node.getG() + GetDistance(node, neighbour);//calculates gCost if (newCostToNeighbour < neighbour.getG() || !openSet.Contains(neighbour)) { neighbour.setG(newCostToNeighbour); neighbour.setH(GetDistance(neighbour, targetNode)); neighbour.setParent(node); if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } }
public virtual void initialiseTile(TileMaster tm) { }
public List <TileMaster> getTileNeighbours(TileMaster tile) { List <TileMaster> neighbours = new List <TileMaster>(); TileMaster t = tile; Vector2 pos = t.getCoords(); if (pos.x == 0) { if (pos.y == 0) { //bottom left neighbours.Add(map[(int)pos.x + 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y + 1]); } else if (pos.y == mapDimensions.y - 1) { //top left neighbours.Add(map[(int)pos.x + 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y - 1]); } else { neighbours.Add(map[(int)pos.x + 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y + 1]); neighbours.Add(map[(int)pos.x, (int)pos.y - 1]); } } else if (pos.x == mapDimensions.x - 1) { if (pos.y == 0) { //bottom right neighbours.Add(map[(int)pos.x - 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y + 1]); } else if (pos.y == mapDimensions.y - 1) { //top right neighbours.Add(map[(int)pos.x - 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y - 1]); } else { neighbours.Add(map[(int)pos.x - 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y + 1]); neighbours.Add(map[(int)pos.x, (int)pos.y - 1]); } } else { if (pos.y == 0) { //bottom right neighbours.Add(map[(int)pos.x - 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y + 1]); neighbours.Add(map[(int)pos.x + 1, (int)pos.y]); } else if (pos.y == mapDimensions.y - 1) { neighbours.Add(map[(int)pos.x - 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y - 1]); neighbours.Add(map[(int)pos.x + 1, (int)pos.y]); } else { neighbours.Add(map[(int)pos.x - 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y - 1]); neighbours.Add(map[(int)pos.x + 1, (int)pos.y]); neighbours.Add(map[(int)pos.x, (int)pos.y + 1]); } } return(neighbours); }