Skip to content

Commit fc1560f

Browse files
committed
💐 Flowers and foilage
1 parent eddf487 commit fc1560f

28 files changed

+574
-284
lines changed
Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,72 @@
11
package com.james090500.blocks;
22

3+
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
4+
5+
import java.util.function.Function;
6+
37
public class Blocks {
48

5-
public static Block[] ids;
6-
7-
public static final GrassBlock grassBlock = new GrassBlock((byte) 1);
8-
public static final DirtBlock dirtBlock = new DirtBlock((byte) 2);
9-
public static final StoneBlock stoneBlock = new StoneBlock((byte) 3);
10-
public static final SandBlock sandBlock = new SandBlock((byte) 4);
11-
public static final WaterBlock waterBlock = new WaterBlock((byte) 5);
12-
public static final OakLeafBlock leafBlock = new OakLeafBlock((byte) 6);
13-
public static final SpruceLeafBlock spruceLeafBlock = new SpruceLeafBlock((byte) 7);
14-
public static final OakLogBlock logBlock = new OakLogBlock((byte) 8);
15-
public static final SpruceLogBlock spruceLogBlock = new SpruceLogBlock((byte) 9);
16-
public static final BirchLogBlock birchLogBlock = new BirchLogBlock((byte) 10);
17-
public static final OakPlanksBlock oakPlanksBlock = new OakPlanksBlock((byte) 11);
18-
public static final SprucePlanksBlock sprucePlanksBlock = new SprucePlanksBlock((byte) 12);
19-
public static final BirchPlanksBlock birchPlanksBlock = new BirchPlanksBlock((byte) 13);
20-
public static final GlassBlock glassBlock = new GlassBlock((byte) 14);
21-
public static final SnowyGrassBlock snowyGrassBlock = new SnowyGrassBlock((byte) 15);
22-
public static final CactusBlock cactusBlock = new CactusBlock((byte) 16);
23-
public static final ShortGrassBlock shortGrassBlock = new ShortGrassBlock((byte) 17);
9+
// 0 reserved for "air"/null
10+
private static final int MAX_BLOCKS = 256;
11+
private static final Int2ObjectArrayMap<Block> REGISTRY = new Int2ObjectArrayMap<>();
12+
private static int nextId = 1;
2413

25-
static {
26-
ids = new Block[] {
27-
null,
28-
grassBlock,
29-
dirtBlock,
30-
stoneBlock,
31-
sandBlock,
32-
waterBlock,
33-
leafBlock,
34-
spruceLeafBlock,
35-
logBlock,
36-
spruceLogBlock,
37-
birchLogBlock,
38-
oakPlanksBlock,
39-
sprucePlanksBlock,
40-
birchPlanksBlock,
41-
glassBlock,
42-
snowyGrassBlock,
43-
cactusBlock,
44-
shortGrassBlock,
45-
};
46-
47-
// Build models
48-
for(Block block : Blocks.ids) {
49-
if(block instanceof IBlockRender) {
50-
block.getModel().create();
51-
}
52-
}
14+
/**
15+
* Stores block in registry by instance
16+
* @param ctor The block instance
17+
* @return The block instance
18+
* @param <T> A block ::new
19+
*/
20+
private static <T extends Block> T register(Function<Byte, T> ctor) {
21+
if (nextId >= MAX_BLOCKS) throw new IllegalStateException("Block registry full");
22+
byte id = (byte) nextId;
23+
T block = ctor.apply(id);
24+
REGISTRY.put(id & 0xFF, block);
25+
nextId++;
26+
return block;
27+
}
28+
29+
/**
30+
* Get the block by ID (0 is null)
31+
* @param id Block ID
32+
* @return The block instance
33+
*/
34+
public static Block get(int id) {
35+
return REGISTRY.get(id & 0xFF);
5336
}
5437

55-
}
38+
/**
39+
* Get the total blocks added
40+
* @return
41+
*/
42+
public static int getTotalBlocks() {
43+
return REGISTRY.size();
44+
}
45+
46+
// ---- Declarations (order == IDs). Append new ones at the end. ----
47+
public static final GrassBlock grassBlock = register(GrassBlock::new);
48+
public static final DirtBlock dirtBlock = register(DirtBlock::new);
49+
public static final StoneBlock stoneBlock = register(StoneBlock::new);
50+
public static final SandBlock sandBlock = register(SandBlock::new);
51+
public static final WaterBlock waterBlock = register(WaterBlock::new);
52+
public static final OakLeafBlock leafBlock = register(OakLeafBlock::new);
53+
public static final SpruceLeafBlock spruceLeafBlock = register(SpruceLeafBlock::new);
54+
public static final OakLogBlock logBlock = register(OakLogBlock::new);
55+
public static final SpruceLogBlock spruceLogBlock = register(SpruceLogBlock::new);
56+
public static final BirchLogBlock birchLogBlock = register(BirchLogBlock::new);
57+
public static final OakPlanksBlock oakPlanksBlock = register(OakPlanksBlock::new);
58+
public static final SprucePlanksBlock sprucePlanksBlock = register(SprucePlanksBlock::new);
59+
public static final BirchPlanksBlock birchPlanksBlock = register(BirchPlanksBlock::new);
60+
public static final GlassBlock glassBlock = register(GlassBlock::new);
61+
public static final SnowyGrassBlock snowyGrassBlock = register(SnowyGrassBlock::new);
62+
public static final CactusBlock cactusBlock = register(CactusBlock::new);
63+
public static final ShortGrassBlock shortGrassBlock = register(ShortGrassBlock::new);
64+
public static final RedFlowerBlock redFlowerBlock = register(RedFlowerBlock::new);
65+
public static final YellowFlowerBlock yellowFlowerBlock = register(YellowFlowerBlock::new);
66+
67+
static {
68+
Blocks.REGISTRY.forEach((id, block) -> {
69+
if(block instanceof IBlockRender) block.getModel().create();
70+
});
71+
}
72+
}

