How to create a multi-dimensional array and have a property attached to a class determine where values are in the array? C# -
i'm trying create multidimensional array sort of 2d map text-based rpg game. want create multidimensional array that's, example, 5x5. array filled 0s represent space void of object. other numbers represent player
on map, enemy
, npc
, skill-object
(like enchantment table, etc.), doors
link maps, , chests
. have different values represent object there. if player
tries walking chest
, instead loot chest
. chest
reset 0 represent empty space, letting player
walk there , representing there no more chest. if player
tries walking on enemy
, instead engage combat (handled separate class , functions). walking on number representing door
link multidimensional array represents map.
i want know kind of property need put inside player
class handle on map. function change value move him around map. creating class map idea? store values objects there or in individual classes objects? how go this? thank help. reference, here's player class (careful, it's long...):
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 value reaches 0) public int health_max { get; set; } //player's maximum health public int stamina { get; set; } //player's stamina (used power attacks, 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 100) public int hunger { get; set; } //player's hunger level (player becomes weaker hunger increases) public int style { get; set; } //player's fighting style (refer styles.txt) //thievery skills - lockpicking / lock_p, luck / luck, pickpocketing / pick_p, sneaking / sneak skill lock_p = new skill(); //ability pick locks open locked doors or chests. trained unlocking locks successfully. higher level, better locks. skill luck = new skill(); //the higher level, more lucky. loot enemies, dungeons, , chests better. higher level, higher crit chance. leveled 3 times every time overall thievery levels up. skill pick_p = new skill(); //ability steal npcs' pockets without turning them against you. trained sucessfully stealing items. skill sneak = new skill(); //ability move unseen. 25% of sneak level boosts pickpocketing level. 100% crit chance attack sneaking. trained 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 fight better melee weapons in combat. trained dealing damage melee weapons. skill sor = new skill(); //ability fight better sorcerey , spells in combat. trained dealing damage spells. skill magicka = new skill(); //affects how many spells can cast before must regenerate magicka pool. trained casting spells. skill archery = new skill(); //ability fight better ranged weapons, bows, in combat. trained dealing damage ranged weapons. skill h_arm = new skill(); //affects how effective wearing heavy armor, metal armor, is. trained taking damage while wearing heavy armor. skill l_arm = new skill(); //affects how effective wearing light armor, leather armor, is. trained taking damage while wearing light armor. //craftship skills - smithing / smith, crafting / craft, enchantment / ench, herblore / herb, fletching / fletch skill smith = new skill(); //ability create heavy armor , forge melee weapons. trained creating mentioned items. skill craft = new skill(); //ability create jewlery enchanted. trained creating jewlery. skill ench = new skill(); //ability enchant items give stat boosts wearer. trained enchanting items. skill herb = new skill(); //ability create potions collected materials , plants. trained creating potions. skill fletch = new skill(); //ability create bows, arrows, , crossbow stocks. trained creating mentioned items. //misc. skills - agility / agility, mining / mining, woodcutting / wood_c, cooking / cook, slayer / slay skill agility = new skill(); //ability pass obstacles. trained passing obstacles. skill mining = new skill(); //ability mine ore ore veins used in smithing. trained mining ore. skill wood_c = new skill(); //ability cut wood trees , vines used in fletching. trained cutting wood. skill cook = new skill(); //ability cook food feed hunger / heal health. skill slay = new skill(); //the knowledge of how slay advanced monsters using special equipment. trained completeing tasks. public void set_all_skills() //function set values skills. called once @ beginning of game. { string[] names = { "thievery", "combat", "craftship", "misc" }; //group names 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 player's skills (int = 0; < 20; i++) { skills[i].set_level(1); } //for loop set each level @ 1 (for base values). int counter = 0, name = 0; //creates variables while loop set tag names. while (counter < 20) //while loop set tag names of each skill. { if (counter < 4) { skills[counter].set_tag(names[0]); } //first 4 skills given first tag if (counter > 4 && counter < 11) { skills[counter].set_tag(names[1]); } //next 6 skills given second tag if (counter > 11 && counter < 16) { skills[counter].set_tag(names[2]); } //next 5 skills given third tag if (counter > 16 && counter < 21) { skills[counter].set_tag(names[3]); } //last 5 skills given last tag counter++; //increment counter 1. } } //health / stamina alteration functions public void take_blunt_damage(int dmg) { this.health -= dmg; } //decrement player's health value of `dmg` public void take_weak_damage(int dmg, int weak) { this.health -= dmg + weak; } //take dmg if player weak enemy's type of attack public void take_strong_damage(int dmg, int str) { this.health -= dmg - str; } //take dmg if player strong enemy's type of attack public void heal(int x) { this.health += x; } //increment player's health `x` amount public void heal() { this.health = health_max; } //fully heal player public void die() { this.health = 0; } //kill player setting health 0 public void dec_stamina(int x) { this.stamina -= x; } //decrement player's stamina `x` amount public void fill_stamina(int x) { this.stamina += x; } //increment player's stamina `x` amount public void fill_stamina() { this.stamina = stamina_max; } //fully fill player's stamina //stat alteration public void lvl_stat(int x, skill s) { s.level_up_x(x); } //level skill `s` `x` levels public void lvl_stat(skill s) { s.level_up(); } //level skill `s` once }
and want reference, here's skill
class:
public class skill { public string tag { get; set; } //tags sub-skill major-skill public int level { get; set; } //level of skill (average of sub-skills' levels) public int xp { get; set; } //xp towards leveling 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 level public void set_tag(string tag) { this.tag = tag; } //sets tag of skill public void set_level(int x) { this.level = x; } //set level int x public bool level_up() { this.level++; return true; } //increment level 1 public bool level_up_x(int x) { this.level += x; return true; } //increment level int x public void add_xp(int x) { this.xp += x; } //add xp towards next level public int calc_where_on_array(int level, int[] xp_to_level_up) //calculate int on array based on level { return xp_to_level_up[level]; } public bool check_xp(int xp, int where_on_array, int[] xp_to_level_up) //check if current xp have can level { if (xp >= xp_to_level_up[where_on_array]) //if current xp greater needed level up... { this.level_up(); //... level skill up. return true; //return true, show leveled up. } else { return false; } //if cannot level up, return false. } }
first, can define 2d map array this:
int[,] map = new int[5, 5];
see msdn on multidimensional arrays more. yes suggest putting map class although if learning stuff whatever can cobble in short term guess!
second, think player have x, y position. keeping style above, maybe add player class:
public int x_pos { get; set; } public int y_pos { get; set; }
you can use function update location , afterward map[player.x_pos, player.y_pos] = 1, or whatever code "player" is.
third, consider maybe square on map has lava. , monster standing there. or square shallow water, , player standing there. how plan handle having multiple things on 1 square? current design has bit of limitation there. can ridiculous: maybe have square has darkness spell cast on it, cloud of poison gas floating there, along 10 gold pieces on ground, made of smooth marble moss on it, , there goblin sleeping there.
if understand project correctly, many of things trying discussed here @ rogue basin. advise poke around in there , see if useful you.
finally, code skill[] skills full of hard-coded skills not way programmers handle it. echo jon skeet's comment should collections.
best of luck, think you'll have fun playing around project.
Comments
Post a Comment