Skip to content

Commit

Permalink
Merge pull request #3 from asarium/develop
Browse files Browse the repository at this point in the history
Prepare for release 0.3
  • Loading branch information
asarium committed Aug 3, 2013
2 parents 29492cf + ae1cb6e commit 345a8cd
Show file tree
Hide file tree
Showing 20 changed files with 454 additions and 99 deletions.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ Backup*/
UpgradeLog*.XML

### Plugin specific ignores ###
assets/Plugins/
assets/**/*.dll
assets/**/*.zip
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "TacLib"]
path = TacLib
url = https://github.com/taraniselsu/TacLib.git
111 changes: 69 additions & 42 deletions PAPIPlugin/Arrays/PAPIArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ namespace PAPIPlugin.Arrays
{
public class PAPIArray : AbstractLightArray, IConfigNode
{
private const int PAPIPartCount = 4;
public const int DefaultPartCount = 4;

private const float PAPILightRadius = 10.0f;
public const float DefaultLightRadius = 10.0f;

private const double DefaultTargetGlidePath = 6;
public const double DefaultTargetGlidePath = 6;

/// <summary>
/// If the difference of the gliepath from the target is more than this the whole array will show either red or white.
/// </summary>
private const double DefaultBadGlidepathVariance = 1.5;

private static readonly Vector3 PAPILightDifference = Vector3.right * PAPILightRadius * 1.5f;
public const double DefaultGlideslopeTolerance = 1.5;

private GameObject _papiGameObject;

Expand All @@ -34,8 +32,8 @@ public class PAPIArray : AbstractLightArray, IConfigNode

public PAPIArray()
{
TargetGlidePath = DefaultTargetGlidePath;
BadGlidepathVariance = DefaultBadGlidepathVariance;
TargetGlideslope = DefaultTargetGlidePath;
GlideslopeTolerance = DefaultGlideslopeTolerance;

EnabledChanged += (sender, args) =>
{
Expand All @@ -51,22 +49,34 @@ public PAPIArray()
};
}

public double BadGlidepathVariance { get; set; }
public double GlideslopeTolerance { get; set; }

public double TargetGlidePath { get; set; }
public double TargetGlideslope { get; set; }

public double Longitude { get; set; }

public double Latitude { get; set; }

public double Heading { get; set; }

public double HeightAboveTerrain { get; set; }

public int PartCount { get; set; }

public float LightRadius { get; set; }

public float LightDistance { get; set; }

#region IConfigNode Members

public void Load(ConfigNode node)
{
BadGlidepathVariance = node.ConvertValue("BadGlidepath", DefaultBadGlidepathVariance);
TargetGlidePath = node.ConvertValue("TargetGlidepath", DefaultTargetGlidePath);
GlideslopeTolerance = node.ConvertValue("GlideslopeTolerance", DefaultGlideslopeTolerance);
TargetGlideslope = node.ConvertValue("TargetGlideslope", DefaultTargetGlidePath);
HeightAboveTerrain = node.ConvertValue("Height", 0);
PartCount = node.ConvertValue("PartCount", DefaultPartCount);
LightRadius = node.ConvertValue("LightRadius", DefaultLightRadius);
LightDistance = node.ConvertValue("LightDistance", LightRadius * 0.5f);

try
{
Expand Down Expand Up @@ -110,6 +120,11 @@ public override void Update()

var currentCamera = Camera.main;

if (currentCamera == null)
{
return;
}

var relativePosition = _papiGameObject.transform.InverseTransformPoint(currentCamera.transform.position);

var normalizedPosition = relativePosition.normalized;
Expand All @@ -120,9 +135,9 @@ public override void Update()

var angle = 90 - Math.Acos(normalDot) * (180 / Math.PI);

var difference = angle - TargetGlidePath;
var difference = angle - TargetGlideslope;

for (var i = 0; i < PAPIPartCount; i++)
for (var i = 0; i < PartCount; i++)
{
if (directionDot <= 0)
{
Expand Down Expand Up @@ -184,15 +199,13 @@ private void InitializePAPIParts(double lat, double lon, double heading)
_papiGameObject.transform.localRotation = Quaternion.LookRotation(headingVector, surfaceNormal);

var maxHeight = double.MinValue;
_partObjects = new GameObject[PAPIPartCount];
for (var i = 0; i < PAPIPartCount; i++)
_partObjects = new GameObject[PartCount];
for (var i = 0; i < PartCount; i++)
{
var obj = new GameObject();

AddPAPIPart(obj);
var obj = AddPAPIPart();

obj.transform.parent = _papiGameObject.transform;
obj.transform.localPosition = (i - (PAPIPartCount / 2)) * PAPILightDifference;
obj.transform.localPosition = GetLocalLighPosition(i);

maxHeight = Math.Max(maxHeight, parentBody.GetSurfaceHeight(Latitude, Longitude));

Expand All @@ -201,10 +214,26 @@ private void InitializePAPIParts(double lat, double lon, double heading)

maxHeight = Math.Max(0, maxHeight);
_relativeSurfacePosition =
parentBody.transform.InverseTransformPoint(parentBody.GetWorldSurfacePosition(lat, lon, maxHeight + PAPILightRadius * 0.5));
parentBody.transform.InverseTransformPoint(parentBody.GetWorldSurfacePosition(lat, lon, maxHeight + HeightAboveTerrain + LightRadius));
_papiGameObject.transform.localPosition = _relativeSurfacePosition;
}

/// <summary>
/// Gets the local position given a zero-based index.
/// </summary>
/// <param name="i">The index of the light, zero-based</param>
/// <returns>A local position specifying the light position</returns>
private Vector3 GetLocalLighPosition(int i)
{
var countHalf = PartCount / 2.0;

var offsetMult = (float) (i - countHalf - 0.5);

var distance = LightRadius + LightDistance;

return Vector3.right * offsetMult * distance;
}

private static Vector3d Orthonormalise(Vector3d direction, Vector3d firstVector)
{
// This is basically the first step of a Gram–Schmidt process
Expand All @@ -213,48 +242,46 @@ private static Vector3d Orthonormalise(Vector3d direction, Vector3d firstVector)
return direction - Vector3d.Dot(firstVector, direction) * firstVector;
}

private static void AddPAPIPart(GameObject obj)
private GameObject AddPAPIPart()
{
var lineRenderer = obj.AddComponent<LineRenderer>();

lineRenderer.useWorldSpace = false;
lineRenderer.transform.parent = obj.transform;
lineRenderer.transform.localPosition = Vector3.zero;
lineRenderer.transform.eulerAngles = Vector3.zero;

lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.SetColors(Color.red, Color.red);
lineRenderer.SetWidth(PAPILightRadius, PAPILightRadius);
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(0, Vector3.zero);
lineRenderer.SetPosition(1, Vector3.up * PAPILightRadius);
var obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);

var material = new Material(Shader.Find("Particles/Additive"));
obj.renderer.sharedMaterial = material;

obj.transform.localScale = new Vector3(LightRadius, LightRadius, LightRadius);

var sphereCollider = obj.GetComponent<SphereCollider>();
sphereCollider.enabled = false;

return obj;
}

private void UpdatePAPIPart(int index, double difference, float alpha)
{
var gameObj = _partObjects[index];

var lineRenderer = gameObj.GetComponent<LineRenderer>();

var color = GetArrayPartColor(index, difference);
color.a = alpha;
lineRenderer.SetColors(color, color);

gameObj.renderer.material.SetColor("_TintColor", color);
}

private Color GetArrayPartColor(int index, double difference)
{
if (difference < -BadGlidepathVariance)
if (difference < -GlideslopeTolerance)
{
return Color.red;
}
if (difference > BadGlidepathVariance)
if (difference > GlideslopeTolerance)
{
return Color.white;
}

// This should map temp into [-1, 1]
double temp = index - (PAPIPartCount / 2);
temp = temp / (PAPIPartCount / 2);
double temp = index - (PartCount / 2);
// ReSharper disable once PossibleLossOfFraction
temp = temp / (PartCount / 2);

return temp > difference ? Color.red : Color.white;
}
Expand Down
57 changes: 47 additions & 10 deletions PAPIPlugin/Impl/DefaultLightArrayManager.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
#region Usings

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using PAPIPlugin.Interfaces;
using PAPIPlugin.Internal;
using PAPIPlugin.UI;
using Tac;
using UnityEngine;

#endregion

namespace PAPIPlugin.Impl
{
public class DefaultLightArrayManager : ILightArrayManager
{
private readonly Icon<DefaultLightArrayManager> _groupWindowIcon;

private GroupWindow<ILightArrayConfig> _groupWindow;

private ILightArrayConfig _lightConfig;

public DefaultLightArrayManager()
{
_groupWindowIcon = new Icon<DefaultLightArrayManager>(new Rect(Screen.width * 0.8f, 0.0f, 80.0f, 20.0f), "icon.png", "Light groups",
"Opens the light group overview", OnIconClickHandler);
_groupWindowIcon.SetVisible(true);
}

#region ILightArrayManager Members

public event EventHandler ParsingFinished;
Expand All @@ -25,23 +38,17 @@ public ILightArrayConfig LightConfig
set
{
if (Equals(_lightConfig, value))
{
return;
}

_lightConfig = value;

InitializeConfig(_lightConfig);
}
}

private void InitializeConfig(ILightArrayConfig lightConfig)
{
foreach (var lightArray in lightConfig.LightArrayGroups.SelectMany(group => group.LightArrays))
{
lightArray.InitializeDisplay(this);
}
}

public void LoadConfig()
public ILightArrayConfig LoadConfig()
{
Util.LogInfo("Starting to parse light definitions...");

Expand All @@ -56,6 +63,8 @@ public void LoadConfig()
LightConfig.LightArrayGroups.Count(), LightConfig.LightArrayGroups.Sum(group => group.LightArrays.Count()), stopwatch.Elapsed));

OnParsingFinished();

return LightConfig;
}

public void Update()
Expand All @@ -73,6 +82,27 @@ public void Dispose()

#endregion

private void OnIconClickHandler()
{
if (_groupWindow == null)
{
_groupWindow = new GroupWindow<ILightArrayConfig>(LightConfig);
_groupWindow.SetVisible(true);
}
else
{
_groupWindow.ToggleVisible();
}
}

private void InitializeConfig(ILightArrayConfig lightConfig)
{
foreach (var lightArray in lightConfig.LightArrayGroups.SelectMany(group => group.LightArrays))
{
lightArray.InitializeDisplay(this);
}
}

protected virtual void OnParsingFinished()
{
var handler = ParsingFinished;
Expand All @@ -90,6 +120,13 @@ protected virtual void OnParsingFinished()
protected virtual void Dispose(bool disposing)
{
LightConfig.Destroy();

_groupWindowIcon.SetVisible(false);

if (_groupWindow != null)
{
_groupWindow.SetVisible(false);
}
}
}
}
Loading

0 comments on commit 345a8cd

Please sign in to comment.