void Update() { if (mCurrentCount < mMaxCount) { if (Time.time > mPrevSpawnTime + Random.Range(1.5f, 3f)) { mPrevSpawnTime = Time.time; //刷怪 GameObject obj = RoleManager.Instance.LoadRole("RoleModel_Xiaobing1"); obj.transform.parent = this.transform; obj.transform.position = this.transform.position; RoleBehaviour controller = obj.GetComponent <RoleBehaviour>(); controller.SpawnPosition = obj.transform.position; EnemyInfo enemyInfo = new EnemyInfo(); enemyInfo.roleLocalID = 1; enemyInfo.roleServerID = 106807008888; enemyInfo.currentHP = enemyInfo.maxHP = 100; enemyInfo.username = "******"; EnemyAI_Battle enemyAI = new EnemyAI_Battle(controller); controller.Init(RoleType.ENEMY, enemyInfo, enemyAI); controller.OnRoleDie = OnRoleDie; mCurrentCount++; } } }
public void ShowHurtUiEffect(EntityModel entityModel, uint bulletId) { if (entityModel == null) { return; } BulletData bulletData = SkillDataManager.Instance.GetBulletData((int)bulletId); RoleBehaviour roleBehavior = entityModel.GO.GetComponent <RoleBehaviour>(); if (null != roleBehavior && null != roleBehavior.HurtPoint) { if (bulletData != null) { if (null != bulletData.m_hurt_Ui_Effect) { GameObject uiEffectObj = GameObjectPool.Instance.AcquireLocal(bulletData.m_hurt_Ui_Effect, Vector3.zero, Quaternion.identity); uiEffectObj.transform.parent = PopupObjManager.Instance.UICamera.transform; Vector3 uiPosition = PopupTextController.GetPopupPos(roleBehavior.HurtPoint.position, PopupObjManager.Instance.UICamera); uiEffectObj.transform.position = uiPosition; } } else { TraceUtil.Log(SystemModel.Common, TraceLevel.Error, "没子弹数据"); } } else { TraceUtil.Log(SystemModel.Common, TraceLevel.Error, "没受击点"); } }
public void ShowHurtEffect(EntityModel entityModel, uint bulletId, Int64 ownerUID) { if (entityModel == null) { return; } var bulletData = SkillDataManager.Instance.GetBulletData((int)bulletId); TypeID type; EntityModel ownerEntityModel = EntityController.Instance.GetEntityModel(ownerUID, out type); if (type != TypeID.TYPEID_PLAYER || ownerEntityModel.EntityDataStruct.SMsg_Header.IsHero) { BattleManager.Instance.TryShakeCamera(bulletData); } RoleBehaviour roleBehavior = entityModel.GO.GetComponent <RoleBehaviour>(); if (null != roleBehavior && null != roleBehavior.HurtPoint) { if (bulletData != null) { if ("0" != bulletData.m_hurtEffectPath) { if (BattleManager.Instance != null && BattleManager.Instance.CanShowHurtEffect()) { BattleManager.Instance.OnHurtEffectCreate(); GameObject hurtPrefab = MapResManager.Instance.GetMapEffectPrefab(bulletData.m_hurtEffectPath); GameObjectPool.Instance.AcquireLocal(hurtPrefab, roleBehavior.HurtPoint.position , bulletData.m_hurtEffectRotationFlag == 0 ? Quaternion.identity : Quaternion.Euler(0, UnityEngine.Random.Range(0, 360), 0)); } //bulletData.m_sfx_id;音效 //bulletData.m_hurt_flash;闪光 //bulletData.m_hurt_shake;震屏 } if (bulletData.m_hurtFlash > 0) { float hurtDuration = (float)(bulletData.m_hurtFlash) / 1000.0f; roleBehavior.ShowHurtFlash(true, hurtDuration); } if ("0" != bulletData.m_sfx_id) { SoundManager.Instance.PlaySoundEffect(bulletData.m_sfx_id); } } else { TraceUtil.Log(SystemModel.Common, TraceLevel.Error, "没打到子弹数据"); } } else { TraceUtil.Log(SystemModel.Common, TraceLevel.Error, "没打到受击点"); } }
//选择角色: private void CloneMyRole(Account_LoginGameServerRespProto.RoleItem roleItem) { GameObject roleObj = Instantiate(GlobalInit.Instance.mClassDic[roleItem.RoleClass]); roleObj.transform.parent = CreateRoleContainers[0]; //加载已有角色时默认使用第一个台柱子 roleObj.transform.localScale = Vector3.one; roleObj.transform.localPosition = Vector3.zero; roleObj.transform.localRotation = Quaternion.Euler(Vector3.zero); RoleBehaviour roleCtrl = roleObj.GetComponent <RoleBehaviour>(); //全局缓存,每次使用直接加载 GlobalCache.Instance.CurrentRoleUIPrefab = GlobalInit.Instance.mClassDic[roleItem.RoleClass]; }
/// <summary> /// 构造函数,实例化manager对象时,同时实例化字典,添加所有状态 /// </summary> /// <param name="playercontroller"></param> public RoleFSMStateManager(RoleBehaviour playercontroller) { currentplayerController = playercontroller; roleStateDic = new Dictionary <RoleState, RoleStateBase>(); roleStateDic[RoleState.Idle] = new RoleStateIdle(this); roleStateDic[RoleState.Run] = new RoleStateRun(this); roleStateDic[RoleState.Attack] = new RoleStateAttack(this); roleStateDic[RoleState.Hurt] = new RoleStateHurt(this); roleStateDic[RoleState.Die] = new RoleStateDie(this); roleStateDic[RoleState.Fight] = new RoleStateFight(this); if (roleStateDic.ContainsKey(currentRoleStateEnum)) { currentRoleState = roleStateDic[currentRoleStateEnum]; } }
//点击UI时,人物还是会移动,增加UICamera的判断,当点击UI时,不让人物移动 void OnPlayerClick() { if (EventSystem.current.IsPointerOverGameObject()) { return; //UI挡住ray,防止角色点击UI同时走路 } ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hitAll = Physics.RaycastAll(ray, Mathf.Infinity, 1 << LayerMask.NameToLayer("Role")); if (hitAll.Length > 0) { RoleBehaviour enemy = hitAll[0].collider.gameObject.GetComponent <RoleBehaviour>(); if (enemy.currentRoleType == RoleType.ENEMY) { //如果射线碰撞到了敌人,就跑到攻击范围内进行攻击 GlobalInit.Instance.currentPlayer.viewedEnemy = enemy; } } else { if (Physics.Raycast(ray, out hit)) { if (hit.collider.gameObject.tag == "Road") { if (GlobalInit.Instance.currentPlayer != null) { GlobalInit.Instance.currentPlayer.viewedEnemy = null; GlobalInit.Instance.currentPlayer.ToRun(hit.point); } } } } //如果UICamera存在 if (UICamera.currentCamera != null) { Ray rayUI = UICamera.currentCamera.ScreenPointToRay(Input.mousePosition); //检测rayUI是否碰撞了UI层 if (Physics.Raycast(rayUI, Mathf.Infinity, 1 << LayerMask.NameToLayer("UI"))) { //发现是UI时,返回,不让角色移动 return; } } }
/// <summary> /// 创建角色:把class.xls里所有职业的prefab全部克隆到场景中 /// </summary> private void CloneAllRoles() { if (CreateRoleContainers == null && CreateRoleContainers.Length < 4) { return; } for (int i = 0; i < mClassList.Count; i++) { GameObject roleObj = Instantiate(GlobalInit.Instance.mClassDic[mClassList[i].Id]); roleObj.transform.parent = CreateRoleContainers[i]; roleObj.transform.localScale = Vector3.one; roleObj.transform.localPosition = Vector3.zero; roleObj.transform.localRotation = Quaternion.Euler(Vector3.zero); RoleBehaviour roleCtrl = roleObj.GetComponent <RoleBehaviour>(); if (roleCtrl != null) { mRoleCtrlDic.Add(mClassList[i].Id, roleCtrl); //把rolecontroller添加进字典,实际就是把角色prefab添加进去 } } }
public EnemyAI_Battle(RoleBehaviour controller) { CurrentRole = controller; }
private void Awake() { Instance = this; //allMonster = new List<MonsterController>(); attackJudgment = new AttackJudgment(); }
private float mNextAttackTime = 0; //下次攻击时间 public PlayerAI_Battle(RoleBehaviour controller) { CurrentRole = controller; }
public void OnRoleDie(RoleBehaviour controller) { mCurrentCount--; Destroy(controller.gameObject); }
public PlayerAI_City(RoleBehaviour controller) { CurrentRole = controller; }
void CreateAnimatorByConfig(AnimatorControllerConfig animatorControllerConfig) { if (animatorControllerConfig != null) { Debug.Log("target = " + target); Undo.RecordObject(target, "animatorController"); // 支持撤销 add by TangJian 2018/04/16 15:41:34 AnimatorController animatorController = target as AnimatorController; AnimationClip[] animationClips = animatorController.animationClips; AnimatorControllerLayer[] animatorControllerLayers = animatorController.layers; AnimatorStateMachine animatorStateMachine = animatorControllerLayers[0].stateMachine; string animatorControllerPath = AssetDatabase.GetAssetPath(animatorController); Object[] clips = AssetDatabase.LoadAllAssetRepresentationsAtPath(animatorControllerPath); animatorController.parameters = null; foreach (var parameterConfig in animatorControllerConfig.parameters) { animatorController.AddParameter(parameterConfig.name, parameterConfig.type); } // 添加所有状态 add by TangJian 2018/04/17 20:08:41 foreach (var stateConfig in animatorControllerConfig.states) { var state = animatorStateMachine.AddStateEx(stateConfig.name); state.behaviours = null; state.transitions = null; // 清空所有状态 add by TangJian 2018/04/17 20:45:08 state.tag = stateConfig.tag; state.speed = stateConfig.Speed; CountsThrow(clips, stateConfig.animName); state.motion = clips.First(o => o.name == stateConfig.animName) as Motion; if (state.motion is AnimationClip animationClip) { AnimationClipSettings animationClipSettings = AnimationUtility.GetAnimationClipSettings(animationClip); animationClipSettings.loopTime = stateConfig.loop; AnimationUtility.SetAnimationClipSettings(animationClip, animationClipSettings); } // 添加BlendTree if (stateConfig.blendTree != null) { if (state.motion is UnityEditor.Animations.BlendTree) { UnityEditor.Animations.BlendTree blendTree = state.motion as UnityEditor.Animations.BlendTree; // BlendTree属性设置 add by TangJian 2018/9/18 18:06 blendTree.name = "blendTree_" + state.name; blendTree.blendParameter = stateConfig.blendTree.blendParameter; blendTree.useAutomaticThresholds = stateConfig.blendTree.useAutomaticThresholds; List <UnityEditor.Animations.ChildMotion> addChildMotionList = new List <UnityEditor.Animations.ChildMotion>(); foreach (var childMotionConfig in stateConfig.blendTree.childMotions) { UnityEditor.Animations.ChildMotion childMotion = new UnityEditor.Animations.ChildMotion(); childMotion.threshold = childMotionConfig.threshold; childMotion.timeScale = childMotionConfig.timeScale; addChildMotionList.Add(childMotion); } var oldBlendTree = state.motion as UnityEditor.Animations.BlendTree; var oldChildren = oldBlendTree.children; var newChildren = new List <UnityEditor.Animations.ChildMotion>(); // 更新oldChildren add by TangJian 2018/9/18 18:07 for (int i = 0; i < oldChildren.Length; i++) { if (i < addChildMotionList.Count) { oldChildren[i].threshold = addChildMotionList[i].threshold; oldChildren[i].timeScale = addChildMotionList[i].timeScale; } } // 添加oldChildren中的motion到newChildren add by TangJian 2018/9/18 18:08 foreach (var item in oldChildren) { newChildren.Add(item); } // 添加剩余addChildMotionList中的motion到newChildren add by TangJian 2018/9/18 18:08 if (blendTree.children.Length > oldChildren.Length) { for (int i = oldChildren.Length; i < blendTree.children.Length; i++) { newChildren.Add(blendTree.children[i]); } } blendTree.children = newChildren.ToArray(); if (stateConfig.blendTree.useAutomaticThresholds) { if (stateConfig.blendTree.minThreshold > int.MinValue && stateConfig.blendTree.maxThreshold > int.MinValue) { blendTree.minThreshold = stateConfig.blendTree.minThreshold; blendTree.maxThreshold = stateConfig.blendTree.maxThreshold; } else { blendTree.minThreshold = 0; blendTree.maxThreshold = blendTree.children.Length - 1; } } state.motion = blendTree; } else { state.motion = GetBlendTreeToMotion(animatorController, "blendTree_" + state.name, stateConfig.blendTree); } } if (stateConfig.script != null) { if (stateConfig.script == "RoleHide") { state.AddStateMachineBehaviour <RoleHideStateMachineBehaviour>(); } } } // 添加额外的跳转 add by TangJian 2018/04/17 20:20:52 foreach (var transitionConfig in animatorControllerConfig.transitions) { if (transitionConfig.sourceAnimName == "Any") { foreach (var stateConfig in animatorControllerConfig.states) { var fromState = animatorStateMachine.GetState(stateConfig.name); var toState = animatorStateMachine.GetState(transitionConfig.destinationAnimName); if (fromState != null && toState != null) { fromState.AddTransitionEx(toState, transitionConfig); } } } else { var fromState = animatorStateMachine.GetState(transitionConfig.sourceAnimName); var toState = animatorStateMachine.GetState(transitionConfig.destinationAnimName); if (fromState != null && toState != null) { fromState.AddTransitionEx(toState, transitionConfig); } } } // 添加状态的跳转 add by TangJian 2018/04/17 20:11:08 foreach (var stateConfig in animatorControllerConfig.states) { // 添加状态的跳转 add by TangJian 2018/04/17 20:11:39 foreach (var transitionConfig in stateConfig.transitions) { var fromState = animatorStateMachine.GetState(stateConfig.name); var toState = animatorStateMachine.GetState(transitionConfig.destinationAnimName); if (fromState != null && toState != null) { fromState.AddTransitionEx(toState, transitionConfig); } } } // 添加脚本 add by TangJian 2018/04/17 21:16:02 foreach (var stateConfig in animatorControllerConfig.states) { var state = animatorStateMachine.GetState(stateConfig.name); RoleBehaviour roleBehaviour = state.AddStateMachineBehaviour <RoleBehaviour>(); // 限制状态可以执行到的时间 add by TangJian 2019/5/14 10:35 roleBehaviour.StateDuration = stateConfig.duration; if (state.motion != null) { if (state.motion is UnityEditor.Animations.BlendTree) { var blendTree = state.motion as UnityEditor.Animations.BlendTree; if (blendTree.children.Length != 0) { FrameEventBehaviour frameEventBehaviour = state.AddStateMachineBehaviour <FrameEventBehaviour>(); Motion motion = blendTree.children[0].motion; if (motion != null) { frameEventBehaviour.animName = motion.name; frameEventBehaviour.Parameter = blendTree.blendParameter; frameEventBehaviour.namelist.Clear(); frameEventBehaviour.beginTime = stateConfig.beginTime; frameEventBehaviour.endTime = stateConfig.endTime; for (int i = 0; i < blendTree.children.Length; i++) { UnityEditor.Animations.ChildMotion child = blendTree.children[i]; Motion childmotion = blendTree.children[i].motion; if (childmotion != null && string.IsNullOrWhiteSpace(childmotion.name) == false) { // frameEventBehaviour.namelist.Insert(i, childmotion.name); frameEventBehaviour.floatList.Insert(i, child.threshold); } } } } } else { FrameEventBehaviour frameEventBehaviour = state.AddStateMachineBehaviour <FrameEventBehaviour>(); Motion motion = state.motion; frameEventBehaviour.animName = motion.name; if (motion is AnimationClip animationClip) { frameEventBehaviour.beginTime = stateConfig.beginTime / animationClip.length; frameEventBehaviour.endTime = stateConfig.endTime / animationClip.length; } } } state.AddStateMachineBehaviour <HumanMoveXYByAnimBehaviour>(); if (state.tag == "attack") { state.AddStateMachineBehaviour <HumanMainHandAttackBehaviour>(); } if (state.name == "jump_1") { state.AddStateMachineBehaviour <RolePreJumpBehaviour>(); } if (state.name == "JumpPre") { state.AddStateMachineBehaviour <RolePreJumpBehaviour>(); } if (state.tag == "hurtHold") { state.AddStateMachineBehaviour <RoleHurtHoldStateMachineBehaviour>(); } if (stateConfig.behaviours != null) { foreach (var item in stateConfig.behaviours) { MethodDelegate <bool> func = CSScript.CreateFunc <bool>( @" using Tang; bool Sum(UnityEditor.Animations.AnimatorState state) { state.AddStateMachineBehaviour<" + item + @" > (); return true; }"); func(state); } } } // 记录未使用的状态 add by TangJian 2018/04/19 14:36:28 List <UnityEditor.Animations.AnimatorState> unuseAnimatorStates = new List <UnityEditor.Animations.AnimatorState>(); foreach (var state in animatorStateMachine.states) { int index = animatorControllerConfig.states.FindIndex((AnimatorState a) => { return(a.name == state.state.name); }); if (index < 0) { unuseAnimatorStates.Add(state.state); } } // 移除未使用的状态 add by TangJian 2018/04/19 14:38:34 foreach (var state in unuseAnimatorStates) { animatorStateMachine.RemoveState(state); } // 整理状态机位置 add by TangJian 2018/04/19 14:40:00 ChildAnimatorState[] childAnimatorStates = animatorStateMachine.states; int cols = 3; for (int i = 0; i < childAnimatorStates.Length; i++) { float x = (i % cols) * 204; float y = (i / cols) * 50; childAnimatorStates[i].position = new Vector3(x, y, 0); } animatorStateMachine.states = childAnimatorStates; // 保存生成的状态机 add by TangJian 2019/1/30 17:21 EditorUtility.SetDirty(animatorController); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } SupportEventDispatch(); }