Skip to content

Commit

Permalink
Cache rounds till they arrive
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Martin committed Sep 18, 2014
1 parent 86394b1 commit 4bbf81b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
Expand Up @@ -45,7 +45,7 @@ public class SelectableObjectRepository : IDisposable, ISelectableObjectReposito

private void OnAdd(AddObjectMessage message)
{
var path = new Path(4, new CircularMotion(0, 50, new Angle(0), new Angle(Math.PI / 10), 20, Vector.Zero));
var path = new Path(4, new CircularMotion(0, 50, new Angle(0), new Angle(Math.PI / 10), BaseConstants.EscortSpeed, Vector.Zero));
var selectableObject = new SelectableObject(m_Id.Id ,message.Name, path);
m_Objects.Add(selectableObject);

Expand All @@ -55,6 +55,7 @@ private void OnAdd(AddObjectMessage message)
public void AddObject(SelectableObject newObject)
{
m_Objects.Add(newObject);
m_VesselRepository.Add(new Vessel(newObject));
}

public void OnPathRequest(RequestPathMessage message)
Expand Down
24 changes: 24 additions & 0 deletions TrianglesInSpace/TrianglesInSpace/Primitives/BaseConstants.cs
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrianglesInSpace.Primitives
{
/// <summary>
/// Central place for storing convesion values to allow
/// the changing of constants while keeping the correct baseline
/// </summary>
public static class BaseConstants
{
// 750 km/s
public const double BaseSpeed = 5;

//eqivelant to 1 500km/s
public const double EscortSpeed = BaseSpeed * 2.0;

// 30 000 km/s
public const double WeaponSpeed = EscortSpeed * 20.0;
}
}
Expand Up @@ -31,6 +31,9 @@ public class TargetedVesselRenderer : IDisposable, ITargetedVesselRenderer

private string m_CurrentSelection;

private List<Attack> m_Attacks;


public TargetedVesselRenderer(IBus bus,
ISelectableObjectRepository targets,
IRenderer renderer,
Expand All @@ -45,9 +48,12 @@ public class TargetedVesselRenderer : IDisposable, ITargetedVesselRenderer
m_Id = id;
m_VesselRepository = vesselRepository;

m_Attacks = new List<Attack>();

m_Bus.Subscribe<HighlightTargetMessage>(OnHighlight).AddTo(m_Disposer);
m_Bus.Subscribe<AttackTargetMessage>(OnAttack).AddTo(m_Disposer);
m_Bus.Subscribe<SelectedObjectMessage>(OnSelection).AddTo(m_Disposer);
m_Bus.Subscribe<TimeUpdateMessage>(OnTick).AddTo(m_Disposer);
}

private void OnHighlight(HighlightTargetMessage message)
Expand Down Expand Up @@ -99,6 +105,10 @@ private void OnAttack(AttackTargetMessage message)
var weapons = vessel.AvailableWeapons(facingWeapons, vesselToTarget.Length);
// post firing.
// send???
m_Attacks.Add(new Attack(message.Time, m_CurrentTarget, vesselPositon));

m_Renderer.Scene.Add("Attack"+ message.Time, m_CurrentTarget, "square", "target_highlight");
m_Bus.Send(new RequestPathMessage(m_CurrentTarget));
}

m_CurrentTarget = null;
Expand All @@ -107,6 +117,80 @@ private void OnAttack(AttackTargetMessage message)
}
}

private class Attack
{
//private List<IWeaponSystem> m_Weapons;
private ulong m_FiringTime;
private string m_Target;
private Vector m_FiringPosition;
public Attack(ulong firingTime, string target, Vector firingPosition)
{
m_FiringTime = firingTime;
m_Target = target;
m_FiringPosition = firingPosition;
}

public ulong FiringTime
{
get
{
return m_FiringTime;
}
}

public string Target
{
get
{
return m_Target;
}
}

public Vector FiringPosition
{
get
{
return m_FiringPosition;
}
}
}

public void OnTick(TimeUpdateMessage message)
{
var remove = new List<Attack>();

foreach(var attack in m_Attacks)
{
var target = m_VesselRepository.GetByName(attack.Target);
if (target != null)
{
Vector targetPosition = target.GetPosition(message.Time);

var distance = (targetPosition - attack.FiringPosition).Length;

var travelTime = distance / BaseConstants.WeaponSpeed * 1000;

double elapsedTime = (double)( message.Time - attack.FiringTime);

if( travelTime <= elapsedTime)
{
// we hit explode things here
m_Renderer.Scene.Remove("Attack" + attack.FiringTime);
remove.Add(attack);
}
}
else
{
remove.Add(attack);
}
}

foreach (var attack in remove)
{
m_Attacks.Remove(attack);
}
}

private void OnSelection(SelectedObjectMessage message)
{
if (message.Owned)
Expand Down
4 changes: 4 additions & 0 deletions TrianglesInSpace/TrianglesInSpace/Time/SynchronizedClock.cs
Expand Up @@ -66,6 +66,10 @@ public void UpdateTime(double timeSinceLastUpdate)
m_NextSync = m_NextSync + m_SyncRate;
m_Bus.Send(new TimeUpdateMessage(m_Time));
}
else
{
m_Bus.SendLocal(new TimeUpdateMessage(m_Time));
}
}

public void Dispose()
Expand Down
1 change: 1 addition & 0 deletions TrianglesInSpace/TrianglesInSpace/TrianglesInSpace.csproj
Expand Up @@ -141,6 +141,7 @@
<Compile Include="Objects\SelectableObject.cs" />
<Compile Include="Objects\SelectableObjectRepository.cs" />
<Compile Include="Primitives\Angle.cs" />
<Compile Include="Primitives\BaseConstants.cs" />
<Compile Include="Primitives\Box.cs" />
<Compile Include="Primitives\Facing.cs" />
<Compile Include="Primitives\InputMode.cs" />
Expand Down

0 comments on commit 4bbf81b

Please sign in to comment.