public bool ActionForwardTo(Vector3 targetPos) { StandAction action = Owner.ActionControl.LookupAction(ActorAction.ENType.enStandAction) as StandAction; if (action != null) { action.ForwardTarget(targetPos); } return(true); }
public override void Update() { base.BaseUpdate(); if (Self.IsDead) {//死亡 return; } m_nextAITime -= Time.deltaTime; if (m_nextAITime > 0.0f) { return; } m_nextAITime = UnityEngine.Random.Range(0.1f, 0.5f); FindSneak(); switch (m_fearType) { case ENFearType.enEscape: { Actor target = ActorManager.Singleton.Lookup(m_targetID); if (target != null) { float distance = ActorTargetManager.GetTargetDistance(Owner.MainPos, target.MainPos); if (distance < m_escapeDistance) { //恐惧逃跑 Vector3 direction = Owner.MainPos - target.MainPos; direction.y = 0; direction.Normalize(); Vector3 targetPos = Owner.MainPos + direction * (m_escapeDistance - distance); MoveAction action = Owner.ActionControl.AddAction(MoveAction.SGetActionType()) as MoveAction; if (action != null) { action.Retarget(targetPos); } return; } } //恐惧站立 Owner.ActionControl.RemoveAction(MoveAction.SGetActionType()); StandAction standAction = Owner.ActionControl.LookupAction(StandAction.SGetActionType()) as StandAction; if (standAction == null) { standAction = Owner.ActionControl.AddAction(StandAction.SGetActionType()) as StandAction; } if (standAction != null) { standAction.AnimName = "standby"; } } break; } }
//!角色整体旋转 void RotateMainBody() { if (CurActionType == ActorAction.ENType.enMoveAction) { MoveAction action = CurrentActor.ActionControl.LookupAction(ActorAction.ENType.enMoveAction) as MoveAction; if (null == action) { return; } Vector3 vMainForward = new Vector3(CurrentActor.MainObj.transform.forward.x, 0f, CurrentActor.MainObj.transform.forward.z); //Vector3 vMainForward = mMoveTargetPos - CurrentActor.MainPos; vMainForward.y = 0f; Vector3 vNPCDirection = CurrentActor.TargetManager.CurrentTarget.MainPos - CurrentActor.MainPos; vNPCDirection.y = 0f; float fForwardAngle = Vector3.Angle(vNPCDirection.normalized, mTargetDirection.normalized); //if (!mIsSwitchOn) { if (fForwardAngle >= (mMaxAngle + 10f)) { if (action.AnimName != "backward") { action.AnimName = "backward"; // mIsSwitchOn = true; //Debug.Log("backward"); } } else if (fForwardAngle <= (mMaxAngle - 10f)) { if (action.AnimName != "run") { action.AnimName = "run"; // mIsSwitchOn = true; //Debug.Log("run"); } } //Debug.Log("action.AnimName=" + action.AnimName + " fForwardAngle=" + fForwardAngle + " mIsSwitchOn=" + mIsSwitchOn); } Vector3 vDirection = new Vector3(mTargetDirection.x, 0, mTargetDirection.z); Vector3 vbodyTurnDirection = Vector3.zero; if (action.AnimName == "run") { vbodyTurnDirection = vDirection; } else { vbodyTurnDirection = -vDirection; } float fBodyTurnAngle = Vector3.Angle(vbodyTurnDirection.normalized, vMainForward.normalized); if (fBodyTurnAngle >= -float.Epsilon && fBodyTurnAngle <= float.Epsilon) { return; } float rotateSpeed = Time.deltaTime * GameSettings.Singleton.m_bodyRotateSpeed * 360f; //Debug.Log("bodyTurnAngle=" + fBodyTurnAngle+" rotateSpeed="+rotateSpeed); if (fBodyTurnAngle < rotateSpeed) { CurrentActor.MoveRotation(Quaternion.LookRotation(vbodyTurnDirection.normalized) * Quaternion.LookRotation(Vector3.forward)); return; } //int nRote = 0; float fRote = 0f; if (action.AnimName == "run") { Vector3 verticalVec = Vector3.Cross(vbodyTurnDirection.normalized, new Vector3(0f, 1.0f, 0f)); fRote = Vector3.Angle(verticalVec.normalized, vMainForward.normalized); //nRote = Mathf.RoundToInt(fRote); if (fRote >= 90f) { rotateSpeed = -rotateSpeed; } } else { float fNPCTurnAngle = Vector3.Angle(vbodyTurnDirection.normalized, vNPCDirection.normalized); if (fNPCTurnAngle >= fBodyTurnAngle) { Vector3 verticalVec = Vector3.Cross(vbodyTurnDirection.normalized, new Vector3(0f, 1.0f, 0f)); fRote = Vector3.Angle(verticalVec.normalized, vMainForward.normalized); if (fRote >= 90f) { rotateSpeed = -rotateSpeed; } } else { Vector3 verticalVec = Vector3.Cross(vNPCDirection.normalized, new Vector3(0f, 1.0f, 0f)); fRote = Vector3.Angle(verticalVec.normalized, vMainForward.normalized); if (fRote >= 90f) { rotateSpeed = -rotateSpeed; } } //Debug.Log("fBodyTurnAngle=" + fBodyTurnAngle + " rotateSpeed=" + rotateSpeed + " action.AnimName=" + action.AnimName + " fRote=" + fRote); } //Debug.Log("moveaction"); CurrentActor.MainObj.transform.Rotate(0.0f, rotateSpeed, 0.0f); } else if (CurActionType == ActorAction.ENType.enStandAction) { StandAction action = CurrentActor.ActionControl.LookupAction(ActorAction.ENType.enStandAction) as StandAction; if (null == action) { return; } float fBodyTurnAngle = 0f; Vector3 vDirection = CurrentActor.TargetManager.CurrentTarget.RealPos - CurrentActor.RealPos; vDirection.y = 0f; vDirection.Normalize(); Vector3 vForward = new Vector3(CurrentActor.MainObj.transform.forward.x, 0f, CurrentActor.MainObj.transform.forward.z); vForward.Normalize(); fBodyTurnAngle = Vector3.Angle(vForward.normalized, vDirection.normalized); //m_fCurAngle = 0f; //action.AnimName = "readytofight"; float rotateSpeed = Time.deltaTime * GameSettings.Singleton.m_standRotateSpeed * 360f; if (fBodyTurnAngle < rotateSpeed) { CurrentActor.MoveRotation(Quaternion.LookRotation(vDirection.normalized) * Quaternion.LookRotation(Vector3.forward)); return; } fBodyTurnAngle = rotateSpeed; Vector3 verticalVec = Vector3.Cross(vForward.normalized, new Vector3(0, 1.0f, 0)); verticalVec.Normalize(); float fOtherRot = Vector3.Angle(verticalVec.normalized, vDirection.normalized); if (fOtherRot <= 90f) { fBodyTurnAngle = -fBodyTurnAngle; } Quaternion bodyRotation = Quaternion.Euler(0, fBodyTurnAngle, 0); CurrentActor.MainObj.transform.rotation = bodyRotation * CurrentActor.MainObj.transform.rotation; //CurrentActor.MainObj.transform.Rotate(0.0f, fBodyTurnAngle, 0.0f); //Debug.Log("enStandAction body turn angle="+fBodyTurnAngle); } }
void Start() { fsm = new FSM("Boss Ai"); standState = fsm.AddState("StandState"); moveTargetState = fsm.AddState("MoveTargetState"); retreatState = fsm.AddState("RetreatState"); runTargetState = fsm.AddState("RunTargetState"); jumpState = fsm.AddState("JumpState"); standAction = new StandAction(standState); moveTargetAction = new MoveTargetAction(moveTargetState); retreatAction = new RetreatAction(retreatState); runTargetAction = new MoveTargetAction(runTargetState); jumpAction = new JumpAction(jumpState); finishEventRun = new List <string> { "to move target", "to retreat", "to stand", "to jump" }; finishEventStand = new List <string> { "to move target", "to retreat", "to run", "to jump" }; finishEventMoveTarget = new List <string> { "to move target", "to stand", "to retreat", "to jump" }; finishEventRetreat = new List <string> { "to stand", "to run" }; finishEventJump = new List <string> { "to stand" }; standState.AddAction(standAction); runTargetState.AddAction(runTargetAction); moveTargetState.AddAction(moveTargetAction); retreatState.AddAction(retreatAction); jumpState.AddAction(jumpAction); standState.AddTransition("to move target", moveTargetState); standState.AddTransition("to retreat", retreatState); standState.AddTransition("to run", runTargetState); standState.AddTransition("to jump", jumpState); moveTargetState.AddTransition("to move target", moveTargetState); moveTargetState.AddTransition("to stand", standState); moveTargetState.AddTransition("to retreat", retreatState); moveTargetState.AddTransition("to jump", jumpState); retreatState.AddTransition("to stand", standState); retreatState.AddTransition("to run", runTargetState); runTargetState.AddTransition("to move target", moveTargetState); runTargetState.AddTransition("to stand", standState); runTargetState.AddTransition("to retreat", retreatState); runTargetState.AddTransition("to jump", jumpState); jumpState.AddTransition("to stand", standState); vecPos = GameObject.Find("Boss").transform.position; vecEnnemis = new Vector3(0.1f, 0, 0); vecJump = new Vector3(0f, 0.15f, 0); moveTargetAction.init(this.transform, vecEnnemis, 2.0f, finishEventMoveTarget); retreatAction.init(this.transform, vecEnnemis, 0.5f, finishEventRetreat); runTargetAction.init(this.transform, new Vector3(0.3f, 0, 0), 1.0f, finishEventRun); jumpAction.init(this.transform, vecJump, 2f, finishEventJump); standAction.init(1.0f, finishEventStand); fsm.Start("StandState"); }
private ActorAction CreateObj(ActorAction.ENType newType) { ActorAction action = m_pool.GetObjectFromPool(newType) as ActorAction; if (action != null) { return(action); } switch (newType) { case ActorAction.ENType.enStandAction: action = new StandAction(); break; //站立 case ActorAction.ENType.enMoveAction: action = new MoveAction(); break; //移动 case ActorAction.ENType.enAttackAction: action = new AttackAction(); break; //攻击 case ActorAction.ENType.enSpasticityAction: action = new SpasticityAction(); break; //被动僵直 case ActorAction.ENType.enBeAttackAction: action = new BeAttackAction(); break; //受击 case ActorAction.ENType.enUndownAction: action = new UndownAction(); break; //霸体 case ActorAction.ENType.enDeadAction: action = new DeadAction(); break; //死亡 case ActorAction.ENType.enReliveAction: action = new ReliveAction(); break; //复活 case ActorAction.ENType.enPlayEffectAction: action = new PlayEffectAction(); break; //播放特效 case ActorAction.ENType.enSearchEnemyAction: //action = new SearchEnemyAction(); break; //搜索敌人 case ActorAction.ENType.enTeleportAction: action = new TeleportAction(); break; //瞬移 case ActorAction.ENType.enControlMoveAction: action = new ControlMoveAction(); break; //控制技能定身 case ActorAction.ENType.enHoldDownAction: action = new HoldDownAction(); break; //按下状态 case ActorAction.ENType.enSelfSpasticityAction: action = new SelfSpasticityAction(); break; //主动僵直 case ActorAction.ENType.enAlertAction: action = new AlertAction(); break; //警戒 case ActorAction.ENType.enJumpinAction: action = new JumpinAction(); break; //入场 case ActorAction.ENType.enJumpoutAction: action = new JumpoutAction(); break; //退场 case ActorAction.ENType.enRollAction: action = new RollAction(); break; //翻滚 case ActorAction.ENType.enFakeBeAttackAction: action = new FakeBeAttackAction(); break; //假受击 case ActorAction.ENType.enActorExitAction: action = new ActorExitAction(); break; //主控角色退场 case ActorAction.ENType.enActorEnterAction: action = new ActorEnterAction(); break; //主控角色入场 case ActorAction.ENType.enControlAttackAction: action = new ControlAttackAction(); break; //不能攻击 case ActorAction.ENType.enControlBeAttackAction: action = new ControlBeAttackAction(); break; //不能受击 case ActorAction.ENType.enAttackingMoveAction: action = new AttackingMoveAction(); break;//攻击时移动 case ActorAction.ENType.enDragMoveAction: action = new DragMoveAction(); break;//拖拽 default: throw new Exception("Miss Action Create for" + newType.ToString()); } return(action); }
public void Tick() { string strAnim = "standby"; ActorAction.ENType curActionType = ActorAction.ENType.enNone; Vector3 mDirection = Vector3.zero; Vector3 syncPos = Vector3.zero; if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enBeAttackAction)) { BeAttackAction action = mActor.ActionControl.LookupAction(ActorAction.ENType.enBeAttackAction) as BeAttackAction; bool isNewAction = IsNewAction(ActorAction.ENType.enBeAttackAction); if (action.mStrAnim != mLastAnimName || isNewAction) { bool isHitedFly = false; if (action.m_isBack) { isHitedFly = action.m_isFly; } mLastAnimName = action.mStrAnim; string animFullName = ""; float animLength = mActor.PlayAnimation(action.mStrAnim, mDirection, out animFullName, syncPos); if (action.fBlendFactor > 0f) { mActor.BlendAnimation(action.mAniBlend, action.fBlendFactor); } if (isHitedFly) { mActor.BlendAnimation("knock", 1.0f); } if (isNewAction) { action.AnimStartTime = Time.time; action.AnimLength = animLength; action.AnimFullName = animFullName; } } return; } else if (mActor.ActionControl.IsActionRunning(AttackingMoveAction.SGetActionType())) { AttackingMoveAction action = mActor.ActionControl.LookupAction(AttackingMoveAction.SGetActionType()) as AttackingMoveAction; strAnim = action.AnimName; curActionType = AttackingMoveAction.SGetActionType(); } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enAttackAction)) { AttackAction action = mActor.ActionControl.LookupAction(ActorAction.ENType.enAttackAction) as AttackAction; bool isNewAction = IsNewAction(ActorAction.ENType.enAttackAction); if (isNewAction) { if (!action.m_isPlay) { strAnim = "standby"; } else { if (null != action.m_skillTarget) { mDirection = action.m_skillTarget.RealPos - mActor.RealPos; } strAnim = action.GetAnimationName(); } if (strAnim == "standby") { // if (mActor.IsState_ReadyToFight) // { // strAnim = "readytofight"; // } } mLastAnimName = strAnim; string animFullName = ""; if (action.IsSyncPosition) { syncPos = action.SyncPosition; } float animLength = mActor.PlayAnimation(strAnim, mDirection, out animFullName, syncPos); if (isNewAction) { action.AnimStartTime = Time.time; action.AnimLength = animLength; action.AnimFullName = animFullName; } AttackAction aAction = action as AttackAction;// mActor.ActionControl.LookupAction(curActionType) as AttackAction; // AttackAction.AttackStepInfo info = aAction.GetAttackStepInfo(); if (aAction.m_isPlay) { //攻击中 if (aAction.m_skillTarget != null) { //有技能目标 float targetWeight = 0; string blendAnimName = ""; Vector3 direction = aAction.m_skillTarget.RealPos - mActor.RealPos; direction.y = 0f; if (aAction.m_skillInfo.SlashMotion == strAnim) {//计算冲刺的权重 if (aAction.m_skillInfo.SlashMotionDistance > 0) { targetWeight = (direction.magnitude + aAction.m_skillInfo.SlashTargetPosOffset) / aAction.m_skillInfo.SlashMotionDistance; blendAnimName = aAction.m_skillInfo.SlashBlendMotionName; } } else if (aAction.m_skillInfo.ReleaseMotion == strAnim) {//计算释放的权重 if (aAction.m_skillInfo.ReleaseMotionDistance > 0) { targetWeight = (direction.magnitude + aAction.m_skillInfo.ReleaseTargetPosOffset) / aAction.m_skillInfo.ReleaseMotionDistance; blendAnimName = aAction.m_skillInfo.ReleaseBlendMotionName; } } if (!string.IsNullOrEmpty(blendAnimName)) {//动作融合 if (targetWeight < 0) { targetWeight = 0; } mActor.BlendAnimation(strAnim, targetWeight, 0); mActor.BlendAnimation(blendAnimName, 1 - targetWeight, 0); } } } SelfSpasticityAction spAction = mActor.ActionControl.AddAction(ActorAction.ENType.enSelfSpasticityAction) as SelfSpasticityAction; if (null != spAction) { spAction.AnimLength = animLength; } } FakeBeAttackAction beFakeAction = mActor.ActionControl.LookupAction(ActorAction.ENType.enFakeBeAttackAction) as FakeBeAttackAction; if (null != beFakeAction && IsNewAction(ActorAction.ENType.enFakeBeAttackAction)) { if ((action.IsNormalAttack())) { mActor.BlendAnimation(beFakeAction.mStrAnim, GameSettings.Singleton.m_fakeBlendWeightValue, GameSettings.Singleton.m_fakeBlendFadeTime, AnimationBlendMode.Additive); } } return; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enFakeBeAttackAction)) { FakeBeAttackAction action = mActor.ActionControl.LookupAction(ActorAction.ENType.enFakeBeAttackAction) as FakeBeAttackAction; bool isNewAction = IsNewAction(ActorAction.ENType.enFakeBeAttackAction); if (action.mStrAnim != mLastAnimName || isNewAction) { mLastAnimName = action.mStrAnim; if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enMoveAction)) { mActor.BlendAnimation(action.mStrAnim, GameSettings.Singleton.m_fakeBlendWeightValue, GameSettings.Singleton.m_fakeBlendFadeTime, AnimationBlendMode.Additive); } else { string animFullName = ""; float animLength = mActor.PlayAnimation(action.mStrAnim, mDirection, out animFullName, syncPos); if (action.fBlendFactor > 0f) { mActor.BlendAnimation(action.mAniBlend, action.fBlendFactor); } action.AnimStartTime = Time.time; action.AnimLength = animLength; action.AnimFullName = animFullName; } } return; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enMoveAction)) { MoveAction action = mActor.ActionControl.LookupAction(ActorAction.ENType.enMoveAction) as MoveAction; strAnim = action.AnimName; float fDiff = action.mRealSpeed / action.m_currentSpeed; if (action.mIsRotating) { //strAnim = "standby"; } else if ((fDiff <= 0.1f || action.m_currentSpeed == 0)) { strAnim = "standby"; } bool isNewAction = IsNewAction(ActorAction.ENType.enMoveAction); if (strAnim != mLastAnimName || isNewAction) { // if (strAnim == "run") // { // float fBase = mActor.PropConfig.RunBaseSpeed - mActor.PropConfig.WalkBaseSpeed; // float fPercent = (mActor.MoveSpeed - mActor.PropConfig.WalkBaseSpeed) / fBase; // if (fPercent <= 0.0f || fPercent <= 1 / fBase) // { // strAnim = "walk"; // } // } // if (strAnim == "standby") // { // if (mActor.IsState_ReadyToFight) // { // strAnim = "readytofight"; // } // } mLastAnimName = strAnim; string animFullName = ""; float animLength = mActor.PlayAnimation(strAnim, mDirection, out animFullName, syncPos); if (isNewAction) { action.AnimStartTime = Time.time; action.AnimLength = animLength; action.AnimFullName = animFullName; } } return; //Debug.Log("strAnim=" + strAnim + " realSpeed=" + action.mRealSpeed + " action.m_currentSpeed=" + action.m_currentSpeed); } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enStandAction)) { StandAction action = mActor.ActionControl.LookupAction(StandAction.SGetActionType()) as StandAction; strAnim = action.AnimName; curActionType = ActorAction.ENType.enStandAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enDeadAction)) { strAnim = "dead"; curActionType = ActorAction.ENType.enDeadAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enReliveAction)) { strAnim = "reborn"; curActionType = ActorAction.ENType.enReliveAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enAlertAction)) { strAnim = "alert"; curActionType = ActorAction.ENType.enAlertAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enControlMoveAction)) {//控制移动 ControlMoveAction action = mActor.ActionControl.LookupAction(ControlMoveAction.SGetActionType()) as ControlMoveAction; strAnim = action.AnimName; curActionType = ActorAction.ENType.enControlMoveAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enJumpinAction)) { strAnim = "jumpin"; curActionType = ActorAction.ENType.enJumpinAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enJumpoutAction)) { strAnim = "jumpout"; curActionType = ActorAction.ENType.enJumpoutAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enRollAction)) { strAnim = "rolling"; curActionType = ActorAction.ENType.enRollAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enActorExitAction)) { strAnim = "jumpout"; curActionType = ActorAction.ENType.enActorExitAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enActorEnterAction)) { strAnim = "jumpin"; curActionType = ActorAction.ENType.enActorEnterAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enControlAttackAction)) {//控制攻击 strAnim = "standby"; curActionType = ActorAction.ENType.enControlAttackAction; } else if (mActor.ActionControl.IsActionRunning(ActorAction.ENType.enControlBeAttackAction)) {//控制受击 strAnim = "standby"; curActionType = ActorAction.ENType.enControlBeAttackAction; } if (strAnim == "") { return; } if (strAnim == "standby") { // if (mActor.IsState_ReadyToFight) // { // strAnim = "readytofight"; // } } bool isNewAction1 = IsNewAction(curActionType); if (strAnim != mLastAnimName || isNewAction1) { mLastAnimName = strAnim; ActorAction action = mActor.ActionControl.LookupAction(curActionType); if (action != null) { if (action.IsSyncPosition) { syncPos = action.SyncPosition; } } string animFullName = ""; float animLength = mActor.PlayAnimation(strAnim, mDirection, out animFullName, syncPos); if (action != null) {//设置animation播放的时间 if (isNewAction1) { action.AnimStartTime = Time.time; action.AnimLength = animLength; action.AnimFullName = animFullName; } } } }