class City
{
public void PaintAttackColor()
{
var color = new asd.Color(255, 0, 0);
foreach(var m in _maps)
{
m.SetColor(color);
}
}
public void PaintDeffenceColor()
{
var color = new asd.Color(0, 0, 255);
foreach (var m in _maps)
{
m.SetColor(color);
}
}
public void ClearPaint()
{
foreach (var m in _maps)
{
m.SetColor(_color);
}
}
class Battle
{
private List<City> cityRandomReplace(List<City> beforeCities)
{
var r = Singleton.GetRandom();
List<City> afterCities = new List<City>();
int max = beforeCities.Count;
for (int i = 0; i < max; i++)
{
int index = r.Next(0, beforeCities.Count - 1);
afterCities.Add(beforeCities[index]);
beforeCities.RemoveAt(index);
}
return afterCities;
}
class HokkaidoWar
{
public void Run()
{
_battle = new Battle(cities);
while (asd.Engine.DoEvents())
{
if (asd.Engine.Mouse.LeftButton.ButtonState == asd.ButtonState.Push)
{
_battle.NextTurn();
}
asd.Engine.Update();
}
asd.Engine.Terminate();
class Battle
{
private List<City> _cities = null;
private int turn;
private int cityCnt;
public Battle(List<City> cities)
{
turn = 1;
cityCnt = 0;
_cities = new List<City>();
foreach(var c in cities)
{
_cities.Add(c);
}
_cities = cityRandomReplace(_cities);
}
public void NextTurn()
{
cityCnt++;
if(cityCnt >= _cities.Count)
{
_cities = cityRandomReplace(_cities);
cityCnt = 0;
turn++;
}
var info = Singleton.GetGameProcessInfomation();
info.ShowText(_cities[cityCnt].GetPosition(), string.Format("{0} turn {1} / {2} {3}", turn, cityCnt, _cities.Count, _cities[cityCnt].Name));
}
class Map
{
public Map Up {
get {
var field = Singleton.GetFieldMap();
return field.GetMap(_x, _y - 1);
}
}
public Map Down
{
get
{
var field = Singleton.GetFieldMap();
return field.GetMap(_x, _y + 1);
}
}
public Map Left
{
get
{
var field = Singleton.GetFieldMap();
return field.GetMap(_x - 1, _y);
}
}
public Map Right
{
get
{
var field = Singleton.GetFieldMap();
return field.GetMap(_x + 1, _y);
}
}
class FieldMap
{
public Map GetMap(int x, int y)
{
if(x < 0 || x >= MaxX || y < 0 || y >= MaxY)
{
return null;
}
else
{
return _map[x, y];
}
}
これでだいぶ扱いやすくなったはず。
動作結果は前回と同じ。
で、ここからが今回の本題。
マップと都市のリンクを作成します。
マップから所属する都市を取得できるようにします。
まぁ、やっていることは簡単ですが。
class Map
{
public void SetCity(City city)
{
_city = city;
}
public City GetCity()
{
return _city;
}
class City
{
public City(string name, Point[] points, int population)
{
_name = name;
_population = population;
_maps = new List<Map>();
var r = Singleton.GetRandom();
_color = new asd.Color((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255));
var fieldMap = Singleton.GetFieldMap();
foreach (var p in points)
{
Map m = new Map(p.x, p.y, _color);
m.SetCity(this);
_maps.Add(m);
fieldMap.SetMap(m);
}
}
private List<City> GetLinkedCities()
{
List<City> cities = new List<City>();
foreach (var m in _maps)
{
if (m.Up != null)
{
var c = m.Up.GetCity();
if (cities.Contains(c) == false && c != this)
{
cities.Add(c);
}
}
if (m.Down != null)
{
var c = m.Down.GetCity();
if (cities.Contains(c) == false && c != this)
{
cities.Add(c);
}
}
if (m.Left != null)
{
var c = m.Left.GetCity();
if (cities.Contains(c) == false && c != this)
{
cities.Add(c);
}
}
if (m.Right != null)
{
var c = m.Right.GetCity();
if (cities.Contains(c) == false && c != this)
{
cities.Add(c);
}
}
}
return cities;
}
public void OnMouse(asd.Vector2DF pos)
{
foreach (var m in _maps)
{
if(m.IsOnMouse(pos))
{
var info = Singleton.GetInfomationWindow();
info.ShowText(pos, _name + "\r\n" + _population.ToString());
// test
var cities = GetLinkedCities();
foreach (var c in cities)
{
c.linkedCity();
}
}
}
}
//test
private void linkedCity()
{
foreach(var m in _maps)
{
m.linkedMap();
}
}
class HokkaidoWar
{
public void Run()
{
while (asd.Engine.DoEvents())
{
FieldMap fieldMap = Singleton.GetFieldMap();
fieldMap.unlinkMap();
class FieldMap
{
public int MaxX { get { return 44; } }
public int MaxY { get { return 35; } }
private Map[,] _map;
public FieldMap()
{
_map = new Map[44, 35];
}
public Map GetMap(int x, int y)
{
return _map[x, y];
}
public void SetMap(Map map)
{
_map[map.X, map.Y] = map;
}
class Singleton
{
private static FieldMap _map = null;
public static FieldMap GetFieldMap()
{
if (_map == null)
{
_map = new FieldMap();
}
return _map;
}
public City(string name, Point[] points, int population)
{
_name = name;
_population = population;
_maps = new List<Map>();
var r = Singleton.GetRandom();
_color = new asd.Color((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255));
var fieldMap = Singleton.GetFieldMap();
foreach (var p in points)
{
Map m = new Map(p.x, p.y, _color);
_maps.Add(m);
fieldMap.SetMap(m);
}
}
たぶん、これだけでOKのはずなので、うまく管理出来ているか、テストコードで確認したいと思います。
マウスカーソルのあるマップの隣にあるマップの色を変化させます。
class City
{
public void OnMouse(asd.Vector2DF pos)
{
var fieldMap = Singleton.GetFieldMap();
foreach (var m in _maps)
{
if(m.IsOnMouse(pos))
{
var info = Singleton.GetInfomationWindow();
info.ShowText(pos, _name + "\r\n" + _population.ToString());
// test
fieldMap.onMouse(m);
}
}
}
class FieldMap
{
// Test
public void onMouse(Map map)
{
for(int x = 0; x < MaxX; x++)
{
for(int y = 0; y < MaxY; y++)
{
if(_map[x,y] != null)
{
_map[x, y].unlinkedMap();
}
}
}
if(map.X > 0)
{
if (_map[map.X + 1, map.Y] != null)
{
_map[map.X + 1, map.Y].linkedMap();
}
}
if (map.X < MaxX)
{
if (_map[map.X - 1, map.Y] != null)
{
_map[map.X - 1, map.Y].linkedMap();
}
}
if (map.Y > 0)
{
if (_map[map.X, map.Y - 1] != null)
{
_map[map.X, map.Y - 1].linkedMap();
}
}
if (map.Y < MaxY)
{
if (_map[map.X, map.Y + 1] != null)
{
_map[map.X, map.Y + 1].linkedMap();
}
}
}
class Map
{
// Test
public void linkedMap()
{
var changeColor = new asd.Color(200, 200, 200);
_geometryObj.Color = changeColor;
}
public void unlinkedMap()
{
_geometryObj.Color = _color;
}
class Map
{
private int _x;
private int _y;
private asd.GeometryObject2D _geometryObj;
private readonly int width = 24;
private readonly int height = 24;
private readonly int offsetx = 50;
private readonly int offsety = 50;
public Map(int x, int y, asd.Color color)
{
_x = x;
_y = y;
_geometryObj = new asd.GeometryObject2D();
_geometryObj.Color = color;
asd.Engine.AddObject2D(_geometryObj);
var rect = new asd.RectangleShape();
rect.DrawingArea = new asd.RectF(width * _x + offsetx, height * _y + offsety, width, height);
_geometryObj.Shape = rect;
}
public void SetColor(asd.Color color)
{
_geometryObj.Color = color;
}
public bool IsOnMouse(asd.Vector2DF pos)
{
if (pos.X > width * _x + offsetx && pos.X < width * (_x + 1) + offsetx
&& pos.Y > height * _y + offsety && pos.Y < height * (_y + 1) + offsety)
{
return true;
}
else
{
return false;
}
}
}
これを使用する都市クラスを作成します。
class City
{
private string _name = string.Empty;
private List<Map> _maps = null;
private asd.Color _color;
public City(string name, Point[] points)
{
_name = name;
_maps = new List<Map>();
var r = Singleton.GetRandom();
_color = new asd.Color((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255));
foreach (var p in points)
{
Map m = new Map(p.x, p.y, _color);
_maps.Add(m);
}
}
public void OnMouse(asd.Vector2DF pos)
{
foreach(var m in _maps)
{
if(m.IsOnMouse(pos))
{
var info = Singleton.GetInfomationWindow();
info.ShowText(pos, _name);
}
}
}
public bool IsOnMouse(asd.Vector2DF pos)
{
bool ret = false;
foreach (var m in _maps)
{
if (m.IsOnMouse(pos))
{
ret = true;
}
}
return ret;
}
}
と、ここでひょっこり出てきた都市名を表示する窓クラスも作成します。
class InfomationWindow
{
private asd.TextObject2D _valueText;
public InfomationWindow()
{
_valueText = new asd.TextObject2D();
_valueText.Font = Singleton.GetFont();
asd.Engine.AddObject2D(_valueText);
}
public void ShowText(asd.Vector2DF pos, string text)
{
_valueText.Text = text;
_valueText.Position = new asd.Vector2DF(pos.X, pos.Y);
}
}
これをどこでも取り出せるようにシングルトンにします。
class Singleton
{
private static InfomationWindow _info = null;
private static Random random = null;
private static asd.Font _font = null;
public static Random GetRandom()
{
if(random == null)
{
random = new Random();
}
return random;
}
public static asd.Font GetFont()
{
if(_font == null)
{
_font = asd.Engine.Graphics.CreateFont("FontText.aff");
}
return _font;
}
public static InfomationWindow GetInfomationWindow()
{
if (_info == null)
{
_info = new InfomationWindow();
}
return _info;
}
}
最終的にこれらを組み合わせると、
public void Run()
{
asd.Engine.Initialize("北海道大戦", 1800, 1000, new asd.EngineOption());
// 下地
var background = new asd.GeometryObject2D();
asd.Engine.AddObject2D(background);
var bgRect = new asd.RectangleShape();
bgRect.DrawingArea = new asd.RectF(0, 0, 1800, 1000);
background.Shape = bgRect;
cities = new List<City>();
var r = new Random();
foreach (var map in mapData.list)
{
City city = new City(map.name, map.point);
cities.Add(city);
}
while (asd.Engine.DoEvents())
{
asd.Vector2DF pos = asd.Engine.Mouse.Position;
if (isOnMaouseMap(pos))
{
foreach (var city in cities)
{
city.OnMouse(pos);
}
}
else
{
var info = Singleton.GetInfomationWindow();
info.ShowText(pos, string.Empty);
}
asd.Engine.Update();
}
asd.Engine.Terminate();
}
private bool isOnMaouseMap(asd.Vector2DF pos)
{
bool ret = false;
foreach (var city in cities)
{
if (city.IsOnMouse(pos))
{
ret = true;
}
}
return ret;
}