2011-07-25 38 views
0

如果問題標題不清楚,但用我便宜的英語,我很抱歉,我無法找到明確提出問題的方法。如何檢測玩家是否相交世界的一部分

但我可以長期解釋。所以我已經意識到如果我設計我的世界(和世界,我的意思是整個遊戲,它將是一個級別)10.000x10.000 ...這將是非常足夠的,除了少數另一個精靈(和我意思是4或5,最大值爲50x50,沒什麼大的。)

所以我想,爲什麼我不把我的整個地圖做成10.000x10.000(或者說可以說噸的512x512)圖片? 但我有一個問題,你可以「交互」的東西很少。他們會(和他們在一起,我的意思是我的「world.jpg」中的東西)總是停留在同一個地方,但是玩家(實際上是你所知道的一個精靈)將會移動,因此我的10.000x10.000將「移動」。

所以看下面的圖片,有一個黑點是「玩家」和紅點,也就是說,一個門。

世界總是以黑點爲中心,除非他走到世界的盡頭。正如你所看到的,當他向東移動一點點時(看圖片第1部分和第2部分),紅點看起來會移動。但我只是移動了我的10.000x10.000圖像。那就是我對10kx10k圖片上的東西的意思。

不管怎麼說,但你可以在PIC的最後部分看到,當他去附近的紅點,我想我的「行動」

怎麼辦呢?下面

-part是不是真的涉及到的主要問題

是否使用10kx10 PIC,而不是出現在世界,他的動作時,另一個精靈有用嗎?但如果我想這樣做,不僅我會檢查他是否在附近,但我也會檢查他的觀點,以瞭解我是否應該或不應該向他展示精靈。

如果我在展示自己想要的座標時顯示自己的東西,或者正在使用一張大圖片,那麼它會更有用嗎?

謝謝。

+1

真的很難理解你的問題。我建議試着讓某人爲你翻譯它。 –

回答

1

我建議在地圖的結構有點像這個..

public class Map 
{ 
    public MapPoint[,] mapPoints;  //the map 
    public Player player;    //the player/user object 
    public Vector2 DrawHeroPosition;  
    //where at the screen the player is going to be drawn 
    public Vector2 RangeStart;   
    //what part of the map who is going to be drawn 
    public int SizeX;  //number of mapPoints the screen can contain at one time 
    public int SizeY;  //number of mapPoints the screen can contain at one time 

    //MapPoint represents a specific 512x512 point (mapPoint) its position at 
    //the map but also includes the sprite that is going to be drawn and objects 
    //that the player can interact with at that place (like the door) 

    //the player object includes reference to where in the world it is place 

    public Map(ContentManager theContentManager, int x, int y) 
    { 
     MapSizeX = x; 
     MapSizeY = y; 
     int ScreenSizeX = 9; 
     int ScreenSizeY = 9; 
     mapPoints = new MapPoint[MapSizeX , MapSizeY]; 

     //ad code for generating/creating map... 
     //important that you store the MapPoints position inside each mapPoint 

     player = new Player(mapPoints[0,0]); //crate a player who knows where he is 
    } 

    public void Update() 
    { 
     //in the update method you do a lot of things like movement and so 
     //set what part of the map the game should draw if the game for example 
     //can show 9x9 512points at a single time 

     //give range value from the players position 
     RangeStart.X = player.PositionX; 

     //test if the maps position is in the left corner of the map 
     //if it is draw the map from the start..(RangeStart.X = 0) 
     if (player.PositionX - (ScreenSizeX/2) < 0) { RangeStart.X = 0; } 
     //if not draw the hero in the mitle of the screen 
     else 
     { 
      RangeStart.X = player.PositionX - (ScreenSizeX/2); 
     } 
     //if the hero is in the right corer, fix his position 
     while (RangeStart.X + ScreenSizeX > MapSizeX) 
     { 
      RangeStart.X--; 
     } 

     //the same thing for the Y axle 
     RangeStart.Y = player.PositionY; 
     if (player.PositionY - (ScreenSizeY/2) < 0) { RangeStart.Y = 0; } 
     else 
     { 
      RangeStart.Y = player.PositionY - (ScreenSizeY/2); 
     } 
     while (RangeStart.Y + ScreenSizeY > MapSizeY) 
     { 
      RangeStart.Y--; 
     } 

     //time to set the position of the hero... 
     //he works like the opposite of the range, if you move what part of the map 
     //you draw you dont change the heros draw position, if you dont move the range 
     //you have to move the hero to create the illusion of "moment" 

     //if you are in the left part you have to move the heros draw position.. 
     if (player.PositionX - (ScreenSizeX/2) < 0) 
     { DrawHeroPosition.X = player.PositionX; } 

     //if you are in the right part 
     else if (player.PositionX+1 > MapSizeX - (ScreenSizeX/2)) 
     { 
      DrawHeroPosition.X = player.PositionX - (MapSizeX - ScreenSizeX); 
     } 

     //if you aint in a corner, just place the hero in the middle of the map 
     else 
     { 
      DrawHeroPosition.X = (ScreenSizeX/2); 
     } 


     //the same thing for Y 
     if (player.PositionY - (ScreenSizeY/2) < 0) 
     { DrawHeroPosition.Y = player.PositionY; } 
     else if (player.PositionY+1 > MapSizeY - (ScreenSizeY/2)) 
     { 
      DrawHeroPosition.Y = player.PositionY - (MapSizeY - ScreenSizeY); 
     } 
     else 
     { 
      DrawHeroPosition.Y = (ScreenSizeY/2); 
     } 

    } 

