Skip to content

Commit 4a37d20

Browse files
committed
Cleanup tile Class's
1 parent 8afbab5 commit 4a37d20

File tree

4 files changed

+44
-100
lines changed

4 files changed

+44
-100
lines changed

src/com/redomar/game/level/tiles/AnimatedTile.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ public class AnimatedTile extends BasicTile {
77
private long lastIterationTime;
88
private final int animationSwitchDelay;
99

10-
/**
11-
* @param id Unique ID for the Tile
12-
* @param animationCoords A 2D array of all x,y-coordinates in Integers.
13-
* @param tileColour Colours from the SpriteSheet.
14-
* @param levelColour Colours to be displayed in the Game World.
15-
* @param animationSwitchDelay Length of time to be delayed to the next frame from the 2D array.
16-
*/
1710
public AnimatedTile(int id, int[][] animationCoords, int tileColour, int levelColour, int animationSwitchDelay) {
1811
super(id, animationCoords[0][0], animationCoords[0][1], tileColour, levelColour);
1912
this.animationTileCoords = animationCoords;
@@ -22,11 +15,12 @@ public AnimatedTile(int id, int[][] animationCoords, int tileColour, int levelCo
2215
this.animationSwitchDelay = animationSwitchDelay;
2316
}
2417

18+
@Override
2519
public void tick() {
26-
if ((System.currentTimeMillis() - lastIterationTime) >= (animationSwitchDelay)) {
20+
if ((System.currentTimeMillis() - lastIterationTime) >= animationSwitchDelay) {
2721
lastIterationTime = System.currentTimeMillis();
2822
currentAnimationIndex = (currentAnimationIndex + 1) % animationTileCoords.length;
29-
this.tileId = (animationTileCoords[currentAnimationIndex][0] + (animationTileCoords[currentAnimationIndex][1] * 32));
23+
this.tileId = animationTileCoords[currentAnimationIndex][0] + (animationTileCoords[currentAnimationIndex][1] * 32);
3024
}
3125
}
3226
}

src/com/redomar/game/level/tiles/BasicTile.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,20 @@
66
public class BasicTile extends Tile {
77

88
protected int tileId;
9-
protected int tileColour;
9+
protected final int tileColour;
1010

11-
/**
12-
* This is a tile that does not emit light, nor is sold. This tile does not have any animations.
13-
*
14-
* @param id Unique ID for the Tile
15-
* @param x Specifies the x-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31
16-
* @param y Specifies the y-coordinate from the SpriteSheet, measured in 8 pixels. Must be 0 - 31
17-
* @param tileColour Colours from the SpriteSheet.
18-
* @param levelColour Colours to be displayed in the Game World.
19-
*/
2011
public BasicTile(int id, int x, int y, int tileColour, int levelColour) {
2112
super(id, false, false, levelColour);
22-
2313
this.tileId = x + y * 32;
2414
this.tileColour = tileColour;
2515
}
2616

17+
@Override
2718
public void tick() {
19+
// Basic tiles do not update
2820
}
2921

22+
@Override
3023
public void render(Screen screen, LevelHandler level, int x, int y) {
3124
screen.render(x, y, tileId, tileColour, 0x00, 1);
3225
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.redomar.game.level.tiles;
22

3-
public class BasicSolidTile extends BasicTile {
3+
public class SolidTile extends BasicTile {
44

55
/**
66
* This is a solid tile, but does not emit any light. This tile does not have any animations.
@@ -11,9 +11,8 @@ public class BasicSolidTile extends BasicTile {
1111
* @param tileColour Colours from the SpriteSheet.
1212
* @param levelColour Colours to be displayed in the Game World.
1313
*/
14-
public BasicSolidTile(int id, int x, int y, int tileColour, int levelColour) {
14+
public SolidTile(int id, int x, int y, int tileColour, int levelColour) {
1515
super(id, x, y, tileColour, levelColour);
1616
this.solid = true;
1717
}
18-
1918
}

src/com/redomar/game/level/tiles/Tile.java

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,44 @@
44
import com.redomar.game.gfx.Screen;
55
import com.redomar.game.level.LevelHandler;
66

7+
8+
@SuppressWarnings({"StaticInitializerReferencesSubClass", "OctalInteger"})
79
public abstract class Tile {
810

911
private static final Tile[] tiles = new Tile[256];
10-
private static final Tile VOID = new BasicSolidTile(0, 0, 0, Colours.get(0, -1, -1, -1), 0xFF000000);
11-
private static final Tile STONE = new BasicSolidTile(1, 1, 0, Colours.get(-1, 444, 333, -1), 0xFF555555);
12-
private static final Tile CHISELED_stone = new BasicTile(2, 2, 0, Colours.get(-1, 333, 222, -1), 0xFF666666);
13-
private static final Tile GRASS = new BasicTile(3, 3, 0, Colours.get(-1, 131, 141, -1), 0xFF00FF00);
14-
private static final Tile WATER = new AnimatedTile(4, new int[][] { { 0, 5 }, { 1, 5 }, { 2, 5 }, { 1, 5 } }, Colours.get(-1, 004, 115, -1), 0xFF0000FF, 1000);
15-
private static final Tile FLOWER_rose = new BasicTile(5, 4, 0, Colours.get(131, 151, 510, 553), 0xFFCCFF33);
16-
private static final Tile FLOWER_dandelion = new BasicTile(6, 4, 0, Colours.get(131, 151, 553, 510), 0xFFFFCC33);
17-
private static final Tile SAND = new BasicTile(7, 5, 0, Colours.get(-1, 553, 554, 555), 0xFFFFFF99);
18-
private static final Tile CHEST_a = new BasicSolidTile(8, 0, 1, Colours.get(333, 111, 420, 000), 0xFFFF0001);
19-
private static final Tile CHEST_b = new BasicSolidTile(9, 1, 1, Colours.get(333, 111, 420, 000), 0xFFFF0002);
20-
private static final Tile CARPET_red = new BasicTile(10, 5, 0, Colours.get(-1, 311, 411, 311), 0xFFAA3636);
21-
private static final Tile PORTAL = new AnimatedTile(11, new int[][] { { 3, 5 }, { 4, 5 }, { 5, 5 }, { 6, 5 }, { 7, 5 }, { 8, 5 }, { 9, 5 }, { 10, 5 } }, Colours.get(-1, 005, 305, -1), 0xFF00EAFF, 100);
22-
private static final Tile MAGMA = new AnimatedTile(12, new int [][] { { 0, 5 }, { 1, 5 }, { 2, 5 }, { 1, 5 } }, Colours.get(-1, 400, 511, -1), 0xFFF00F0F, 1000);
23-
private static final Tile DIRT = new BasicTile(13, 3, 0, Colours.get(0, 210, 321, -1), 0xFF442200);
24-
private static final Tile DIRT_WET = new AnimatedTile(14, new int[][] { { 1, 5 }, { 2, 5 } }, Colours.get(-1, 211, 322, -1), 0xFF663300, 1500);
25-
26-
protected byte id;
12+
static {
13+
/* VOID */ tiles[0] = new SolidTile(0, 0, 0, Colours.get(0, -1, -1, -1), 0xFF000000);
14+
/* STONE */ tiles[1] = new SolidTile(1, 1, 0, Colours.get(-1, 444, 333, -1), 0xFF555555);
15+
/* CHISELED_stone */ tiles[2] = new BasicTile(2, 2, 0, Colours.get(-1, 333, 222, -1), 0xFF666666);
16+
/* GRASS */ tiles[3] = new BasicTile(3, 3, 0, Colours.get(-1, 131, 141, -1), 0xFF00FF00);
17+
/* WATER */ tiles[4] = new AnimatedTile(4, new int[][] { { 0, 5 }, { 1, 5 }, { 2, 5 }, { 1, 5 } }, Colours.get(-1, 004, 115, -1), 0xFF0000FF, 1000);
18+
/* FLOWER_rose */ tiles[5] = new BasicTile(5, 4, 0, Colours.get(131, 151, 510, 553), 0xFFCCFF33);
19+
/* FLOWER_dandelion */ tiles[6] = new BasicTile(6, 4, 0, Colours.get(131, 151, 553, 510), 0xFFFFCC33);
20+
/* SAND */ tiles[7] = new BasicTile(7, 5, 0, Colours.get(-1, 553, 554, 555), 0xFFFFFF99);
21+
/* CHEST_a */ tiles[8] = new SolidTile(8, 0, 1, Colours.get(333, 111, 420, 000), 0xFFFF0001);
22+
/* CHEST_b */ tiles[9] = new SolidTile(9, 1, 1, Colours.get(333, 111, 420, 000), 0xFFFF0002);
23+
/* CARPET_red */ tiles[10] = new BasicTile(10, 5, 0, Colours.get(-1, 311, 411, 311), 0xFFAA3636);
24+
/* PORTAL */ tiles[11] = new AnimatedTile(11, new int[][] { { 3, 5 }, { 4, 5 }, { 5, 5 }, { 6, 5 }, { 7, 5 }, { 8, 5 }, { 9, 5 }, { 10, 5 } }, Colours.get(-1, 005, 305, -1), 0xFF00EAFF, 100);
25+
/* MAGMA */ tiles[12] = new AnimatedTile(12, new int[][] { { 0, 5 }, { 1, 5 }, { 2, 5 }, { 1, 5 } }, Colours.get(-1, 400, 511, -1), 0xFFF00F0F, 1000);
26+
/* DIRT */ tiles[13] = new BasicTile(13, 3, 0, Colours.get(0, 210, 321, -1), 0xFF442200);
27+
/* DIRT_WET */ tiles[14] = new AnimatedTile(14, new int[][] { { 1, 5 }, { 2, 5 } }, Colours.get(-1, 211, 322, -1), 0xFF663300, 1500);
28+
}
29+
protected final byte id;
2730
protected boolean solid;
28-
protected boolean emitter;
29-
private int levelColour;
31+
protected final boolean emitter;
32+
protected final int levelColour;
3033

31-
public Tile(int id, boolean isSolid, boolean isEmitter, int colour) {
34+
protected Tile(int id, boolean solid, boolean emitter, int levelColour) {
3235
this.id = (byte) id;
36+
this.solid = solid;
37+
this.emitter = emitter;
38+
this.levelColour = levelColour;
3339

34-
if (getTiles()[id] != null) {
35-
throw new RuntimeException("Duplicate tile id on:" + id);
40+
if (tiles[id] != null) {
41+
throw new RuntimeException("Duplicate tile id: " + id);
3642
}
3743

38-
this.solid = isSolid;
39-
this.emitter = isEmitter;
40-
this.levelColour = colour;
41-
42-
getTiles()[id] = this;
44+
tiles[id] = this;
4345
}
4446

4547
public byte getId() {
@@ -62,68 +64,24 @@ public int getLevelColour() {
6264
return levelColour;
6365
}
6466

65-
public static Tile getStone() {
66-
return STONE;
67+
public static Tile getTile(int id) {
68+
return tiles[id];
6769
}
6870

69-
public static Tile getChiseledStone() {
70-
return CHISELED_stone;
71+
public static Tile getStone() {
72+
return getTile(1);
7173
}
7274

7375
public static Tile getGrass() {
74-
return GRASS;
76+
return getTile(2);
7577
}
7678

77-
public static Tile getFlowerRose() {
78-
return FLOWER_rose;
79-
}
80-
81-
public static Tile getFlowerDandelion() {
82-
return FLOWER_dandelion;
83-
}
84-
85-
public static Tile getSand() {
86-
return SAND;
87-
}
88-
89-
public static Tile getWater() {
90-
return WATER;
91-
}
9279

9380
public static Tile getVoid() {
94-
return VOID;
81+
return getTile(0);
9582
}
9683

9784
public static Tile[] getTiles() {
9885
return tiles;
9986
}
100-
101-
public static Tile getChestA() {
102-
return CHEST_a;
103-
}
104-
105-
public static Tile getChestB() {
106-
return CHEST_b;
107-
}
108-
109-
public static Tile getCarpetRed() {
110-
return CARPET_red;
111-
}
112-
113-
public static Tile getPortal() {
114-
return PORTAL;
115-
}
116-
117-
public static Tile getMagma() {
118-
return MAGMA;
119-
}
120-
121-
public static Tile getDirt() {
122-
return DIRT;
123-
}
124-
125-
public static Tile getDirtWet() {
126-
return DIRT_WET;
127-
}
128-
12987
}

0 commit comments

Comments
 (0)