private ParticlePopulation(JumpPhase jumpPhase, double spawnsPerSecond, double zSpeedPerSecond, Color color) { JumpPhase = jumpPhase; SpawnsPerSecond = spawnsPerSecond; ZSpeedPerSecond = zSpeedPerSecond; Color = color; }
void FixedUpdate() { float gravity = (-2 * maxJumpHeight) / (maxJumpTimeToApex * maxJumpTimeToApex); if (inverted) { gravity *= -1; } if (jumpPhase == JumpPhase.TerminatedRising) { gravity *= 3; } float jumpVelocity = (2 * maxJumpHeight) / maxJumpTimeToApex; rigidBody.AddForce(new Vector2(0, gravity) - Physics2D.gravity); if (jumpPhase == JumpPhase.Grounded) { airTime = 0.0f; } else { airTime += Time.fixedDeltaTime; } if (jumpPhase == JumpPhase.PreJump) { rigidBody.velocity = new Vector2(rigidBody.velocity.x, inverted ? -jumpVelocity : jumpVelocity); jumpPhase = JumpPhase.Rising; } if (jumpPhase == JumpPhase.Rising) { bool letgo = Input.touchSupported ? Input.touchCount == 0 : !Input.GetButton("Jump"); if (airTime >= minJumpTime && letgo) { jumpPhase = JumpPhase.TerminatedRising; } } if (inverted ? rigidBody.velocity.y > 0 : rigidBody.velocity.y < 0) { jumpPhase = JumpPhase.Falling; } if (jumpPhase == JumpPhase.Falling) { if (groundCheck.GetComponent <Collider2D>().IsTouchingLayers(groundMask)) { SpawnPoof(); jumpPhase = JumpPhase.Grounded; } } }
// Update is called once per frame void Update() { if (jumpPhase == JumpPhase.Grounded && (Input.GetButtonDown("Jump") || rebound)) { rebound = false; jumpPhase = JumpPhase.PreJump; } if (jumpPhase == JumpPhase.Falling && Input.GetButtonDown("Jump")) { rebound = true; } }
void FixedUpdate() { float gravity = (-2 * maxJumpHeight) / (maxJumpTimeToApex * maxJumpTimeToApex); if (jumpPhase == JumpPhase.TerminatedRising) { gravity *= 3; } float jumpVelocity = (2 * maxJumpHeight) / maxJumpTimeToApex; rigidBody.AddForce(new Vector2(0, gravity)); if (jumpPhase == JumpPhase.Grounded) { airTime = 0.0f; } else { airTime += Time.fixedDeltaTime; } if (jumpPhase == JumpPhase.PreJump) { rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpVelocity); jumpPhase = JumpPhase.Rising; } if (jumpPhase == JumpPhase.Rising) { if (airTime >= minJumpTime && !Input.GetButton("Jump")) { jumpPhase = JumpPhase.TerminatedRising; } } if (rigidBody.velocity.y < 0) { jumpPhase = JumpPhase.Falling; } if (jumpPhase == JumpPhase.Falling) { if (Physics2D.OverlapArea(groundCheckTopLeft.position, groundCheckBottomRight.position, groundMask)) { jumpPhase = JumpPhase.Grounded; } } }
// Update is called once per frame void Update() { bool jump = Input.GetButtonDown("Jump"); if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) { jump = true; } if (jumpPhase == JumpPhase.Grounded && (jump || rebound)) { rebound = false; secondJump = false; jumpPhase = JumpPhase.PreJump; AudioSource.PlayClipAtPoint(jumpSound, this.rigidBody.transform.position); } else if (canDoubleJump && !secondJump && jumpPhase != JumpPhase.Grounded && jump) { airTime = 0.0f; // Reset air-time. rebound = false; secondJump = true; jumpPhase = JumpPhase.PreJump; } if (jumpPhase == JumpPhase.Falling && jump) { rebound = true; } // Detects if player ever falls off screen. If so, kill him! if (transform.position.y < cam.transform.position.y - cameraHeight / 2) { this.GetComponent <HealthManager> ().Kill(); } }
protected override void OnRender(ChromaCanvas canvas) { if (Game.FsdJumpType == FsdJumpType.None) { StopAnimation(); _particleField.Clear(); _lastTick = null; return; } StartAnimation(); var deltaT = Now - (_lastTick ?? Now); _lastTick = Now; JumpPhase phase = GetJumpPhase(); foreach (var p in _population) { if (p.JumpPhase != phase) { continue; } _particleField.Add(p.SpawnsPerSecond * deltaT.TotalSeconds, p.Color, p.ZSpeedPerSecond); } _particleField.MoveAndDraw(deltaT, canvas); if (!Game.InWitchSpace) { return; } RenderHazardLevel(canvas); }
public void Refresh(Vector2 velocity, Vector2 input, Vector2 groundNormal, Vector2 groundPos, bool grounded, bool wallSliding, bool jumped, bool wallJumped, bool bounced, bool dead) { // change state if needed: bool wasIdle = this.state == State.Idle; bool wasRunning = this.state == State.Run; bool wasJumping = this.state == State.Jump || this.state == State.WallJump; bool wasWallJumping = false; if (this.state == State.Dead && !dead) { this.state = State.Idle; } string runAnimName = "Run"; switch (this.state) { case State.Idle: { if (jumped || (!grounded && Mathf.Abs(velocity.y) > this.fallSpeedThreshold)) { this.state = State.Jump; } else { if (Mathf.Abs(input.x) > 0.4f) { this.state = State.Run; } } } break; case State.Jump: { if (grounded) { if (Mathf.Abs(input.x) > 0.4f) { this.state = State.Run; } else { this.state = State.Idle; } } else if (wallSliding && velocity.y < 0) { this.state = State.WallSlide; } } break; case State.Run: { if (jumped || (!grounded && Mathf.Abs(velocity.y) > this.fallSpeedThreshold)) { this.state = State.Jump; } else if (wallSliding && velocity.y < 0) { this.state = State.WallSlide; } else if (Mathf.Abs(input.x) <= 0.4f) { this.state = State.Idle; } } break; case State.WallSlide: { if (wallJumped) { this.state = State.WallJump; } else { if (!wallSliding) { this.state = State.Jump; // falling down } } } break; case State.WallJump: { wasWallJumping = true; this.state = State.Jump; } break; } if (dead && this.state != State.Dead) { this.state = State.Dead; this.bounceParticles.Stop(); } // state logic: switch (this.state) { case State.Idle: { if (!wasIdle) { if (wasRunning) { this.idleAnimName = "IdleSlowdown"; PlayDust(groundPos, groundNormal, this.dustStopPrefab); } else if (wasJumping) { this.idleAnimName = "JumpLand"; Landed(groundPos, groundNormal); } else { this.idleAnimName = "Idle"; } } else { if (this.idleAnimName == "IdleCapeWave") { if (this.frame == 0) { this.idleAnimName = "Idle"; } } else if (this.idleAnimName != "Idle") { if (this.finishedSingleShot) { this.idleAnimName = "Idle"; } } else { if (this.frame == 0 && Random.value < 0.02f) { this.idleAnimName = "IdleCapeWave"; } } } } break; case State.Jump: { if ((!wasJumping && jumped) || wallJumped) { SoundManager.instance.PlayPlayerJumpSfx(); } if (Mathf.Abs(velocity.y) < this.jumpMidPointSpeedThreshold) { this.jumpPhase = JumpPhase.MidPoint; } else if (velocity.y > 0) { this.jumpPhase = JumpPhase.Up; } else if (velocity.y > -this.jumpDownSlowSpeedThreshold) { this.jumpPhase = JumpPhase.DownSlow; } else { this.jumpPhase = JumpPhase.DownFast; } } break; case State.Run: { if (!wasRunning) { if (wasJumping) { Landed(groundPos, groundNormal); } } if (Time.time - this.lastStepSfxTime > this.stepPeriod) { SoundManager.instance.PlayPlayerStepSfx(); this.lastStepSfxTime = Time.time + Random.value * this.stepPeriodRand; } if (Time.time - this.lastStepDustTime > this.dustStepPeriod) { PlayDust(groundPos, groundNormal, this.dustRunPrefab); this.lastStepDustTime = Time.time + Random.value * this.dustStepPeriodRand; } } break; case State.WallSlide: { } break; case State.WallJump: { if (wallJumped) { SoundManager.instance.PlayPlayerJumpSfx(); } } break; } // set active animation according to state: switch (this.state) { case State.Idle: SetActive(this.idleAnimName); break; case State.Run: SetActive(runAnimName); break; case State.WallSlide: SetActive("Wallslide1"); break; case State.WallJump: SetActive("Walljump"); break; case State.Jump: switch (this.jumpPhase) { case JumpPhase.Up: SetActive("JumpUp", true, !wasWallJumping); // don't reset timer in case we're coming from wall jump break; case JumpPhase.MidPoint: SetActive("JumpMidPoint"); break; case JumpPhase.DownSlow: SetActive("JumpDownSlow"); break; case JumpPhase.DownFast: SetActive("JumpDownFast"); break; } break; case State.Dead: SetActive("Death"); break; } // misc: if (bounced) { this.bounceParticles.Play(); } }
public void Refresh(Vector2 velocity, bool grounded, bool jumped, bool prepareForJump, bool landed, bool dead) { // change state: switch (this.state) { case State.Ground: { if (jumped || (!grounded && Mathf.Abs(velocity.y) > this.fallSpeedThreshold)) { if (jumped) { SoundManager.instance.PlayAtPosition(SoundId.BouncyEnemyJump, this.transform.position); } this.state = State.Jump; } else if (dead) { SoundManager.instance.PlayAtPosition(SoundId.BouncyEnemyDeath, this.transform.position); this.state = State.Dead; } else if (prepareForJump) { this.state = State.JumpPrepare; } } break; case State.JumpPrepare: { if (jumped || (!grounded && Mathf.Abs(velocity.y) > this.fallSpeedThreshold)) { if (jumped) { SoundManager.instance.PlayAtPosition(SoundId.BouncyEnemyJump, this.transform.position); } this.state = State.Jump; } else if (dead) { SoundManager.instance.PlayAtPosition(SoundId.BouncyEnemyDeath, this.transform.position); this.state = State.Dead; } else if (!prepareForJump) { this.state = State.Ground; } } break; case State.Jump: { if (landed || grounded) { SoundManager.instance.PlayAtPosition(SoundId.BouncyEnemyLand, this.transform.position); this.state = State.JumpLand; } } break; case State.JumpLand: { if (jumped || (!grounded && Mathf.Abs(velocity.y) > this.fallSpeedThreshold)) { if (jumped) { SoundManager.instance.PlayAtPosition(SoundId.BouncyEnemyJump, this.transform.position); } this.state = State.Jump; } else if (!landed) { this.state = State.Ground; } } break; } // state logic: if (this.state == State.JumpPrepare) { this.transform.localPosition = this.airLocalPos; } else { this.transform.localPosition = this.normalLocalPos; } switch (this.state) { case State.Jump: { if (Mathf.Abs(velocity.y) < this.jumpMidPointSpeedThreshold) { this.jumpPhase = JumpPhase.MidPoint; } else if (velocity.y > 0) { this.jumpPhase = JumpPhase.Up; } else { this.jumpPhase = JumpPhase.Down; } } break; case State.Dead: { if (this.finishedSingleShot) { this.enemy.FullDeath(); } } break; } // set animation according to state: switch (this.state) { case State.Ground: { SetActive("Ground"); } break; case State.JumpPrepare: { SetActive("JumpKick"); } break; case State.Jump: { switch (this.jumpPhase) { case JumpPhase.Up: SetActive("JumpUp"); break; case JumpPhase.MidPoint: SetActive("JumpMid"); break; case JumpPhase.Down: SetActive("JumpDown"); break; } } break; case State.JumpLand: { SetActive("JumpLand"); } break; case State.Dead: { SetActive("Death"); } break; } }