    public void Draw() 
    { 

     int x = (int)RangeStart.X; 
     int y = (int)RangeStart.Y; 

     for(int counterX = 0; x < ((MapSizeX)); x++, counterX++) 
     { 
      for (int counterY = 0; y < (MapSizeY); y++, counterY++) 
      { 
       if (mapPoints[x, y] != null) 
       { 
       mapPoints[x, y].Draw(spriteBatch, mapPoints[counterX,counterY].positonInMatrix); 
       //mapPoints[counterX,counterY] = where to draw 
       //mapPoints[x, y] = what to draw 
       } 
      } 
      y = (int)RangeStart.Y; 
     } 
    } 
} 

我的MapPoint的類中如何繪製...

public void Draw(SpriteBatch theSpriteBatch, Vector2 positonOnScreen) 
    { 
     positonOnScreen = new Vector2(positonOnScreen.X * base.Scale * 16, 
     positonOnScreen.Y * base.Scale * 16); 

     //base.Scale is just a variable for have the ability to zoom in/out 
     //16 represents the original size of the picture (16x16 pixels) 

     theSpriteBatch.Draw(mSpriteTexture, new Rectangle((int)positonOnScreen.X, 
     (int)(positonOnScreen.Y), 64, 64),new Rectangle(0, 0, 16, 16), Color.White); 
    } 
+0

如果我誤解你或某事不清楚只是留下評論=) – Olle89

+0

如果我改變這段代碼使用10.000x10.000圖片會好嗎,或者我應該使用噸的512x512圖片?如果玩家(精靈)觸及的部分它也會實現,比如說100x,150y部分? – Foresp

+0

已經做了一些大的改變,這個新版本的工作,幷包括一個可能的方式來處理球員的位置和繪圖。 – Olle89

1

如果你問用於紅點半徑範圍內的碰撞檢測。你可以簡單地使用下面的測試(僞代碼,我不寫C#:-)

if((player.GetPosition() - point.GetPosition()).length() < radius) 
{ /* Do code here */ } 

這將檢測,如果您的播放器是您點的一定半徑範圍內,就可以執行你所希望的任何行動。

希望這會有所幫助! :)

0

好的,根據我對你的問題的理解,你有一個大的圖像,其中包含不同的對象,你希望你的玩家進行交互,是的?我的意思是,圖像文件本身具有門或丘陵或玩家將與之交互的其他東西。

這是一個壞主意,說實話,所以我希望我誤解。讓你的背景圖像變得一般化並使你的遊戲中的所有交互對象類更好。如果你這樣做,那麼你可以讓你的對象類包含行爲來相互交叉,根據它們的距離(圓形碰撞)或基於你爲它們定義的邊界框。

圈碰撞:

if (Math.Abs(Vector2.Distance(player.Position - obj.Position)) < player.Radius + obj.Radius) 
    //do the action 

矩形碰撞:

if (player.Bounds.Intersects(obj.Bounds)) 
    //do the action 

另外,如果你是在做10,000×10,000像素的形象策劃,明白XNA內容管道將不導入圖像大於4,000像素的一面。

如果您將播放器設置爲與圖像背景中的像素進行交互,您可以手動創建一組交互式對象位置,也可以使用Texture2D.GetData()方法來加載處理圖像中的每個像素 - 但要注意,這將需要很長時間,特別是對於大量或多個紋理。

相關問題