DEADSOFTWARE

Fix bugs
[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;
5 import com.badlogic.gdx.utils.ArrayMap;
6 import ru.deadsoftware.cavecraft.CaveGame;
7 import ru.deadsoftware.cavecraft.GameState;
8 import ru.deadsoftware.cavecraft.Items;
10 public class GameWorld {
12 private final int WIDTH, HEIGHT;
14 private int[][] foreMap;
15 private int[][] backMap;
17 public GameWorld(int w, int h) {
18 WIDTH = w;
19 HEIGHT = h;
20 WorldGen.genWorld(WIDTH,HEIGHT);
21 foreMap = WorldGen.getForeMap();
22 backMap = WorldGen.getBackMap();
23 WorldGen.clear();
24 }
26 public int getWidth() {
27 return WIDTH;
28 }
30 public int getHeight() {
31 return HEIGHT;
32 }
34 public int getForeMap(int x, int y) {
35 int ret = 0;
36 try {
37 ret = foreMap[x][y];
38 } catch (ArrayIndexOutOfBoundsException e) {
39 Gdx.app.error("GameWorld",e.toString());
40 }
41 return ret;
42 }
44 public void setForeMap(int x, int y, int value) {
45 try {
46 foreMap[x][y] = value;
47 } catch (ArrayIndexOutOfBoundsException e) {
48 Gdx.app.error("GameWorld", e.toString());
49 }
50 }
52 public int getBackMap(int x, int y) {
53 int ret = 0;
54 try {
55 ret = backMap[x][y];
56 } catch (ArrayIndexOutOfBoundsException e) {
57 Gdx.app.error("GameWorld",e.toString());
58 }
59 return ret;
60 }
62 public void setBackMap(int x, int y, int value) {
63 try {
64 backMap[x][y] = value;
65 } catch (ArrayIndexOutOfBoundsException e) {
66 Gdx.app.error("GameWorld", e.toString());
67 }
68 }
70 public void placeToForeground(int x, int y, int value) {
71 if (getForeMap(x,y) == 0 || value == 0) {
72 setForeMap(x,y,value);
73 }
74 }
76 public void placeToBackground(int x, int y, int value) {
77 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) {
78 setBackMap(x,y,value);
79 }
80 }
82 public Vector2 getSpawnPoint() {
83 float x=0, y=0;
84 boolean found = false;
85 x = getWidth()/2;
86 while (!found) {
87 for (int i = 0; i < getHeight(); i++) {
88 if (getForeMap((int)x, i)>0 &&
89 Items.BLOCKS.getValueAt(getForeMap((int)x, i)).collision &&
90 getForeMap((int)x, i-1)==0 && getForeMap((int)x, i-1)==0) {
91 y = i-2;
92 found = true;
93 break;
94 }
95 }
96 if (!found) {
97 x--;
98 if (x<0) x=getWidth()-1;
99 if ((int)x == getWidth()/2+1) {
100 x--;
101 y=0;
102 break;
106 x = x*16 + 4;
107 y *= 16;
108 return new Vector2(x,y);