private void StartDash() { if (stamina <= 0.0 || playerEntity.State == PlayerState.Dizzy) { return; } playerEntity.SwitchToState(PlayerState.Dashing); dashTime = 0; Entity.GetComponent <AudioSourceComponent>().PlaySound("Audio/dashSwoosh"); var physicsComponent = playerEntity.GetComponent <PhysicsComponent>(); physicsComponent.ReplaceShape(new CircleShape(0.5f, 1.0f)); physicsComponent.Fixture.IsSensor = true; physicsComponent.Fixture.CollisionCategories = GameConstants.CollisionCategorySensor; physicsComponent.Fixture.CollidesWith = Category.All & ~GameConstants.CollisionCategorySensor; physicsComponent.Fixture.OnCollision += OnCollision; stamina -= dashStaminaCost; if (Entity.Body.LinearVelocity.LengthSquared() > 0.0000001f) { dashDirection = Vector2.Normalize(Entity.Body.LinearVelocity); } else { dashDirection = VectorExtensions.AngleToUnitVector(Entity.Body.Rotation); } }
public void Update(float elapsedSeconds, float totalSeconds) { Vector2 newDirection = barnPosition - Entity.Body.Position; newDirection.Normalize(); float desiredRotation = newDirection.UnitVectorToAngle(); float actualRotation = Entity.Body.Rotation; float angleDistance = desiredRotation - actualRotation; angleDistance = MathHelper.WrapAngle(angleDistance); if (angleDistance > 0) { Entity.Body.AngularVelocity = Math.Min(angleDistance / elapsedSeconds, 6); } else if (angleDistance < 0) { Entity.Body.AngularVelocity = Math.Max(angleDistance / elapsedSeconds, -6); } else { Entity.Body.AngularVelocity = 0; } if ((barnPosition - Entity.Body.Position).LengthSquared() < 0.5 * 0.5) { this.Entity.Die(); } Entity.Body.LinearVelocity = VectorExtensions.AngleToUnitVector(Entity.Body.Rotation); }
private void Move(float elapsedSeconds, float desiredSpeed) { Vector2 desiredVelocity = VectorExtensions.AngleToUnitVector(Entity.Body.Rotation); desiredVelocity *= desiredSpeed; float drag = 0.1f; Entity.Body.LinearVelocity = drag * desiredVelocity + (1.0f - drag) * Entity.Body.LinearVelocity; }
protected override void UpdateBehaviour(float elapsedSeconds, float totalSeconds) { if (obstacleSensor.SensedEntity != null && obstacleSensor.SensedEntity.Body.BodyType == FarseerPhysics.Dynamics.BodyType.Static) { float obstacleDistance = Vector2.Dot(Entity.Body.Position - obstacleSensor.Point, obstacleSensor.Normal); if (obstacleDistance < AvoidingDistance) { Vector2 newDirection = Vector2.Reflect(VectorExtensions.AngleToUnitVector(Entity.Body.Rotation), obstacleSensor.Normal); movement.Steer(newDirection, Weight); } } }
public void DebugDraw(DebugRenderer renderer, float elapsedSeconds, float totalSeconds) { Vector2 position = Entity.Body.Position; Vector2 lookPosition = position + VectorExtensions.AngleToUnitVector(Entity.Body.Rotation) * SensingDistance; var debugShapeColor = SensedEntity != null ? Color.Aqua : Color.FloralWhite; renderer.DebugDrawLine(position, lookPosition, color: debugShapeColor); if (SensedEntity != null) { Vector2 positionInWorldCoords = Point; renderer.DebugDrawPoint(positionInWorldCoords, debugShapeColor); renderer.DebugDrawLine(positionInWorldCoords, positionInWorldCoords + Normal, color: debugShapeColor); } }
private void Arrive(ref Vector2 desiredVelocity, float distance, float maxSpeed) { float slowingRadius = 1f; if (desiredVelocity.Length() > 0) { desiredVelocity = VectorExtensions.AngleToUnitVector(Entity.Body.Rotation); } if (distance < slowingRadius) { // Inside the slowing area desiredVelocity.Normalize(); desiredVelocity = desiredVelocity * maxSpeed * (distance / slowingRadius); } else { // Outside the slowing area. desiredVelocity.Normalize(); desiredVelocity = desiredVelocity * maxSpeed; } }
protected override void UpdateBehaviour(float elapsedSeconds, float totalSeconds) { // Switch to next state after some time if (timerForChange.Tick(elapsedSeconds)) { SwitchToOutputState(); } Body body = this.Entity.Body; var position = body.Position; var rotation = body.Rotation; Vector2 desiredPosition = position + VectorExtensions.AngleToUnitVector(rotation) * CircleCenterOffset; desiredPosition += VectorExtensions.AngleToUnitVector(RandomExt.GetRandomAngle()) * CircleRadius; Vector2 direction = (desiredPosition - position); movement.Speed = RandomExt.GetRandomFloat(WanderingSpeedMin, WanderingSpeedMax); movement.TurningSpeed = MaxTurningSpeed; movement.Steer(direction, Weight); }
public static RaycastResult Raycast(this World world, Body startBody, float sensingDistance) { return(RayCast(world, startBody.Position, startBody.Position + VectorExtensions.AngleToUnitVector(startBody.Rotation) * sensingDistance)); }
private bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact) { var other = (Entity)fixtureB.Body.UserData; if (playerEntity.State == PlayerState.Dashing) { var velocity = Entity.Body.LinearVelocity; if (velocity.LengthSquared() < 0.01f) { velocity = VectorExtensions.AngleToUnitVector(Entity.Body.Rotation); } Vector2 worldNormal; FixedArray2 <Vector2> worldPoints; contact.CalculateContactPoints(out worldNormal, out worldPoints); Vector2 collisionPoint = worldPoints[0]; var dirToOther = collisionPoint - Entity.Body.Position; dashCollisionPoint = collisionPoint; // if one of our vectors somehow got 0, cancel, as otherwise NaNs can crash farseer if (dirToOther.LengthSquared() < 0.01f) { return(true); } var dirToOtherNorm = dirToOther.Normalized(); dashCollisionDirection = dirToOther; var velocityNorm = velocity.Normalized(); // if the other is not in the direction of the dash, cancel if (velocityNorm.AngleBetween(dirToOtherNorm) > MathHelper.PiOver2 - 0.09f) { return(true); } (other as BoarEntity)?.Hit(Entity); (other as ChickenEntity)?.Hit(Entity); other.Body.ApplyLinearImpulse(dirToOtherNorm * 10.0f, other.Body.Position); if ((other as ChickenEntity) != null) { return(true); } var reflectedDir = Vector2.Reflect(Entity.Body.LinearVelocity, contact.Manifold.LocalNormal); if (reflectedDir.LengthSquared() > 0.1f) { Entity.Body.ApplyLinearImpulse((-reflectedDir).Normalized() * 10.0f, Entity.Body.Position); } // SHAKE THAT SCREEN!!! Entity.Game.Camera.Shake(TimeSpan.FromSeconds(0.5f), 20f); var randI = RandomExt.GetRandomInt(1, 4); Entity.GetComponent <AudioSourceComponent>().PlaySound($"Audio/Hit{randI}"); var otherPlayer = other as PlayerEntity; if (otherPlayer != null) { otherPlayer.GetComponent <AudioSourceComponent>().PlaySound("Audio/Dizzy"); otherPlayer.SwitchToState(PlayerState.Dizzy); // since the other player becomes dizzy, it should vibrate stronger Entity.Game.InputManager.SetRumble(otherPlayer.PlayerInfo, 1.0f, 0.7f, 0.6f); } if (other.Body.IsStatic) { ResetDash(); playerEntity.GetComponent <AudioSourceComponent>().PlaySound("Audio/Dizzy"); playerEntity.SwitchToState(PlayerState.Dizzy); Entity.Game.InputManager.SetRumble(playerEntity.PlayerInfo, 1.0f, 0.7f, 0.6f); } else { Entity.Game.InputManager.SetRumble(playerEntity.PlayerInfo, 0.75f, 0.25f, 0.3f); } } return(true); }
public void Update(float elapsedSeconds, float totalSeconds) { float currentMovementSpeed = GetCurrentSpeed(); Vector2 desiredVelocity; // Regenerate stamina if (stamina < 1.0f) { stamina += staminaRegenerationRate * elapsedSeconds; } // While dashing if (playerEntity.State == PlayerState.Dashing) { desiredVelocity = dashDirection * currentMovementSpeed; dashTime += elapsedSeconds; //Entity.Body.Mass = 2000; } else if (playerEntity.State == PlayerState.Dizzy) { dizzyTime += elapsedSeconds; var intendedSpeed = inputMovementDirection.Length(); var isStandingStill = intendedSpeed <= 0.001f; var perpendicularV = (isStandingStill) ? (VectorExtensions.AngleToUnitVector(Entity.Body.Rotation + (float)Math.PI / 2.0f)) : (new Vector2(inputMovementDirection.Y, -inputMovementDirection.X).Normalized()); var inDirectionV = new Vector2(perpendicularV.Y, -perpendicularV.X); // Add 'drunk' on movement on top of input float drunkNoise1 = (float)(0.5 * Math.Sin(3.4 + totalSeconds * 7.1f) + 0.8 * Math.Sin(totalSeconds * 5.1f) + 1.3 * Math.Sin(1.2f + totalSeconds * 3.3f)); float drunkNoise2 = (float)(0.3 * Math.Sin(2.1 + totalSeconds * 8.3f) + 0.5 * Math.Sin(totalSeconds * 4.7f) + 0.7 * Math.Sin(5.2f + totalSeconds * 2.5f)); // adjust the noise when standing still, as otherwise the player just spinns around like crazy float d1 = (isStandingStill) ? 0.1f * drunkNoise1 : drunkNoise1; float d2 = (isStandingStill) ? (drunkNoise2 + 1.5f) * 0.3f : drunkNoise2; desiredVelocity = (inputMovementDirection + inDirectionV * d2 + perpendicularV * d1).Normalized() * (intendedSpeed * 0.3f + 0.3f) * currentMovementSpeed; UpdateRotation(elapsedSeconds, desiredVelocity); } else { desiredVelocity = inputMovementDirection * currentMovementSpeed; UpdateRotation(elapsedSeconds, desiredVelocity); } // reset dash if (playerEntity.State == PlayerState.Dashing && !(dashTime < totalDashTime)) { ResetDash(); } // reset dizzy if (playerEntity.State == PlayerState.Dizzy && !(dizzyTime < totalDizzyTime)) { ResetDizzy(); } float drag = 0.2f; // interpolate from the current velocity to the desired velocity Entity.Body.LinearVelocity = drag * desiredVelocity + (1.0f - drag) * Entity.Body.LinearVelocity; // player animation if (playerEntity.State == PlayerState.Dashing) { animationComponent.SwitchTo("dashing"); } else if (playerEntity.State == PlayerState.Dizzy) { animationComponent.SwitchTo("dizzy"); } else { if (Entity.Body.LinearVelocity.LengthSquared() > 0.1f) { animationComponent.SwitchTo("walking"); } else { animationComponent.SwitchTo("standing"); } } }