好的,键入问题标题我意识到我可以使用数组。但是,我想知道C#中是否有替代方案可用于我正在尝试做的事情。
我有一个电子表格,表示适用于我的游戏船的升级。每个条目都有四列使用int来指定升级类型,另一列使用要应用的数量,允许每个项目中有四次升级。
因此,在我的“船体”电子表格中,我有upgd_1_type,upgd_2_type,upgd_3_type,upgd_4_type,它们指的是七种升级类型中的一种,updg_1_amnt等等,用于申请多少。
我刚刚输入了下面的代码,这是一个很棒的转换语句,包含许多可怕的重复代码,这让我想知道是否有更好的方法来做事。
void GetHullUpgradeVals(GDEHullData hullDat){
switch(hullDat.upgd_1_type){
case (int)UPGRADES_HULL.COST:
upg_Hull_cost *= 1/hullDat.upgd_1_amnt; break;
case (int)UPGRADES_HULL.IMPACT_RES:
upg_Hull_impactRes *= hullDat.upgd_1_amnt; break;
case (int)UPGRADES_HULL.MASS:
upg_Hull_mass *= 1/hullDat.upgd_1_amnt; break;
case (int)UPGRADES_HULL.MAX_HEALTH:
upg_Hull_mxhlth *= hullDat.upgd_1_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_1_amnt; break;
case (int)UPGRADES_HULL.REGEN:
upg_Hull_regen *= hullDat.upgd_1_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_1_amnt; break;
}
switch(hullDat.upgd_2_type){
case (int)UPGRADES_HULL.COST:
upg_Hull_cost *= 1/hullDat.upgd_2_amnt; break;
case (int)UPGRADES_HULL.IMPACT_RES:
upg_Hull_impactRes *= hullDat.upgd_2_amnt; break;
case (int)UPGRADES_HULL.MASS:
upg_Hull_mass *= 1/hullDat.upgd_2_amnt; break;
case (int)UPGRADES_HULL.MAX_HEALTH:
upg_Hull_mxhlth *= hullDat.upgd_2_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_2_amnt; break;
case (int)UPGRADES_HULL.REGEN:
upg_Hull_regen *= hullDat.upgd_2_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_2_amnt; break;
}
switch(hullDat.upgd_3_type){
case (int)UPGRADES_HULL.COST:
upg_Hull_cost *= 1/hullDat.upgd_3_amnt; break;
case (int)UPGRADES_HULL.IMPACT_RES:
upg_Hull_impactRes *= hullDat.upgd_3_amnt; break;
case (int)UPGRADES_HULL.MASS:
upg_Hull_mass *= 1/hullDat.upgd_3_amnt; break;
case (int)UPGRADES_HULL.MAX_HEALTH:
upg_Hull_mxhlth *= hullDat.upgd_3_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_3_amnt; break;
case (int)UPGRADES_HULL.REGEN:
upg_Hull_regen *= hullDat.upgd_3_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_3_amnt; break;
}
switch(hullDat.upgd_4_type){
case (int)UPGRADES_HULL.COST:
upg_Hull_cost *= 1/hullDat.upgd_4_amnt; break;
case (int)UPGRADES_HULL.IMPACT_RES:
upg_Hull_impactRes *= hullDat.upgd_4_amnt; break;
case (int)UPGRADES_HULL.MASS:
upg_Hull_mass *= 1/hullDat.upgd_4_amnt; break;
case (int)UPGRADES_HULL.MAX_HEALTH:
upg_Hull_mxhlth *= hullDat.upgd_4_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_4_amnt; break;
case (int)UPGRADES_HULL.REGEN:
upg_Hull_regen *= hullDat.upgd_4_amnt; break;
case (int)UPGRADES_HULL.PICK_RAD:
upg_Hull_pickRad *= hullDat.upgd_4_amnt; break;
}
}
我会将7个单独的变量换成7个数组并按索引引用,但是有没有办法根据索引选择变量?像指针一样给新变量引用另一个变量?还有其他聪明的解决方案吗?
编辑:使用数组
void GetHullUpgradeVals(GDEHullData hullDat){
upgs_Hull[hullDat.upgd_1_type] = hullDat.upgd_1_amnt;
upgs_Hull[hullDat.upgd_2_type] = hullDat.upgd_2_amnt;
upgs_Hull[hullDat.upgd_3_type] = hullDat.upgd_3_amnt;
upgs_Hull[hullDat.upgd_4_type] = hullDat.upgd_4_amnt;
upgs_Hull[(int)UPGRADES_HULL.COST] = 1/upgs_Hull[(int)UPGRADES_HULL.COST];
upgs_Hull[(int)UPGRADES_HULL.MASS] = 1/upgs_Hull[(int)UPGRADES_HULL.MASS];
}
答案 0 :(得分:0)
我不知道更优雅的解决方案。
嗨大卫,再次!您可以创建自定义变量。它将是参考变量。 HM ...
例如:
public class IntVariable{
public int Value;
public void DivideAndMultiply(){ // for example some method we can create
Value *= 1 / Value;
}
}
并使用它:
IntVariable upg_Hull_cost;
IntVariable upg_Hull_impactRes;
//.....
IntVariable GetVariable(UpgradeType type) {
switch(type) {
case UpgradeType.COST:
return upg_Hull_cost;
case UpgradeType.IMPACT_RES:
return upg_Hull_impactRes;
//....
}
return null;
}
///...
void GetHullUpgradeVals(GDEHullData hullDat){
GetVariable(hullDat.upgd_1_type).Value = hullDat.upgd_1_amnt;
GetVariable(hullDat.upgd_2_type).Value = hullDat.upgd_2_amnt;
//...
GetVariable(UPGRADES_HULL.COST).DivideAndMultiply();
}
希望这有助于您寻找其他解决方案。
您可以在游戏中使用ScriptableObjects
进行数据库,项目,升级,人工智能等。