我正在使用C#编写一个简单的RPG类游戏,它主要是一个原型,只是测试出来的想法(我知道代码可以改进并且在某些地方很粗糙,但是它的大部分确实有效)。
我目前遇到了战斗系统的障碍。
目前,您控制1个字符。你可以面对最多3个敌人。这是一个基于回合制的ATB系统,这部分工作正常。一旦玩家转身,他们就可以选择攻击并使用它。
我想添加一项新功能,以便在时间效果上造成伤害。然而,这打破了战斗。
我做了一个代表DOT效果的课程:
public class DoT
{
public int Duration { get; set; }
public int Elapsed { get; set; }
public int Damage { get; set; }
public Enemy Target { get; set; }
public String DName { get; set; }
public DoT()
{
Duration = 3;
Elapsed = 0;
DName = "";
}
public void Tick()
{
Elapsed++;
}
}
这是在创建它们时添加到异能中,这个部分有效,我可以为异能添加DOT效果。
在战斗系统中,当选择攻击时,我会创建一个名为DOTS的DOT效果列表。当用户选择攻击时,如果攻击附加了DOT效果,它将被添加到DOT效果列表中:
try
{
string s = ((Control)sender).Text;//Stores name of label that triggered the event
int i = s.Length;//can't remember why I added this
if (playerTarget != null)//check that we have a target
{
if (currentTurn.owner == c && canAttack)//if the current turn is ours and we can attack
{
if (c.GetAbility(s).AbilityCost <= c.currentMP)//if we have enough MP for the attack
{
if(c.GetAbility(s).DamageOverTime!=null)//checks if the attack has a DOT effect
{
c.GetAbility(s).DamageOverTime.Target = playerTarget;//the DOT now targets the target
AddDOT(c.GetAbility(s).DamageOverTime );//Adds the DOT effect to DOTS
//Trace statements I was using for debugging
UpdateTest("Added "+ c.GetAbility(s).AbilityName + " to DOT");
UpdateTest("Time left- " + (c.GetAbility(s).DamageOverTime.Duration-c.GetAbility(s).DamageOverTime.Elapsed).ToString() );
}
currentTurn.ability = c.GetAbility(s);//Sets the current ability to be used
UseTurn();//Executes a turn
}
else
{
MessageBox.Show("Not enough MP!");
}
}
}
else
{
MessageBox.Show("You must select a target!");
}
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
添加DOT效果的方法是:
public void AddDOT(DoT d)
{
int exists = 0;
if (DOTS.Count > 0)
{
foreach (DoT dot in DOTS)
{
if (dot.Equals(d))
{
dot.Elapsed = 0;
exists++;
}
}
if(exists==0)
{
DOTS.Add(d);
}
}
else
{
DOTS.Add(d);
}
这是为了停止正在应用的相同DOT的倍数,如果它已经存在,我们只是重置当前DOT经过的时间。
我还有一种方法可以在每个回合中应用DOT效果:
public void UpdateDots()
{
try
{
if (DOTS.Count > 0)//Check that we have a DOT
{
foreach (DoT d in DOTS)//Loop through each DOT
{
DotDamage(d);//Apply the DOT damage to the DOT's target
d.Tick();//call the tick method of DOT to add 1 to elapsed
//Trace statement to help debug
UpdateTest("Duration on " + d.DName + " is " + (d.Duration - d.Elapsed).ToString());
if (d.Elapsed == d.Duration)//Check if we have ticks left
{
DOTS.Remove(d);//If not, remove the DOT effect
}
}
}
else
{
UpdateTest("No DOTS active");//Trace statement
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
在代码中调用UpdateDots方法以使用转弯:
public void UseTurn()
{
UpdateDots();//Execute any DOT effects
pause.Tick += new EventHandler(PauseTick);//just a timer to delay each turn, I need to move this
UpdateTest("Owner is..."+currentTurn.owner.characterName );//Trace statement
if (currentTurn.owner == c)//Checks if the current turn is by the player (c=player)
{
if (currentTurn.ability.DamageOverTime == null)//if we aren't applying a DOT
{
if (currentTurn.ability.MultiTarget == false)//If it is single target
{
Attack(c, playerTarget, currentTurn.ability);//Single target attack
}
else
{
MultiAttack(playerTargets, currentTurn.ability);//Multi target attack
}
}
lblPlayerTimer.Width = 0;//Resets the label showing the players ATB bar
currentTurn.owner = null;//Free the current owner to be claimed by other characters
canAttack = false;//Can no longer attack
playerTimer.Start();//Start the players timer again
UpdateAbilities();//Simply used to update the display of abilities
}
else if(currentTurn.owner is Enemy)//If an enemy is attacking
{
Enemy en = (Enemy)currentTurn.owner;//Get the details of the enemy attacking
string n = en.characterName;//this was from previous code
Label l = new Label();
l = Controls.Find(en.timer.lblName ,true).FirstOrDefault() as Label;//Get the label being used as the enemy timer
PickEnemyAttack(en);//Select an attack for the enemy
Attack(en, c, currentTurn.ability);//Execute attack for enemy
l.Width = 0;//Reset the enemy ATB bar
currentTurn.owner = null;//Free up the current turn
//This part is just used to cause a short delay before the next turn
pause.Start();
enemyCanAttack = false;
en.timer.Start();
}
ShowTurn();//Updates display of the current turn
turnCount += 1;
CheckWinner();//Checks if the player has won, or lost
}
问题是,当我申请DOT时,它会被应用,并且会对敌人造成伤害。它会继续打勾,直到DOT到期,此时所有计时器都会停止。如果没有DOT效果,则战斗正常。
似乎是从DOTS中删除DOT时引起的。它抛出了一个无效的操作异常,声明该集合已被修改。是因为我删除了一个项目,但是因为我在ForEach循环中,使用枚举删除这些混乱?
正如你所知,我不是名单的专家,我看不出这个问题,我希望有更专业眼光的人能帮助我解决这个问题。
感谢。
答案 0 :(得分:1)
foreach
不允许您修改集合,但您可以使用简单的for
循环手动循环它,然后修改您喜欢的任何内容(因为您控制迭代)。所以不要这样:
foreach (DoT d in DOTS)
{
// do things with d
}
你会有这个:
for (var i = 0; i < DOTS.Count; i++)
{
// do things with DOTS[i]
}
请注意,通过此手动控制,您需要承担更多责任。如果您希望继续在从中删除元素后迭代DOTS
,您需要手动修改i
(将其减1)以反映事实上,您现在正在重新迭代到同一个索引,因为该集合已被修改。但是,如果在从集合中删除项目后,您只需退出循环,那么它不是问题。像这样:
DOTS.Remove(DOTS[i]);
break;
答案 1 :(得分:0)
使用foreach时无法修改数组。请改用。
答案 2 :(得分:0)
要考虑的一个选择是改变:
foreach (DoT d in DOTS)
为:
foreach (DoT d in DOTS.ToList())
这样,你迭代的东西与'原始'DOTS
数组/列表不同。这意味着您可以更改DOTS
(添加/删除项目等)。