Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using SlimDX;
  5.  
  6. namespace Asteroids {
  7. class WrapingUniverse {
  8. private readonly Random RNG = new Random();
  9. public readonly float Width=25000f, Height=15000f;
  10.  
  11. /// <summary>
  12. /// ~= a - b
  13. /// </summary>
  14. public Vector2 NearDelta( Vector2 a, Vector2 b ) {
  15. var nd = a-b;
  16. var w_2 = Width/2;
  17. var h_2 = Height/2;
  18. nd.X
  19. = nd.X > +Width/2 ? nd.X-Width
  20. : nd.X < -Width/2 ? nd.X+Width
  21. : nd.X
  22. ;
  23. nd.Y
  24. = nd.Y > +Height/2 ? nd.Y-Height
  25. : nd.Y < -Height/2 ? nd.Y+Height
  26. : nd.Y
  27. ;
  28. return nd;
  29. }
  30.  
  31. public void SpawnRandomAsteroid() {
  32. Asteroids.Add( new Asteroid(100)
  33. { Position = new Vector2(RNG.NextFloat(0,Width),RNG.NextFloat(0,Height))
  34. , Velocity = new Vector2(RNG.NextFloat(-50,+50), RNG.NextFloat(-50,+50))
  35. });
  36. }
  37. public readonly WrapingSpatialBag<Asteroid > Asteroids;
  38. public readonly List<Bullet > Bullets = new List<Bullet>();
  39. public readonly List<Explosion> Explosions = new List<Explosion>();
  40. public readonly HashSet<Ship > Ships = new HashSet<Ship>();
  41.  
  42. public WrapingUniverse() {
  43. Asteroids = new WrapingSpatialBag<Asteroid>(this)
  44. { new Asteroid( 10) { Position = new Vector2(-100,-100) }
  45. , new Asteroid( 10) { Position = new Vector2(+100,-100) }
  46. , new Asteroid( 10) { Position = new Vector2(-100,+100) }
  47. , new Asteroid( 10) { Position = new Vector2(+100,+100) }
  48. , new Asteroid(100) { Position = new Vector2( 0,-200) }
  49. , new Asteroid(100) { Position = new Vector2( 0,+200) }
  50. , new Asteroid(100) { Position = new Vector2(-200, 0) }
  51. , new Asteroid(100) { Position = new Vector2(+200, 0) }
  52. };
  53. }
  54.  
  55. public void Update( TimeSpan time ) {
  56. float dt = (float)time.TotalSeconds;
  57.  
  58. foreach ( var s in Ships ) foreach ( var hit in s.SweepAgainst( this, Asteroids, 0f, dt ) ) {
  59. Explosions.Add( new Explosion(ExplosionAnimation.Default) { Position = s.Position, Velocity = s.Velocity } );
  60. s.Dead = true;
  61. break;
  62. }
  63. for ( int i=0, n=Bullets.Count ; i<n ; ++i ) {
  64. var bullet = Bullets[i];
  65. foreach ( var hit in bullet.SweepAgainst( this, Asteroids.GetObjectsNear(bullet)/*.ToArray()*/, 0f, dt ) ) {
  66. var asteroid = hit.Target;
  67. Explosions.Add( new Explosion(ExplosionAnimation.Default) { Position = bullet.Position, Velocity = bullet.Velocity } );
  68. bullet.Dead = true;
  69.  
  70. float newr = asteroid.Radius/2;
  71. if ( newr > 15 ) {
  72. Asteroids.Remove(asteroid);
  73. var children = RNG.Next(2,4);
  74. for ( int j=0 ; j<children ; ++j ) {
  75. Asteroids.Add( new Asteroid(newr) { Position = asteroid.Position, Velocity = new Vector2(RNG.NextFloat(-50,+50),RNG.NextFloat(-50,+50)) } );
  76. }
  77. } else {
  78. Asteroids.Remove(asteroid);
  79. SpawnRandomAsteroid();
  80. }
  81.  
  82. break;
  83. }
  84. Bullets[i] = bullet;
  85. }
  86. Bullets.RemoveAll(b=>b.Dead);
  87. //Ships.RemoveAll(s=>s.Dead);
  88. Ships.RemoveWhere(s=>s.Dead);
  89.  
  90. foreach ( var a in Asteroids ) Asteroids.Relocate(a,()=>{
  91. var p = a.Position;
  92. p += dt*a.Velocity;
  93. if ( (p.X %= Width ) < 0 ) p.X += Width;
  94. if ( (p.Y %= Height) < 0 ) p.Y += Height;
  95. a.Position = p;
  96. });
  97.  
  98. for ( int i=0, n=Bullets.Count ; i<n ; ++i ) {
  99. var b = Bullets[i];
  100. var p = b.Position;
  101. p += dt*b.Velocity;
  102. if ( (p.X %= Width ) < 0 ) p.X += Width;
  103. if ( (p.Y %= Height) < 0 ) p.Y += Height;
  104. b.Position = p;
  105. Bullets[i] = b;
  106. }
  107.  
  108. for ( int i=0, n=Explosions.Count ; i<n ; ++i ) {
  109. var e = Explosions[i];
  110. var p = e.Position;
  111. p += dt*e.Velocity;
  112. if ( (p.X %= Width ) < 0 ) p.X += Width;
  113. if ( (p.Y %= Height) < 0 ) p.Y += Height;
  114. e.Position = p;
  115. e.AdvanceAnimation(dt);
  116. Explosions[i] = e;
  117. }
  118. Explosions.RemoveAll(e=>!e.IsAlive);
  119.  
  120. foreach ( var s in Ships ) {
  121. var p = s.Position;
  122. p += dt*s.Velocity;
  123. if ( (p.X %= Width) < 0 ) p.X += Width;
  124. if ( (p.Y %= Height) < 0 ) p.Y += Height;
  125. s.Position = p;
  126.  
  127. var rot_facing = Matrix.RotationZ(s.Facing);
  128. var acceleration = new Vector2( s.InputState.Strafe, s.InputState.Thrust );
  129. acceleration.X *= 30;
  130. acceleration.Y *= 60;
  131. acceleration = Vector2.TransformCoordinate(acceleration,rot_facing);
  132. s.Velocity += dt*acceleration;
  133.  
  134. s.Facing += dt*3*s.InputState.Rotation;
  135.  
  136. foreach ( var weapon in s.Weapons ) {
  137. weapon.Cooldown -= dt;
  138. if ( s.InputState.Firing ) while ( weapon.Cooldown < 0f ) {
  139. Bullets.Add( new Bullet() { Position = s.Position + Vector2.TransformCoordinate(weapon.Offset,rot_facing), Velocity = s.Velocity + Vector2.TransformCoordinate(weapon.InitialRelativeBulletVelocity,rot_facing) } );
  140. weapon.Cooldown += 1/weapon.RoF;
  141. } else if ( weapon.Cooldown < 0f ) {
  142. weapon.Cooldown = 0f;
  143. }
  144. //
  145. //Bullets.Add( new Bullet() { Position = s.Position, Velocity = s.Velocity } );
  146. }
  147. }
  148. }
  149. }
  150. }
Add Comment
Please, Sign In to add comment