DEADSOFTWARE

7885d2e8ea4bf98d5d18a3e237d3b100e950e490
[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;
10 private int[][] foreMap;
11 private int[][] backMap;
13 public int getWidth() {
14 return WIDTH;
15 }
17 public int getHeight() {
18 return HEIGHT;
19 }
21 public int[][] getFullForeMap() {
22 return foreMap;
23 }
25 public int[][] getFullBackMap() {
26 return backMap;
27 }
29 public int getForeMap(int x, int y) {
30 int map = 0;
31 try {
32 x = x%getWidth();
33 if (x<0) x=getWidth()-Math.abs(x);
34 map = foreMap[x][y];
35 } catch (ArrayIndexOutOfBoundsException e) {
36 Gdx.app.error("GameWorld",e.toString());
37 }
38 return map;
39 }
41 public void setForeMap(int x, int y, int value) {
42 try {
43 x = x%getWidth();
44 if (x<0) x=getWidth()-Math.abs(x);
45 foreMap[x][y] = value;
46 } catch (ArrayIndexOutOfBoundsException e) {
47 Gdx.app.error("GameWorld", e.toString());
48 }
49 }
51 public int getBackMap(int x, int y) {
52 int map = 0;
53 try {
54 x = x%getWidth();
55 if (x<0) x=getWidth()-Math.abs(x);
56 map = backMap[x][y];
57 } catch (ArrayIndexOutOfBoundsException e) {
58 Gdx.app.error("GameWorld",e.toString());
59 }
60 return map;
61 }
63 public void setBackMap(int x, int y, int value) {
64 try {
65 x = x%getWidth();
66 if (x<0) x=getWidth()-Math.abs(x);
67 backMap[x][y] = value;
68 } catch (ArrayIndexOutOfBoundsException e) {
69 Gdx.app.error("GameWorld", e.toString());
70 }
71 }
73 public void placeToForeground(int x, int y, int value) {
74 if (getForeMap(x,y) == 0 || value == 0) {
75 setForeMap(x,y,value);
76 }
77 }
79 public void placeToBackground(int x, int y, int value) {
80 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) {
81 setBackMap(x,y,value);
82 }
83 }
85 public Vector2 getSpawnPoint() {
86 int x=0,y=0;
87 while (true) {
88 y++;
89 if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
90 }
91 x = x*16 + 4;
92 y = y*16 - 32;
93 return new Vector2(x,y);
94 }
96 public void generate(int w, int h) {
97 WIDTH = w;
98 HEIGHT = h;
99 WorldGen.genWorld(WIDTH,HEIGHT);
100 foreMap = WorldGen.getForeMap();
101 backMap = WorldGen.getBackMap();
102 WorldGen.clear();
105 public void setMaps(int[][] foreMap, int[][] backMap) {
106 this.foreMap = foreMap.clone();
107 this.backMap = backMap.clone();
108 WIDTH = foreMap.length;
109 HEIGHT = foreMap[0].length;