git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1129 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
beedee
2007-07-07 05:37:55 +00:00
parent 4d5b454d5b
commit d429d26f34
3 changed files with 85 additions and 51 deletions

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenRa.TechTreeTest
{
class Building
{
readonly string friendlyName;
public string FriendlyName
{
get { return friendlyName; }
}
string[] prerequisites;
public string[] Prerequisites
{
get { return prerequisites; }
set { prerequisites = value; }
}
int techLevel;
public int TechLevel
{
get { return techLevel; }
set { techLevel = value; }
}
public Building(string friendlyName)
{
this.friendlyName = friendlyName;
}
public bool ShouldMakeBuildable(IEnumerable<string> buildings)
{
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == 0;
}
public bool ShouldMakeUnbuildable(IEnumerable<string> buildings)
{
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == prerequisites.Length;
}
bool buildable = false;
public bool Buildable { get { return buildable; } }
public void CheckPrerequisites(IEnumerable<string> buildings)
{
if (buildable && ShouldMakeUnbuildable(buildings))
buildable = false;
else if (!buildable && ShouldMakeBuildable(buildings))
buildable = true;
}
}
}

View File

@@ -10,6 +10,7 @@ namespace OpenRa.TechTreeTest
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
TechTree techTree;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();

View File

@@ -10,6 +10,7 @@ namespace OpenRa.TechTreeTest
class TechTree class TechTree
{ {
Dictionary<string, Building> buildings = new Dictionary<string,Building>(); Dictionary<string, Building> buildings = new Dictionary<string,Building>();
List<string> built;
public TechTree() public TechTree()
{ {
LoadBuildings(); LoadBuildings();
@@ -40,65 +41,31 @@ namespace OpenRa.TechTreeTest
buildings.Add(m.Groups[0].Value, new Building(m.Groups[1].Value)); buildings.Add(m.Groups[0].Value, new Building(m.Groups[1].Value));
} }
} }
}
class Building public bool Build(string key)
{
readonly string friendlyName;
public string FriendlyName
{ {
get { return friendlyName; } Building b = buildings[key];
} if (!b.Buildable) return false;
built.Add(key);
string[] prerequisites; CheckAll();
return true;
public string[] Prerequisites
{
get { return prerequisites; }
set { prerequisites = value; }
} }
int techLevel; public bool Unbuild(string key)
public int TechLevel
{ {
get { return techLevel; } Building b = buildings[key];
set { techLevel = value; } if (!built.Contains(key)) return false;
built.Remove(key);
CheckAll();
return true;
} }
public Building(string friendlyName) void CheckAll()
{ {
this.friendlyName = friendlyName; foreach (Building building in buildings.Values)
} {
building.CheckPrerequisites(built);
public bool ShouldMakeBuildable(IEnumerable<string> buildings) }
{
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == 0;
}
public bool ShouldMakeUnbuildable(IEnumerable<string> buildings)
{
List<string> p = new List<string>(prerequisites);
foreach (string b in buildings)
p.Remove(b);
return p.Count == prerequisites.Length;
}
bool buildable = false;
public bool Buildable { get { return buildable; } }
public void CheckPrerequisites(IEnumerable<string> buildings)
{
if (buildable && ShouldMakeUnbuildable(buildings))
buildable = false;
else if (!buildable && ShouldMakeBuildable(buildings))
buildable = true;
} }
} }
} }