public override void Process(Entity entity) { ThrustComponent tc = entity.GetComponent <ThrustComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); if (InputManager.Instance.isKeyReleased(Keys.Space)) { PositionComponent pc = entity.GetComponent <PositionComponent>(); InertiaComponent ic = entity.GetComponent <InertiaComponent>(); Entity laser = this.EntityWorld.CreateEntity(); laser.AddComponent(new ProjectileComponent(15, new TimeSpan(0, 0, 0, 0, 450))); laser.AddComponent(new PositionComponent(pc.Position)); laser.AddComponent(new RotationComponent(rc.Angle, 0f, false)); float radians = MathHelper.ToRadians(rc.Angle); laser.AddComponent(new InertiaComponent(Vector2.Multiply(Vector2.Normalize(new Vector2((float)Math.Sin(radians), -(float)Math.Cos(radians))), laser.GetComponent <ProjectileComponent>().MaxSpeed))); laser.AddComponent(new TextureComponent(new Coord(0, 0), "lasers", new Vector2(0, 0))); } }
public override void Process(Entity entity) { ThrustComponent tc = entity.GetComponent <ThrustComponent>(); if (tc.ForwardThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); double angleInDegrees = MathHelper.ToRadians(rc.Angle); ic.Inertia.X += (float)Math.Sin(angleInDegrees) * tc.Acceleration; ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * tc.Acceleration; if (ic.Inertia.Length() > tc.MaxSpeed) { ic.Inertia.Normalize(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed); } } if (tc.BackwardThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); float currentSpeed = ic.Inertia.Length(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.Deceleration); if (ic.Inertia.Length() < tc.Acceleration) { ic.Inertia.X = 0; ic.Inertia.Y = 0; } } if (tc.LeftThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); double angleInDegrees = MathHelper.ToRadians(rc.Angle - 90); // 90 degrees anti-clockwise ic.Inertia.X += (float)Math.Sin(angleInDegrees) * 0.05f; ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * 0.05f; if (ic.Inertia.Length() > tc.MaxSpeed) { ic.Inertia.Normalize(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed); } } if (tc.RightThrust) { InertiaComponent ic = entity.GetComponent <InertiaComponent>(); RotationComponent rc = entity.GetComponent <RotationComponent>(); double angleInDegrees = MathHelper.ToRadians(rc.Angle + 90); // 90 degrees clockwise ic.Inertia.X += (float)Math.Sin(angleInDegrees) * 0.05f; ic.Inertia.Y += -(float)Math.Cos(angleInDegrees) * 0.05f; if (ic.Inertia.Length() > tc.MaxSpeed) { ic.Inertia.Normalize(); ic.Inertia = Vector2.Multiply(ic.Inertia, tc.MaxSpeed); } } }
public override void Process(Entity entity) { NavigationComponent nc = entity.GetComponent <NavigationComponent>(); if (nc.TargetLocation.HasValue) { PositionComponent pc = entity.GetComponent <PositionComponent>(); float deltaX = pc.Position.X - nc.TargetLocation.Value.X; float deltaY = pc.Position.Y - nc.TargetLocation.Value.Y; float distanceToTarget = (float)Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY)); if (distanceToTarget < 15) { nc.TargetLocation = null; entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = true; return; } float angle = (float)Math.Atan2((nc.TargetLocation.Value.Y - pc.Position.Y), (nc.TargetLocation.Value.X - pc.Position.X)) + MathHelper.PiOver2; float angleDegrees = MathHelper.ToDegrees(angle); if (angleDegrees < 0) { angleDegrees += 360; } RotationComponent rc = entity.GetComponent <RotationComponent>(); float angleDiff = angleDegrees - rc.Angle; if (angleDiff > 180) { angleDiff -= 360; } if (angleDiff < -180) { angleDiff += 360; } if (angleDiff < -rc.RotationSpeed) { entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = true; rc.Angle -= rc.RotationSpeed; if (rc.Angle < 0) { rc.Angle += 360; } } else if (angleDiff > rc.RotationSpeed) { entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = true; rc.Angle += rc.RotationSpeed; if (rc.Angle > 360) { rc.Angle -= 360; } } else { ThrustComponent tc = entity.GetComponent <ThrustComponent>(); InertiaComponent ic = entity.GetComponent <InertiaComponent>(); float ticksToTarget = distanceToTarget / ic.Inertia.Length(); float ticksToStop = (float)Math.Log(ic.Inertia.Length() / tc.Deceleration) / tc.Deceleration; if (ticksToTarget > ticksToStop + 15) { tc.ForwardThrust = true; tc.BackwardThrust = false; } else { tc.ForwardThrust = false; tc.BackwardThrust = true; } } } else { if (entity.GetComponent <InertiaComponent>().Inertia.Length() == 0) { entity.GetComponent <ThrustComponent>().ForwardThrust = false; entity.GetComponent <ThrustComponent>().BackwardThrust = false; } } }