src/main/java/com/james090500/blocks/CactusBlock.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.james090500.blocks;
22

33
import com.james090500.blocks.model.CactusModel;
4+
import it.unimi.dsi.fastutil.objects.ObjectList;
45
import org.joml.Vector3i;
56

67
public class CactusBlock extends Block implements IBlockRender {
@@ -25,7 +26,7 @@ public float[] getTexture(String face) {
2526
}
2627

2728
@Override
28-
public void render(Vector3i position) {
29+
public void render(ObjectList<Vector3i> position) {
2930
this.model.render(position);
3031
}
3132
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.james090500.blocks;
22

3+
import it.unimi.dsi.fastutil.objects.ObjectList;
34
import org.joml.Vector3i;
45

56
public interface IBlockRender {
67

78

8-
void render(Vector3i position);
9+
void render(ObjectList<Vector3i> position);
910

1011
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.james090500.blocks;
2+
3+
import com.james090500.blocks.model.VegetationModel;
4+
import it.unimi.dsi.fastutil.objects.ObjectList;
5+
import org.joml.Vector3i;
6+
7+
public class RedFlowerBlock extends VegetationBlock implements IBlockRender {
8+
9+
public RedFlowerBlock(byte id) {
10+
super(id);
11+
this.name = "Red Flower";
12+
this.sound = "grass";
13+
this.texture = 22;
14+
this.transparent = true;
15+
this.solid = false;
16+
this.model = new VegetationModel(this.getTexture());
17+
}
18+
19+
@Override
20+
public void render(ObjectList<Vector3i> position) {
21+
this.model.render(position);
22+
}
23+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.james090500.blocks;
22

33
import com.james090500.blocks.model.VegetationModel;
4+
import it.unimi.dsi.fastutil.objects.ObjectList;
45
import org.joml.Vector3i;
56

67
public class ShortGrassBlock extends VegetationBlock implements IBlockRender {
@@ -9,14 +10,14 @@ public ShortGrassBlock(byte id) {
910
super(id);
1011
this.name = "Short Grass";
1112
this.sound = "grass";
12-
this.texture = 2;
13+
this.texture = 21;
1314
this.transparent = true;
1415
this.solid = false;
1516
this.model = new VegetationModel(this.getTexture());
1617
}
1718

1819
@Override
19-
public void render(Vector3i position) {
20+
public void render(ObjectList<Vector3i> position) {
2021
this.model.render(position);
2122
}
2223
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.james090500.blocks;
2+
3+
import com.james090500.blocks.model.VegetationModel;
4+
import it.unimi.dsi.fastutil.objects.ObjectList;
5+
import org.joml.Vector3i;
6+
7+
public class YellowFlowerBlock extends VegetationBlock implements IBlockRender {
8+
9+
public YellowFlowerBlock(byte id) {
10+
super(id);
11+
this.name = "Yellow Flower";
12+
this.sound = "grass";
13+
this.texture = 23;
14+
this.transparent = true;
15+
this.solid = false;
16+
this.model = new VegetationModel(this.getTexture());
17+
}
18+
19+
@Override
20+
public void render(ObjectList<Vector3i> position) {
21+
this.model.render(position);
22+
}
23+
}

src/main/java/com/james090500/blocks/model/CactusModel.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.james090500.renderer.ModelBuilder;
55
import com.james090500.renderer.ShaderManager;
66
import com.james090500.utils.TextureManager;
7+
import it.unimi.dsi.fastutil.objects.ObjectList;
78
import org.joml.Matrix4f;
89
import org.joml.Vector3f;
910
import org.joml.Vector3i;
@@ -13,7 +14,7 @@
1314

1415
public class CactusModel implements IBlockModel {
1516

16-
ModelBuilder.Model cactusModel;
17+
ModelBuilder cactusModel;
1718

1819
public void create() {
1920
// inputs
@@ -22,26 +23,14 @@ public void create() {
2223
float[] bottomUV = Blocks.cactusBlock.getTexture("bottom");
2324
float[][] uvBases = new float[][] { sideUV, sideUV, sideUV, sideUV, topUV, bottomUV };
2425

25-
cactusModel = ModelBuilder.create().addCube(0f, 0f, 0f, 1f, 1f, 0.9375f).setTexture(uvBases).build();
26+
cactusModel = new ModelBuilder().addCube(0f, 0f, 0f, 1f, 1f, 0.9375f).setTexture(uvBases).build();
2627
}
2728

2829
/**
2930
* Render
30-
* @param position
31+
* @param positions
3132
*/
32-
public void render(Vector3i position) {
33-
Matrix4f model = new Matrix4f().translate(new Vector3f(position));
34-
35-
ShaderManager.basicBlockShader.use();
36-
ShaderManager.basicBlockShader.setMat4("model", model);
37-
ShaderManager.basicBlockShader.useFog();
38-
39-
glBindVertexArray(cactusModel.vao());
40-
glDrawElements(GL_TRIANGLES, cactusModel.indicies(), GL_UNSIGNED_INT, 0);
41-
42-
glBindTexture(GL_TEXTURE_2D, TextureManager.terrainTexture);
43-
44-
glBindVertexArray(0);
45-
ShaderManager.basicBlockShader.stop();
33+
public void render(ObjectList<Vector3i> positions) {
34+
this.cactusModel.render(positions);
4635
}
4736
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.james090500.blocks.model;
22

3+
import it.unimi.dsi.fastutil.objects.ObjectList;
34
import org.joml.Vector3i;
45

56
public interface IBlockModel {
67

78
void create();
89

9-
void render(Vector3i position);
10+
void render(ObjectList<Vector3i> positions);
1011
}

0 commit comments

Comments
 (0)