2017-03-14 11 views
-3

私は自分自身と私の家族のためにMenuStripを戦闘のアクションコマンドとして使用しています。しかし、私は魔法のClick機能をどのようにセットアップするのかを決めることはできません。 menustripはほとんど空白から始まるので、私は呪文とターゲットのリストを作成することも、それらの呪文のクリックメソッドを設定することもできません。私はあなたがサブクラスにあなたのコードを分割開始すべきだと思う...行き詰まりのビットでMenuStripItemのクリックメソッドを設定するにはどうすればいいですか?

This is what it looks like

string[] Spells = { }; 
    string[] Elements = { }; 
    string[] Targets = { }; 
    double[] Power = { }; 
    double[] ManaCost = { }; 

    StreamReader strReader = null; 

      try 
      { 
       FileStream flsStream = new FileStream(Application.StartupPath + "\\Spells.txt", FileMode.Open, FileAccess.Read); 
       strReader = new StreamReader(flsStream); 
       while (strReader.Peek() != -1) 
       { 
        string line = strReader.ReadLine(); 
        string[] lines = line.Split(':'); 

        Array.Resize(ref Spells, Spells.Length + 1); 
        Array.Resize(ref Elements, Elements.Length + 1); 
        Array.Resize(ref Targets, Targets.Length + 1); 
        Array.Resize(ref Power, Power.Length + 1); 
        Array.Resize(ref ManaCost, ManaCost.Length + 1); 

        Spells[Spells.Length - 1] = lines[0]; 
        Elements[Elements.Length - 1] = lines[1]; 
        Targets[Targets.Length - 1] = lines[2]; 
        Power[Power.Length - 1] = Convert.ToDouble(lines[3]); 
        ManaCost[ManaCost.Length - 1] = Convert.ToDouble(lines[4]); 
       } 
       for (int x = 0; x < Spells.Length; x++) 
       { 
        mnuMagic.DropDownItems.Add(Spells[x]); 
        mnuMagic.DropDownItems[x].Name = string.Format("tsmi{0}", Spells[x]); 
        mnuMagic.DropDownItems[x].Text += string.Format(", {0} MP", ManaCost[x]); 
        if (Targets[x] == "Single") 
        { 
         try 
         { 
          if (Foe1.Status != "Dead") 
          { 
           if (Foe1.Health > Foe1.MaxHealth/2) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[0]); 
           else if (Foe1.Health > Foe1.Health/4) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[1]); 
           else 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe1, imgEnemyStatus.Images[2]); 
          } 
          if (Foe2.Status != "Dead") 
          { 
           if (Foe2.Health > Foe2.MaxHealth/2) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[0]); 
           else if (Foe2.Health > Foe2.Health/4) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[1]); 
           else 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe2, imgEnemyStatus.Images[2]); 
          } 
          if (Foe3.Status != "Dead") 
          { 
           if (Foe3.Health > Foe3.MaxHealth/2) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[0]); 
           else if (Foe3.Health > Foe3.Health/4) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[1]); 
           else 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe3, imgEnemyStatus.Images[2]); 
          } 
          if (Foe4.Status != "Dead") 
          { 
           if (Foe4.Health > Foe4.MaxHealth/2) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[0]); 
           else if (Foe4.Health > Foe4.Health/4) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[1]); 
           else 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe4, imgEnemyStatus.Images[2]); 
          } 
          if (Foe5.Status != "Dead") 
          { 
           if (Foe5.Health > Foe5.MaxHealth/2) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[0]); 
           else if (Foe5.Health > Foe5.Health/4) 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[1]); 
           else 
            (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(strFoe5, imgEnemyStatus.Images[2]); 
          } 
         } 
         catch { } 
        } 
        else if (Targets[x] == "Ally") 
        { 
         try 
         { 
          (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter1.Name,imgEnemyStatus.Images[0]); 
          if (Fighter2.Name != null) 
           (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter2.Name); 
          if (Fighter3.Name != null) 
           (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add(Fighter3.Name); 
         } 
         catch { } 
        } 
        else if (Targets[x] == "All") 
        { 
         (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add("All Enemies"); 
        } 
        else if (Targets[x] == "Team") 
         (mnuMagic.DropDownItems[x] as ToolStripMenuItem).DropDownItems.Add("All Allies"); 

        if (Elements[x] == "Fire") 
         mnuMagic.DropDownItems[x].ForeColor = Color.Red; 
        else if (Elements[x] == "Water") 
         mnuMagic.DropDownItems[x].ForeColor = Color.Blue; 
        else if (Elements[x] == "Earth") 
         mnuMagic.DropDownItems[x].ForeColor = Color.FromArgb(255, 128, 0); 
        else if (Elements[x] == "Wind") 
         mnuMagic.DropDownItems[x].ForeColor = Color.LimeGreen; 
        if (ManaCost[x] > Fighter1.Mana) { mnuMagic.DropDownItems[x].Enabled = false; } 
       } 
      } 
      catch { } 
     } 
