Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using VRage.Game;
  7. using VRageMath;
  8.  
  9. namespace ShipyardMod.ItemClasses
  10. {
  11.     public class PacketManager
  12.     {
  13.         public Vector3D Origin;
  14.         public Vector3D Target;
  15.         private double _multiplier;
  16.         private List<PacketItem> _packets = new List<PacketItem>();
  17.         private double _travelDist;
  18.         private bool _init;
  19.         private Vector4 _color;
  20.  
  21.         private class PacketItem
  22.         {
  23.             public PacketItem( Vector3D position )
  24.             {
  25.                 Position = position;
  26.                 Ticks = 0;
  27.             }
  28.             public Vector3D Position;
  29.             public int Ticks;
  30.         }
  31.  
  32.         public PacketManager( Vector3D origin, Vector3D target )
  33.         {
  34.             this.Origin = origin;
  35.             this.Target = target;
  36.         }
  37.  
  38.         private void Init()
  39.         {
  40.             _travelDist = Vector3D.Distance(Origin, Target);
  41.             _packets.Add(new PacketItem(Origin));
  42.             //packets move at 5m/s
  43.             _multiplier = 1 / ((_travelDist / 20) * 60);
  44.             //_multiplier = 0.001;
  45.             _color = Color.Orange.ToVector4();
  46.         }
  47.  
  48.         public void DrawPackets()
  49.         {
  50.             UpdatePackets();
  51.  
  52.             foreach ( var packet in _packets )
  53.             {
  54.                 //Vector3D end = packet.Position + (0.5 * ( Target - packet.Position ));
  55.                 //Vector3D end = Vector3D.Lerp( packet.Position, Target, 1 / _travelDist);
  56.                 Vector3D end = 1.2 * Vector3D.Normalize( Target - packet.Position ) + packet.Position;
  57.                 MySimpleObjectDraw.DrawLine( packet.Position, end, "ShotgunParticle", ref _color, 0.6f );
  58.             }
  59.         }
  60.  
  61.         private void UpdatePackets()
  62.         {
  63.             if ( !_init )
  64.             {
  65.                 _init = true;
  66.                 Init();
  67.             }
  68.  
  69.             List<PacketItem> toRemove = new List<PacketItem>();
  70.             foreach ( var packet in _packets )
  71.             {
  72.                 packet.Ticks++;
  73.                 packet.Position = Vector3D.Lerp( Origin, Target, (_multiplier * packet.Ticks) );
  74.  
  75.                 //delete the packet once it gets to the destination
  76.                 if ( (_multiplier * packet.Ticks) > 1 )
  77.                     toRemove.Add( packet );
  78.             }
  79.  
  80.             foreach ( var removePacket in toRemove )
  81.                 _packets.Remove( removePacket );
  82.  
  83.             //if the last packet to go out is more than 10m from origin, add a new one
  84.             var lastPacket = _packets.Last();
  85.             if ( Vector3D.DistanceSquared( lastPacket.Position, Origin ) > 100 )
  86.                 _packets.Add( new PacketItem( Origin ) );
  87.  
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement