Java: What is a good data structure for storing a coordinate map for an infinite game world?

前端 未结 11 1256
心在旅途
心在旅途 2020-12-23 11:35

I am used to coding in PHP but I am not really proficient with Java and this has been a problem for some time now. I expect it to be a fairly easy solution, however I cannot

11条回答
  •  囚心锁ツ
    2020-12-23 12:00

    1) Instead of an array you could use a Map> or Map, which would of course allow negative indexes

    2) If you know the dimensions of your world from the start you could just modify your getter to allow the API to accept negatives and [linearly] transform them into positives. So for example if your world is 100x1000 tiles and you want (-5,-100), you would have WorldMap.getTile(-5,-100) which would translate to return tileArray[x+mapWidth/2][y+mapHeight/2]; which is (45,400)

提交回复
热议问题