Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using VRage.Game;
- using VRageMath;
- namespace ShipyardMod.ItemClasses
- {
- public class PacketManager
- {
- public Vector3D Origin;
- public Vector3D Target;
- private double _multiplier;
- private List<PacketItem> _packets = new List<PacketItem>();
- private double _travelDist;
- private bool _init;
- private Vector4 _color;
- private class PacketItem
- {
- public PacketItem( Vector3D position )
- {
- Position = position;
- Ticks = 0;
- }
- public Vector3D Position;
- public int Ticks;
- }
- public PacketManager( Vector3D origin, Vector3D target )
- {
- this.Origin = origin;
- this.Target = target;
- }
- private void Init()
- {
- _travelDist = Vector3D.Distance(Origin, Target);
- _packets.Add(new PacketItem(Origin));
- //packets move at 5m/s
- _multiplier = 1 / ((_travelDist / 20) * 60);
- //_multiplier = 0.001;
- _color = Color.Orange.ToVector4();
- }
- public void DrawPackets()
- {
- UpdatePackets();
- foreach ( var packet in _packets )
- {
- //Vector3D end = packet.Position + (0.5 * ( Target - packet.Position ));
- //Vector3D end = Vector3D.Lerp( packet.Position, Target, 1 / _travelDist);
- Vector3D end = 1.2 * Vector3D.Normalize( Target - packet.Position ) + packet.Position;
- MySimpleObjectDraw.DrawLine( packet.Position, end, "ShotgunParticle", ref _color, 0.6f );
- }
- }
- private void UpdatePackets()
- {
- if ( !_init )
- {
- _init = true;
- Init();
- }
- List<PacketItem> toRemove = new List<PacketItem>();
- foreach ( var packet in _packets )
- {
- packet.Ticks++;
- packet.Position = Vector3D.Lerp( Origin, Target, (_multiplier * packet.Ticks) );
- //delete the packet once it gets to the destination
- if ( (_multiplier * packet.Ticks) > 1 )
- toRemove.Add( packet );
- }
- foreach ( var removePacket in toRemove )
- _packets.Remove( removePacket );
- //if the last packet to go out is more than 10m from origin, add a new one
- var lastPacket = _packets.Last();
- if ( Vector3D.DistanceSquared( lastPacket.Position, Origin ) > 100 )
- _packets.Add( new PacketItem( Origin ) );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement