DEADSOFTWARE

65c9d0300f354eb94aa182de4b1635cc0eed7634
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameWorld.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.math.Vector2;
6 public class GameWorld {
8 private int WIDTH, HEIGHT;
9 private int[][] foreMap;
10 private int[][] backMap;
12 public int getWidth() {
13 return WIDTH;
14 }
16 public int getHeight() {
17 return HEIGHT;
18 }
20 public int[][] getFullForeMap() {
21 return foreMap;
22 }
24 public int[][] getFullBackMap() {
25 return backMap;
26 }
28 private int transformX(int x) {
29 x = x%getWidth();
30 if (x<0) x=getWidth()-Math.abs(x);
31 return x;
32 }
34 public int getForeMap(int x, int y) {
35 int map = 0;
36 try {
37 x = transformX(x);
38 map = foreMap[x][y];
39 } catch (ArrayIndexOutOfBoundsException e) {
40 Gdx.app.error("GameWorld",e.toString());
41 }
42 return map;
43 }
45 public void setForeMap(int x, int y, int value) {
46 try {
47 x = transformX(x);
48 foreMap[x][y] = value;
49 } catch (ArrayIndexOutOfBoundsException e) {
50 Gdx.app.error("GameWorld", e.toString());
51 }
52 }
54 public int getBackMap(int x, int y) {
55 int map = 0;
56 try {
57 x = transformX(x);
58 map = backMap[x][y];
59 } catch (ArrayIndexOutOfBoundsException e) {
60 Gdx.app.error("GameWorld",e.toString());
61 }
62 return map;
63 }
65 public void setBackMap(int x, int y, int value) {
66 try {
67 x = transformX(x);
68 backMap[x][y] = value;
69 } catch (ArrayIndexOutOfBoundsException e) {
70 Gdx.app.error("GameWorld", e.toString());
71 }
72 }
74 public void placeToForeground(int x, int y, int value) {
75 if (getForeMap(x,y) == 0 || value == 0) {
76 setForeMap(x, y, value);
77 }
78 }
80 public void placeToBackground(int x, int y, int value) {
81 if (value==0 || (getBackMap(x,y) == 0 && Items.BLOCKS.getValueAt(value).collision) &&
82 (!Items.BLOCKS.getValueAt(value).transparent || value == 18)) {
83 setBackMap(x,y,value);
84 }
85 }
87 public Vector2 getSpawnPoint() {
88 int x=0,y=0;
89 while (true) {
90 y++;
91 if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
92 }
93 x = x*16 + 4;
94 y = y*16 - 32;
95 return new Vector2(x,y);
96 }
98 public void generate(int w, int h) {
99 WIDTH = w;
100 HEIGHT = h;
101 WorldGen.genWorld(WIDTH,HEIGHT);
102 foreMap = WorldGen.getForeMap();
103 backMap = WorldGen.getBackMap();
104 WorldGen.clear();
107 public void setMaps(int[][] foreMap, int[][] backMap) {
108 this.foreMap = foreMap.clone();
109 this.backMap = backMap.clone();
110 WIDTH = foreMap.length;
111 HEIGHT = foreMap[0].length;