DEADSOFTWARE

Better spawn point pick
[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 ru.deadsoftware.cavecraft.Items;
7 public class GameWorld {
9 private final int WIDTH, HEIGHT;
10 private int[][] foreMap;
11 private int[][] backMap;
13 public GameWorld(int w, int h) {
14 WIDTH = w;
15 HEIGHT = h;
16 WorldGen.genWorld(WIDTH,HEIGHT);
17 foreMap = WorldGen.getForeMap();
18 backMap = WorldGen.getBackMap();
19 WorldGen.clear();
20 }
22 public int getWidth() {
23 return WIDTH;
24 }
26 public int getHeight() {
27 return HEIGHT;
28 }
30 public int getForeMap(int x, int y) {
31 int ret = 0;
32 try {
33 ret = foreMap[x][y];
34 } catch (ArrayIndexOutOfBoundsException e) {
35 Gdx.app.error("GameWorld",e.toString());
36 }
37 return ret;
38 }
40 public void setForeMap(int x, int y, int value) {
41 try {
42 foreMap[x][y] = value;
43 } catch (ArrayIndexOutOfBoundsException e) {
44 Gdx.app.error("GameWorld", e.toString());
45 }
46 }
48 public int getBackMap(int x, int y) {
49 int ret = 0;
50 try {
51 ret = backMap[x][y];
52 } catch (ArrayIndexOutOfBoundsException e) {
53 Gdx.app.error("GameWorld",e.toString());
54 }
55 return ret;
56 }
58 public void setBackMap(int x, int y, int value) {
59 try {
60 backMap[x][y] = value;
61 } catch (ArrayIndexOutOfBoundsException e) {
62 Gdx.app.error("GameWorld", e.toString());
63 }
64 }
66 public void placeToForeground(int x, int y, int value) {
67 if (getForeMap(x,y) == 0 || value == 0) setForeMap(x,y,value);
68 }
70 public void placeToBackground(int x, int y, int value) {
71 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground))
72 setBackMap(x,y,value);
73 }
75 public Vector2 getSpawnPoint() {
76 float x=0, y=0;
77 boolean found = false;
78 x = getWidth()/2;
79 while (!found) {
80 for (int i = 0; i < getHeight(); i++) {
81 if (getForeMap((int)x, i)>0 &&
82 Items.BLOCKS.getValueAt(getForeMap((int)x, i)).collision) {
83 y = i-3;
84 found = true;
85 break;
86 }
87 }
88 if (!found) x--;
89 }
90 x = x*16 + 4;
91 y *= 16;
92 return new Vector2(x,y);
93 }
95 }