|
| 1 | +package se.llbit.chunky.entity; |
| 2 | + |
| 3 | +import se.llbit.chunky.model.Model; |
| 4 | +import se.llbit.chunky.resources.Texture; |
| 5 | +import se.llbit.chunky.world.material.TextureMaterial; |
| 6 | +import se.llbit.json.JsonObject; |
| 7 | +import se.llbit.json.JsonValue; |
| 8 | +import se.llbit.math.Quad; |
| 9 | +import se.llbit.math.Transform; |
| 10 | +import se.llbit.math.Vector3; |
| 11 | +import se.llbit.math.Vector4; |
| 12 | +import se.llbit.math.primitive.Primitive; |
| 13 | +import se.llbit.nbt.CompoundTag; |
| 14 | +import se.llbit.util.JsonUtil; |
| 15 | + |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.LinkedList; |
| 18 | + |
| 19 | +public class Lectern extends Entity { |
| 20 | + private static final Quad[] quadsNorth = new Quad[]{ |
| 21 | + new Quad( |
| 22 | + new Vector3(16 / 16.0, 2 / 16.0, 0 / 16.0), |
| 23 | + new Vector3(0 / 16.0, 2 / 16.0, 0 / 16.0), |
| 24 | + new Vector3(16 / 16.0, 2 / 16.0, 16 / 16.0), |
| 25 | + new Vector4(0 / 16.0, 16 / 16.0, 0 / 16.0, 16 / 16.0) |
| 26 | + ), |
| 27 | + new Quad( |
| 28 | + new Vector3(0 / 16.0, 0 / 16.0, 0 / 16.0), |
| 29 | + new Vector3(16 / 16.0, 0 / 16.0, 0 / 16.0), |
| 30 | + new Vector3(0 / 16.0, 0 / 16.0, 16 / 16.0), |
| 31 | + new Vector4(0 / 16.0, 16 / 16.0, 0 / 16.0, 16 / 16.0) |
| 32 | + ), |
| 33 | + new Quad( |
| 34 | + new Vector3(0 / 16.0, 2 / 16.0, 16 / 16.0), |
| 35 | + new Vector3(0 / 16.0, 2 / 16.0, 0 / 16.0), |
| 36 | + new Vector3(0 / 16.0, 0 / 16.0, 16 / 16.0), |
| 37 | + new Vector4(16 / 16.0, 0 / 16.0, 10 / 16.0, 8 / 16.0) |
| 38 | + ), |
| 39 | + new Quad( |
| 40 | + new Vector3(16 / 16.0, 2 / 16.0, 0 / 16.0), |
| 41 | + new Vector3(16 / 16.0, 2 / 16.0, 16 / 16.0), |
| 42 | + new Vector3(16 / 16.0, 0 / 16.0, 0 / 16.0), |
| 43 | + new Vector4(16 / 16.0, 0 / 16.0, 10 / 16.0, 8 / 16.0) |
| 44 | + ), |
| 45 | + new Quad( |
| 46 | + new Vector3(0 / 16.0, 2 / 16.0, 0 / 16.0), |
| 47 | + new Vector3(16 / 16.0, 2 / 16.0, 0 / 16.0), |
| 48 | + new Vector3(0 / 16.0, 0 / 16.0, 0 / 16.0), |
| 49 | + new Vector4(16 / 16.0, 0 / 16.0, 2 / 16.0, 0 / 16.0) |
| 50 | + ), |
| 51 | + new Quad( |
| 52 | + new Vector3(16 / 16.0, 2 / 16.0, 16 / 16.0), |
| 53 | + new Vector3(0 / 16.0, 2 / 16.0, 16 / 16.0), |
| 54 | + new Vector3(16 / 16.0, 0 / 16.0, 16 / 16.0), |
| 55 | + new Vector4(16 / 16.0, 0 / 16.0, 10 / 16.0, 8 / 16.0) |
| 56 | + ), |
| 57 | + new Quad( |
| 58 | + new Vector3(4 / 16.0, 2 / 16.0, 12 / 16.0), |
| 59 | + new Vector3(4 / 16.0, 15 / 16.0, 12 / 16.0), |
| 60 | + new Vector3(4 / 16.0, 2 / 16.0, 4 / 16.0), |
| 61 | + new Vector4(15 / 16.0, 2 / 16.0, 8 / 16.0, 0 / 16.0) |
| 62 | + ), |
| 63 | + new Quad( |
| 64 | + new Vector3(12 / 16.0, 2 / 16.0, 4 / 16.0), |
| 65 | + new Vector3(12 / 16.0, 15 / 16.0, 4 / 16.0), |
| 66 | + new Vector3(12 / 16.0, 2 / 16.0, 12 / 16.0), |
| 67 | + new Vector4(15 / 16.0, 2 / 16.0, 0 / 16.0, 8 / 16.0) |
| 68 | + ), |
| 69 | + new Quad( |
| 70 | + new Vector3(4 / 16.0, 15 / 16.0, 4 / 16.0), |
| 71 | + new Vector3(12 / 16.0, 15 / 16.0, 4 / 16.0), |
| 72 | + new Vector3(4 / 16.0, 2 / 16.0, 4 / 16.0), |
| 73 | + new Vector4(8 / 16.0, 0 / 16.0, 16 / 16.0, 3 / 16.0) |
| 74 | + ), |
| 75 | + new Quad( |
| 76 | + new Vector3(12 / 16.0, 15 / 16.0, 12 / 16.0), |
| 77 | + new Vector3(4 / 16.0, 15 / 16.0, 12 / 16.0), |
| 78 | + new Vector3(12 / 16.0, 2 / 16.0, 12 / 16.0), |
| 79 | + new Vector4(16 / 16.0, 8 / 16.0, 13 / 16.0, 0 / 16.0) |
| 80 | + ) |
| 81 | + }; |
| 82 | + |
| 83 | + private static final Quad[] topQuadsNorth = Model.rotateX(new Quad[]{ |
| 84 | + new Quad( |
| 85 | + new Vector3(15.99 / 16.0, 16 / 16.0, 3 / 16.0), |
| 86 | + new Vector3(0.01 / 16.0, 16 / 16.0, 3 / 16.0), |
| 87 | + new Vector3(15.99 / 16.0, 16 / 16.0, 16 / 16.0), |
| 88 | + new Vector4(0 / 16.0, 16 / 16.0, 2 / 16.0, 15 / 16.0) |
| 89 | + ), |
| 90 | + new Quad( |
| 91 | + new Vector3(0.01 / 16.0, 12 / 16.0, 3 / 16.0), |
| 92 | + new Vector3(15.99 / 16.0, 12 / 16.0, 3 / 16.0), |
| 93 | + new Vector3(0.01 / 16.0, 12 / 16.0, 16 / 16.0), |
| 94 | + new Vector4(0 / 16.0, 16 / 16.0, 3 / 16.0, 16 / 16.0) |
| 95 | + ), |
| 96 | + new Quad( |
| 97 | + new Vector3(0.01 / 16.0, 16 / 16.0, 16 / 16.0), |
| 98 | + new Vector3(0.01 / 16.0, 16 / 16.0, 3 / 16.0), |
| 99 | + new Vector3(0.01 / 16.0, 12 / 16.0, 16 / 16.0), |
| 100 | + new Vector4(13 / 16.0, 0 / 16.0, 12 / 16.0, 8 / 16.0) |
| 101 | + ), |
| 102 | + new Quad( |
| 103 | + new Vector3(15.99 / 16.0, 16 / 16.0, 3 / 16.0), |
| 104 | + new Vector3(15.99 / 16.0, 16 / 16.0, 16 / 16.0), |
| 105 | + new Vector3(15.99 / 16.0, 12 / 16.0, 3 / 16.0), |
| 106 | + new Vector4(13 / 16.0, 0 / 16.0, 12 / 16.0, 8 / 16.0) |
| 107 | + ), |
| 108 | + new Quad( |
| 109 | + new Vector3(0.01 / 16.0, 16 / 16.0, 3 / 16.0), |
| 110 | + new Vector3(15.99 / 16.0, 16 / 16.0, 3 / 16.0), |
| 111 | + new Vector3(0.01 / 16.0, 12 / 16.0, 3 / 16.0), |
| 112 | + new Vector4(16 / 16.0, 0 / 16.0, 16 / 16.0, 12 / 16.0) |
| 113 | + ), |
| 114 | + new Quad( |
| 115 | + new Vector3(15.99 / 16.0, 16 / 16.0, 16 / 16.0), |
| 116 | + new Vector3(0.01 / 16.0, 16 / 16.0, 16 / 16.0), |
| 117 | + new Vector3(15.99 / 16.0, 12 / 16.0, 16 / 16.0), |
| 118 | + new Vector4(16 / 16.0, 0 / 16.0, 12 / 16.0, 8 / 16.0) |
| 119 | + ) |
| 120 | + }, Math.toRadians(-22.5)); |
| 121 | + |
| 122 | + static final Quad[][] orientedQuads = new Quad[4][]; |
| 123 | + |
| 124 | + static final Quad[][] orientedTopQuads = new Quad[4][]; |
| 125 | + |
| 126 | + static { |
| 127 | + orientedQuads[0] = quadsNorth; |
| 128 | + orientedQuads[1] = Model.rotateY(orientedQuads[0]); |
| 129 | + orientedQuads[2] = Model.rotateY(orientedQuads[1]); |
| 130 | + orientedQuads[3] = Model.rotateY(orientedQuads[2]); |
| 131 | + |
| 132 | + orientedTopQuads[0] = topQuadsNorth; |
| 133 | + orientedTopQuads[1] = Model.rotateY(orientedTopQuads[0]); |
| 134 | + orientedTopQuads[2] = Model.rotateY(orientedTopQuads[1]); |
| 135 | + orientedTopQuads[3] = Model.rotateY(orientedTopQuads[2]); |
| 136 | + } |
| 137 | + |
| 138 | + public static final Texture[] tex = { |
| 139 | + Texture.lecternBase, Texture.oakPlanks, |
| 140 | + Texture.lecternBase, Texture.lecternBase, Texture.lecternBase, Texture.lecternBase, |
| 141 | + |
| 142 | + Texture.lecternSides, Texture.lecternSides, Texture.lecternFront, Texture.lecternFront, |
| 143 | + |
| 144 | + Texture.lecternTop, Texture.oakPlanks, |
| 145 | + Texture.lecternSides, Texture.lecternSides, Texture.lecternSides, Texture.lecternSides |
| 146 | + }; |
| 147 | + |
| 148 | + private final String facing; |
| 149 | + |
| 150 | + public Lectern(Vector3 position, String facing) { |
| 151 | + super(position); |
| 152 | + this.facing = facing; |
| 153 | + } |
| 154 | + |
| 155 | + public Lectern(JsonObject json) { |
| 156 | + super(JsonUtil.vec3FromJsonObject(json.get("position"))); |
| 157 | + this.facing = json.get("facing").stringValue("north"); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public Collection<Primitive> primitives(Vector3 offset) { |
| 162 | + Collection<Primitive> faces = new LinkedList<>(); |
| 163 | + Transform transform = Transform.NONE |
| 164 | + .translate(position.x + offset.x, position.y + offset.y, position.z + offset.z); |
| 165 | + int facing = getOrientationIndex(this.facing); |
| 166 | + for (int i = 0; i < orientedQuads[facing].length; i++) { |
| 167 | + orientedQuads[facing][i].addTriangles(faces, new TextureMaterial(tex[i]), transform); |
| 168 | + } |
| 169 | + for (int i = 0; i < orientedTopQuads[facing].length; i++) { |
| 170 | + orientedTopQuads[facing][i].addTriangles(faces, new TextureMaterial(tex[i + orientedQuads[facing].length]), transform); |
| 171 | + } |
| 172 | + return faces; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public JsonValue toJson() { |
| 177 | + JsonObject json = new JsonObject(); |
| 178 | + json.add("kind", "lectern"); |
| 179 | + json.add("position", position.toJson()); |
| 180 | + json.add("facing", facing); |
| 181 | + return json; |
| 182 | + } |
| 183 | + |
| 184 | + public static Entity fromJson(JsonObject json) { |
| 185 | + return new Lectern(json); |
| 186 | + } |
| 187 | + |
| 188 | + private static int getOrientationIndex(String facing) { |
| 189 | + switch (facing) { |
| 190 | + case "north": |
| 191 | + return 0; |
| 192 | + case "east": |
| 193 | + return 1; |
| 194 | + case "south": |
| 195 | + return 2; |
| 196 | + case "west": |
| 197 | + return 3; |
| 198 | + default: |
| 199 | + return 0; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments