DEADSOFTWARE

Add logic to some blocks and fluids
[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 || !Items.BLOCKS.getValueAt(getForeMap(x, y)).collision) {
76 setForeMap(x, y, value);
77 GameProc.UPD_X = x-8;
78 GameProc.UPD_Y = y-8;
79 GameProc.DO_UPD = true;
80 }
81 }
83 public void placeToBackground(int x, int y, int value) {
84 if (value==0 || (getBackMap(x,y) == 0 && Items.BLOCKS.getValueAt(value).collision) &&
85 (!Items.BLOCKS.getValueAt(value).transparent || value == 18)) {
86 setBackMap(x,y,value);
87 }
88 }
90 public Vector2 getSpawnPoint() {
91 int x=0,y=0;
92 while (true) {
93 y++;
94 if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
95 }
96 x = x*16 + 4;
97 y = y*16 - 32;
98 return new Vector2(x,y);
99 }
101 public void generate(int w, int h) {
102 WIDTH = w;
103 HEIGHT = h;
104 WorldGen.genWorld(WIDTH,HEIGHT);
105 foreMap = WorldGen.getForeMap();
106 backMap = WorldGen.getBackMap();
107 WorldGen.clear();
110 public void setMaps(int[][] foreMap, int[][] backMap) {
111 this.foreMap = foreMap.clone();
112 this.backMap = backMap.clone();
113 WIDTH = foreMap.length;
114 HEIGHT = foreMap[0].length;