/// <summary> /// Starts the safe coroutine. /// </summary> /// <returns>The safe coroutine.</returns> /// <param name="a_MonoBehaviour">A mono behaviour.</param> /// <param name="a_SafeCoroutine">A safe coroutine.</param> public static SafeCoroutine StartSafeCoroutine(this MonoBehaviour a_MonoBehaviour, IEnumerator a_SafeCoroutine) { SafeCoroutine l_Result = new SafeCoroutine(a_SafeCoroutine, a_MonoBehaviour.gameObject); a_MonoBehaviour.StartCoroutine(l_Result.WrappedCoroutine); return(l_Result); }
public void TakeDamage(float damage, AttackEffect[] attackEffects) { if (enemyState != EnemyStates.Dead) { behaviourCoroutine = this.StartSafeCoroutine(Damaged(damage, attackEffects)); } }
public void Idle() { float animationSpeed = .1f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(idleSprites, animationSpeed, true)); }
public void Dead() { float animationSpeed = .025f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(deadSprites, animationSpeed)); }
public IEnumerator MoveTile(Tile tile, uint row, uint col) { if (tile.IsMoving) { return(null); } var dst_tile = GetTile(row, col); if (tile == dst_tile) { return(null); } tile.IsMoving = true; if (dst_tile.ValidateMove(tile)) { var routine = new SafeCoroutine(dst_tile.BeginTrigger(tile)); yield return(routine); var result = (bool)routine.Result; if (result == true) { routine = new SafeCoroutine(tile.MoveTo(row, col)); yield return(routine); } } tile.IsMoving = false; return(null); }
public void Move() { float animationSpeed = .025f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(movementSprites, animationSpeed, true)); }
void KillCoroutine(SafeCoroutine target) { if (target != null && target.IsRunning) { target.Stop(); } }
public void Thrown() { float animationSpeed = .25f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(thrownSprites, animationSpeed)); }
void Start() { player = GameObject.Find("PlayerCharacter").GetComponent <PlayerController>(); enemyState = EnemyStates.Idle; animateEnemy = GetComponentInChildren <AnimateEnemy>(); behaviourCoroutine = this.StartSafeCoroutine(Primer()); }
public void StopSafeCoroutine(SafeCoroutine c) { if (c == null) { return; } mCoroutines.Remove(c); }
IEnumerator OuterExampleCoroutine() { m_InnerCoroutine = Controller.StartSafeCoroutine(InnerExampleCoroutine()); yield return(m_InnerCoroutine); while (finishOuterCoroutine) { yield return(null); } }
/// <summary> /// Starts the safe coroutine. /// </summary> /// <returns>The safe coroutine.</returns> /// <param name="a_MonoBehaviour">A mono behaviour.</param> /// <param name="a_SafeCoroutine">A safe coroutine.</param> /// <typeparam name="TResult">The result type parameter.</typeparam> public static SafeCoroutine <TResult> StartSafeCoroutine <TResult> (this MonoBehaviour a_MonoBehaviour, IEnumerator a_SafeCoroutine) { SafeCoroutine <TResult> l_Result; try { l_Result = new SafeCoroutine <TResult> (a_SafeCoroutine, a_MonoBehaviour.gameObject); } catch { throw; } a_MonoBehaviour.StartCoroutine(l_Result.WrappedCoroutine); return(l_Result); }
IEnumerator WaitForAttackEffects(AttackEffect[] attackEffects) { SafeCoroutine[] coroutines = new SafeCoroutine[attackEffects.Length]; for (int i = 0; i < attackEffects.Length; i++) { coroutines[i] = attackEffects[i].InvokeEffect(gameObject); } while (coroutines.Any(x => x.IsRunning)) { yield return(null); } }
private IEnumerator WrappingCoroutine(IEnumerator a_Coroutine) { State = SafeCoroutineState.Running; while (true) { if (State == SafeCoroutineState.Paused) { yield return(null); } else { if (State == SafeCoroutineState.Stopped) { m_WrappedCoroutine = null; yield break; } object l_Yield = a_Coroutine.Current; SafeCoroutine l_EncapsulatedSafeCoroutine = l_Yield as SafeCoroutine; bool l_HasEncapsulatedSafeCoroutine = (l_EncapsulatedSafeCoroutine != null && l_EncapsulatedSafeCoroutine.m_WrappedCoroutine != null); if (!l_HasEncapsulatedSafeCoroutine) { try { if (!a_Coroutine.MoveNext()) { State = SafeCoroutineState.Finished; m_WrappedCoroutine = null; yield break; } } catch (Exception l_Exception) { State = SafeCoroutineState.ThrewException; m_Exception = l_Exception; Debug.LogError("SafeCoroutine threw exception:\n" + m_Exception.Message + "\nWith stack trace:\n" + m_Exception.StackTrace, invokingGameObject); m_WrappedCoroutine = null; yield break; } yield return(a_Coroutine.Current); } else { yield return(null); } } } }
private IEnumerator WrappingCoroutine(IEnumerator a_Coroutine) { State = SafeCoroutineState.Running; while (true) { if (State == SafeCoroutineState.Paused) { yield return(null); } else { if (State == SafeCoroutineState.Stopped) { m_WrappedCoroutine = null; yield break; } object l_Yield = a_Coroutine.Current; SafeCoroutine l_EncapsulatedSafeCoroutine = l_Yield as SafeCoroutine; bool l_HasEncapsulatedSafeCoroutine = (l_EncapsulatedSafeCoroutine != null && l_EncapsulatedSafeCoroutine.m_WrappedCoroutine != null); if (!l_HasEncapsulatedSafeCoroutine) { try { if (!a_Coroutine.MoveNext()) { State = SafeCoroutineState.Finished; m_WrappedCoroutine = null; yield break; } } catch (Exception l_Exception) { State = SafeCoroutineState.ThrewException; m_Exception = l_Exception; m_WrappedCoroutine = null; if (!CatchExceptions) { throw; } yield break; } yield return(a_Coroutine.Current); } else { yield return(null); } } } }
private void ResumeChildCoroutines() { IEnumerator l_Enumerator = m_WrappedCoroutine; while (l_Enumerator != null) { object l_Yield = l_Enumerator.Current; if (l_Yield != null) { if (l_Yield is IEnumerator) { l_Enumerator = l_Yield as IEnumerator; } else if (l_Yield is SafeCoroutine) { SafeCoroutine l_SafeCoroutine = l_Yield as SafeCoroutine; if (l_SafeCoroutine.IsPaused) { l_SafeCoroutine.m_IsParentPaused = false; if (l_SafeCoroutine.IsSelfPaused) { l_Enumerator = null; } else { l_SafeCoroutine.State = SafeCoroutineState.Running; l_Enumerator = l_SafeCoroutine.m_WrappedCoroutine; } } else { l_Enumerator = null; } } else { // Stop the loop. l_Enumerator = null; } } else { // Stop the loop. l_Enumerator = null; } } }
IEnumerator FlickerLights() { while (true) { if (lightFlickerA == null || !lightFlickerA.IsRunning) { flickerA = GetRandomLight(flickerA); lightFlickerA = this.StartSafeCoroutine(FlickerLight(flickerA, Random.Range(2, 5))); } if (lightFlickerB == null || !lightFlickerB.IsRunning) { flickerB = GetRandomLight(flickerB); lightFlickerB = this.StartSafeCoroutine(FlickerLight(flickerB, Random.Range(2, 5))); } yield return(null); } }
void Awake() { baseAttacks = new Dictionary <AttackInputType, PlayerAttack> { { AttackInputType.Attack1, attack1 }, { AttackInputType.Attack2, attack2 } }; behaviourCoroutine = this.StartSafeCoroutine(Primer()); if (handleInputFlash == null) { handleInputFlash = GameObject.Find("PlayerCharacter").GetComponent <HandlePlayerInputFlash>(); } InitializeAttackDictionary(); }
private void PauseChildCoroutines() { IEnumerator l_Enumerator = m_WrappedCoroutine; while (l_Enumerator != null) { object l_Yield = l_Enumerator.Current; if (l_Yield != null) { if (l_Yield is IEnumerator) { l_Enumerator = l_Yield as IEnumerator; } else if (l_Yield is SafeCoroutine) { SafeCoroutine l_SafeCoroutine = l_Yield as SafeCoroutine; if (l_SafeCoroutine.State == SafeCoroutineState.Running) { l_SafeCoroutine.m_IsParentPaused = true; l_SafeCoroutine.State = SafeCoroutineState.Paused; l_Enumerator = l_SafeCoroutine.m_WrappedCoroutine; } else if (l_SafeCoroutine.State == SafeCoroutineState.Paused) { // Paused means in that context self paused. l_SafeCoroutine.m_IsParentPaused = true; l_Enumerator = null; } else { l_Enumerator = null; } } else { // Stop the loop. l_Enumerator = null; } } else { // Stop the loop. l_Enumerator = null; } } }
public SafeCoroutine StartSafeCoroutine(IEnumerator iterator) { if (iterator == null) { return(null); } SafeCoroutine c = new SafeCoroutine(iterator); if (mIsUpdating) { mSalvageCoroutines.Add(c); } else { mCoroutines.Add(c); } return(c); }
static void CoroutineGUI(string a_Name, SafeCoroutine a_SafeCoroutine) { GUILayout.BeginHorizontal(); GUI.enabled = a_SafeCoroutine.IsPause; if (GUILayout.Button("Resume " + a_Name)) { a_SafeCoroutine.Resume(); } GUI.enabled = a_SafeCoroutine.CanPause; if (GUILayout.Button("Pause " + a_Name)) { a_SafeCoroutine.Pause(); } GUI.enabled = a_SafeCoroutine.IsRunning || a_SafeCoroutine.IsPause; if (GUILayout.Button("Stop " + a_Name)) { a_SafeCoroutine.Stop(); } GUILayout.EndHorizontal(); }
private void StopChildRoutines() { IEnumerator l_Enumerator = m_WrappedCoroutine; while (l_Enumerator != null) { object l_Yield = l_Enumerator.Current; if (l_Yield != null) { if (l_Yield is IEnumerator) { l_Enumerator = l_Yield as IEnumerator; } else if (l_Yield is SafeCoroutine) { SafeCoroutine l_SafeCoroutine = l_Yield as SafeCoroutine; if (l_SafeCoroutine.State == SafeCoroutineState.Running || l_SafeCoroutine.State == SafeCoroutineState.Paused) { l_SafeCoroutine.Stop(); } // The safe coroutine takes care of further child coroutines. l_Enumerator = null; } else { // Stop the loop. l_Enumerator = null; } } else { // Stop the loop. l_Enumerator = null; } } }
public void Attack(Sprite[] attackSprites) { CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(attackSprites)); }
public void TakeDamage(float damage, AttackEffect[] attackEffects) { if (enemyState != EnemyStates.Dead){ behaviourCoroutine = this.StartSafeCoroutine(Damaged(damage, attackEffects)); } }
IEnumerator WaitForAttackEffects(AttackEffect[] attackEffects) { SafeCoroutine[] coroutines = new SafeCoroutine[attackEffects.Length]; for (int i = 0; i < attackEffects.Length; i++){ coroutines[i] = attackEffects[i].InvokeEffect(gameObject); } while (coroutines.Any(x => x.IsRunning)){ yield return null; } }
void Start() { player = GameObject.Find("PlayerCharacter").GetComponent<PlayerController>(); enemyState = EnemyStates.Idle; animateEnemy = GetComponentInChildren<AnimateEnemy>(); behaviourCoroutine = this.StartSafeCoroutine(Primer()); }
void HandleInput() { if (Input.GetKeyDown(KeyCode.A)){ turningRight = false; turningLeft = true; } else if (Input.GetKeyDown(KeyCode.D)){ turningLeft = false; turningRight = true; } if (Input.GetKeyDown(KeyCode.W)){ accelerating = true; } if (Input.GetKeyUp(KeyCode.A)){ turningLeft = false; } else if (Input.GetKeyUp(KeyCode.D)){ turningRight = false; } if (Input.GetKeyUp(KeyCode.W)){ if (turningRight || turningLeft){ if (inertiaForceCoroutine == null || !inertiaForceCoroutine.IsRunning){ inertiaForce = carRigidbody.velocity; inertiaForceCoroutine = this.StartSafeCoroutine(InertiaFalloff()); } } accelerating = false; } }
public void Dead() { CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(deadSprites)); }
private IEnumerator Wrap(CoroutineChainLink link) { SafeCoroutine safeCoroutine = new SafeCoroutine(); safeCoroutine.Coroutine = StartCoroutine(safeCoroutine.RunCoroutine(link.CoOnStart())); yield return safeCoroutine.Coroutine; if (safeCoroutine.Exception != null) { link.OnFailure(safeCoroutine.Exception); } }
void Start() { animationCoroutine = this.StartSafeCoroutine(Primer()); spriteRenderer = GetComponentInChildren<SpriteRenderer>(); }
public void Damage() { float animationSpeed = .025f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(damageSprites, animationSpeed)); }
public void HitGround() { float animationSpeed = .025f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(hitGroundSprites, animationSpeed)); }
void Awake() { baseAttacks = new Dictionary<AttackInputType, PlayerAttack> { { AttackInputType.Attack1, attack1 }, { AttackInputType.Attack2, attack2 } }; behaviourCoroutine = this.StartSafeCoroutine(Primer()); if (handleInputFlash == null){ handleInputFlash = GameObject.Find("PlayerCharacter").GetComponent<HandlePlayerInputFlash>(); } InitializeAttackDictionary(); }
public void Punch() { float animationSpeed = .025f; CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(punchSprites, animationSpeed)); }
public void HighlightTile(Texture2D tex){ targetRenderer.material.SetTexture("_MainTex", tex); hintDisplayCoroutine = this.StartSafeCoroutine(ShowHint()); }
public void Idle() { CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(idleSprites)); }
public void Damage() { CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(damageSprites)); }
public void Block() { CancelCurrentAnimation(); animationCoroutine = this.StartSafeCoroutine(Animate(blockSprites)); }
public void RotateCharacterToFaceRight() { KillRotationCoroutine(); rotationCoroutine = this.StartSafeCoroutine(AnimateFacingRight()); }
// Use this for initialization void Start() { // m_InnerCoroutine = Controller.StartSafeCoroutine(InnerExampleCoroutine()); m_OuterCoroutine = Controller.StartSafeCoroutine(OuterExampleCoroutine()); }
void Update() { inDestinationRange = InRangeOfDestinationPosition(); inAttackRange = InRangeForAttack(); if (enemyState != EnemyStates.Dead){ var pos = transform.localPosition; if (health <= 0 && enemyState != EnemyStates.Dead){ enemyState = EnemyStates.Dead; CancelBehaviourCoroutine(); this.StartSafeCoroutine(Dead()); } if (enemyState == EnemyStates.Moving){ // Check if close enough if (InRangeOfDestinationPosition() && InRangeForAttack()){ CancelBehaviourCoroutine(); enemyState = EnemyStates.Attacking; } else{ if (!moving){ CancelBehaviourCoroutine(); moving = true; behaviourCoroutine = this.StartSafeCoroutine(Move()); } // Move towards destination if (destinationPosition.y <= pos.y + .01f){ pos.y -= vMoveSpeed; } else if (destinationPosition.y > pos.y - .01f){ pos.y += vMoveSpeed; } if (Mathf.Abs(pos.y - destinationPosition.y) < .03){ pos.y = destinationPosition.y; } if (destinationPosition.x <= pos.x){ pos.x -= hMoveSpeed; } else if (destinationPosition.x > pos.x){ pos.x += hMoveSpeed; } } } else if (enemyState == EnemyStates.Attacking){ if (InRangeForAttack()){ if (!punching){ CancelBehaviourCoroutine(); punching = true; behaviourCoroutine = this.StartSafeCoroutine(Punch()); } } else{ enemyState = EnemyStates.Moving; } } else if (enemyState == EnemyStates.Damaged){ return; } else if (enemyState == EnemyStates.Idle){ if (!InRangeOfDestinationPosition()){ pos = Vector3.MoveTowards(transform.position, destinationPosition, .025f); } else{ if (!idle){ CancelBehaviourCoroutine(); idle = true; behaviourCoroutine = this.StartSafeCoroutine(Idle()); } } } else if (enemyState == EnemyStates.Dead){ } if (enemyState != EnemyStates.Dead){ transform.rotation = player.transform.position.x < transform.position.x ? Quaternion.Euler(new Vector3(0, 180, 0)) : Quaternion.Euler(Vector3.zero); } transform.localPosition = Vector3.Lerp(transform.localPosition, pos, Time.deltaTime * generalMoveSpeed); } }
public void GoIdle() { enemyState = EnemyStates.Idle; behaviourCoroutine = this.StartSafeCoroutine(Idle()); }
void StartGlitching(){ glitching = true; glitchCoroutine = this.StartSafeCoroutine(GlitchOut()); }
/// <summary> /// 是否完成了 /// 每帧调用 /// </summary> /// <param name="delta_time"></param> /// <returns>完成了返回true</returns> public bool IsComplete(float delta_time) { if (mIterator == null) { return(finish()); } if (IsPause) { return(false); } if (mIterator.Current == null) { if (!mIterator.MoveNext()) { return(finish()); } } else if ((mIterator.Current as SafeCoroutine) != null) { // 当前执行的是子协程 // 子协程不需要在这里更新,它会在CoroutineController里更新 var child = mIterator.Current as SafeCoroutine; if (mChildCoroutine == null) { // 第一次执行,设置父子关系 mChildCoroutine = child; child.mParentCoroutine = this; } else if (mChildCoroutine.IsFinish) { // 如果已经完成了,执行下一步 mChildCoroutine = null; if (!mIterator.MoveNext()) { return(finish()); } } } else if ((mIterator.Current as ISafeYieldInstruction) != null) { // 当前执行的是普通的yield指令 ISafeYieldInstruction yieldBase = mIterator.Current as ISafeYieldInstruction; // 更新指令 bool result = yieldBase.IsComplete(delta_time); if (result && !mIterator.MoveNext()) { return(finish()); } } else { if (!mIterator.MoveNext()) { // 已经没有下一步了,设置完成状态 mResult = mIterator.Current; return(finish()); } } return(false); }
void Start() { animationCoroutine = this.StartSafeCoroutine(Primer()); spriteRenderer = GetComponentInChildren <SpriteRenderer>(); }
public void LockMinAndMaxX(float targetX) { cameraScrollLocked = true; cameraMovementOverride = this.StartSafeCoroutine(CenterCameraOnFight(targetX)); }
void Start() { playerTransform = PlayerController.Instance.transform; shootingCoroutine = this.StartSafeCoroutine(ShootAtPlayer()); }
private IEnumerator Wrap(Func<IEnumerator> coroutine) { SafeCoroutine safeCoroutine = new SafeCoroutine(); IEnumerator i = null; try { i = coroutine(); } catch (Exception exception) { Handle(exception); } if (i != null) { safeCoroutine.Coroutine = StartCoroutine(safeCoroutine.RunCoroutine(i)); yield return safeCoroutine.Coroutine; } if (safeCoroutine.Exception != null) { Handle(safeCoroutine.Exception); } }