PageRenderTime 48ms CodeModel.GetById 37ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/newcode/src/com/prupe/mcpatcher/mob/LineRenderer.java

https://bitbucket.org/michael_rodrigues/mcpatcher
Java | 173 lines | 156 code | 16 blank | 1 comment | 31 complexity | 0a2ff8842a9ffb2d85873315998aa0ac MD5 | raw file
  1package com.prupe.mcpatcher.mob;
  2
  3import com.prupe.mcpatcher.*;
  4import net.minecraft.src.ResourceLocation;
  5import net.minecraft.src.Tessellator;
  6import org.lwjgl.input.Keyboard;
  7import org.lwjgl.opengl.GL11;
  8
  9import java.util.Properties;
 10
 11public class LineRenderer {
 12    private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.RANDOM_MOBS);
 13
 14    private static final double D_WIDTH = 1.0 / 1024.0;
 15    private static final double D_POS = 1.0 / 256.0;
 16
 17    private static final boolean enable = Config.getBoolean(MCPatcherUtils.RANDOM_MOBS, "leashLine", true);
 18    private static final LineRenderer[] renderers = new LineRenderer[2];
 19
 20    private final ResourceLocation texture;
 21    private final double width;
 22    private final double a;
 23    private final double b;
 24    private final double sx;
 25    private final double sy;
 26    private final double sz;
 27    private final int segments;
 28    private final double tileFactor;
 29    private final boolean active;
 30    private final InputHandler keyboard;
 31
 32    private double plusWidth;
 33    private double plusTile;
 34    private double plusSX;
 35    private double plusSY;
 36    private double plusSZ;
 37
 38    public static boolean renderLine(int type, double x, double y, double z, double dx, double dy, double dz) {
 39        LineRenderer renderer = renderers[type];
 40        return renderer != null && renderer.render(x, y, z, dx, dy, dz);
 41    }
 42
 43    static void reset() {
 44        if (enable) {
 45            setup(0, "fishingline", 0.0075, 0.0, 0.25, 16);
 46            setup(1, "lead", 0.025, 4.0 / 3.0, 0.125, 24);
 47        }
 48    }
 49
 50    private static void setup(int type, String name, double defaultWidth, double a, double b, int segments) {
 51        LineRenderer renderer = new LineRenderer(name, defaultWidth, a, b, segments);
 52        if (renderer.active) {
 53            logger.fine("using %s", renderer);
 54            renderers[type] = renderer;
 55        } else {
 56            logger.fine("%s not found", renderer);
 57            renderers[type] = null;
 58        }
 59    }
 60
 61    private LineRenderer(String name, double width, double a, double b, int segments) {
 62        texture = TexturePackAPI.newMCPatcherResourceLocation("line/" + name + ".png");
 63        active = TexturePackAPI.hasResource(texture);
 64        Properties properties = TexturePackAPI.getProperties(TexturePackAPI.transformResourceLocation(texture, ".png", ".properties"));
 65        this.width = MCPatcherUtils.getDoubleProperty(properties, "width", width);
 66        this.a = MCPatcherUtils.getDoubleProperty(properties, "a", a);
 67        this.b = MCPatcherUtils.getDoubleProperty(properties, "b", b);
 68        this.sx = MCPatcherUtils.getDoubleProperty(properties, "sx", 0.0);
 69        this.sy = MCPatcherUtils.getDoubleProperty(properties, "sy", 0.0);
 70        this.sz = MCPatcherUtils.getDoubleProperty(properties, "sz", 0.0);
 71        this.segments = MCPatcherUtils.getIntProperty(properties, "segments", segments);
 72        this.tileFactor = MCPatcherUtils.getDoubleProperty(properties, "tileFactor", 24.0);
 73        keyboard = new InputHandler(name, MCPatcherUtils.getBooleanProperty(properties, "debug", false));
 74    }
 75
 76    private boolean render(double x, double y, double z, double dx, double dy, double dz) {
 77        if (keyboard.isKeyDown(Keyboard.KEY_MULTIPLY)) {
 78            return false;
 79        }
 80        boolean changed = false;
 81        if (!keyboard.isEnabled()) {
 82            // nothing
 83        } else if (keyboard.isKeyPressed(Keyboard.KEY_ADD)) {
 84            changed = true;
 85            plusWidth += D_WIDTH;
 86        } else if (keyboard.isKeyPressed(Keyboard.KEY_SUBTRACT)) {
 87            changed = true;
 88            plusWidth -= D_WIDTH;
 89        } else if (keyboard.isKeyPressed(Keyboard.KEY_DIVIDE)) {
 90            changed = true;
 91            plusWidth = plusTile = plusSX = plusSY = plusSZ = 0.0;
 92        } else if (keyboard.isKeyPressed(Keyboard.KEY_NUMPAD3)) {
 93            changed = true;
 94            plusTile--;
 95        } else if (keyboard.isKeyPressed(Keyboard.KEY_NUMPAD9)) {
 96            changed = true;
 97            plusTile++;
 98        } else if (keyboard.isKeyDown(Keyboard.KEY_NUMPAD4)) {
 99            changed = true;
100            plusSX -= D_POS;
101        } else if (keyboard.isKeyDown(Keyboard.KEY_NUMPAD6)) {
102            changed = true;
103            plusSX += D_POS;
104        } else if (keyboard.isKeyDown(Keyboard.KEY_NUMPAD1)) {
105            changed = true;
106            plusSY -= D_POS;
107        } else if (keyboard.isKeyDown(Keyboard.KEY_NUMPAD7)) {
108            changed = true;
109            plusSY += D_POS;
110        } else if (keyboard.isKeyDown(Keyboard.KEY_NUMPAD2)) {
111            changed = true;
112            plusSZ += D_POS;
113        } else if (keyboard.isKeyDown(Keyboard.KEY_NUMPAD8)) {
114            changed = true;
115            plusSZ -= D_POS;
116        }
117        TexturePackAPI.bindTexture(texture);
118        Tessellator tessellator = Tessellator.instance;
119        tessellator.startDrawingQuads();
120        GL11.glDisable(GL11.GL_CULL_FACE);
121        dx += sx + plusSX;
122        dy += sy + plusSY;
123        dz += sz + plusSZ;
124        double x0 = x;
125        double y0 = y + a + b;
126        double z0 = z;
127        double u0 = 0.0;
128        double len = Math.sqrt(dx * dx + dy * dy + dz * dz);
129        double t = tileFactor + plusTile;
130        double w = width + plusWidth;
131        if (changed) {
132            logger.info("%s: dx=%f, dy=%f, dz=%f, len=%f(*%d=%f), slen=%f",
133                this, dx, dy, dz, len, (int) t, len * t, len * t / segments
134            );
135            System.out.printf("width=%f\n", w);
136            System.out.printf("tileFactor=%f\n", t);
137            System.out.printf("sx=%f\n", sx + plusSX);
138            System.out.printf("sy=%f\n", sy + plusSY);
139            System.out.printf("sz=%f\n", sz + plusSZ);
140        }
141        len *= t / segments;
142        for (int i = 1; i <= segments; i++) {
143            double s = i / (double) segments;
144            double x1 = x + s * dx;
145            double y1 = y + (s * s + s) * 0.5 * dy + a * (1.0 - s) + b;
146            double z1 = z + s * dz;
147            double u1 = (segments - i) * len;
148
149            tessellator.addVertexWithUV(x0, y0, z0, u0, 1.0);
150            tessellator.addVertexWithUV(x1, y1, z1, u1, 1.0);
151            tessellator.addVertexWithUV(x1 + w, y1 + w, z1, u1, 0.0);
152            tessellator.addVertexWithUV(x0 + w, y0 + w, z0, u0, 0.0);
153
154            tessellator.addVertexWithUV(x0, y0 + w, z0, u0, 1.0);
155            tessellator.addVertexWithUV(x1, y1 + w, z1, u1, 1.0);
156            tessellator.addVertexWithUV(x1 + w, y1, z1 + w, u1, 0.0);
157            tessellator.addVertexWithUV(x0 + w, y0, z0 + w, u0, 0.0);
158
159            x0 = x1;
160            y0 = y1;
161            z0 = z1;
162            u0 = u1;
163        }
164        tessellator.draw();
165        GL11.glEnable(GL11.GL_CULL_FACE);
166        return true;
167    }
168
169    @Override
170    public String toString() {
171        return "LineRenderer{" + texture + ", " + (width + plusWidth) + "}";
172    }
173}