c# - It shows more items in array than expected -
it shows more times expected , when add 1 item , stackable adds stack....but not once 2 times.
using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using system; using system.collections.generic; using system.linq; using system.text; namespace arenarpg { public class inventory { int slotwight; int slotheight; int itemsperpage = 1; int currentpage; int columns, rows; b_item[,] items; backpack backpack; vector2 pos; public inventory(backpack bpack,int itemsperpage,int columns,int rows, int slotwidth, int slotheight, vector2 pos) { this.backpack = bpack; this.slotheight = slotheight; this.slotwight = slotwidth; this.pos = pos; this.columns = columns; this.itemsperpage = itemsperpage; this.rows = rows; items = new b_item[columns, rows]; loaditems(bpack); } public void update(gametime gametime) { double maxpages = math.ceiling((double)(items.length - 1) / itemsperpage); if (input.keypressed(keys.a)) { currentpage++; } if (input.keypressed(keys.d)) { currentpage--; } input.update(gametime); } public void draw(spritebatch spritebatch) { var result = items.cast<b_item>().skip(itemsperpage * (currentpage-1)).take(itemsperpage); foreach (var item in result) { (int x = 0; x < columns; x++) { (int y = 0; y < rows; y++) { int drawx = (int)pos.x + (x * (slotwight + 2)); int drawy = (int)pos.y + (y * (slotwight + 2)); if(items[x,y] != null) { spritebatch.draw(item.texure, new rectangle(drawx, drawy ,32, 32), new rectangle(0, 0, 64, 64), color.white); if (items[x,y].stacksize > 1) {
spritebatch.drawstring(assetmanager.getinstance().font["arial8"], items[x,y].stacksize.tostring(), new vector2(drawx+ 20, drawy+20), color.darkblue); } spritebatch.drawstring(assetmanager.getinstance().font["arial8"], currentpage.tostring(), new vector2(50, 50), color.darkblue); }
} } } } public void loaditems(backpack bpack) { int itemindex = 0; (int x = 0; x < columns; x++) { (int y = 0; y < rows; y++) { if (itemindex < bpack.items.count) { items[x, y] = bpack.items[itemindex]; itemindex++; } } } } } }
backpack simple
public void additems(b_item newitem) { if (items.count < maxslots) { foreach (var in items.toarray()) { if (i.item_name == newitem.item_name ) { if (i.is_stackable == true && i.stacksize < 500) { i.stacksize += 1; } } else { items.add(newitem); } } } else { items.add(newitem); } } private void remove(b_item itemid) { (int = 0; < items.count; i++) { if (items.count > 0) { items[i].stacksize -= 1; } items.removeat(i); } }
why draw more items specified value itemsperpage
in inventory. constructor
inventory thiss = new inventory(new playerback(),2,1,32,32, 32, new vector2(0, 0))
ok, sorry comment it's abit wrong. here correct method:
public void additems(b_item newitem) { if (items.count < maxslots) { foreach (var in items.toarray()) { if (i.item_name == newitem.item_name ) { if (i.is_stackable == true && i.stacksize < 500) { i.stacksize += 1; return; } } } } items.add(newitem); }
Comments
Post a Comment