private fireState() { if (instance != null) { return; } instance = this; }
private bool Body_OnCollision(Fixture fixturea, Fixture fixtureb, Contact contact) { if (!Convert.ToString(fixtureb.UserData).Equals("wall") && !Convert.ToString(fixtureb.UserData).Equals("ship")) { if (fixtureb.Body.IsBullet) { String data = Convert.ToString(fixtureb.UserData); String[] splitdata = data.Split(':'); String shotType = splitdata[0]; int shotEffect = Convert.ToInt32(splitdata[1]); switch(shotType) { case("standard"): { sheilds -= shotEffect; break; } case ("emp"): { currentNegativeState = negativeState.Emped; negativeEffectTime = (shotEffect*1000); break; } case ("net"): { sheilds -= shotEffect; if(shotEffect > 0) { Console.Out.WriteLine("Damage = " + shotEffect); Console.Out.WriteLine("sheilds = " + sheilds); } break; } } //been shot return true; } else if (!Convert.ToString(fixtureb.UserData).Equals("")) { String powerupData = Convert.ToString(fixtureb.UserData); switch (powerupData) { case "shield": { sheilds = 200; break; } case "Shotgun": { currentFireState = fireState.Shotgun; break; } case "Laser": { currentFireState = fireState.Laser; break; } case "Machinegun": { currentFireState = fireState.Machinegun; break; } case "Spiral": { currentPassiveState = passiveState.SpiralFire; break; } case "Emp": { currentSecondaryFire = secondaryFire.EmpShot; altFireQuantity = 5; break; } case "Net": { currentSecondaryFire = secondaryFire.Net; altFireQuantity = 1; break; } } return false; //powerup } else { return true; } } else { float Yvel = base.mSpriteBody.LinearVelocity.Y; float Xvel = base.mSpriteBody.LinearVelocity.X; int wallDamage = (int)Math.Max(Math.Sqrt(Yvel*Yvel), Math.Sqrt(Xvel*Xvel)); sheilds -= wallDamage; return true; } }
void firePatterns_fire() { if (Quaternion.Angle(barrelSwing.localRotation, swingEnd) > 20F) { barrelSwing.Rotate(-Vector3.up * shotSwingSpeed * Time.deltaTime); if (curShtCooldown <= 0) { GameObject shotGo = Instantiate(bossShot, barrelEnd.position, barrelEnd.rotation) as GameObject; Shot shot = shotGo.GetComponent<Shot>(); shot.Initialize(GameData.shotMoveSpeedTable[(int)GameData.Team.Enemy], GameData.shotDamageTable[(int)GameData.EnemyType.Navi], GameData.Team.Enemy, GameData.ShotType.Normal, transform); curShtCooldown = shotCooldown; } else { curShtCooldown--; } } else { if (timesToFire <= 0) { timesToFire = Random.Range(1,3); curSwingCooldown = swingCooldown; } else { timesToFire--; } _fire = fireState.calc; barrelSwing.localRotation = swingStart; } }
/// <summary> /// 状态机 /// </summary> void mind() { { //获取动画状态 AnimatorTransitionInfo transitioInfo = animator.GetAnimatorTransitionInfo(0); AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0); switch (currentState) { //停住 case fireState.Idle: { //一定要转到idle动画为止 animator.SetBool("StopFire", false); //转完再说 if (beginTurnAround) { break; } //想一下 if (thinkTime < 0.5f) { thinkTime += Time.deltaTime; } else { thinkTime = 0.0f; currentState = fireState.SeekingPlayer; } } break; //看看谁是下一个倒霉蛋 case fireState.SeekingPlayer: { isFoundPlayer = false; foreach (var a in SceneNetManager.instance.list) { if (a.Value != null) { //用头比较清真 Vector3 dir = (a.Value.transform.Find("Head").position - new Vector3(0.0f, 0.5f, 0.0f)) - head.position; Ray ray = new Ray(head.position, dir); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { #if UNITY_EDITOR Debug.DrawLine(ray.origin, hit.point, Color.red, 2.0f); Debug.Log(hit.collider.tag); #endif if (hit.collider.tag == "Player") { isFoundPlayer = true; target = a.Value.transform; #if UNITY_EDITOR Debug.Log(target); #endif break; } } } } //找到了 if (isFoundPlayer) { int choice = (int)UnityEngine.Random.Range(0, 3); switch (choice) { case 0: { //转移到下一个状态 currentState = fireState.OpenFire; handup = true; animator.SetBool("handup", true); //netSyncController.SyncVariables(); } break; case 1: case 2: { //转移到下一个状态 currentState = fireState.MissileLaunch; //同步 animator.SetBool("missileLaunch", true); missilLaunch = true; //netSyncController.SyncVariables(); } break; } } //维持seek状态2秒 if (thinkTime >= 2.0f) { thinkTime = 0.0f; currentState = fireState.wandering; } else { thinkTime += Time.deltaTime; } } break; case fireState.wandering: { //TODO:之前状态要加一下isStartWalking的初始化 //先转过去再走 if (!isStartWalking) { //选个目标 currentWanderTarget = wanderPath[UnityEngine.Random.Range(0, wanderPath.Length)]; StartCoroutine(turnAround(currentWanderTarget)); isStartWalking = true; } else { //转到位了 if (!beginTurnAround) { if (!isBeginWandering) { animator.SetBool("walk", true); isWalking = true; //netSyncController.SyncVariables(); isBeginWandering = true; } else { if (Vector3.Distance(transform.position, currentWanderTarget.position) < 10.0f) { isStartWalking = false; isBeginWandering = false; isWalking = false; animator.SetBool("walk", false); //netSyncController.SyncVariables(); currentState = fireState.SeekingPlayer; } } } } } break; //抬起手为止 case fireState.OpenFire: { //切换完毕了 if (stateInfo.IsName("shoot")) { //动画状态转移,同步 if (stateInfo.normalizedTime >= 0.5f) { animator.SetBool("handup", false); animator.SetBool("shoot", true); handup = false; shoot = true; //netSyncController.SyncVariables(); //转移状态 currentState = fireState.KeepFire; } } } break; //抬手到播完再换手 case fireState.KeepFire: { if (stateInfo.IsName("keepShooting")) { //开火 if (fireFromLastTime > intervalBetweenShot) { if (target != null) { //开火 leftHandFireImpl(target.transform.Find("Head").position - new Vector3(0.0f, 0.5f, 0.0f)); netSyncController.RPC(this, "leftHandFireImpl", target.transform.Find("Head").position - new Vector3(0.0f, 0.5f, 0.0f)); fireFromLastTime = 0.0f; } } else { fireFromLastTime += Time.deltaTime; } //直到开火完毕,抬起另一只手 if (animationCurrentTime >= 2.0f) { animator.SetBool("rightHandup", true); animator.SetBool("shoot", false); rightHandup = true; shoot = false; //同步 //netSyncController.SyncVariables(); //下一个状态 currentState = fireState.RightFire; animationCurrentTime = 0.0f; } else { animationCurrentTime += Time.deltaTime; } } } break; //另一只手抬起完成 case fireState.RightFire: { //切换完毕了 if (stateInfo.IsName("shootback")) { //开火 if (stateInfo.normalizedTime >= 0.5f) { animator.SetBool("rightHandup", false); animator.SetBool("shootAgain", true); rightHandup = false; shootAgain = true; //同步 //netSyncController.SyncVariables(); //下一个状态 currentState = fireState.KeepFireAgain; } } } break; case fireState.KeepFireAgain: { Debug.Log("fire"); if (stateInfo.IsName("keepShootingBack")) { if (fireFromLastTime > intervalBetweenShot) { if (target != null) { //开火 rightHandFireImpl(target.transform.Find("Head").position - new Vector3(0.0f, 0.5f, 0.0f)); netSyncController.RPC(this, "rightHandFireImpl", target.transform.Find("Head").position - new Vector3(0.0f, 0.5f, 0.0f)); fireFromLastTime = 0.0f; } } else { fireFromLastTime += Time.deltaTime; } //直到开火完毕 if (animationCurrentTime >= 2.0f) { animator.SetBool("StopFire", true); animator.SetBool("shootAgain", false); stopFire = true; shoot = false; //netSyncController.SyncVariables(); currentState = fireState.StopFire; animationCurrentTime = 0.0f; } else { animationCurrentTime += Time.deltaTime; } } } break; case fireState.StopFire: { Debug.Log("stop"); if (stateInfo.IsName("idle")) { Debug.Log("end of fire"); if (!beginTurnAround) { StartCoroutine(turnAround(target)); currentState = fireState.Idle; } } } break; case fireState.MissileLaunch: { if (stateInfo.IsName("missileLaunch")) { if (stateInfo.normalizedTime >= 0.8f) { animator.SetBool("missileLaunch", false); missilLaunch = false; //同步 //netSyncController.SyncVariables(); if (target != null) { missileLaunchImpl(target.position); netSyncController.RPC(this, "missileLaunchImpl", target.position); } currentState = fireState.StopFire; } } } break; default: break; } } bool[] states = { shoot, handup, rightHandup, shootAgain, stopFire, missilLaunch, isWalking }; netSyncController.RPC(this, "updateAState_Room2", states); }
void firePatterns_calcSlope() { Vector3 lookPos = player.transform.position - barrelSwing.position; lookPos.y = 0; Quaternion rotation = Quaternion.LookRotation(lookPos); barrelSwing.rotation = rotation; midDir = barrelSwing.localRotation; swingStart = midDir *Quaternion.AngleAxis(30f, Vector3.down); swingEnd = midDir * Quaternion.AngleAxis(30F, Vector3.up); barrelSwing.localRotation = swingStart; _fire = fireState.fire; }