-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockType.java
98 lines (86 loc) · 3.07 KB
/
BlockType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BlockType {
private static BufferedImage TEXTURE = null;
public static final int TEXTURE_SIZE = 16;
public static final BlockType STONE;
public static final BlockType WATER;
public static final BlockType GRASS;
public static final BlockType DIRT;
public static final BlockType SAND;
static {
try {
TEXTURE = ImageIO.read(new File("./textures.png"));
} catch (IOException e) {
e.printStackTrace();
}
STONE = new BlockType(new Vector2(0, 0), false, 0, null);
WATER = new BlockType(new Vector2(0, 1), true, 0.8, SpecialBlockRendering.LIQUID);
GRASS = new BlockType(new Vector2(0, 2), new Vector2(1, 2), new Vector2(0, 3), false, 0, null);
DIRT = new BlockType(new Vector2(0, 3), false, 0, null);
SAND = new BlockType(new Vector2(0, 4), false, 0, null);
}
private static BlockType[] BLOCK_TYPES = new BlockType[] {
STONE,
WATER,
GRASS,
DIRT,
SAND
};
private Vector2 topUV;
private Vector2 sideUV;
private Vector2 bottomUV;
private boolean transparent;
private double speedModifier;
private SpecialBlockRendering specialBlockRendering;
public BlockType(Vector2 topUV, Vector2 sideUV, Vector2 bottomUV, boolean transparent, double speedModifier,
SpecialBlockRendering specialBlockRendering) {
this.topUV = topUV;
this.sideUV = sideUV;
this.bottomUV = bottomUV;
this.transparent = transparent;
this.speedModifier = speedModifier;
this.specialBlockRendering = specialBlockRendering;
}
public BlockType(Vector2 uv, boolean transparent, double speedModifier, SpecialBlockRendering specialBlockRendering) {
this(uv, uv, uv, transparent, speedModifier, specialBlockRendering);
}
public BufferedImage getTexture(Orientation orientation) {
if (orientation == Orientation.UP) {
return TEXTURE.getSubimage((int) topUV.getX() * TEXTURE_SIZE, (int) topUV.getY() * TEXTURE_SIZE, TEXTURE_SIZE,
TEXTURE_SIZE);
}
if (orientation == Orientation.NORTH || orientation == Orientation.SOUTH || orientation == Orientation.EAST
|| orientation == Orientation.WEST) {
return TEXTURE.getSubimage((int) sideUV.getX() * TEXTURE_SIZE, (int) sideUV.getY() * TEXTURE_SIZE, TEXTURE_SIZE,
TEXTURE_SIZE);
}
if (orientation == Orientation.DOWN) {
return TEXTURE.getSubimage((int) bottomUV.getX() * TEXTURE_SIZE, (int) bottomUV.getY() * TEXTURE_SIZE,
TEXTURE_SIZE, TEXTURE_SIZE);
}
return null;
}
public boolean isTransparent() {
return transparent;
}
public double getSpeedModifier() {
return speedModifier;
}
public SpecialBlockRendering getSpecialBlockRendering() {
return specialBlockRendering;
}
public static BlockType getBlockType(int id) {
return BLOCK_TYPES[id];
}
public static int getBlockTypeId(BlockType blockType) {
for (int i = 0; i < BLOCK_TYPES.length; i++) {
if (BLOCK_TYPES[i] == blockType) {
return i;
}
}
return -1;
}
}