2017-03-14 10 views
0

私はゲームを作成していて、約10000のようなオブジェクトの束を扱う助けが必要です。私のゲームでは、ランダムな量の岩石が1milマップには、オブジェクトをリストに追加して、それらを更新して描画しますが、それは遅いです。私はこの問題のいくつかの助けが本当に多くの物を扱うことを望む多くの学習者を助けると思う。ここで は、私の世代のコードです:C#XNA、たくさんのオブジェクトを扱う、

public void WorldGeneration() 
    { 
     //Random Compatibility 
     Random rdm = new Random(); 

     //Tile Variables 
     int tileType; 
     int tileCount = 0; 
     Rock nearestRock; 

     //Initialize Coordinates 
     Vector2 tileSize = new Vector2(48f, 48f); 
     Vector2 currentGenVector = new Vector2(48f, 48f); 
     int worldTiles = 1000000; 

     //Do tile generation 
     for(int tile = 1; tile <= worldTiles; tile += 1) 
     { 
      //Generate Classes 
      tileType = rdm.Next(0, 42); 
      if (tileType == 1) 
      { 
       if (rocks.Count != 0) 
       { 
        //Check Rock Distance 
        nearestRock = rocks.FirstOrDefault(x => Vector2.Distance(x.Location, currentGenVector) < 128); 
        if (nearestRock == null) 
        { 
         Rock rock = new Rock(rockSprite, currentGenVector); 
         rocks.Add(rock); 
        } 
       } 
       if (rocks.Count == 0) 
       { 
        Rock rock = new Rock(rockSprite, currentGenVector); 
        rocks.Add(rock); 
       } 
      } 

      //Move Generation Tile 
      if (tileCount == worldTiles/1000) 
      { 
       currentGenVector.X = tileSize.X; 
       currentGenVector.Y += tileSize.Y; 
       tileCount = 0; 
      } 
      else 
      { 
       currentGenVector.X += tileSize.X; 
      } 
      //Keep Count of Tiles per layer. 
      tileCount += 1; 
     } 

そして、ここでは私の岩コードです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Graphics; 

namespace Game2 
{ 
    class Rock 
    { 
     //Draw Support 
     Texture2D sprite; 
     Rectangle drawRectangle; 

    //Support 
    Vector2 location; 
    bool updating = false; 

    //Active 
    bool active = true; 

    public Rock(Texture2D sprite, Vector2 location) 
    { 
     //Initialize Location/Drawing 
     this.sprite = sprite; 
     this.location = location; 

     drawRectangle.Width = sprite.Width; 
     drawRectangle.Height = sprite.Height; 

     drawRectangle.X = (int)location.X - sprite.Width/2; 
     drawRectangle.Y = (int)location.Y - sprite.Height/2; 
    } 

    public void Update(GameTime gameTime, MouseState mouse) 
    { 
     //Mining 
     if (drawRectangle.Contains(mouse.X, mouse.Y)) 
     { 
      if (mouse.LeftButton == ButtonState.Pressed) 
      { 
       location.X = -800; 
       location.Y = -800; 
      } 
     } 
     drawRectangle.X = (int)location.X; 
     drawRectangle.Y = (int)location.Y; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     //Draws The Sprite 
     spriteBatch.Draw(sprite, drawRectangle, Color.White); 
    } 

    //Get Location 
    public Vector2 Location 
    { 
     get { return location; } 
    } 
    public bool Updating 
    { 
     get { return updating; } 
    } 
    public void setUpdating(bool updating) 
    { 
     this.updating = updating; 
    } 
    public Rectangle DrawRectangle 
    { 
     get { return drawRectangle; } 
    } 
} 
} 

私はちょうど PLSのヘルプは高く評価され、これらすべてのオブジェクトを処理する方法についていくつかのヒントを求めている

+0

どちらが遅いですか?更新ループまたは描画ループ?現在のビューポートにない岩を無視することができます。 – itsme86

+0

私はそれがアップデートループだと思うが、私はそれを試してみるだろう、どうすればいい?申し訳ありません。私はXNAとC#の新機能で、まだ学習しています。 – Luny

答えて

0

多くのオブジェクトを扱う方法は、2つのベクトル間の距離を計算し、それを使って現在のオブジェクトが描画に十分近いかどうかをチェックする関数を作成することです。

//Distance checking code 
public float GetDistance(Vector2 v1, Vector2 v2){ 
    float d = Math.Sqrt(Math.Abs((v1.X * v1.X) - (v2.X * v2.X)) 
    + Math.Abs((v1.Y * v1.Y) - (v2.Y * v2.Y))); 
return d; 
} 

//example of using the Distance Check 
if(GetDistance(player.position, rock.position) < 1280){ 
    rock.Update(); 
} 

これは私の頭の上からのものですから、コードが正しく動作しない可能性がありますが、これで十分です。がんばろう!

関連する問題