DEADSOFTWARE

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