/// <summary> /// Liikuttaa hahmoa. /// </summary> /// <param name="horizontalVelocity">Nopeus vaakasuunnassa.</param> public void Walk(double horizontalVelocity) { if (horizontalVelocity > 0 && TurnsWhenWalking) { Turn(Direction.Right); } else if (horizontalVelocity < 0 && TurnsWhenWalking) { Turn(Direction.Left); } if (CanWalk(horizontalVelocity * lastDt)) { this.Velocity = new Vector(horizontalVelocity / 2, this.Velocity.Y); this.X += horizontalVelocity * lastDt; // Hahmo törmäsi tasaisen pinnan reunoihin farseerilla, // clipataan niiden sisälle jolloin moottori "korjaa" sijaintia hieman ylöspäin. } if (state == PlatformCharacterState.Idle || WalkOnAir) { state = PlatformCharacterState.Walking; SetAnimation(AnimWalk); } isWalking = true; }
private void AdjustPosition() { collisionHelpers[0].Object.Position = this.Position - new Vector(-Width / 4, Height * 1 / 8); collisionHelpers[1].Object.Position = this.Position - new Vector(0, Height * 1 / 8); collisionHelpers[2].Object.Position = this.Position - new Vector(Width / 4, Height * 1 / 8); if (state == PlatformCharacterState.Jumping) { return; } PhysicsObject platform = FindPlatform(); if (platform != null && IsStandingOn(platform, lowTolerance)) { this.Y = GetPlatformTopY(platform) + this.Height / 2; if (lastPlatformPosition.HasValue) { this.X += platform.X - lastPlatformPosition.Value.X; } lastPlatformPosition = platform.Position; lastPlatform = platform; } else { IgnoresGravity = false; lastPlatformPosition = null; state = PlatformCharacterState.Falling; SetAnimation(AnimFall, LoopFallAnim); } }
private void OnColliding(PhysicsObject collisionHelperObject, PhysicsObject target, Vector normal) { if (target.IgnoresCollisionResponse) { return; } double dot = Vector.DotProduct(normal, GravityNormal); if (Math.Abs(dot) < 0.5) { return; } if (Platform != null && Platform != target) { return; } if (Platform == null) { Platform = target; } collisionDetected = true; noCollisionCount = 0; PlatformNormal = normal; if (state == PlatformCharacterState.Falling) { state = PlatformCharacterState.Idle; } }
public override void Update(Time time) { if (Platform != null && !collisionDetected) { if (++noCollisionCount > PlatformTolerance) { Platform = null; if (state != PlatformCharacterState.Jumping) { state = PlatformCharacterState.Falling; SetAnimation(AnimFall, LoopFallAnim); } } } collisionDetected = false; if (walkSteps > 0) { // Walking double impulse = Mass * Acceleration * time.SinceLastUpdate.TotalSeconds; Vector unitX = gravityMagnitude > 0 ? gravityNormal.LeftNormal : Vector.UnitX; if (_curDirection == Direction.Left && -Velocity.X < MaxVelocity) { this.Push(Mass * Acceleration * -unitX); } else if (_curDirection == Direction.Right && Velocity.X < MaxVelocity) { this.Push(Mass * Acceleration * unitX); } walkSteps--; if (state == PlatformCharacterState.Idle) { SetAnimation(AnimWalk); } } else { // Not walking if (state == PlatformCharacterState.Idle) { SetAnimation(AnimIdle); } /*switch ( state ) * { * case PlatformCharacterState.Idle: SetAnimation( AnimIdle ); break; * case PlatformCharacterState.Falling: SetAnimation( AnimFall ); break; * case PlatformCharacterState.Jumping: SetAnimation( AnimJump ); break; * }*/ } base.Update(time); }
private static Color GetStateColor(PlatformCharacterState state) { return(state switch { PlatformCharacterState.Falling => Color.Red, PlatformCharacterState.Jumping => Color.Yellow, PlatformCharacterState.Walking => Color.Blue, _ => Color.White });
private void OnCollision(PhysicsObject collisionHelperObject, PhysicsObject target) { // Velocity <= 0 condition is here to allow jumping through a platform without stopping if (IsPlatform(target) && IsStandingOn(target, highTolerance) && Velocity.Y <= 0) { IgnoresGravity = true; StopVertical(); SetAnimation(AnimIdle); state = PlatformCharacterState.Idle; } }
private Color GetStateColor(PlatformCharacterState state) { switch (state) { case PlatformCharacterState.Falling: return(Color.Red); case PlatformCharacterState.Jumping: return(Color.Yellow); default: return(Color.White); } }
/// <summary> /// Hyppää vaikka olio ei olisikaan toisen päällä. /// </summary> /// <param name="speed">Lähtönopeus maasta.</param> public void ForceJump(double speed) { state = PlatformCharacterState.Jumping; IgnoresGravity = false; Hit(new Vector(0, speed * Mass)); SetAnimation(AnimJump, LoopJumpAnim); Timer t = new Timer(); t.Interval = 0.01; t.Timeout += delegate { if (this.Velocity.Y < 0) { t.Stop(); state = PlatformCharacterState.Falling; SetAnimation(AnimFall, LoopFallAnim); } }; t.Start(); }
/// <summary> /// Hyppää vaikka olio ei olisikaan toisen päällä. /// </summary> /// <param name="speed">Lähtönopeus maasta.</param> public void ForceJump(double speed) { IgnoresGravity = false; this.Hit(Mass * speed * -GravityNormal); Platform = null; state = PlatformCharacterState.Jumping; SetAnimation(AnimJump, LoopJumpAnim); Timer t = new Timer(); t.Interval = 0.01; t.Timeout += delegate { if (this.Velocity.Y < 0) { t.Stop(); state = PlatformCharacterState.Falling; SetAnimation(AnimFall, LoopFallAnim); } }; t.Start(); }
/// <summary> /// Resetoi hahmon tilan ja pysäyttää animaation /// </summary> public void Reset() { state = PlatformCharacterState.Idle; customAnimPlaying = false; }