1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.math
.Vector2
;
5 import com
.badlogic
.gdx
.utils
.ArrayMap
;
7 public class GameWorld
{
9 private int WIDTH
, HEIGHT
;
11 private ArrayMap
<String
, Integer
> metaMap
;
12 private int[][] foreMap
;
13 private int[][] backMap
;
15 public int getWidth() {
19 public int getHeight() {
23 public int[][] getFullForeMap() {
27 public int[][] getFullBackMap() {
31 public int getForeMap(int x
, int y
) {
35 if (x
<0) x
=getWidth()-Math
.abs(x
);
37 } catch (ArrayIndexOutOfBoundsException e
) {
38 Gdx
.app
.error("GameWorld",e
.toString());
43 public void setForeMap(int x
, int y
, int value
) {
46 if (x
<0) x
=getWidth()-Math
.abs(x
);
47 foreMap
[x
][y
] = value
;
48 } catch (ArrayIndexOutOfBoundsException e
) {
49 Gdx
.app
.error("GameWorld", e
.toString());
53 public int getBackMap(int x
, int y
) {
57 if (x
<0) x
=getWidth()-Math
.abs(x
);
59 } catch (ArrayIndexOutOfBoundsException e
) {
60 Gdx
.app
.error("GameWorld",e
.toString());
65 public void setBackMap(int x
, int y
, int value
) {
68 if (x
<0) x
=getWidth()-Math
.abs(x
);
69 backMap
[x
][y
] = value
;
70 } catch (ArrayIndexOutOfBoundsException e
) {
71 Gdx
.app
.error("GameWorld", e
.toString());
75 public int getMeta(int x
, int y
) {
76 if (metaMap
.containsKey(x
+"_"+y
)) return metaMap
.get(x
+"_"+y
);
80 public void setMeta(int x
, int y
, int value
) {
81 if (metaMap
.containsKey(x
+"_"+y
)) metaMap
.removeKey(x
+"_"+y
);
82 metaMap
.put(x
+"_"+y
, value
);
85 public void placeToForeground(int x
, int y
, int value
) {
86 if (getForeMap(x
,y
) == 0 || value
== 0) {
87 setForeMap(x
,y
,value
);
91 public void placeToBackground(int x
, int y
, int value
) {
92 if (value
==0 || (getBackMap(x
,y
) == 0 && !Items
.BLOCKS
.getValueAt(value
).background
)) {
93 setBackMap(x
,y
,value
);
97 public Vector2
getSpawnPoint() {
101 if (getForeMap(x
,y
)>0 && Items
.BLOCKS
.getValueAt(getForeMap(x
,y
)).collision
) break;
105 return new Vector2(x
,y
);
108 public void generate(int w
, int h
) {
111 WorldGen
.genWorld(WIDTH
,HEIGHT
);
112 foreMap
= WorldGen
.getForeMap();
113 backMap
= WorldGen
.getBackMap();
114 metaMap
= new ArrayMap
<String
, Integer
>();
118 public void setMaps(int[][] foreMap
, int[][] backMap
) {
119 this.foreMap
= foreMap
.clone();
120 this.backMap
= backMap
.clone();
121 WIDTH
= foreMap
.length
;
122 HEIGHT
= foreMap
[0].length
;