DEADSOFTWARE

Save with BufferedOutputStream
[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 int WIDTH, HEIGHT;
11 private int[][] foreMap;
12 private int[][] backMap;
14 public int getWidth() {
15 return WIDTH;
16 }
18 public int getHeight() {
19 return HEIGHT;
20 }
22 public int[][] getFullForeMap() {
23 return foreMap;
24 }
26 public int[][] getFullBackMap() {
27 return backMap;
28 }
30 public int getForeMap(int x, int y) {
31 int map = 0;
32 try {
33 x = x%getWidth();
34 if (x<0) x=getWidth()-Math.abs(x);
35 map = foreMap[x][y];
36 } catch (ArrayIndexOutOfBoundsException e) {
37 Gdx.app.error("GameWorld",e.toString());
38 }
39 return map;
40 }
42 public void setForeMap(int x, int y, int value) {
43 try {
44 x = x%getWidth();
45 if (x<0) x=getWidth()-Math.abs(x);
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 map = 0;
54 try {
55 x = x%getWidth();
56 if (x<0) x=getWidth()-Math.abs(x);
57 map = backMap[x][y];
58 } catch (ArrayIndexOutOfBoundsException e) {
59 Gdx.app.error("GameWorld",e.toString());
60 }
61 return map;
62 }
64 public void setBackMap(int x, int y, int value) {
65 try {
66 x = x%getWidth();
67 if (x<0) x=getWidth()-Math.abs(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) {
76 setForeMap(x,y,value);
77 }
78 }
80 public void placeToBackground(int x, int y, int value) {
81 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) {
82 setBackMap(x,y,value);
83 }
84 }
86 public Vector2 getSpawnPoint() {
87 int x=0,y=0;
88 while (true) {
89 y++;
90 if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
91 }
92 x = x*16 + 4;
93 y = y*16 - 32;
94 return new Vector2(x,y);
95 }
97 public void generate(int w, int h) {
98 WIDTH = w;
99 HEIGHT = h;
100 WorldGen.genWorld(WIDTH,HEIGHT);
101 foreMap = WorldGen.getForeMap();
102 backMap = WorldGen.getBackMap();
103 WorldGen.clear();
106 public void load() {
107 GameSaver.loadWorld();
108 foreMap = GameSaver.getLoadedForeMap();
109 backMap = GameSaver.getLoadedBackMap();
110 WIDTH = foreMap.length;
111 HEIGHT = foreMap[0].length;