Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. public class LWJGLHelloWorld {
  2.  
  3. public static int SCREEN_WIDTH;
  4. public static int SCREEN_HEIGHT;
  5. public static int WINDOW_WIDTH;
  6. public static int WINDOW_HEIGHT;
  7. public double WIDTH;
  8. public double HEIGHT;
  9. public ArrayList<Hexagon> hexagons = new ArrayList<Hexagon>();
  10. public ArrayList<String> resources = new ArrayList<String>();
  11. public Texture brick;
  12. public Texture stone;
  13. public Texture lumber;
  14. public Texture wool;
  15. public Texture wheat;
  16. public Texture wasteland;
  17. public int textureID;
  18. private float[][] textureCoords = {
  19. {0.5f, 0.5f},
  20. {0.5f, 0.0f},
  21. {0.0f, 0.25f},
  22. {0.0f, 0.75f},
  23. {0.5f, 0.0f},
  24. {1.0f, 0.75f},
  25. {1.0f, 0.25f}
  26. };
  27. private static enum State {
  28. INTRO, MAIN_MENU, GAME;
  29. }
  30.  
  31. private State state = State.INTRO;
  32.  
  33. public LWJGLHelloWorld(){
  34.  
  35. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  36. double SCREEN_WIDTH = screenSize.getWidth();
  37. double SCREEN_HEIGHT = screenSize.getHeight();
  38. double WIDTH = SCREEN_WIDTH * .85;
  39. double HEIGHT = SCREEN_HEIGHT * .85;
  40.  
  41. try {
  42. Display.setDisplayMode(new DisplayMode((int)WIDTH, (int)HEIGHT));
  43. Display.setTitle("Hello, LWJGL!");;
  44. Display.create();
  45. } catch (LWJGLException e){
  46. e.printStackTrace();
  47. }
  48. resetResources();
  49.  
  50.  
  51. brick = loadTexture("brick");
  52. stone = loadTexture("stone");
  53. lumber = loadTexture("lumber");
  54. //Texture wheat = loadTexture("wheat");
  55. wool = loadTexture("wool");
  56. wasteland = loadTexture("wasteland");
  57.  
  58. glMatrixMode(GL_PROJECTION);
  59. glLoadIdentity();
  60. glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
  61. glMatrixMode(GL_MODELVIEW);
  62. glEnable(GL_TEXTURE_2D);
  63.  
  64. int originX = (int)(Display.getDisplayMode().getWidth() / 2);
  65. int originY = (int)(Display.getDisplayMode().getHeight() / 2);
  66. int radius = (int)(HEIGHT * .1);
  67. int padding = (int)(HEIGHT * .005);
  68.  
  69. findHexCoords(originX, originY, 5, radius, padding);
  70.  
  71. while(!Display.isCloseRequested()){
  72. checkInput();
  73. glClear(GL_COLOR_BUFFER_BIT);
  74. for(int h = 0; h < hexagons.size(); h++){
  75. String rsrc = resources.get(h);
  76. switch(rsrc){
  77. case "brick":
  78. brick.bind();
  79. break;
  80. case "stone":
  81. stone.bind();
  82. break;
  83. case "lumber":
  84. lumber.bind();
  85. break;
  86. case "wheat":
  87. //wheat.bind();
  88. break;
  89. case "wool":
  90. wool.bind();
  91. break;
  92. case "wasteland":
  93. wasteland.bind();
  94. break;
  95. }
  96. glBegin(GL_POLYGON);
  97. Hexagon hex = hexagons.get(h);
  98. for(int p = 0; p < hex.points.length; p++){
  99. Point point = hex.points[p];
  100. glVertex2f(point.x, point.y);
  101. glTexCoord2f(textureCoords[p][0], textureCoords[p][0]);
  102. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  103. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  104. }
  105. glEnd();
  106. }
  107.  
  108. Display.update();
  109. Display.sync(60);
  110. }
  111.  
  112. Display.destroy();
  113. }
  114.  
  115. private void bindTexture(String rsrc){
  116.  
  117. }
  118.  
  119. private void findHexCoords(int x, int y, int size, int radius, int padding) {
  120.  
  121. Point origin = new Point(x, y);
  122. double ang30 = Math.toRadians(30);
  123. double xOff = Math.cos(ang30) * (radius + padding);
  124. double yOff = Math.sin(ang30) * (radius + padding);
  125. int half = size / 2;
  126.  
  127. int i = 0;
  128. for (int row = 0; row < size; row++) {
  129.  
  130. int cols = size - Math.abs(row - half);
  131.  
  132. for (int col = 0; col < cols; col++) {
  133.  
  134. int xLbl = row < half ? col - row : col - half;
  135. int yLbl = row - half;
  136. int centerX = (int) (origin.x + xOff * (col * 2 + 1 - cols));
  137. int centerY = (int) (origin.y + yOff * (row - half) * 3);
  138.  
  139. Hexagon hex = new Hexagon(centerX, centerY, radius);
  140. System.out.println(centerX+","+centerY);
  141. hexagons.add(hex);
  142. i++;
  143. }
  144. }
  145. }
  146.  
  147. public void checkInput(){
  148. switch(state){
  149. case INTRO:
  150. if(Keyboard.isKeyDown(Keyboard.KEY_S)){
  151. state = State.MAIN_MENU;
  152. }
  153. if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
  154. Display.destroy();
  155. System.exit(0);;
  156. }
  157. break;
  158. case GAME:
  159. if(Keyboard.isKeyDown(Keyboard.KEY_BACK)){
  160. state = State.MAIN_MENU;
  161. }
  162. break;
  163. case MAIN_MENU:
  164. if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
  165. state = State.GAME;
  166. }
  167. if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
  168. state = State.INTRO;
  169. }
  170. break;
  171. }
  172. }
  173.  
  174. private Texture loadTexture(String key){
  175. try {
  176. return TextureLoader.getTexture("PNG", new FileInputStream(new File("img/" + key + ".png")));
  177. } catch (FileNotFoundException e) {
  178. e.printStackTrace();
  179. } catch (IOException e) {
  180. e.printStackTrace();
  181. }
  182. return null;
  183. }
  184. public static void main(String[] args) {
  185. new LWJGLHelloWorld();
  186.  
  187. }
  188.  
  189. public void resetResources(){
  190. resources.clear();
  191. resources.add("Brick");
  192. resources.add("Brick");
  193. resources.add("Brick");
  194. resources.add("Wool");
  195. resources.add("Wool");
  196. resources.add("Wool");
  197. resources.add("Wool");
  198. resources.add("Lumber");
  199. resources.add("Lumber");
  200. resources.add("Lumber");
  201. resources.add("Lumber");
  202. resources.add("Stone");
  203. resources.add("Stone");
  204. resources.add("Stone");
  205. resources.add("Wheat");
  206. resources.add("Wheat");
  207. resources.add("Wheat");
  208. resources.add("Wheat");
  209. long seed = System.nanoTime();
  210. Collections.shuffle(resources, new Random(seed));
  211. int randomIndex = ThreadLocalRandom.current().nextInt(0, 19);
  212. resources.add(randomIndex, "Wasteland");
  213. for(int r = 0; r < resources.size(); r++){
  214. System.out.println(resources.get(r));
  215. }
  216. }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement