// Use this for initialization void Start() { hexController = GameObject.FindGameObjectWithTag("GameController").GetComponent <HexController>(); boardManager = GameObject.FindGameObjectWithTag("GameController").GetComponent <BoardManager>(); phaseManager = GameObject.FindGameObjectWithTag("GameController").GetComponent <PhaseManager>(); specificBehavior = this.transform.GetComponent <SpecificBehavior>(); currentHex = hexController.GetNearestHex(this.transform.position); //If already on the board, snap to nearest hex. Only use this during development //if (specificBehavior.active == BoardManager.Active.Board) SnapToNearest(); this.transform.GetComponent <Rigidbody>().isKinematic = false; }
//Snap to the nearest eligible hex or the starting hex, whichever is closest void SnapToNearestEligible(List <HexCoord> eligibleList) { //Debug.Log(eligibleList.Count); //Brute force method to get nearest eligible hex float smallestDist = Mathf.Infinity; HexCoord closestHex = startHex; //Default to the starting hex List <HexCoord> eligibleListMod = new List <HexCoord>(eligibleList); eligibleListMod.Add(startHex); //Include the starting hex foreach (HexCoord hex in eligibleListMod) { float dist = Vector2.SqrMagnitude(hex.Position() - (Vector2)this.transform.position); if (dist < smallestDist) { smallestDist = dist; closestHex = hex; } } Vector2 snapPos = closestHex.Position(); this.transform.position = snapPos; //Snap to x,y position first //this.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f)); //Snap to default rotation //For beetles the eligible hex might be occupied. In that case, fix the z distance and don't allow gravity if (specificBehavior.type == BoardManager.UnitType.Beetle && boardManager.occupiedList.Contains(closestHex) && closestHex != startHex) { Vector3 temp = this.transform.position; int numBeetles = boardManager.beetleDict[closestHex]; temp.z = 0.2f + 0.4f * (numBeetles + 1); //Position will scale up if there are multiple beetles this.transform.position = temp; this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; } else { this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY; } currentHex = hexController.GetNearestHex(this.transform.position); }
// Use this for initialization void Start() { hexController = GameObject.FindGameObjectWithTag("GameController").GetComponent<HexController>(); boardManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<BoardManager>(); phaseManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<PhaseManager>(); specificBehavior = this.transform.GetComponent<SpecificBehavior>(); currentHex = hexController.GetNearestHex(this.transform.position); //If already on the board, snap to nearest hex. Only use this during development //if (specificBehavior.active == BoardManager.Active.Board) SnapToNearest(); this.transform.GetComponent<Rigidbody>().isKinematic = false; }
void GetCurrent() { currentHex = hexController.GetNearestHex((Vector2)this.transform.position); //Debug.Log("CurrentHex: " + currentHex); }