[英]How to create a multi-dimensional array and have a property attached to a class determine where values are in the array? C#
我正在尝试创建多维数组,作为基于文本的RPG游戏的一种2D地图。 我想创建一个多维数组,例如5x5。 该数组将填充0,以表示对象的空间空白。 其他数字将表示地图上的player
, enemy
, npc
, skill-object
(例如结界桌等),链接到地图的doors
和chests
。 它们都将具有不同的值来表示存在的对象。 如果player
试图走进chest
,它将抢夺chest
。 然后将chest
重置为0来表示空白,让player
走到那里并表示没有更多箱子。 如果player
试图越过enemy
,它将进行战斗(由单独的类和职能进行处理)。 走过代表一door
的数字将链接到代表另一张地图的另一个多维数组。
我想知道我需要在player
类中放置哪种属性来处理他在地图上的位置。 然后,一个函数将更改该值以在地图上移动他。 为地图创建类是一个好主意吗? 我是将对象的值存储在此处还是将这些对象存储在单独的类中? 我将如何处理? 谢谢您的帮助。 作为参考,这是我的玩家课程(请注意,很长的时间...):
public class Player
{
public string name { get; set; } //Name of Player
public int level { get; set; } //Player's combat level (average of combat-related skills' levels)
public int health { get; set; } //Player's health (player dies when this value reaches 0)
public int health_max { get; set; } //Player's maximum health
public int stamina { get; set; } //Player's stamina (used for power attacks, slowly recharges on own)
public int stamina_max { get; set; } //Player's maxiumum stamina
public int fatigue { get; set; } //Player's fatigue rate (player cannot fight or run while fatigue is 100)
public int hunger { get; set; } //Player's hunger level (player becomes weaker as hunger increases)
public int style { get; set; } //Player's fighting style (refer to styles.txt)
//THIEVERY SKILLS - LOCKPICKING / LOCK_P, LUCK / LUCK, PICKPOCKETING / PICK_P, SNEAKING / SNEAK
skill LOCK_P = new skill(); //Ability to pick locks to open locked doors or chests. Trained by unlocking locks successfully. Higher level, better locks.
skill LUCK = new skill(); //The higher the level, the more lucky. Loot from enemies, dungeons, and chests are better. Higher level, higher crit chance. Leveled up 3 times every time your overall thievery levels up.
skill PICK_P = new skill(); //Ability to steal from NPCs' pockets without turning them against you. Trained by sucessfully stealing items.
skill SNEAK = new skill(); //Ability to move unseen. 25% of your sneak level boosts your pickpocketing level. 100% crit chance with an attack from sneaking. Trained by sneaking / escaping combat.
//COMBAT SKILLS - MELEE / MELEE, SORCERY / SOR, MAGICKA / MAGICKA, ARCHERY / ARCHERY, HEAVY ARMOR / H_ARM, LIGHT ARMOR / L_ARM
skill MELEE = new skill(); //Ability to fight better with melee weapons in combat. Trained by dealing damage with melee weapons.
skill SOR = new skill(); //Ability to fight better with sorcerey and spells in combat. Trained by dealing damage with spells.
skill MAGICKA = new skill(); //Affects how many spells you can cast before you must regenerate your magicka pool. Trained by casting spells.
skill ARCHERY = new skill(); //Ability to fight better with ranged weapons, like bows, in combat. Trained by dealing damage with ranged weapons.
skill H_ARM = new skill(); //Affects how effective wearing heavy armor, like metal armor, is. Trained by taking damage while wearing heavy armor.
skill L_ARM = new skill(); //Affects how effective wearing light armor, like leather armor, is. Trained by taking damage while wearing light armor.
//CRAFTSHIP SKILLS - SMITHING / SMITH, CRAFTING / CRAFT, ENCHANTMENT / ENCH, HERBLORE / HERB, FLETCHING / FLETCH
skill SMITH = new skill(); //Ability to create heavy armor and forge melee weapons. Trained by creating mentioned items.
skill CRAFT = new skill(); //Ability to create jewlery to be enchanted. Trained by creating jewlery.
skill ENCH = new skill(); //Ability to enchant items so that they give stat boosts to the wearer. Trained by enchanting items.
skill HERB = new skill(); //Ability to create potions from collected materials and plants. Trained by creating potions.
skill FLETCH = new skill(); //Ability to create bows, arrows, and crossbow stocks. Trained by creating mentioned items.
//MISC. SKILLS - AGILITY / AGILITY, MINING / MINING, WOODCUTTING / WOOD_C, COOKING / COOK, SLAYER / SLAY
skill AGILITY = new skill(); //Ability to pass obstacles. Trained by passing obstacles.
skill MINING = new skill(); //Ability to mine ore from ore veins to be used in smithing. Trained by mining ore.
skill WOOD_C = new skill(); //Ability to cut wood from trees and vines to be used in fletching. Trained by cutting wood.
skill COOK = new skill(); //Ability to cook food to feed hunger / heal health.
skill SLAY = new skill(); //The knowledge of how to slay advanced monsters using special equipment. Trained by completeing tasks.
public void set_all_skills() //Function to set values for all skills. Called once at beginning of game.
{
string[] names = { "Thievery", "Combat", "Craftship", "Misc" }; //Group names for skills
skill[] skills = { LOCK_P, LUCK, PICK_P, SNEAK, MELEE, SOR, MAGICKA, ARCHERY, H_ARM, L_ARM, SMITH, CRAFT, ENCH, HERB, FLETCH, AGILITY, MINING, WOOD_C, COOK, SLAY }; //Array of all the player's skills
for (int i = 0; i < 20; i++) { skills[i].set_level(1); } //For loop to set each level at 1 (for base values).
int counter = 0, name = 0; //Creates variables for the while loop to set tag names.
while (counter < 20) //While loop to set tag names of each skill.
{
if (counter < 4) { skills[counter].set_tag(names[0]); } //First 4 skills are given the first tag
if (counter > 4 && counter < 11) { skills[counter].set_tag(names[1]); } //Next 6 skills are given the second tag
if (counter > 11 && counter < 16) { skills[counter].set_tag(names[2]); } //Next 5 skills are given the third tag
if (counter > 16 && counter < 21) { skills[counter].set_tag(names[3]); } //Last 5 skills are given the last tag
counter++; //Increment the counter by 1.
}
}
//Health / Stamina Alteration Functions
public void take_blunt_damage(int dmg) { this.health -= dmg; } //Decrement player's health by the value of `dmg`
public void take_weak_damage(int dmg, int weak) { this.health -= dmg + weak; } //Take this dmg if the player is weak to the enemy's type of attack
public void take_strong_damage(int dmg, int str) { this.health -= dmg - str; } //Take this dmg if the player is strong to the enemy's type of attack
public void heal(int x) { this.health += x; } //Increment player's health by `x` amount
public void heal() { this.health = health_max; } //Fully heal the player
public void die() { this.health = 0; } //Kill the player by setting health to 0
public void dec_stamina(int x) { this.stamina -= x; } //Decrement player's stamina by `x` amount
public void fill_stamina(int x) { this.stamina += x; } //Increment player's stamina by `x` amount
public void fill_stamina() { this.stamina = stamina_max; } //Fully fill the player's stamina
//Stat Alteration
public void lvl_stat(int x, skill s) { s.level_up_x(x); } //Level up skill `s` by `x` levels
public void lvl_stat(skill s) { s.level_up(); } //Level up skill `s` once
}
对于想要参考的人,这里是我的skill
课:
public class skill
{
public string tag { get; set; } //Tags a sub-skill to a major-skill
public int level { get; set; } //Level of the skill (average of sub-skills' levels)
public int XP { get; set; } //XP towards leveling up
public int[] XP_to_level_up = { 50, 120, 200, 350, 420, 500, 650, 720, 800, 950, 1020, 2000, 3500, 4200, 5000, 6500, 7200, 8000, 9500, 10200, 11000, 12500, 13200, 14000, 15500 }; //XP needed to level up
public void set_tag(string tag) { this.tag = tag; } //Sets the tag of the skill
public void set_level(int x) { this.level = x; } //Set this level to int x
public bool level_up() { this.level++; return true; } //Increment this level by 1
public bool level_up_x(int x) { this.level += x; return true; } //Increment this level by int x
public void add_XP(int x) { this.XP += x; } //Add XP towards the next level
public int calc_where_on_array(int level, int[] XP_to_level_up) //Calculate what int on the array based on your level
{
return XP_to_level_up[level];
}
public bool check_XP(int XP, int where_on_array, int[] XP_to_level_up) //Check if the current XP you have can level you up
{
if (XP >= XP_to_level_up[where_on_array]) //If current XP is greater than what is needed to level up...
{
this.level_up(); //... level this skill up.
return true; //Return true, to show that you successfully leveled up.
}
else { return false; } //If you cannot level up, return false.
}
}
首先,您可以像这样定义2d地图数组:
int[,] map = new int[5, 5];
有关更多信息,请参见MSDN 。 是的,我建议您将地图归类,尽管我想如果您只是在学习这些内容,请在短期内尽一切可能将其拼凑起来!
其次,我认为您的球员可以拥有X,Y位置。 因此,与您的风格保持一致,也许将其添加到播放器类中:
public int x_pos { get; set; }
public int y_pos { get; set; }
然后,您可以使用函数来更新他的位置,然后说map [player.x_pos,player.y_pos] = 1或“ player”的任何代码。
第三,考虑一下您地图上的一个正方形有熔岩。 一个怪物站在那儿。 或某个广场是浅水区,玩家就站在那儿。 您打算如何在一个正方形上处理多个事物? 您当前的设计在那里有一些限制。 它可能变得荒谬:也许您有一个在其上投射有黑暗咒语的广场,其中漂浮着一团毒气,地面上还有10个金块,该金块由光滑的大理石制成,上面有一些苔藓,那里有一个小妖精在睡觉。
如果我正确地理解了您的项目,那么将在Rogue Basin讨论您将尝试做的许多事情。 我建议您到那里戳一下,看看是否有什么对您有用。
最后,您的代码具有完全硬编码技能的skill []技能,实际上并不是大多数程序员处理它的方式。 我会回应乔恩·斯基特(Jon Skeet)的评论,即您应该研究集合。
祝您好运,我想您会在项目中玩得开心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.