//a method that is called when the collector arrives at the target resource to collect protected override void OnInProgressEnabled(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio) { base.OnInProgressEnabled(reloadTime, activeAnimState, inProgressAudio); if (collectionObjectsDic.TryGetValue(target.GetResourceType().GetName(), out CollectionObject collectionObject)) { unit.SetAnimatorOverrideController(collectionObject.animatorOverrideController); //update the runtime animator controller } if (gameMgr.ResourceMgr.CanAutoCollect()) //if the collector can auto collect, no drop off required { CustomEvents.OnUnitStartCollecting(unit, target); //trigger custom event } else { if (dropOffStatus != DropOffStatus.goingBack) //only if the unit wasn't coming back after a drop off -> first time collecting { UpdateDropOffResources(target.ID, 0); //check if the unit needs to drop off resources CustomEvents.OnUnitStartCollecting(unit, target); //trigger custom event } //if the unit was coming back after a drop off, then that is done & unit is no longer dropping off resources if (dropOffStatus == DropOffStatus.goingBack) { CancelDropOff(); } } }
//a method to change the animator state public void SetAnimState(UnitAnimState newState) { if (LockAnimState == true || animator == null) //if our animation state is locked or there's no animator assigned then don't proceed. { return; } if ((newState == UnitAnimState.takingDamage && HealthComp.IsDamageAnimationEnabled() == false) || //if taking damage animation is disabled (HealthComp.IsDamageAnimationActive() && newState != UnitAnimState.dead)) //or if it's enabled and it's in progress { return; } currAnimatorState = newState; //update the current animator state animator.SetBool("TookDamage", currAnimatorState == UnitAnimState.takingDamage); animator.SetBool("IsIdle", currAnimatorState == UnitAnimState.idle); //stop the idle animation in case take damage animation is played since the take damage animation is broken by the idle anim if (currAnimatorState == UnitAnimState.takingDamage) //because we want to get back to the last anim state after the taking damage anim is done { return; } animator.SetBool("IsBuilding", currAnimatorState == UnitAnimState.building); animator.SetBool("IsCollecting", currAnimatorState == UnitAnimState.collecting); animator.SetBool("IsMoving", currAnimatorState == UnitAnimState.moving); animator.SetBool("IsAttacking", currAnimatorState == UnitAnimState.attacking); animator.SetBool("IsHealing", currAnimatorState == UnitAnimState.healing); animator.SetBool("IsConverting", currAnimatorState == UnitAnimState.converting); animator.SetBool("IsDead", currAnimatorState == UnitAnimState.dead); }
//update component if the collector has a target protected override bool OnActiveUpdate(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio, bool breakCondition = false, bool inProgressEnableCondition = true, bool inProgressCondition = true) { if (base.OnActiveUpdate( target.GetCollectOneUnitDuration(), UnitAnimState.collecting, target.GetResourceType().GetCollectionAudio(), target.IsEmpty(), //target resource must not be empty (gameMgr.ResourceMgr.CanAutoCollect() == true || (dropOffStatus == DropOffStatus.goingBack || dropOffStatus == DropOffStatus.inactive)), //auto collection must be enabled or the unit must currently not be dropping off resources dropOffStatus != DropOffStatus.active //in order to be able to collect resources, unit must not be dropping off resources ) == false) { return(false); } if (dropOffBuilding != null && dropOffStatus == DropOffStatus.active && unit.MovementComp.DestinationReached) //unit is currently dropping off resources while having a valid drop off building { DropOff(); inProgress = false; //unit is no longer collecting => needs to go back to the resource to collect if (currDropOffObject != null) //hide the collection object { currDropOffObject.SetActive(false); } } return(true); }
//update in case the unit has a target protected virtual bool OnActiveUpdate(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio, bool breakCondition = false, bool inProgressEnableCondition = true, bool inProgressCondition = true) { if (breakCondition) //if break condition is met then the unit can no longer be active { unit.MovementComp.Stop(); //stop the movement of the unit in case it was moving towards its target Stop(); //cancel the current job return(false); } if (unit.MovementComp.DestinationReached && inProgress == false && inProgressEnableCondition) //if the unit has reached its target and it hasn't started its job yet + the provided additional condition { OnInProgressEnabled(reloadTime, activeAnimState, inProgressAudio); } if (inProgress == true && inProgressCondition) //if the unit's job is currently in progress { if (timer > 0) //construction timer { timer -= Time.deltaTime; } else { OnInProgress(); timer = reloadTime; //reload timer } } return(true); }
/// <summary> /// 重置 /// </summary> public virtual void Reset() { mState = UnitAnimState.None; mNextAnim = string.Empty; mIndex = 0; mOffsetTime = 0; mAnim.Update(0); }
//update component if the builder has a target protected override bool OnActiveUpdate(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio, bool breakCondition = false, bool inProgressEnableCondition = true, bool inProgressCondition = true) { return(base.OnActiveUpdate( 1.0f, UnitAnimState.building, constructionAudio, target.HealthComp.CurrHealth >= target.HealthComp.MaxHealth)); }
/// <summary> /// 播放动画 /// </summary> /// <param name="name"></param> /// <param name="blendTime"></param> /// <param name="offsetTime"></param> private void PlayAnim(string name, int blendTime, float offsetTime = 0) { if (blendTime == 0) { mState = UnitAnimState.Play; mAnim.Play(name, 0, offsetTime); } else { mState = UnitAnimState.CossFade; float time = blendTime * 0.01f; mAnim.CrossFadeInFixedTime(name, time); } }
//update component if the converter has a target unit protected override bool OnActiveUpdate(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio, bool breakCondition = false, bool inProgressEnableCondition = true, bool inProgressCondition = true) { if (base.OnActiveUpdate( duration, UnitAnimState.converting, conversionAudio, target.FactionID == unit.FactionID || (Vector3.Distance(transform.position, target.transform.position) > maxDistance && inProgress == true) //if the converter and the target have the same faction or the target is outside the max allowed range for conversion -> cancel job ) == false) { return(false); } return(true); }
//called when the unit's job is enabled protected virtual void OnInProgressEnabled(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio) { unit.SetAnimState(activeAnimState); if (inProgressAudio.Length > 0) //if the unit has at least one in progress audio clip { AudioManager.Play(unit.AudioSourceComp, inProgressAudio[Random.Range(0, inProgressAudio.Length - 1)], true); //play a random one } if (inProgressObject != null) //show the in progress object { inProgressObject.SetActive(true); } timer = reloadTime; //start timer inProgress = true; //the unit's job is now in progress ToggleSourceTargetEffect(true); //enable the source and target effect objects }
//update component if the healer has a target unit protected override bool OnActiveUpdate(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio, bool breakCondition = false, bool inProgressEnableCondition = true, bool inProgressCondition = true) { if (base.OnActiveUpdate( 1.0f, UnitAnimState.healing, healingAudio, //if target has max health the healer and the target don't have the same faction or the target is outside the max allowed range for healing -> cancel job target.HealthComp.CurrHealth >= target.HealthComp.MaxHealth || target.FactionID != unit.FactionID || (Vector3.Distance(transform.position, target.transform.position) > maxDistance && inProgress == true) ) == false) { return(false); } return(true); }
/// <summary> /// 动画播放完成 /// </summary> private void AnimEndFun() { AnimatorStateInfo info = mAnim.GetCurrentAnimatorStateInfo(0); if (info.IsName(mEndAnim)) { Reset(); return; } if (info.normalizedTime > 1.0f) { mOffsetTime = 0; if (info.IsName(mAnimGroup[mIndex])) { if (mAnimGroup.Count > mIndex + 1) { mIndex++; Execute(); } else { if (!mIsLoop) { mState = UnitAnimState.None; if (!string.IsNullOrEmpty(mNextAnim)) { return; } Undo(); } else { Reset(); Execute(mIsLoop); } } } } }
//a method that is called when the converter arrives at the target unit to convert protected override void OnInProgressEnabled(float reloadTime, UnitAnimState activeAnimState, AudioClip[] inProgressAudio) { base.OnInProgressEnabled(reloadTime, activeAnimState, inProgressAudio); CustomEvents.OnUnitStartConverting(unit, target); //trigger custom event }