private IEnumerable <Explosion> GenerateThrust(DefaultEnvironment Environment, TimeSpan ElapsedTime) { if (ThrustFlame != null) { var LifeTimes = new float[] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.2f, 0.3f, 0.3f, 1.5f }; while (ThrustFlame.Flames.Count > 0) { var position = ThrustFlame.Flames.Dequeue(); var lifeTime = LifeTimes[Environment.Random.Next(LifeTimes.Length)]; yield return(new Explosion(Data.Energyball) { Position = position.Position, EndOfLife = lifeTime, MinSize = 1.00f * ((0.1f / lifeTime) * 0.2f + 0.8f), MaxSize = 0.015f, StartSpin = 20 * (float)(Environment.Random.NextDouble() * 10 - 5), Spin = 200 }); } } }
public override void UpdateThinking(TimeSpan timeSpan, DefaultEnvironment environment) { if (shootTime == float.NegativeInfinity) { shootTime = -3 - environment.Random.NextDouble() * 3; } time += timeSpan.TotalSeconds; shootTime += timeSpan.TotalSeconds; var next = environment.Random.NextDouble() * direction + 0.5; if (time > next) { time -= next; direction = environment.Random.Next(directions.Length); } var nextShoot = environment.Random.NextDouble() * (shoot ? 0.25 : 1) + (shoot ? 0.25 : 2); if (shootTime > nextShoot) { shootTime -= nextShoot; shoot = !shoot; } var spaceShip = ControlledObject as Spaceship; if (shoot) { spaceShip.Shoot(); } spaceShip.TurnAngle(directions[direction]); }
public override IEnumerable <Object3D> Update(DefaultEnvironment Environment, TimeSpan ElapsedTime) { Age += (float)ElapsedTime.TotalSeconds; if (Age > EndOfLife) { Alive = false; } yield break; }
public override IEnumerable <Object3D> Update(DefaultEnvironment Environment, TimeSpan ElapsedTime) { Position += Direction * (float)ElapsedTime.TotalSeconds * Speed; Age += ElapsedTime.TotalSeconds; if (Age > 5) { Alive = false; } yield break; }
public override IEnumerable <Explosion> Die(DefaultEnvironment Environment, Vector3 CollisionPoint) { if (Alive == true) { var dst = (Position - Environment.ActiveCamera.Position).Length(); Environment.Sounds[Data.ExplosionSound].Play(1 / (1 + dst / 5000), 0, 0); } Alive = false; yield return(new Explosion(Data.Fireball) { Position = CollisionPoint, EndOfLife = 5, MinSize = 1, MaxSize = 10, Spin = (float)Environment.Random.NextDouble() * 2 - 1 }); for (int i = 0; i < 20; i++) { var position = new Vector3(); do { position.X = (float)Environment.Random.NextDouble() * 2f - 1f; position.Y = (float)Environment.Random.NextDouble() * 2f - 1f; position.Z = (float)Environment.Random.NextDouble() * 2f - 1f; } while (position.LengthSquared() > 1); var distance = position.Length(); position *= Boundary.Radius * 2; position += Position; yield return(new Explosion(Data.Fireball) { EndOfLife = (1 - distance) * 10 + 2.5f, MaxSize = (1 - distance) * 15 + 5, MinSize = (1 - distance) * 3 + 1.25f, Position = position, Spin = (float)Environment.Random.NextDouble() * 2 - 1 }); } }
public override void UpdateThinking(TimeSpan timeSpan, DefaultEnvironment environment) { float turnAngle = 0; float acceleration = 0; bool shoot = false; foreach (var touchPoint in TouchPanel.GetState()) { if (touchPoint.Position.X < environment.ScreenSize.Width / 3) { turnAngle += (float)Math.PI / 2; } else if (touchPoint.Position.X > 2 * environment.ScreenSize.Width / 3) { turnAngle -= (float)Math.PI / 2; } else { shoot = true; //if (touchPoint.Position.Y < playerEnvironment.ScreenSize.Height / 2) //{ // acceleration += 1; //} //else //{ // acceleration -= 1; //} } } var spaceShip = ControlledObject as Spaceship; if (shoot) { spaceShip.Shoot(); } spaceShip.TurnAngle(turnAngle + (environment.Flipped ? -1 : 1) * Math.Sign(environment.Acceleration.Y) * MathHelper.Clamp((Math.Abs(environment.Acceleration.Y) - deadZone) / (1 - deadZone), 0, 1)); spaceShip.AccelerateAmount(acceleration /* - playerEnvironment.Acceleration.Z*/); }
public override IEnumerable <Object3D> Update(DefaultEnvironment Environment, TimeSpan ElapsedTime) { UpdateCanon(ElapsedTime); if (ReadyToShoot && Shooting) { var dst = (Position - Environment.ActiveCamera.Position).Length(); Environment.Sounds[Data.LaserSound].Play(1 / (1 + dst / 5000), 0, 0); var bulletDirection = Vector3.Normalize(Vector3.Transform(Vector3.Forward, Orientation)); var bullet = new Bullet(Position, bulletDirection, Speed * 50 + 1000); bullet.Position += (bullet.Boundary.Radius + Boundary.Radius) * 75 * bulletDirection; yield return(bullet); ReadyToShoot = false; } Shooting = false; foreach (var thrustParticle in GenerateThrust(Environment, ElapsedTime)) { yield return(thrustParticle); } Speed += forwardAcceleration * (float)ElapsedTime.TotalSeconds; if (targetRotation > rotation) { rotation += rotationSpeed * (float)ElapsedTime.TotalSeconds; if (rotation >= targetRotation) { rotation = targetRotation; } } else if (targetRotation < rotation) { rotation -= rotationSpeed * (float)ElapsedTime.TotalSeconds; if (rotation <= targetRotation) { rotation = targetRotation; } } if (rotation > maxRotation) { rotation = maxRotation; } if (rotation < -maxRotation) { rotation = -maxRotation; } Orientation *= Rotation; var Direction = Vector3.Transform(Vector3.Forward * Speed, Orientation); var Up = Vector3.Transform(Vector3.Up, Orientation); Position += Direction * (ElapsedTime == TimeSpan.Zero ? 0 : 1); if (Speed > 0) { ThrustFlame.UpdateThrust(Position, Direction, Up, ElapsedTime, Environment); } else { if (ThrustFlame != null) { ThrustFlame.DontThrust(); } } yield break; }
public DefaultLevel(DefaultEnvironment environment) { Environment = environment; Players = new List <Player>(); Objects3D = new List <Object3D>(); var random = new System.Random(); int factor = 3; Objects3D.Add(new Spaceship() { Id = Data.Ship, Position = Vector3.Zero, Orientation = Quaternion.Identity, Speed = 150, Boundary = environment.ModelBoundaries[Data.Ship], ThrustFlame = new ThrustFlame() { ThrustBackshift = 150 } }); for (int y = -factor; y <= factor; y++) { for (int x = -factor; x <= factor; x++) { if (y != 0 || x != 0) { var ids = new string[] { Data.Ship, Data.Drone }; var id = ids[environment.Random.Next(0, ids.Length)]; var spaceship = new Spaceship() { Id = id, Position = 6400 * (Vector3.Forward * y + Vector3.Right * x) * (float)factor * 2 / 5, Orientation = Quaternion.CreateFromAxisAngle(Vector3.Up, x + y), Speed = id == ids[0] ? 150 : 0, Boundary = environment.ModelBoundaries[id], ThrustFlame = id == ids[1] ? null : new ThrustFlame() { ThrustBackshift = 150 } }; Players.Add(new ComputerPlayer() { ControlledObject = spaceship }); Objects3D.Add(spaceship); } } } Terrain = new Terrain(); Skybox = new Skybox() { Color = new Color(0.2f, 0.3f, 0.8f) }; if (Objects3D.Count > 0) { Camera = new SpaceshipFollowingCamera() { Orientation = Quaternion.Identity, Up = Vector3.Up, FieldOFView = (float)Math.PI / 3, NearCutOff = 100, FarCutOff = 80000, Ship = Objects3D.OfType <Spaceship>().First() }; } else { Camera = new FixedCamera() { Orientation = Quaternion.Identity, Up = Vector3.Up, FieldOFView = (float)Math.PI / 4, NearCutOff = 100, FarCutOff = 80000, Position = Vector3.Backward * 2, Target = Vector3.Zero }; } }
public virtual IEnumerable <Explosion> Die(DefaultEnvironment Environment, Vector3 CollisionPoint) { Alive = false; yield break; }
public virtual IEnumerable <Object3D> Update(DefaultEnvironment Environment, TimeSpan ElapsedTime) { yield break; }
public void UpdateThrust(Vector3 Position, Vector3 Direction, Vector3 Up, TimeSpan ElapsedTime, DefaultEnvironment Environment) { if (LastPosition.HasValue && LastDirection.HasValue) { Position -= Vector3.Normalize(Direction) * ThrustBackshift; var count = (Position - LastPosition.Value).Length() / 200 * 3; for (int i = 0; i < count; i++) { var value = (float)(i + Environment.Random.NextDouble()) / (float)count; var flame = new Flame() { Position = Vector3.Hermite(LastPosition.Value, LastDirection.Value, Position, Direction, value) + Environment.RandomPointInUnitSphere() * 0.1f, Up = Up, Direction = -Direction }; flames.Enqueue(flame); } } LastDirection = Direction; LastPosition = Position; }
public abstract void UpdateThinking(TimeSpan timeSpan, DefaultEnvironment playerEnvironment);