Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using SlimDX;
- namespace Asteroids {
- class WrapingUniverse {
- private readonly Random RNG = new Random();
- public readonly float Width=25000f, Height=15000f;
- /// <summary>
- /// ~= a - b
- /// </summary>
- public Vector2 NearDelta( Vector2 a, Vector2 b ) {
- var nd = a-b;
- var w_2 = Width/2;
- var h_2 = Height/2;
- nd.X
- = nd.X > +Width/2 ? nd.X-Width
- : nd.X < -Width/2 ? nd.X+Width
- : nd.X
- ;
- nd.Y
- = nd.Y > +Height/2 ? nd.Y-Height
- : nd.Y < -Height/2 ? nd.Y+Height
- : nd.Y
- ;
- return nd;
- }
- public void SpawnRandomAsteroid() {
- Asteroids.Add( new Asteroid(100)
- { Position = new Vector2(RNG.NextFloat(0,Width),RNG.NextFloat(0,Height))
- , Velocity = new Vector2(RNG.NextFloat(-50,+50), RNG.NextFloat(-50,+50))
- });
- }
- public readonly WrapingSpatialBag<Asteroid > Asteroids;
- public readonly List<Bullet > Bullets = new List<Bullet>();
- public readonly List<Explosion> Explosions = new List<Explosion>();
- public readonly HashSet<Ship > Ships = new HashSet<Ship>();
- public WrapingUniverse() {
- Asteroids = new WrapingSpatialBag<Asteroid>(this)
- { new Asteroid( 10) { Position = new Vector2(-100,-100) }
- , new Asteroid( 10) { Position = new Vector2(+100,-100) }
- , new Asteroid( 10) { Position = new Vector2(-100,+100) }
- , new Asteroid( 10) { Position = new Vector2(+100,+100) }
- , new Asteroid(100) { Position = new Vector2( 0,-200) }
- , new Asteroid(100) { Position = new Vector2( 0,+200) }
- , new Asteroid(100) { Position = new Vector2(-200, 0) }
- , new Asteroid(100) { Position = new Vector2(+200, 0) }
- };
- }
- public void Update( TimeSpan time ) {
- float dt = (float)time.TotalSeconds;
- foreach ( var s in Ships ) foreach ( var hit in s.SweepAgainst( this, Asteroids, 0f, dt ) ) {
- Explosions.Add( new Explosion(ExplosionAnimation.Default) { Position = s.Position, Velocity = s.Velocity } );
- s.Dead = true;
- break;
- }
- for ( int i=0, n=Bullets.Count ; i<n ; ++i ) {
- var bullet = Bullets[i];
- foreach ( var hit in bullet.SweepAgainst( this, Asteroids.GetObjectsNear(bullet)/*.ToArray()*/, 0f, dt ) ) {
- var asteroid = hit.Target;
- Explosions.Add( new Explosion(ExplosionAnimation.Default) { Position = bullet.Position, Velocity = bullet.Velocity } );
- bullet.Dead = true;
- float newr = asteroid.Radius/2;
- if ( newr > 15 ) {
- Asteroids.Remove(asteroid);
- var children = RNG.Next(2,4);
- for ( int j=0 ; j<children ; ++j ) {
- Asteroids.Add( new Asteroid(newr) { Position = asteroid.Position, Velocity = new Vector2(RNG.NextFloat(-50,+50),RNG.NextFloat(-50,+50)) } );
- }
- } else {
- Asteroids.Remove(asteroid);
- SpawnRandomAsteroid();
- }
- break;
- }
- Bullets[i] = bullet;
- }
- Bullets.RemoveAll(b=>b.Dead);
- //Ships.RemoveAll(s=>s.Dead);
- Ships.RemoveWhere(s=>s.Dead);
- foreach ( var a in Asteroids ) Asteroids.Relocate(a,()=>{
- var p = a.Position;
- p += dt*a.Velocity;
- if ( (p.X %= Width ) < 0 ) p.X += Width;
- if ( (p.Y %= Height) < 0 ) p.Y += Height;
- a.Position = p;
- });
- for ( int i=0, n=Bullets.Count ; i<n ; ++i ) {
- var b = Bullets[i];
- var p = b.Position;
- p += dt*b.Velocity;
- if ( (p.X %= Width ) < 0 ) p.X += Width;
- if ( (p.Y %= Height) < 0 ) p.Y += Height;
- b.Position = p;
- Bullets[i] = b;
- }
- for ( int i=0, n=Explosions.Count ; i<n ; ++i ) {
- var e = Explosions[i];
- var p = e.Position;
- p += dt*e.Velocity;
- if ( (p.X %= Width ) < 0 ) p.X += Width;
- if ( (p.Y %= Height) < 0 ) p.Y += Height;
- e.Position = p;
- e.AdvanceAnimation(dt);
- Explosions[i] = e;
- }
- Explosions.RemoveAll(e=>!e.IsAlive);
- foreach ( var s in Ships ) {
- var p = s.Position;
- p += dt*s.Velocity;
- if ( (p.X %= Width) < 0 ) p.X += Width;
- if ( (p.Y %= Height) < 0 ) p.Y += Height;
- s.Position = p;
- var rot_facing = Matrix.RotationZ(s.Facing);
- var acceleration = new Vector2( s.InputState.Strafe, s.InputState.Thrust );
- acceleration.X *= 30;
- acceleration.Y *= 60;
- acceleration = Vector2.TransformCoordinate(acceleration,rot_facing);
- s.Velocity += dt*acceleration;
- s.Facing += dt*3*s.InputState.Rotation;
- foreach ( var weapon in s.Weapons ) {
- weapon.Cooldown -= dt;
- if ( s.InputState.Firing ) while ( weapon.Cooldown < 0f ) {
- Bullets.Add( new Bullet() { Position = s.Position + Vector2.TransformCoordinate(weapon.Offset,rot_facing), Velocity = s.Velocity + Vector2.TransformCoordinate(weapon.InitialRelativeBulletVelocity,rot_facing) } );
- weapon.Cooldown += 1/weapon.RoF;
- } else if ( weapon.Cooldown < 0f ) {
- weapon.Cooldown = 0f;
- }
- //
- //Bullets.Add( new Bullet() { Position = s.Position, Velocity = s.Velocity } );
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment