public void onBallEnterPocket(string pocketID,PoolBall ball) { if(audio) { audio.PlayOneShot( onBallEnterPocketAC ); } }
public override void OnCollisionEnter(Collision col) { if (GameManager.Rules.State == GlobalState.DRAG_WHITEBALL) return; if (col.gameObject.name.Contains("Rail")) { HOAudioManager.BallhitRail(m_rigidbody.velocity); GameManager.Rules.CueBallHitRail(); GameManager.Rules.BallHitRail(); GameStatistics.MarkCueballHitRail(1); } if (col.transform.CompareTag("Ball")) { HOAudioManager.BallhitBall(m_rigidbody.velocity); GameStatistics.MarkCueballHitBall(1); PoolBall ball = col.gameObject.GetComponent<PoolBall>(); GameManager.Rules.WhiteBallHitBall(ball); if (ball && ball == m_targetBall) { m_targetBall.PointAtTarget(m_targetPos); m_targetBall = null; } } }
protected override void Start() { Pools.DisableStandardBalls(); List <LevelData.BallData> lp = LevelDataIndex.CurrentLevel.ballDatas; Transform ooRoot = GameObject.Find("8Ball/OtherObjects").transform; for (int i = 0, count = lp.Count; i < count; i++) { LevelData.BallData d = lp[i]; GameObject o = Resources.Load(d.Type.ToString()) as GameObject; GameObject oo = Instantiate(o) as GameObject; oo.transform.SetParent(ooRoot); d.Position.y = 0; oo.transform.position = d.Position; PoolBall pb = oo.GetComponent <PoolBall>(); pb.SetBallID(d.ID); pb.ballType = d.Type; if (pb.ballType != BallType.JIANGYOU && pb.ballType != BallType.DEMON) { m_TargetBalls.Add(d.ID); } Pools.CustomBalls.Add(pb.GetBallID(), pb); } Pools.CueBall.transform.position = LevelDataIndex.CurrentLevel.cueBallData.Position; PocketTrigger.MarkPocketType(LevelDataIndex.CurrentLevel.StartPunishmentPocket, LevelDataIndex.CurrentLevel.StartRewardPocket); PocketTrigger.Block(LevelDataIndex.CurrentLevel.BlockPockets); m_Player.ShotsRemain = LevelDataIndex.CurrentLevel.shotCount; TurnBegin(); TextDialog.Show(LevelDataIndex.CurrentLevel.DescriptionID); }
private Dictionary <PoolBall, List <PocketTrigger> > GetConsiderBalls() { Dictionary <PoolBall, List <PocketTrigger> > considerableBalls = new Dictionary <PoolBall, List <PocketTrigger> >(); for (int i = 0; i < cMsg.ballList.Count; i++) { PoolBall ball = cMsg.ballList[i]; if (ball.BallState != PoolBall.State.IDLE) { continue; } List <PocketTrigger> triggerList = new List <PocketTrigger>(); for (int j = 0; j < Pools.PocketTriggers.Count; j++) { PocketTrigger trigger = Pools.PocketTriggers[j]; //is any obstacle between the ball and the pocket ? bool b1 = !CheckObastacleBetweenBallAndPocket(ball, trigger); if (b1) { triggerList.Add(trigger); } } if (triggerList.Count != 0) { considerableBalls.Add(ball, triggerList); } } return(considerableBalls); }
public virtual void WhiteBallHitBall(PoolBall ball) { if (!m_WhiteHitBall) { m_WhiteHitBall = true; } }
private Dictionary <PoolBall, PocketTrigger> FilterTheBestPocketForEachBallWithCueBall(Dictionary <PoolBall, List <PocketTrigger> > considerBalls, Vector3 cueBallPosition) { Dictionary <PoolBall, PocketTrigger> optimalBalls = new Dictionary <PoolBall, PocketTrigger>(); foreach (var v in considerBalls) { PoolBall ball = v.Key; float bestAngle = 70; PocketTrigger bestPocket = null; for (int i = 0, count = v.Value.Count; i < count; i++) { PocketTrigger pocket = v.Value[i]; // is any obstacle between the ball hit position and the cueball ? bool b1 = !CheckObastacleBetweenTargetPositionAndCueball(ball, ConsiderHitPoint(ball, pocket), cueBallPosition); if (b1) { float angle = MathTools.AngleBetween(ball.transform.position - cueBallPosition, pocket.pointerPosition - ball.transform.position); if (angle < bestAngle) { bestPocket = pocket; bestAngle = angle; } } } optimalBalls.Add(ball, bestPocket); } return(optimalBalls); }
public override bool canUseBall(PoolBall ball) { bool useBall = m_greaterThen8 == 0; bool allBallsDown = areAllBallsDown(); PoolBall[] balls = (PoolBall[])GameObject.FindObjectsOfType(typeof(PoolKit.PoolBall)); if(m_greaterThen8==0) { useBall = ball.ballType != PoolBall.BallType.BLACK; } if(m_greaterThen8==1) { useBall = ball.ballType == PoolBall.BallType.STRIPE; } if(m_greaterThen8==-1) { useBall =ball.ballType == PoolBall.BallType.SOLID; } if(m_greaterThen8!=0 && ball.ballType==PoolBall.BallType.BLACK && (allBallsDown)) { useBall=true; } if(balls.Length==2 && ball.ballType==PoolBall.BallType.BLACK) { useBall=true; } return useBall; }
private Dictionary <PoolBall, PocketTrigger> FilterTheBestPocketForEachBall(Dictionary <PoolBall, List <PocketTrigger> > considerBalls) { Dictionary <PoolBall, PocketTrigger> optimalBalls = new Dictionary <PoolBall, PocketTrigger>(); foreach (var v in considerBalls) { PoolBall ball = v.Key; float bestDistance = float.MaxValue; PocketTrigger bestPocket = null; for (int i = 0, count = v.Value.Count; i < count; i++) { PocketTrigger pocket = v.Value[i]; if (GetPlacedSpace(ball, ball.transform.position - pocket.pointerPosition) < ball.GetRadius() * 2) { continue; } float distance = Vector3.Distance(ball.transform.position, pocket.pointerPosition); if (distance < bestDistance) { bestPocket = pocket; bestDistance = distance; } } optimalBalls.Add(ball, bestPocket); } return(optimalBalls); }
public static void Add(PoolBall ball) { ball.rigidbody.position = Instance.transform.position; //ball.CloseDrag(); ball.AudioEnable = false; ball.CloseRenderer(); }
private void Awake() { //Save orginal position so we can use later for the reset for (int i = 0; i < PoolBalls.Length; i++) { if (PoolBalls[i].IsCueBall) { pCueBall = PoolBalls[i]; } PoolBalls[i].InitialiseBall(VelocityDecayRate, TrackBallStatus, UpdatePoint); } //Display a warning if cue ball is not found if (pCueBall == null) { Debug.LogWarning("Cue ball not found. Can't start a game without one, right?"); } //Setting up the game engine's physics bounce threshold, //if the threshold is too high, the ball will not bounce //of the side cushion if the velocity lower than the threshold Physics.bounceThreshold = BounceTreshold; HasAwake(); }
public override void OnCollisionEnter(Collision col) { if (GameManager.Rules.State == GlobalState.DRAG_WHITEBALL) { return; } if (col.gameObject.name.Contains("Rail")) { HOAudioManager.BallhitRail(m_rigidbody.velocity); GameManager.Rules.CueBallHitRail(); GameManager.Rules.BallHitRail(); GameStatistics.MarkCueballHitRail(1); } if (col.transform.CompareTag("Ball")) { HOAudioManager.BallhitBall(m_rigidbody.velocity); GameStatistics.MarkCueballHitBall(1); PoolBall ball = col.gameObject.GetComponent <PoolBall>(); GameManager.Rules.WhiteBallHitBall(ball); if (ball && ball == m_targetBall) { m_targetBall.PointAtTarget(m_targetPos); m_targetBall = null; } } }
public override void OnCollisionEnter(Collision col) { if (col.gameObject.name.Contains("Rail")) { //we hit the wall. HOAudioManager.BallhitRail(m_rigidbody.velocity); if (!hitWall) { GameManager.Rules.BallHitRail(); hitWall = true; } } if (col.gameObject.CompareTag("Ball")) { PoolBall ball = col.transform.GetComponent <PoolBall>(); if (ball.ballType != BallType.WHITE && BallState == State.ROLL) { m_AbsorbList.Add(ball); //ball.Hide(); TemporarySlot.Add(ball); } else { HOAudioManager.BallhitBall(m_rigidbody.velocity); } } }
public override void OnCollisionEnter(Collision col) { if (col.gameObject.name.Contains("Rail")) { //we hit the wall. //BaseGameManager.ballHitWall(rigidbody.velocity); HOAudioManager.BallhitRail(m_rigidbody.velocity); if (!hitWall) { GameManager.Rules.BallHitRail(); hitWall = true; } } if (col.gameObject.CompareTag("Ball")) { PoolBall ball = col.transform.GetComponent <PoolBall>(); if (ball.ballType != BallType.WHITE && BallState == State.ROLL) { Instantiate(m_BreakEffect, transform.position, Quaternion.identity); if (SingularityBreakBall != null) { SingularityBreakBall(ball); } HOAudioManager.Break(); } else { HOAudioManager.BallhitBall(m_rigidbody.velocity); } } }
protected void OnSingularityBreakBall(PoolBall ball) { if (ball) { Destroy(ball.gameObject); m_TargetBalls.Remove(ball.GetBallID()); } }
public override void WhiteBallHitBall(PoolBall ball) { if (!m_WhiteHitBall) { m_WhiteHitBallType = ball.ballType; } base.WhiteBallHitBall(ball); }
public void setTarget(PoolBall ball, Vector3 targetPos) { if (ball) { m_targetBall = ball; m_targetPos = targetPos; m_targetPos.y = transform.position.y; } }
private PoolBall[] _GetBallsArray() { PoolBall[] balls = new PoolBall[m_Balls.Count]; for (int i = 0, length = balls.Length; i < length; i++) { balls[i] = m_Balls[i]; } return(balls); }
//a ball lands in the pocket lets gray out the texture. public virtual void enterPocket(PoolBall ball) { if(ball.ballType!=PoolBall.BallType.WHITE) { int ballIndex = ball.ballIndex; ballIndex--; m_ballIcons[ballIndex].color = Color.gray*.2f ; } }
private float ConsiderPowerScale(PoolBall ball, PocketTrigger trigger, Vector3 hitPoint, Vector3 cueBallPosition) { Vector3 targetBallPosition = ball.transform.position, pocketPosition = trigger.pointerPosition; //float angle = MathTools.AngleBetween(hitPoint - cueBallPosition, pocketPosition - targetBallPosition); float mag = (targetBallPosition - cueBallPosition).magnitude + (pocketPosition - targetBallPosition).magnitude; return(Mathf.Min(1, mag / Pools.GetTableSize().x)); }
public override void PotBall(PoolBall ball, PocketIndexes pocket) { base.PotBall(ball, pocket); if(ball.ballType != BallType.WHITE) { m_Player.AddBall(ball.GetBallID()); m_Player.AddScore(ConstantData.QuickFireBallPottedPoint); m_Time += 10; m_Player.PlayTime += 10; } }
public override void PotBall(PoolBall ball, PocketIndexes pocket) { base.PotBall(ball, pocket); if (ball.ballType != BallType.WHITE) { m_Player.AddBall(ball.GetBallID()); m_Player.AddScore(ConstantData.QuickFireBallPottedPoint); m_Time += 10; m_Player.PlayTime += 10; } }
public override void handleFirstBallHitByWhiteBall(PoolBall ball) { if(ball) { m_ballIndex=ball.ballIndex; Debug.Log ("m_ballIndex"+ m_ballIndex + " getLowesBall() + "+ getLowestPoolBall()); m_foul = m_ballIndex != getLowestPoolBall(); }else{ Debug.Log ("NUllball"); } }
public static void PutBallToThePoint(PoolBall ball, ref Vector3 p) { float r = ball.GetRadius(); while (Physics.OverlapSphere(p, r, 1 << LayerMask.NameToLayer("Ball") | 1 << LayerMask.NameToLayer("WhiteBall")).Length != 0) { p.x -= r; } SupportTools.SetPosition(ball.gameObject, p, SupportTools.AxisIgnore.IgnoreY, true); p.x -= r; }
private PoolBall[] _GetCustomBallArray() { PoolBall[] cBalls = new PoolBall[m_CustomBalls.Count]; int i = 0; foreach (var v in m_CustomBalls) { cBalls[i++] = v.Value; } return(cBalls); }
protected virtual IEnumerator CheckResultAndChangeTurn(float time) { yield return(new WaitForSeconds(time)); for (int i = 0, count = m_PottedBallListThisRound.Count; i < count; i++) { PoolBall pb = m_PottedBallListThisRound[i]; m_PottedBallList.Add(pb.GetBallID(), pb); } m_PottedBallListThisRound.Clear(); TurnBegin(); }
//our ball has landed in the water, lets call the ball bowling pit void OnTriggerEnter(Collider col) { PoolBall pb = col.GetComponent <PoolBall>(); if (col.CompareTag("Ball")) { GameManager.Rules.PotBall(pb, m_RefTrigger.PocketIndex); Pools.StorageRack.Add(pb); if (PocketTrigger.pocketTriggerBallList.Contains(col.GetInstanceID())) { PocketTrigger.pocketTriggerBallList.Remove(col.GetInstanceID()); } } }
public void Add(PoolBall ball) { ball.AudioEnable = true; ball.LightRenderer.Close(); Rigidbody rb = ball.rigidbody; rb.position = m_InitTrans.position; rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; rb.useGravity = false; rb.isKinematic = true; rb.collider.enabled = false; rb.renderer.enabled = false; m_BallQueue.Enqueue(rb); }
protected override IEnumerator CheckResultAndChangeTurn(float time) { yield return(new WaitForSeconds(time)); if (m_PottedBallListThisRound.Count == 0) { m_Player.Combo = 0; } if (m_WhiteBallPotted) { m_Player.Score -= m_ScoreThisRound; BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(106), m_ScoreThisRound)); yield return(new WaitForSeconds(ConstantData.MissionFoulTimeWait)); for (int i = 0, count = m_PottedBallListThisRound.Count; i < count; i++) { m_PottedBallListThisRound[i].BackToPrevRoundState(); } } else { for (int i = 0, count = m_PottedBallListThisRound.Count; i < count; i++) { PoolBall pb = m_PottedBallListThisRound[i]; m_PottedBallList.Add(pb.GetBallID(), pb); } } m_PottedBallListThisRound.Clear(); if (CheckGameOver()) { State = GlobalState.GAMEOVER; if (onGameOver != null) { if (m_TargetBalls.Count == 0) { onGameOver(m_Player); } else if (m_Player.ShotsRemain == 0) { onGameOver(null); } } } else { TurnBegin(); } }
public override void PotBall(PoolBall ball, PocketIndexes pocket) { base.PotBall(ball, pocket); if (CurrentPlayer.TargetBallType == BallType.NONE && ball.ballType != BallType.WHITE && ball.ballType != BallType.BLACK && !firstRound) { CurrentPlayer.TargetBallType = ball.ballType; OpponentPlayer.TargetBallType = ball.ballType == BallType.SOLID ? BallType.STRIPE : BallType.SOLID; } BallType b = ball.ballType; if (b != BallType.WHITE) { CurrentPlayer.combo++; } }
void HandleRotate() { if (Pools.CueBall && Pools.CueBall.sphereCollider) { SphereCollider sc = Pools.CueBall.sphereCollider; Ray ray = new Ray(Pools.CueBall.transform.position, m_CueTrans.forward); float r = Pools.CueBall.GetRadius(); RaycastHit rch; if (Physics.SphereCast(ray, r - Mathf.Epsilon, out rch, 1000f, layerMask.value)) { Vector3 pos = rch.point; BaseUIController.cueAndLines.GuidePointerAt(rch.point, rch.transform, rch.normal, m_FirePoint.forward); Vector3 vec = pos - sc.transform.position; pos = sc.transform.position + vec.normalized * (vec.magnitude - r); Vector3 nrm = rch.normal; nrm.y = 0; m_targetBall = null; if (rch.collider.CompareTag("Ball")) { m_targetBall = rch.transform.GetComponent <PoolBall>(); m_targetPos = rch.point - nrm; } if (GameManager.GType >= GameType.Standard && m_targetBall != null) { BasePlayer player = ((PoolRulesStandard)GameManager.Rules).CurrentPlayer; bool b1 = player.TargetBallType == BallType.NONE && m_targetBall.ballType == BallType.BLACK; bool b2 = player.TargetBallType != BallType.NONE && player.TargetBallType != m_targetBall.ballType; if (b1 || b2) { BaseUIController.cueAndLines.Forbidden(); } else { BaseUIController.cueAndLines.Allow(); } } else { BaseUIController.cueAndLines.Allow(); } } } }
private float GetPlacedSpace(PoolBall ball, Vector3 dir) { ball.collider.enabled = false; ball.rigidbody.isKinematic = true; RaycastHit hit; float space = 0; if (Physics.SphereCast(ball.transform.position, ball.GetRadius(), dir, out hit, ConstantData.OulineAndBallLayer)) { space = (hit.transform.position - ball.transform.position).magnitude * .5f; } ball.collider.enabled = true; ball.rigidbody.isKinematic = false; return(space); }
public virtual void PotBall(PoolBall ball, PocketIndexes pocket) { ball.Potted(pocket); if (ball.ballType == BallType.WHITE) { m_WhiteBallPotted = true; if (onCueballPotted != null) { onCueballPotted(); } } else if (ball.ballType != BallType.JIANGYOU && ball.ballType != BallType.DEMON) { m_PottedBallListThisRound.Add(ball); } }
private bool CheckObastacleBetweenTargetPositionAndCueball(PoolBall ball, Vector3 targetPosition, Vector3 cueBallPosition) { Vector3 dir = targetPosition - cueBallPosition; RaycastHit[] hit = Physics.SphereCastAll(cueBallPosition, Pools.CueBall.GetRadius(), dir, dir.magnitude, ConstantData.OulineAndBallLayer); foreach (RaycastHit h in hit) { //Debug.Log("Obastacle between hit point-" + GetHitPoint(ball, pocket) + " and cue ball : " + h.transform.name); if (h.collider.name.CompareTo(ball.name) != 0)//the sphere always hit the cue ball { return(true); } } return(false); }
private bool CheckObastacleBetweenBallAndPocket(PoolBall originBall, PocketTrigger pocket) { Vector3 dir = pocket.pointerPosition - originBall.transform.position; RaycastHit[] hit = Physics.SphereCastAll(originBall.transform.position, originBall.GetRadius(), dir, dir.magnitude, ConstantData.OulineAndBallLayer); foreach (RaycastHit h in hit) { //Debug.Log("Obastacle between " + originBall.name + " and " + pocket.name + " : " + h.transform.name); if (h.collider.name.CompareTo(originBall.name) != 0) { return(true); } } return(false); }
public override void enterPocket(PoolBall ball) { if(ball.ballType!=PoolBall.BallType.WHITE) { if(ball.ballType!=PoolBall.BallType.BLACK) { int ballIndex = ball.ballIndex; bool greater8 = (ballIndex>8); if(m_setEvenOdd==false) { setEvenOdd(greater8); m_setEvenOdd=true; } } base.enterPocket(ball); } }
private Vector3 ConsiderHitPoint(Dictionary <PoolBall, List <PocketTrigger> > considerBalls, Vector3 cueBallPosition, out PoolBall targetBall, out PocketTrigger targetTrigger) { Dictionary <PoolBall, PocketTrigger> optimalBalls = FilterTheBestPocketForEachBallWithCueBall(considerBalls, cueBallPosition); //Dictionary<PoolBall, float> ballCuebalAngles = new Dictionary<PoolBall, float>(); List <float> angles = new List <float>(); List <PoolBall> balls = new List <PoolBall>(); float totleAngle = 0; foreach (var v in optimalBalls) { PoolBall ball = v.Key; PocketTrigger trigger = v.Value; if (!trigger) { continue; } float angle = MathTools.AngleBetween(ball.transform.position - cueBallPosition, trigger.pointerPosition - ball.transform.position); //夹角越大, 旋转击打这个球的概率就越小,所以这里用90 - angle angle = 90 - angle; totleAngle += angle; balls.Add(ball); angles.Add(angle); } //随机选择一个击打球 float rand = Random.Range(0, totleAngle), threshold = 0; for (int i = 0, count = angles.Count; i < count; i++) { threshold += angles[i]; if (rand < threshold) { PoolBall k = balls[i]; targetBall = k; targetTrigger = optimalBalls[k]; return(ConsiderHitPoint(targetBall, targetTrigger)); } } targetTrigger = null; targetBall = null; return(Vector3.zero); }
protected override IEnumerator CheckResultAndChangeTurn(float time) { yield return(new WaitForSeconds(time)); if (m_WhiteBallPotted) { m_Time -= 30; } if (m_PottedBallListThisRound.Count == 0 && !m_WhiteBallPotted) { m_Player.ComboBreak(); } for (int i = 0, count = m_PottedBallListThisRound.Count; i < count; i++) { PoolBall pb = m_PottedBallListThisRound[i]; m_PottedBallList.Add(pb.GetBallID(), pb); } m_PottedBallListThisRound.Clear(); TurnBegin(); }
public override bool isBallOkay(PoolBall ball) { bool isOkay=true; if(areAllBallsDown==false) { if(greaterThen8==0) { isOkay = m_targetBall.ballType != PoolBall.BallType.BLACK; } if(greaterThen8==1) { isOkay = m_targetBall.ballType == PoolBall.BallType.STRIPE; }else if(greaterThen8==-1) { isOkay = m_targetBall.ballType == PoolBall.BallType.SOLID; } }else{ isOkay = m_targetBall.ballType == PoolBall.BallType.BLACK; } return isOkay; }
public override void enterPocket(PoolBall ball) { if(ball && ball == m_whiteBall) { m_whiteEnteredPocket = true; }else { m_totalBallsPocketed++; m_ballsPocketed++; } //we sunk the 8 ball if(ball.ballIndex==9) { m_gameover=true; if(m_totalBallsPocketed==9) { PoolKit.BaseGameManager.gameover(m_players[m_playerTurn].playerName + " Wins!"); }else{ PoolKit.BaseGameManager.gameover( m_players[m_playerTurn].playerName + " Loses!"); } } Debug.Log ("enterPocket" + ball.ballIndex + " m_totalBallsPocketed " + m_totalBallsPocketed); }
public override void Start() { base.Start(); sphereCollider = gameObject.GetComponent<SphereCollider>(); ball = gameObject.GetComponent<PoolBall>(); m_constraint = gameObject.GetComponent<Constraint>(); m_constraint.enabled=false; //m_cue = (PoolCue)GameObject.FindObjectOfType(typeof(PoolCue)); stopBall(); }
private bool CheckObastacleBetweenTargetPositionAndCueball(PoolBall ball, Vector3 targetPosition, Vector3 cueBallPosition) { Vector3 dir = targetPosition - cueBallPosition; RaycastHit[] hit = Physics.SphereCastAll(cueBallPosition, Pools.CueBall.GetRadius(), dir, dir.magnitude, ConstantData.OulineAndBallLayer); foreach (RaycastHit h in hit) { //Debug.Log("Obastacle between hit point-" + GetHitPoint(ball, pocket) + " and cue ball : " + h.transform.name); if (h.collider.name.CompareTo(ball.name) != 0)//the sphere always hit the cue ball return true; } return false; }
private Vector3 ConsiderHitPoint(PoolBall ball, PocketTrigger trigger) { Vector3 vec = ball.transform.position - trigger.pointerPosition; return ball.transform.position + vec.normalized * ball.GetRadius() * 2; }
private bool CheckObastacleBetweenBallAndPocket(PoolBall originBall, PocketTrigger pocket) { Vector3 dir = pocket.pointerPosition - originBall.transform.position; RaycastHit[] hit = Physics.SphereCastAll(originBall.transform.position, originBall.GetRadius(), dir, dir.magnitude, ConstantData.OulineAndBallLayer); foreach (RaycastHit h in hit) { //Debug.Log("Obastacle between " + originBall.name + " and " + pocket.name + " : " + h.transform.name); if (h.collider.name.CompareTo(originBall.name) != 0) return true; } return false; }
private PoolBall[] _GetCustomBallArray() { PoolBall[] cBalls = new PoolBall[m_CustomBalls.Count]; int i = 0; foreach(var v in m_CustomBalls) { cBalls[i++] = v.Value; } return cBalls; }
public void setTarget(PoolBall ball, Vector3 p2) { poolCueGO.SetActive(true); lineRenderer.SetColors(Color.green,Color.green); m_lr2.SetColors(Color.blue,Color.blue); lineRenderer.SetPosition(0,m_whiteBall.transform.position); lineRenderer.SetPosition(1,ball.transform.position); if(m_lr2) { m_lr2.gameObject.SetActive(true); m_lr2.SetPosition(0,ball.transform.position); m_lr2.SetPosition(1,p2); } }
void HandleRotate() { if (Pools.CueBall && Pools.CueBall.sphereCollider) { SphereCollider sc = Pools.CueBall.sphereCollider; Ray ray = new Ray(Pools.CueBall.transform.position, m_CueTrans.forward); float r = Pools.CueBall.GetRadius(); RaycastHit rch; if (Physics.SphereCast(ray, r - Mathf.Epsilon, out rch, 1000f, layerMask.value)) { Vector3 pos = rch.point; BaseUIController.cueAndLines.GuidePointerAt(rch.point, rch.transform, rch.normal, m_FirePoint.forward); Vector3 vec = pos - sc.transform.position; pos = sc.transform.position + vec.normalized * (vec.magnitude - r); Vector3 nrm = rch.normal; nrm.y = 0; m_targetBall = null; if (rch.collider.CompareTag("Ball")) { m_targetBall = rch.transform.GetComponent<PoolBall>(); m_targetPos = rch.point - nrm; } if (GameManager.GType >= GameType.Standard && m_targetBall != null) { BasePlayer player = ((PoolRulesStandard)GameManager.Rules).CurrentPlayer; bool b1 = player.TargetBallType == BallType.NONE && m_targetBall.ballType == BallType.BLACK; bool b2 = player.TargetBallType != BallType.NONE && player.TargetBallType != m_targetBall.ballType; if (b1 || b2) BaseUIController.cueAndLines.Forbidden(); else BaseUIController.cueAndLines.Allow(); } else BaseUIController.cueAndLines.Allow(); } } }
private float GetPlacedSpace(PoolBall ball, Vector3 dir) { ball.collider.enabled = false; ball.rigidbody.isKinematic = true; RaycastHit hit; float space = 0; if (Physics.SphereCast(ball.transform.position, ball.GetRadius(), dir, out hit, ConstantData.OulineAndBallLayer)) { space = (hit.transform.position - ball.transform.position).magnitude * .5f; } ball.collider.enabled = true; ball.rigidbody.isKinematic = false; return space; }
private float ConsiderPowerScale(PoolBall ball, PocketTrigger trigger, Vector3 hitPoint, Vector3 cueBallPosition) { Vector3 targetBallPosition = ball.transform.position, pocketPosition = trigger.pointerPosition; //float angle = MathTools.AngleBetween(hitPoint - cueBallPosition, pocketPosition - targetBallPosition); float mag = (targetBallPosition - cueBallPosition).magnitude + (pocketPosition - targetBallPosition).magnitude; return Mathf.Min(1, mag / Pools.GetTableSize().x); }
private Vector3 ConsiderHitPointWithDrag(Dictionary<PoolBall, List<PocketTrigger>> considerBalls, out Vector3 cueBallPosition, out PoolBall targetBall, out PocketTrigger targetTrigger) { Dictionary<PoolBall, PocketTrigger> optimalBalls = FilterTheBestPocketForEachBall(considerBalls); cueBallPosition = Vector3.zero; targetTrigger = null; targetBall = null; if(optimalBalls.Count == 0) { return Vector3.zero; } else { foreach (KeyValuePair<PoolBall, PocketTrigger> kvp in optimalBalls) { targetBall = kvp.Key; targetTrigger = kvp.Value; if (!targetTrigger) continue; Vector3 dir = targetBall.transform.position - targetTrigger.pointerPosition; //白球摆成和目标球和袋口一条直线 cueBallPosition = targetBall.transform.position + (dir.normalized * Mathf.Min(GetPlacedSpace(targetBall, targetBall.transform.position - targetTrigger.pointerPosition), 5 * targetBall.GetRadius())); //这里随机会跳出循环,代表随机取一个球的意思, optimalBalls.count 为了使概率平均 if (Random.Range(1, optimalBalls.Count) == 1) break; } if (!targetTrigger) { return Vector3.zero; } return ConsiderHitPoint(targetBall, targetTrigger); } }
private Vector3 ConsiderHitPoint(Dictionary<PoolBall, List<PocketTrigger>> considerBalls, Vector3 cueBallPosition, out PoolBall targetBall, out PocketTrigger targetTrigger) { Dictionary<PoolBall, PocketTrigger> optimalBalls = FilterTheBestPocketForEachBallWithCueBall(considerBalls, cueBallPosition); //Dictionary<PoolBall, float> ballCuebalAngles = new Dictionary<PoolBall, float>(); List<float> angles = new List<float>(); List<PoolBall> balls = new List<PoolBall>(); float totleAngle = 0; foreach(var v in optimalBalls) { PoolBall ball = v.Key; PocketTrigger trigger = v.Value; if (!trigger) continue; float angle = MathTools.AngleBetween(ball.transform.position - cueBallPosition, trigger.pointerPosition - ball.transform.position); //夹角越大, 旋转击打这个球的概率就越小,所以这里用90 - angle angle = 90 - angle; totleAngle += angle; balls.Add(ball); angles.Add(angle); } //随机选择一个击打球 float rand = Random.Range(0, totleAngle), threshold = 0; for (int i = 0, count = angles.Count; i < count; i++) { threshold += angles[i]; if (rand < threshold) { PoolBall k = balls[i]; targetBall = k; targetTrigger = optimalBalls[k]; return ConsiderHitPoint(targetBall, targetTrigger); } } targetTrigger = null; targetBall = null; return Vector3.zero; }
void handleRotate() { if(m_whiteBall && m_whiteBall.sphereCollider) { poolCueGO.SetActive(true); SphereCollider sc = m_whiteBall.sphereCollider; Ray ray = new Ray(m_whiteBall.transform.position + new Vector3(0,sc.radius*ballScalarRadius,0),sc.transform.forward); RaycastHit rch; if(Physics.SphereCast(ray,sc.radius*ballScalarRadius,out rch,1000f,layerMask.value)) { Vector3 pos = rch.point; lineRenderer.SetPosition(0,m_whiteBall.transform.position); float radius = sc.radius * ballScalarRadius; Vector3 vec = pos-sc.transform.position; pos = sc.transform.position + vec.normalized * (vec.magnitude - radius); if(lineRenderer) lineRenderer.SetPosition(1,pos); Vector3 nrm = rch.normal; nrm.y=0; if(m_lr2) { m_lr2.gameObject.SetActive(false); } m_targetBall=null; lineRenderer.SetColors(Color.yellow,Color.yellow); if(rch.collider.name.Contains("Ball")) { m_targetBall = rch.collider.GetComponent<PoolBall>(); bool isOkay = isBallOkay(m_targetBall); if(isOkay) { lineRenderer.SetColors(Color.green,Color.green); m_lr2.SetColors(Color.blue,Color.blue); }else{ lineRenderer.SetColors(Color.red,Color.red); m_lr2.SetColors(Color.magenta,Color.magenta); } if(m_lr2) { m_lr2.gameObject.SetActive(true); } Vector3 otherBallPos = rch.point; Vector3 incomingVec = rch.point - sc.transform.position; Vector3 reflectVec = Vector3.Reflect(-incomingVec, rch.normal); Debug.DrawLine(sc.transform.position, rch.point, Color.red); Debug.DrawRay(rch.point, reflectVec, Color.green); Vector3 dir = Vector3.Reflect ( m_whiteBall.transform.forward,nrm).normalized; m_lr2.SetPosition(0,rch.point); Vector3 mirrorPos = rch.point + Quaternion.AngleAxis(angleReflect,Vector3.up) * dir.normalized * reflectDistance; m_lr2.SetPosition(1,mirrorPos); m_targetPos = mirrorPos; mirrorPos = pos - Quaternion.AngleAxis(angleReflect,Vector3.up) * dir.normalized * reflectDistance; } } } }
public override void OnCollisionEnter(Collision col) { float vel = Mathf.Max(rigidbody.velocity.x,rigidbody.velocity.z); if (col.gameObject.name.Contains("Ball")) { PoolBall ball = col.gameObject.GetComponent<PoolBall>(); // Debug.Log ("whiteBallHit"+ ball.name + m_hitBall); if(ball && m_hitBall==false) { PoolKit.BaseGameManager.whiteBallHitBall(m_hitBall,ball); m_hitBall=true; } //we hit our target, lets set it to the target position if(ball && ball==m_targetBall) { PoolKit.BaseGameManager.ballHitBall(rigidbody.velocity); m_targetBall.pointAtTarget(m_targetPos); m_targetBall=null; } } }
public virtual bool isBallOkay(PoolBall ball) { return true; }
private PoolBall[] _GetBallsArray() { PoolBall[] balls = new PoolBall[m_Balls.Count]; for (int i = 0, length = balls.Length; i < length; i++) { balls[i] = m_Balls[i]; } return balls; }
void onEnterPocket(string pocketIndex, PoolBall ball) { enterPocket(ball); }