[英]XNA how to store and draw a 2d map?
我有問題,不確定如何解決。 我需要為XNA應用程序創建2D地圖編輯器,並使用一定數量的圖塊。 假設地圖將是50x100的圖塊。
我不確定用於地圖,圖塊的數據結構以及如何將其存儲在硬盤驅動器上以便以后加載。
我現在在想的是這個。 我將地圖存儲在一個文本文件中,如下所示:
//x, y, ground_type, object_type
0, 0, 1, 0
0, 1, 2, 1
其中0 =草,1 =河流etcc用於地面地形,0 =空,1 =牆壁用於對象類型。
然后,我將有一個Game Component Map類,可以讀取該文件或從頭開始創建一個新文件:
class Map : DrawableGameComponent {
//These are things like grass, whater, sand...
Tile ground_tiles[,];
//These are things like walls that can be destroyed
Tile object_tiles[,];
public Map(Game game, String filepath){
for line in open(filepath){
//Set the x,y tile to a new tile
ground_tiles[line[0], line[1]] = new Tile(line[3])
object_tiles[line[0], line[1]] = new Tile(line[4])
}
}
public Map(Game game, int width, int heigth){
//constructor
init_map()
}
private void init_map(){
//initialize all the ground_tiles
//to "grass"
for i,j width, heigth{
ground_tiles[i,j] = new Tile(TILE.GRASS)
}
public override Draw(game_time){
for tile in tiles:
sprite_batch.draw(tile.texture, tile.x, tile.y etc..)
}
我的Tile類可能不會成為游戲組件。 我還是不太確定如何處理碰撞檢測,例如,從玩家發來的子彈與地圖對象之間的碰撞。 應該由Map類還是某種超級管理器類處理?
歡迎任何提示。 謝謝!
為什么不將其存儲為:
Height
Width
GroundType * (Height * Width)
給像
4
4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
它既簡單又緊湊。 :)至於游戲中的存儲,除非您有其他特定需求,否則2D陣列非常適合此功能。 一種典型的碰撞檢測技術是由一個物理子系統完成一個寬泛的階段,例如,使用邊界球或與軸對齊的邊界框,然后讓成對的可能碰撞的對象來計算是否確實存在碰撞。
除非您有非常令人信服的理由,否則您的tile類可能不應該是組件。
編輯:忘記了那里的對象類型,但是也很容易集成它。
謝謝,但是我決定了(ground_type,object_type)對。 所以輸入文件像
0, 1
0, 0,
0, 0,
1, 1
使用以下映射:GROUND = {0:grass,1:road} OBJECTS = {0:none,1:crate},地圖的width = 2和height = 2將產生以下結果:
grass+crate | grass
grass | road+crate
當然,我必須知道地圖的寬度/高度才能理解文件。 我也可以將其放在輸入文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.