c# - Optimize procedurally generated rectangle map position updates -


i procedurally-generating 2d cavern made of rectangles in c# usign monogame. generation process works right, i'm having problems amount of operations , processing power taking move such cavern (updating position).

here's how update caverns's position:

the cavern class constructor:

  1. i parametrize viewport's rectangle, tiles dimensions, , percentage of 'fillness' of cavern.
  2. i use viewport's dimensions create container rectangle, twice big viewport.
  3. i generate new map storing in (basicsprite)arraylist. basicsprite class specified below. resulting cavern same size , in same place container.

    public cavern(rectangle viewport, int tilewidth, int tileheight, int randomfillpercent) {     this.viewport = viewport;      container.x = viewport.x - (viewport.width / 2);     container.y = viewport.y - (viewport.height / 2);     container.width = 2 * viewport.width;     container.height = 2 * viewport.height;      supercontainer.x = container.x - (container.width / 2);     supercontainer.y = container.y - (container.height / 2);     supercontainer.width = 2 * container.width;     supercontainer.height = 2 * container.height;      tilelist = new arraylist();     generator = new caverngenerator(container, tilewidth, tileheight, randomfillpercent);     //generator = new caverngenerator(viewport.width / 20, viewport.height / 20, tilewidth, tileheight, randomfillpercent);     mousemove = new mousemove();      tilelist = generator.generatecavern(); } 

the container:

container dimensions this: container , viewport dimensions

the basicsprite class:

the basicsprite class includes has 3 main variables: texture, destinationrectangle , spritecolor, of type texture2d, rectangle , color respectively. has methods loadcontent() , draw(). important stuff:

class basicsprite {     private texture2d texture;     private rectangle destinationrectangle;     private color spritecolor;      public void loadcontent(contentmanager content, string filename)     {         texture = content.load<texture2d>(filename);     }      public void draw(spritebatch spritebatch)     {         spritebatch.begin();         spritebatch.draw(texture, destinationrectangle, spritecolor);         spritebatch.end();     } } 

the problem: cavern class' update , draw methods

the cavern class' update method might benefit finite state machine design, switching states instead of checking if keys down.

  1. a temporal rectangle (tmprectangle) generated, going used limit how far cavern can moved (it can't keep moving after x,y position, come back).
  2. if keys down, program checks key pressed, , updates every rectangle in cavern.

    public void update() {     rectangle tmprectangle;      if (keyboard.getstate().iskeydown(keys.left) ||         keyboard.getstate().iskeydown(keys.right) ||         keyboard.getstate().iskeydown(keys.up) ||         keyboard.getstate().iskeydown(keys.down))     {         (int = 0; < tilelist.count; i++)         {             tmprectangle = ((tile)tilelist[i]).destinationrectangle;              if (keyboard.getstate().iskeydown(keys.left))                 tmprectangle.x -= 3;             if (keyboard.getstate().iskeydown(keys.right))                 tmprectangle.x += 3;             if (keyboard.getstate().iskeydown(keys.up))                 tmprectangle.y -= 3;             if (keyboard.getstate().iskeydown(keys.down))                 tmprectangle.y += 3;              ((tile)tilelist[i]).destinationrectangle = tmprectangle;         }     }     } 

the draw method more straight forward: each basicsprite (texture2d, rectangle , color) in tilelist arraylist drawn.

public void draw(spritebatch spritebatch) {     (int = 0; < tilelist.count; i++)         ((tile)tilelist[i]).draw(spritebatch); } 

the problem instantiated:

this how monogame's game1 class looks like, details ommited:

public class game1 : game {     /* details ommited */              cavern cavern;     bool play;      public game1()     {         graphics = new graphicsdevicemanager(this);         content.rootdirectory = "content";     }      protected override void initialize()     {         play = true;          graphics.isfullscreen = true;         graphics.applychanges();          /* details ommited */          // creates cavern, passing viewport's rectangle:          // (x: 0; y: 0; width: 800; height: 480)           // passes tilewidth , tileheight         // 45% 'fillness'         cavern = new cavern(             graphicsdevice.viewport.bounds,             20, 20, 45);          base.initialize();     }      /* loadcontent() method ommited */      protected override void update(gametime gametime)     {         if (play)         {             cavern.update();         }          base.update(gametime);     }      protected override void draw(gametime gametime)     {         cavern.draw(spritebatch);         base.draw(gametime);     } } 

with container's width , height of 1600 , 960 respectively, , tilewidth , tileheight both of 20, tilelist arraylist holds total of 80 x 48 basicsprites, e.i. (1600/20) x (960/20), that 3,840 basicsprites.

so, each time cavern updated , key pressed, computer has check if , arrow key being pressed, , update 3,840 basicsprites' position rectangles.

how can optimize position updates?
is there way procedurally generate 2d caverns able update positions , collide rectangles?


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -