Skip to content

Commit

Permalink
Hide internal resources dictionary behind indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Majiir committed Sep 3, 2014
1 parent 625886f commit 048b216
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
19 changes: 12 additions & 7 deletions Plugin/KethaneData.cs
Expand Up @@ -28,28 +28,33 @@ public static KethaneData Current
}
}

public Dictionary<string, ResourceData> ResourceData = new Dictionary<string,ResourceData>();
private Dictionary<string, ResourceData> resources = new Dictionary<string,ResourceData>();

public ResourceData this[string resourceName]
{
get { return resources[resourceName]; }
}

public ICellResource GetCellDeposit(string resourceName, CelestialBody body, Cell cell)
{
if (resourceName == null || body == null || !ResourceData.ContainsKey(resourceName)) { return null; }
if (resourceName == null || body == null || !resources.ContainsKey(resourceName)) { return null; }

return ResourceData[resourceName][body].GetCellDeposit(cell);
return resources[resourceName][body].GetCellDeposit(cell);
}

public override void OnLoad(ConfigNode config)
{
var timer = System.Diagnostics.Stopwatch.StartNew();

ResourceData.Clear();
resources.Clear();

var resourceNodes = config.GetNodes("Resource");

foreach (var resource in KethaneController.ResourceDefinitions)
{
var resourceName = resource.Resource;
var resourceNode = resourceNodes.SingleOrDefault(n => n.GetValue("Resource") == resourceName) ?? new ConfigNode();
ResourceData[resourceName] = Kethane.ResourceData.Load(resource, resourceNode);
resources[resourceName] = Kethane.ResourceData.Load(resource, resourceNode);
}

timer.Stop();
Expand All @@ -58,7 +63,7 @@ public override void OnLoad(ConfigNode config)

public void ResetGeneratorConfig(ResourceDefinition resource)
{
ResourceData[resource.Resource] = Kethane.ResourceData.Load(resource, new ConfigNode());
resources[resource.Resource] = Kethane.ResourceData.Load(resource, new ConfigNode());
}

public override void OnSave(ConfigNode configNode)
Expand All @@ -67,7 +72,7 @@ public override void OnSave(ConfigNode configNode)

configNode.AddValue("Version", System.Reflection.Assembly.GetExecutingAssembly().GetInformationalVersion());

foreach (var resource in ResourceData)
foreach (var resource in resources)
{
var resourceNode = new ConfigNode("Resource");
resourceNode.AddValue("Resource", resource.Key);
Expand Down
10 changes: 5 additions & 5 deletions Plugin/MapOverlay.cs
Expand Up @@ -243,8 +243,8 @@ private void refreshCellColors()
private void refreshCellColor(Cell cell, CelestialBody body, Color32[] colors, KethaneData data)
{
var deposit = data.GetCellDeposit(resource.Resource, body, cell);
var scanned = data.ResourceData[resource.Resource][body].IsCellScanned(cell);
var bodyResources = data.ResourceData[resource.Resource][body];
var scanned = data[resource.Resource][body].IsCellScanned(cell);
var bodyResources = data[resource.Resource][body];
var color = (revealAll ? deposit != null : scanned) ? getDepositColor(resource, bodyResources, deposit) : colorUnknown;
setCellColor(cell, color, colors);
}
Expand Down Expand Up @@ -380,7 +380,7 @@ private void mouseWindow(int windowId)

GUILayout.Label(String.Format("{0}:", definition.Resource));
GUILayout.FlexibleSpace();
if (revealAll || KethaneData.Current.ResourceData[definition.Resource][body].IsCellScanned(cell))
if (revealAll || KethaneData.Current[definition.Resource][body].IsCellScanned(cell))
{
var deposit = KethaneData.Current.GetCellDeposit(definition.Resource, body, cell);
GUILayout.Label(deposit != null ? String.Format("{0:N1}", deposit.Quantity) : "(none)");
Expand Down Expand Up @@ -452,7 +452,7 @@ private void controlWindow(int windowID)

if (GUILayout.Button("Reset " + (body ? body.name : "[null]") + " Data"))
{
KethaneData.Current.ResourceData[resource.Resource].ResetBodyData(body);
KethaneData.Current[resource.Resource].ResetBodyData(body);
refreshCellColors();
}

Expand Down Expand Up @@ -497,7 +497,7 @@ private static void export()
{
foreach (var cell in Cell.AtLevel(MapOverlay.GridLevel))
{
var scanned = KethaneData.Current.ResourceData[resource.Resource][body].IsCellScanned(cell);
var scanned = KethaneData.Current[resource.Resource][body].IsCellScanned(cell);
var deposit = KethaneData.Current.GetCellDeposit(resource.Resource, body, cell);

sb.Append(String.Format("{0},{1},", body.name, resource.Resource));
Expand Down
4 changes: 2 additions & 2 deletions Plugin/PartModules/KethaneDetector.cs
Expand Up @@ -163,10 +163,10 @@ public override void OnFixedUpdate()
{
var detected = false;
var cell = MapOverlay.GetCellUnder(vessel.mainBody, vessel.transform.position);
if (resources.All(r => KethaneData.Current.ResourceData[r][vessel.mainBody].IsCellScanned(cell))) { return; }
if (resources.All(r => KethaneData.Current[r][vessel.mainBody].IsCellScanned(cell))) { return; }
foreach (var resource in resources)
{
KethaneData.Current.ResourceData[resource][vessel.mainBody].ScanCell(cell);
KethaneData.Current[resource][vessel.mainBody].ScanCell(cell);
if (KethaneData.Current.GetCellDeposit(resource, vessel.mainBody, cell) != null)
{
detected = true;
Expand Down
2 changes: 1 addition & 1 deletion Plugin/Scenarios/KethaneScanningTutorial.cs
Expand Up @@ -187,7 +187,7 @@ protected override void OnTutorialSetup()

timewarp.OnUpdate = () =>
{
surfaceCoverage = (float)Cell.AtLevel(MapOverlay.GridLevel).Count(c => KethaneData.Current.ResourceData["Kethane"][FlightGlobals.currentMainBody].IsCellScanned(c)) / Cell.CountAtLevel(MapOverlay.GridLevel);
surfaceCoverage = (float)Cell.AtLevel(MapOverlay.GridLevel).Count(c => KethaneData.Current["Kethane"][FlightGlobals.currentMainBody].IsCellScanned(c)) / Cell.CountAtLevel(MapOverlay.GridLevel);
if (surfaceCoverage >= 0.02f)
{
Tutorial.GoToNextPage();
Expand Down

0 comments on commit 048b216

Please sign in to comment.