DEADSOFTWARE

TP player when crossing world's edge
[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;
11 private int[][] foreMap;
12 private int[][] backMap;
14 public GameWorld(int w, int h) {
15 WIDTH = w;
16 HEIGHT = h;
17 WorldGen.genWorld(WIDTH,HEIGHT);
18 foreMap = WorldGen.getForeMap();
19 backMap = WorldGen.getBackMap();
20 WorldGen.clear();
21 }
23 public int getWidth() {
24 return WIDTH;
25 }
27 public int getHeight() {
28 return HEIGHT;
29 }
31 public int getForeMap(int x, int y) {
32 int map = 0;
33 try {
34 if (x<0) {
35 x = x % (getWidth());
36 x = getWidth()- Math.abs(x);
37 } else if (x>0) {
38 x = x % (getWidth());
39 }
40 map = foreMap[x][y];
41 } catch (ArrayIndexOutOfBoundsException e) {
42 Gdx.app.error("GameWorld",e.toString());
43 }
44 return map;
45 }
47 public void setForeMap(int x, int y, int value) {
48 try {
49 if (x<0) {
50 x = x % (getWidth());
51 x = getWidth()- Math.abs(x);
52 } else if (x>0) {
53 x = x % (getWidth());
54 }
55 foreMap[x][y] = value;
56 } catch (ArrayIndexOutOfBoundsException e) {
57 Gdx.app.error("GameWorld", e.toString());
58 }
59 }
61 public int getBackMap(int x, int y) {
62 int map = 0;
63 try {
64 if (x<0) {
65 x = x % (getWidth());
66 x = getWidth()- Math.abs(x);
67 } else if (x>0) {
68 x = x % (getWidth());
69 }
70 map = backMap[x][y];
71 } catch (ArrayIndexOutOfBoundsException e) {
72 Gdx.app.error("GameWorld",e.toString());
73 }
74 return map;
75 }
77 public void setBackMap(int x, int y, int value) {
78 try {
79 if (x<0) {
80 x = x % (getWidth());
81 x = getWidth()- Math.abs(x);
82 } else if (x>0) {
83 x = x % (getWidth());
84 }
85 backMap[x][y] = value;
86 } catch (ArrayIndexOutOfBoundsException e) {
87 Gdx.app.error("GameWorld", e.toString());
88 }
89 }
91 public void placeToForeground(int x, int y, int value) {
92 if (getForeMap(x,y) == 0 || value == 0) {
93 setForeMap(x,y,value);
94 }
95 }
97 public void placeToBackground(int x, int y, int value) {
98 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) {
99 setBackMap(x,y,value);
103 public Vector2 getSpawnPoint() {
104 float x=0, y=0;
105 x = x*16 + 4;
106 y *= 16;
107 return new Vector2(x,y);