IEnumerator MoveCoins() { yield return(new WaitForSeconds(1.0f)); foreach (GameObject p2c in runHumanVsAIGame.p2coins) { if (p2c == null) { continue; } float sqrdist = 100000; GameObject closestHumanCoin = null; foreach (GameObject p1c in runHumanVsAIGame.p1coins) { if (p1c == null) { continue; } Vector3 testdisp = p2c.transform.position - p1c.transform.position; float testdist = Vector3.SqrMagnitude(testdisp); if (testdist < sqrdist) { sqrdist = testdist; closestHumanCoin = p1c; } } //closest coin hit if (closestHumanCoin == null) { yield return(null); } else { coinProps info = p2c.GetComponent <coinProps> (); info.toggleLocked(true); info.locked = true; Vector3 dir = closestHumanCoin.transform.position - p2c.transform.position; float forceFactor = p2c.GetComponent <coinProps> ().forceFactor; Vector3 forceToAdd = dir.normalized * .85f * forceFactor; // why not .8 for now //Debug.Log(coin.name + forceToAdd); p2c.GetComponent <Rigidbody>().AddForceAtPosition(forceToAdd, p2c.transform.position); yield return(new WaitForSeconds(1.0f)); } } turnInProgress = false; yield return(null); }
//s void CastRayToWorld(Vector2 pos) { //Debug.Log ("initially: " + clicks); Ray r = Camera.main.ScreenPointToRay(pos); //Vector3 p = r.origin + r.direction*(10 - (-.17f)); Vector3 p = Vector3.zero; RaycastHit test; LayerMask lm; //our layermask for raycasting to ignore coins not in turn lm = lm2; lm = ~lm; bool hitCoin = false; GameObject coinHit; if (Physics.Raycast(r.origin, r.direction, out test, Mathf.Infinity, lm)) { p = test.point; GameObject gotHit = test.collider.gameObject; if (!gotHit.name.StartsWith("Terrain")) { if (clicks == 0) { focusSimple coinF = gotHit.GetComponent <focusSimple> (); if (!coinF) { return; } coinF.handleSelection(); return; } Vector3 adjust = test.normal; adjust.Normalize(); p += -.1f * adjust; } else { deselectAllCoins(); Vector3 adjust = test.normal; adjust.Normalize(); p += .1f * adjust; } } if (clicks == 0) { clickPos = p; clicks += 1; //Debug.Log("h " + clicks);//added } else { clicks = 0; Vector3 dir = p - clickPos; //k so here rather than debug dray ray do a line renderer so viewer can see in scene StartCoroutine(drawLine(clickPos, p, 1.0f)); Debug.DrawRay(clickPos, dir, Color.blue, 5.0f); RaycastHit hit; float dist = dir.magnitude; if (Physics.Raycast(clickPos, dir, out hit, dist, lm)) { GameObject target = hit.collider.gameObject; coinProps info = target.GetComponent <coinProps> (); //if (info.owner != turn){return;} // so exit if WAIT NO use raycast layber bitmask and here just check for locked/set it if false to true and increment a var to decide if turn over if (!info) { return; } if (info.locked) //so this coin is locked meaning have already launched it { return; } info.toggleLocked(true); info.locked = true; //so we're going to launch this so need to lock it so can't double launch in same turn Rigidbody coin = target.GetComponent <Rigidbody> (); float distToHit = Vector3.Distance(hit.point, clickPos); float ratio = distToHit / dist; //Debug.Log("N/D: " + ratio); Vector3 offset = hit.point - coin.position; float offsetsize = offset.magnitude; //Debug.Log(offsetsize); offset = useless for coins--always = .5. For pencilwars (original) it had meaning... forceFactor = target.GetComponent <coinProps> ().forceFactor; Vector3 forceToAdd = dir.normalized * ratio * forceFactor; // * Mathf.Pow (offsetsize,spower); //Debug.Log(coin.name + forceToAdd); coin.AddForceAtPosition(forceToAdd, hit.point); } } //Debug.Log ("at the end: " + clicks); }
void handleTurn() { bool canChangeTurn = true; //so start off saying yes can change turn from px but then go through all of px's coins and if it is the case that at least one remains unlocked, then nope, can't switch if (turn == 1) { foreach (GameObject c in p1coins) { if (c) { coinProps info = c.GetComponent <coinProps>(); //Debug.Log(info.locked); if (!info.locked) { canChangeTurn = false; } } } } else { foreach (GameObject c in p2coins) { if (c) { coinProps info = c.GetComponent <coinProps>(); if (!info.locked) { canChangeTurn = false; } } } } if (canChangeTurn) { if (turn == 1) { foreach (GameObject c in p2coins) { if (c) { coinProps info = c.GetComponent <coinProps>(); info.toggleLocked(false); info.locked = false; } } turn = 2; } else { foreach (GameObject c in p1coins) { if (c) { coinProps info = c.GetComponent <coinProps>(); info.toggleLocked(false); info.locked = false; } } turn = 1; } } }