PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

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