前回までの状況はこちら。
最新ソースはこちら(gitHub)
https://github.com/takishita2nd/HokkaidoWar
以前作成したこれ。
これの「自分の行動」の部分を作成していきます。
while (asd.Engine.DoEvents())
{
asd.Vector2DF pos = asd.Engine.Mouse.Position;
switch (gameStatus)
{
case GameStatus.SelectCity:
cycleProcessSelectCity(pos);
break;
case GameStatus.ActionEnemy:
cycleProcessActionEnemy(pos);
break;
case GameStatus.ActionPlayer:
cycleProcessActionPlayer(pos);
break;
}
if (asd.Engine.Mouse.LeftButton.ButtonState == asd.ButtonState.Push)
{
switch (gameStatus)
{
case GameStatus.SelectCity:
onClickMouseSelectCity(pos);
break;
case GameStatus.ActionEnemy:
break;
case GameStatus.ActionPlayer:
onClickMouseActionPlayer(pos);
break;
}
}
asd.Engine.Update();
}
分かりやすいようにサブルーチン化した。
private void cycleProcessActionPlayer(asd.Vector2DF pos)
{
_battle.MyTurn(_player.City);
_player.City.PaintAttackColor();
var info = Singleton.GetInfomationWindow();
info.ShowText(pos, "都市を選択してください\r\n");
var cities = _player.City.GetLinkedCities();
foreach(var city in cities)
{
if (city.IsOnMouse(pos))
{
city.OnMouse(pos);
city.PaintDeffenceColor();
}
else
{
city.ClearPaint();
}
}
}
public void MyTurn(City player)
{
if (lastDeffece != null)
{
lastDeffece.ClearPaint();
}
if (lastAttack != null)
{
lastAttack.ClearPaint();
}
var info = Singleton.GetGameProcessInfomation();
info.ShowText(player.GetPosition(), string.Format("{0} turn {1} / {2} {3}", turn, cityCnt + 1, _cities.Count, player.Name));
}
周期処理です。
まず、自分の都市を赤くします。
攻撃対象を選択するのですが、自分の都市の上にマウスカーソルがある場合のみ、その都市を青く表示します。
private City _target = null;
private void onClickMouseActionPlayer(asd.Vector2DF pos)
{
var info = Singleton.GetInfomationWindow();
info.ShowText(pos, String.Empty);
var cities = _player.City.GetLinkedCities();
foreach (var city in cities)
{
if (city.IsOnMouse(pos))
{
_target = city;
_battle.MyTrunAttack(_player.City, _target);
gameStatus = GameStatus.ShowResult;
}
}
}
public void MyTrunAttack(City player, City target)
{
var info = Singleton.GetGameProcessInfomation();
var r = Singleton.GetRandom();
lastAttack = player;
lastDeffece = target;
double attack = lastAttack.Population * (double)(r.Next(minRate, maxRate) / 10.0);
double deffence = lastDeffece.Population * (double)(r.Next(minRate, maxRate) / 10.0);
if (attack > deffence)
{
info.ShowText(lastAttack.GetPosition(), string.Format("{0} turn {1} / {2} {3}\r\ntarget {4} \r\n{5} vs {6}\r\nwin",
turn, cityCnt + 1, _cities.Count, lastAttack.Name, lastDeffece.Name, (int)attack, (int)deffence));
lastAttack.CombinationCity(lastDeffece);
_cities.Remove(lastDeffece);
lastDeffece = null;
}
else
{
info.ShowText(lastAttack.GetPosition(), string.Format("{0} turn {1} / {2} {3}\r\ntarget {4} \r\n{5} vs {6}\r\nlose",
turn, cityCnt + 1, _cities.Count, lastAttack.Name, lastDeffece.Name, (int)attack, (int)deffence));
}
}
マウスクリック時の処理です。
マウスカーソルにある都市に対して攻撃処理を行います。
攻撃対象は記憶しておきます。