+0

あなたが試したこと、使用中のコード、スクリーンショットがうまくいかないことを教えてください。 – Nico

答えて

0

です。小さなゲームでも非常に役立ちます。私が一緒に手を加えた少しのコードがあります。あなたが持っているものはすべて処理するわけではありませんが、その機能を簡単に拡張することはできます。追加情報が必要な場合は教えてください。

public partial class Form1 : Form 
{ 
    private double CurrentMana { get; set; } = 50; 
    private List<BasicSpell> m_lstSpells = new List<BasicSpell>(); 
    private List<BasicTarget> m_lstTargets = new List<BasicTarget>(); 

    public Form1() 
    { 
     InitializeComponent(); 

     Init(); 

     try 
     { 
      ToolStripMenuItem tsmiMagic = new ToolStripMenuItem("Magic"); 

      foreach (BasicSpell spell in m_lstSpells) 
      { 
       ToolStripMenuItem tsmiSpell = spell.ToToolStripMenuItem(); 

       if (spell.CanHitAllEnemies) 
       { 
        ToolStripItem tsiAll = tsmiSpell.DropDownItems.Add("All"); 
        tsiAll.Click += (sender, arguments) => m_lstTargets.ForEach((target) => TryCastSpell(spell, target)); 
       } 
       else { } // This spell can not hit all enemies 

       for (int i = 0; i < m_lstTargets.Count; i++) 
       { 
        BasicTarget target = m_lstTargets[i]; 

        ToolStripItem tsiTarget = tsmiSpell.DropDownItems.Add($"Target {i}: {target.Name}"); 
        tsiTarget.Image = target.StatusImage; 
        tsiTarget.Click += (sender, arguments) => TryCastSpell(spell, target); 
       } 

       tsmiMagic.DropDownItems.Add(tsmiSpell); 
      } 

      m_tsCombatOptions.Items.Add(tsmiMagic); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show($"{ex}"); 
     } 
    } 

    private void TryCastSpell(BasicSpell spell, BasicTarget target) 
    { 
     if (!spell.Cast(target)) 
     { 
      MessageBox.Show($"You can not cast this spell with your current mana!"); 
     } 
    } 

    private void Init() 
    { 
     m_lstSpells.Add(new Fireball()); 
     m_lstSpells.Add(new Fireball()); 
     m_lstSpells.Add(new Fireball()); 
     m_lstSpells.Add(new Fireball()); 

     m_lstTargets.Add(new Monster("Reißzahn", 500)); 
     m_lstTargets.Add(new Monster("Reißzahn", 500)); 
     m_lstTargets.Add(new Monster("Reißzahn", 500)); 
     m_lstTargets.Add(new Monster("Reißzahn", 500)); 
     m_lstTargets.Add(new Monster("Reißzahn", 500)); 
    } 
} 

// TODO: POWER? 

[Flags] 
public enum Element 
{ 
    Fire = 0x0, 
    Earth = 0x1, 
    Water = 0x2, 
    Wind = 0x4 
} 

public abstract class BasicSpell 
{ 
    public string Name { get; set; } 
    public Element DamageType { get; set; } 
    public double ManaCost { get; set; } 
    public bool CanHitAllEnemies { get; set; } = false; 

    public abstract bool Cast(BasicTarget target); 

    public ToolStripMenuItem ToToolStripMenuItem() 
    { 
     ToolStripMenuItem tsmiSpell = new ToolStripMenuItem(); 
     tsmiSpell.Text = $"{this.Name}, {ManaCost} MP"; 

     return tsmiSpell; 
    } 
} 

public class Fireball : BasicSpell 
{ 
    public override bool Cast(BasicTarget target) 
    { 
     // TODO: Check if castable (maybe add caster as parameter) 

     target.HP -= 500; 
     return true; 
    } 
} 

public abstract class BasicTarget 
{ 
    public enum CombatStatus 
    { 
     Alive = 0, 
     Dead = 1 
    } 

    public enum TargetRelation 
    { 
     Enemy = 0, 
     Ally = 1 
    } 

    public string Name { get; set; } 
    public double HP { get; set; } 
    public CombatStatus Status { get; set; } 
    public TargetRelation Relation { get; set; } 

    public Image StatusImage => null; 
} 

public class Monster : BasicTarget 
{ 
    public Monster(string strName, int nHP) 
    { 
     this.Name = strName; 
     this.HP = nHP; 
     this.Status = CombatStatus.Alive; 
     this.Relation = TargetRelation.Enemy; 
    } 
} 
関連する問題