public void MoveSelectedUnit() { switch (state) { case STATE.IDLE: break; case STATE.MOVING: if (moveCounter < moveList.Count) { Move(tgs.CellGetPosition(moveList[moveCounter])); } else { moveCounter = 0; state = STATE.MOVESELECT; PositionUnitInCenterOfCell(); } break; case STATE.MOVESELECT: if (Input.GetMouseButtonUp(0) && this.gameObject.layer == 12) { //definition of target cell int targetCell = tgs.cellHighlightedIndex; if (targetCell != -1) { //check if cell is selected int startCell = tgs.CellGetIndex(tgs.CellGetAtPosition(transform.position, true)); float totalCost; //builds a path from startCell to targetCell moveList = tgs.FindPath(startCell, targetCell, out totalCost); if (moveList == null) { return; } //check if path exceeds unitRange if (unitMovementPoints >= totalCost) { moveCounter = 0; state = STATE.MOVING; unitMovementPoints -= totalCost; Debug.Log("UnitMovementPoints: " + unitMovementPoints); } else { Debug.Log("Movement Range exceeded"); } } else { Debug.Log("No Cell"); } } break; } }
void Start() { // playAud = GetComponent<playSequence>(); //grabs Audio treeSounds = gameObject.AddComponent <AudioSource>(); //TerrainGridSystem ref tgs = TerrainGridSystem.instance; randomRotation = 60 * Random.Range(0, 6); // Clone Sapling prefabs and Instantiate groundTile = tgs.CellGetAtPosition(transform.position, true); //transform.SetParent (); cellIndex = tgs.CellGetIndex(groundTile); neighbors = tgs.CellGetNeighbours(groundTile); tgs.CellSetCanCross(cellIndex, false); //fills up neighborIndexes with the proper cell indexes for (int i = 0; i < neighbors.Count; i++) { int index = tgs.CellGetIndex(neighbors[i]); neighborIndexes.Add(index); neighbourPos[i] = tgs.CellGetPosition(index); //this gives you neighbor cell position //can be used to see which cell you’re being given //raycast to this position to get the plant collider and get the sequencer if (tgs.CellGetTag(index) == 1) { //this cell has a tree planted, but don’t run this in start //send to an array of “plants nearby” //for any rule make a bool in plantlife for “isFollowingRule” to see if neighbours are occupied or following a rule already //are two adjacent cells in the index occupied that’s a triad //if all cells are occupied that’s an arp, arp beats triad } } if (plantedInEditor) { ageCounter -= 1; transform.position = tgs.CellGetPosition(cellIndex); tgs.CellSetTag(cellIndex, 1); tgs.CellToggleRegionSurface(cellIndex, true, growingTexture); } else { //Set age and fruit ageCounter = 0; //fruitAmount = 0; growthPeriod = 1; } StartCoroutine(Growth()); }
void CheckAngleToTarget() { if (Input.GetMouseButtonUp(1)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; float angleBetweenUnits = 0f; if (Physics.Raycast(ray, out hit)) { angleBetweenUnits = Vector3.Angle(hit.transform.position - this.transform.position, transform.forward); } int degree = Mathf.RoundToInt(angleBetweenUnits); if (degree < unitMaxFovAngle) { Debug.Log($"{degree} - possible"); CheckDistanceToTarget(hit); } else { Debug.Log($"{degree} - not possible"); } } void CheckDistanceToTarget(RaycastHit hit) { Vector3 unitPosition = this.transform.position; Vector3 targetPosition = hit.transform.position; Transform target = hit.transform; attackingUnit = tgs.CellGetIndex(tgs.CellGetAtPosition(unitPosition, true)); defendingUnit = tgs.CellGetIndex(tgs.CellGetAtPosition(targetPosition, true)); int distanceToTarget = tgs.CellGetHexagonDistance(attackingUnit, defendingUnit); if (distanceToTarget <= maxRange) { RangedAttack(target); } else { Debug.Log("Target too far away"); } } void RangedAttack(Transform target) { Debug.Log("Attacking---"); target.GetComponent <UnitHealth>().Damage(); } }
private void InitialCellBehaviour() { allCells = tgs.cells; foreach (Cell cell in allCells) { int cellIndex = tgs.CellGetIndex(cell); //green = wood; if (tgs.CellGetTexture(cellIndex) == tgs.textures[1]) { tgs.CellSetCrossCost(cellIndex, 2); tgs.CellToggleRegionSurface(cellIndex, false, tgs.textures[1]); tgs.CellSetGroup(cellIndex, group1); } //apple = swamp else if (tgs.CellGetTexture(cellIndex) == tgs.textures[2]) { tgs.CellSetCrossCost(cellIndex, 4); tgs.CellToggleRegionSurface(cellIndex, false, tgs.textures[2]); tgs.CellSetGroup(cellIndex, group2); } //map = mountain else if (tgs.CellGetTexture(cellIndex) == tgs.textures[3]) { tgs.CellSetCanCross(cellIndex, false); tgs.CellToggleRegionSurface(cellIndex, false, tgs.textures[3]); } } }
public void PositionUnitInCenterOfCell() { Cell cell = tgs.CellGetAtPosition(transform.position, true); int cellIndex = tgs.CellGetIndex(cell); Bounds bounds = tgs.CellGetRectWorldSpace(cellIndex); transform.position = bounds.center; }
private void MarkAllGameObjects() { GameObject[] gameobjects; gameobjects = GameObject.FindGameObjectsWithTag("Enemy"); foreach (GameObject g in gameobjects) { Cell cell = tgs.CellGetAtPosition(g.transform.position, true); int cellIndex = tgs.CellGetIndex(cell); tgs.CellSetCrossCost(cellIndex, 12000); } }
void Update() { //Checks if has been picked up and equipped //Sends out raycast Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //Checks if raycast hits if (Physics.Raycast(ray, out hit)) { //Checks if the hit is a ground tile and within Distance for hoeing if (hit.transform.gameObject.tag == "Ground" && Vector3.Distance(_player.transform.position, hit.point) <= shovelDistance && !textureShowing) { //grabs Cell tile and index Cell fertile = tgs.CellGetAtPosition(hit.point, true); int cellIndex = tgs.CellGetIndex(fertile); currentCellIndex = cellIndex; if (currentCellIndex != previousCellIndex) { previousCellIndex = currentCellIndex; } //checks if cell is normal Ground if (tgs.CellGetTag(cellIndex) == 0) { //Sets texture to clickable tgs.CellToggleRegionSurface(cellIndex, true, canClickTexture); //Takes click, sets tile to Fertile if (Input.GetMouseButtonDown(0)) { { tgs.CellSetTag(fertile, 1); StartCoroutine(ChangeTexture(cellIndex, fertileTexture)); } //soundBoard.PlayOneShot(InteractSound); } } //Switches tile back to normal Ground if (tgs.CellGetTag(previousCellIndex) == 0) { StartCoroutine(ChangeTexture(currentCellIndex, groundTexture)); } } } }
//Checks if fruit has collided with ground void OnCollisionEnter(Collision collision) { rb.isKinematic = true; if (collision.gameObject.tag == "Ground") { onGround = true; //Checks what ground tile this is using collision point Vector3 collisionPoint = collision.contacts[0].point; Cell groundTile = tgs.CellGetAtPosition(collisionPoint, true); int cellIndex = tgs.CellGetIndex(groundTile); //Starts Decompose using ground tile StartCoroutine(Decompose(groundTile, cellIndex, tgs.CellGetTag(cellIndex))); } }
void Update() { //Checks if has been picked up and equipped if (inventMan.underPlayerControl) { //Sends out raycast Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //Checks if raycast hits if (Physics.Raycast(ray, out hit)) { //Checks if the hit is a ground tile and within Distance for planting if (hit.transform.gameObject.tag == "Ground" && Vector3.Distance(_player.transform.position, hit.point) <= withinPlantingRange && !textureShowing) { //grabs Cell tile and index Cell fertile = tgs.CellGetAtPosition(hit.point, true); int cellIndex = tgs.CellGetIndex(fertile); currentCellIndex = cellIndex; if (currentCellIndex != previousCellIndex) { previousCellIndex = currentCellIndex; } //checks if cell is fertile if (tgs.CellGetTag(cellIndex) == 1) { //Sets texture to clickable tgs.CellToggleRegionSurface(cellIndex, true, canClickTexture); //If player clicks, we plant seed and clear up Equip slot if (Input.GetMouseButtonDown(0)) { plantSeed = true; targetPos = hit.point; playerControl.isHoldingSeed = false; inventMan.underPlayerControl = false; invent.usedNowTakeAgain(inventMan.slotNumRetake); } } //If it's a new cell, set last cell back to fertileTexture if (tgs.CellGetTag(previousCellIndex) == 1) { StartCoroutine(ChangeTexture(currentCellIndex, fertileTexture)); } } } //This will be true either after a fruit decomposes, or after player plants seed } if (plantSeed) { //grabs Cell tile and index Cell plantTile = tgs.CellGetAtPosition(targetPos, true); int cellIndex = tgs.CellGetIndex(plantTile); //checks if cell is Fertile if (tgs.CellGetTag(cellIndex) == 1 || planting) { // Debug.Log("planter"); //Centers seed on tile transform.position = new Vector3(tgs.CellGetPosition(cellIndex).x, transform.position.y, tgs.CellGetPosition(cellIndex).z); //Calls PlantSeed function on selected tile PlantSeed(plantTile, cellIndex); } } //always rotate seed in world space transform.Rotate(0, 1, 0 * Time.deltaTime); }