Advertisement
ChaoticCactus

2D Minecraft - Not Working?

Apr 16th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.49 KB | None | 0 0
  1. public class Component {
  2.  
  3. public static int width = 640, height = 480;
  4. private String title = "Side Scrolling Magyk!";
  5.  
  6. private BlockGrid grid;
  7. private BlockType selection = BlockType.STONE;
  8.  
  9. private Texture sky;
  10.  
  11. private Player player;
  12.  
  13. long lastFrame, lastFPS;
  14. int fps;
  15.  
  16. public Component() {
  17. try {
  18. Display.setDisplayMode(new DisplayMode(width, height));
  19. Display.setTitle(title);
  20. Display.create();
  21. } catch(LWJGLException e) {
  22. e.printStackTrace();
  23. Display.destroy();
  24. System.exit(0);
  25. }
  26.  
  27. sky = loadTexture("sky");
  28.  
  29. Entity player = new Player((width / 2) - 16, (height / 2) - 32, 16, 32);
  30. grid = new BlockGrid();
  31. new Camera();
  32.  
  33. initGL();
  34. getDelta();
  35. lastFPS = getTime();
  36.  
  37.  
  38.  
  39. while(!Display.isCloseRequested()) {
  40. int delta = getDelta();
  41.  
  42. glClear(GL_COLOR_BUFFER_BIT);
  43.  
  44. input();
  45.  
  46.  
  47. render();
  48.  
  49. Player.player = Player.loadTexture("player_outline");
  50. player.render();
  51. player.update(delta);
  52. grid.generateWorld();
  53.  
  54. Display.update();
  55. Display.sync(60);
  56.  
  57. }
  58. }
  59.  
  60. private void input() {
  61. int mousex = Mouse.getX();
  62. int mousey = height - Mouse.getY() - 1;
  63. int grid_x = Math.round(mousex / World.BLOCK_SIZE);
  64. int grid_y = Math.round(mousey / World.BLOCK_SIZE);
  65. boolean mouseClicked = Mouse.isButtonDown(0);
  66.  
  67. //Input
  68. if(mouseClicked) {
  69. grid.setAt(grid_x, grid_y, BlockType.AIR);
  70. }
  71. if(Mouse.isButtonDown(1)) {
  72. grid.setAt(grid_x, grid_y, selection);
  73. }
  74. while(Keyboard.next()) {
  75. //Saving and Loading//
  76. if(Keyboard.getEventKey() == Keyboard.KEY_S) {
  77. grid.save(new File("save.xml"));
  78. }
  79. if(Keyboard.getEventKey() == Keyboard.KEY_L) {
  80. grid.load(new File("save.xml"));
  81. }
  82.  
  83. //Selection//
  84. if(Keyboard.getEventKey() == Keyboard.KEY_1) {
  85. selection = BlockType.STONE;
  86. }
  87. if(Keyboard.getEventKey() == Keyboard.KEY_2) {
  88. selection = BlockType.DIRT;
  89. }
  90. if(Keyboard.getEventKey() == Keyboard.KEY_3) {
  91. selection = BlockType.GRASS;
  92. }
  93.  
  94. //Closing//
  95. if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
  96. Display.destroy();
  97. System.exit(0);
  98. }
  99. }
  100.  
  101. updateFPS();
  102. }
  103.  
  104. public void render() {
  105.  
  106. sky.bind();
  107. glBegin(GL_QUADS);
  108. glTexCoord2f(0, 0);
  109. glVertex2f(0, 0);
  110. glTexCoord2f(1, 0);
  111. glVertex2f(width + 400, 0);
  112. glTexCoord2f(1, 1);
  113. glVertex2f(width + 400, height + 400);
  114. glTexCoord2f(0, 1);
  115. glVertex2f(0, height + 400);
  116. glEnd();
  117. }
  118.  
  119. public void initGL() {
  120. glMatrixMode(GL_PROJECTION);
  121. glLoadIdentity();
  122. glOrtho(0, width, height, 0, 1, -1);
  123. glMatrixMode(GL_MODELVIEW);
  124. glEnable(GL_TEXTURE_2D);
  125.  
  126. glEnable(GL_BLEND);
  127. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  128. }
  129.  
  130. public long getTime() {
  131. return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  132. }
  133.  
  134. public int getDelta() {
  135. long time = getTime();
  136. int delta = (int) (time - lastFrame);
  137. lastFrame = time;
  138.  
  139. return delta;
  140. }
  141.  
  142. public void updateFPS() {
  143. if(getTime() - lastFPS > 1000) {
  144. fps = 0;
  145. lastFPS += 1000;
  146. }
  147.  
  148. fps++;
  149. }
  150.  
  151. private Texture loadTexture(String key) {
  152. try {
  153. return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/images/" + key + ".png")));
  154. } catch (FileNotFoundException e) {
  155. e.printStackTrace();
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159.  
  160. return null;
  161. }
  162.  
  163. public static int getWidth() {
  164. return width;
  165. }
  166.  
  167. public static int getHeight() {
  168. return height;
  169. }
  170.  
  171. public static void main(String[] args) {
  172. new Component();
  173. }
  174. }
  175.  
  176. // NEXT CLASS //
  177.  
  178. public class Player extends AbstractMoveableEntity {
  179.  
  180. public float vertSpeed = 0;
  181. public float moveSpeed = 0.2f,
  182. translateX = 0;
  183. public static Texture player;
  184.  
  185. private boolean canMove = true;
  186.  
  187. BlockGrid grid;
  188. Block block;
  189.  
  190. Camera cam;
  191.  
  192. public Player(double x, double y, int width, int height) {
  193. super(x, y, width, height);
  194. }
  195.  
  196. @Override
  197. public void render() {
  198. glBegin(GL_QUADS);
  199. glTexCoord2f(0, 0);
  200. glVertex2f((float) x, (float) y);
  201. glTexCoord2f(1, 0);
  202. glVertex2f((float) (x + width), (float) y);
  203. glTexCoord2f(1, 1);
  204. glVertex2f((float) (x + width), (float) (y + height));
  205. glTexCoord2f(0, 1);
  206. glVertex2f((float) x, (float) (y + height));
  207. glEnd();
  208.  
  209.  
  210. }
  211.  
  212. @Override
  213. public void update(int delta) {
  214. if(Keyboard.isKeyDown(Keyboard.KEY_LEFT) && canMove) {
  215. x -= moveSpeed * delta;
  216. }
  217. if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && canMove) {
  218. x += moveSpeed * delta;
  219. }
  220. if(Keyboard.isKeyDown(Keyboard.KEY_UP) && canMove) {
  221. y -= moveSpeed * delta;
  222. }
  223. if(Keyboard.isKeyDown(Keyboard.KEY_DOWN) && canMove) {
  224. y += moveSpeed * delta;
  225. }
  226.  
  227. glTranslatef((int) x, (int) y, 0);
  228. }
  229.  
  230. public static Texture loadTexture(String path) {
  231. try {
  232. return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/images/" + path + ".png")));
  233. } catch (FileNotFoundException e) {
  234. e.printStackTrace();
  235. } catch (IOException e) {
  236. e.printStackTrace();
  237. }
  238.  
  239. return null;
  240. }
  241.  
  242. public Rectangle getBounds() {
  243. return new Rectangle((int) x, (int) y, (int) width, (int) height);
  244. }
  245. }
  246.  
  247. // NEXT CLASS //
  248.  
  249. public class BlockGrid {
  250.  
  251. public Block[][] block = new Block[worldW][worldH];
  252.  
  253. public BlockGrid(File loadFile) {
  254.  
  255. }
  256.  
  257. public BlockGrid() {
  258. Random rand = new Random();
  259.  
  260. for(int x = 0; x < worldW; x++) {
  261. for(int y = 0; y < worldH; y++) {
  262. block[x][y] = new Block(BlockType.AIR, x * BLOCK_SIZE, y * BLOCK_SIZE);
  263. }
  264. }
  265.  
  266. for(int y = 0; y < worldW; y++) {
  267. for(int x = 0; x < worldH; x++) {
  268.  
  269. //Random dirt (world) generation//
  270. if(y > worldH / 6) {
  271. if(rand.nextInt(100) > 10) {
  272. try {
  273. if(block[x - 1][y - 1].getType() == BlockType.DIRT) {
  274. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  275. }
  276. } catch(Exception e) { }
  277. }
  278.  
  279. if(rand.nextInt(100) > 10) {
  280. try {
  281. if(block[x + 1][y - 1].getType() == BlockType.DIRT) {
  282. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  283. }
  284. } catch(Exception e) { }
  285. }
  286.  
  287. try {
  288. if(block[x][y - 1].getType() == BlockType.DIRT) {
  289. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  290. }
  291. } catch(Exception e) { }
  292. if(rand.nextInt(100) < 10) {
  293. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  294. }
  295. }
  296.  
  297. //Random stone generation//
  298. if(y > worldH / 4) {
  299. if(rand.nextInt(100) > 20) {
  300. try {
  301. if(block[x - 1][y - 1].getType() == BlockType.STONE) {
  302. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  303. }
  304. } catch(Exception e) { }
  305. }
  306.  
  307. if(rand.nextInt(100) > 20) {
  308. try {
  309. if(block[x + 1][y - 1].getType() == BlockType.STONE) {
  310. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  311. }
  312. } catch(Exception e) { }
  313. }
  314.  
  315. try {
  316. if(block[x][y - 1].getType() == BlockType.STONE) {
  317. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  318. }
  319. } catch(Exception e) { }
  320.  
  321. if(rand.nextInt(100) < 5) {
  322. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  323. }
  324. }
  325. }
  326. }
  327.  
  328. //Placing grass blocks//
  329. for(int y = 0; y < block.length; y++) {
  330. for(int x = 0; x < block[0].length; x++) {
  331. if(block[x][y].getType() == BlockType.DIRT && block[x][y - 1].getType() == BlockType.AIR) {
  332. block[x][y] = new Block(BlockType.GRASS, x * BLOCK_SIZE, y * BLOCK_SIZE);
  333. }
  334. }
  335. }
  336. }
  337.  
  338. public void generateWorld() {
  339. for(int x = 0; x < worldW - 1; x++) {
  340. for(int y = 0; y < worldH - 1; y++) {
  341. block[x][y].render();
  342. }
  343. }
  344. }
  345.  
  346. public void save(File saveFile) {
  347. Document document = new Document();
  348. Element root = new Element("blocks");
  349. document.setRootElement(root);
  350. for(int x = 0; x < worldW - 1; x++) {
  351. for(int y = 0; y < worldH - 1; y++) {
  352. Element blocks = new Element("blocks");
  353. blocks.setAttribute("x", String.valueOf((int) (block[x][y].getX() / BLOCK_SIZE)));
  354. blocks.setAttribute("y", String.valueOf((int) (block[x][y].getY() / BLOCK_SIZE)));
  355. blocks.setAttribute("type", String.valueOf(block[x][y].getType()));
  356. root.addContent(blocks);
  357. }
  358.  
  359. XMLOutputter output = new XMLOutputter();
  360. try {
  361. output.output(document, new FileOutputStream(saveFile));
  362. } catch (FileNotFoundException e) {
  363. e.printStackTrace();
  364. } catch (IOException e) {
  365. e.printStackTrace();
  366. }
  367. }
  368. }
  369.  
  370. public void load(File loadFile) {
  371. try {
  372. SAXBuilder builder = new SAXBuilder();
  373. Document document = builder.build(loadFile);
  374. Element root = document.getRootElement();
  375. for(Object blocks : root.getChildren()) {
  376. Element e = (Element) blocks;
  377. int x = Integer.parseInt(e.getAttributeValue("x"));
  378. int y = Integer.parseInt(e.getAttributeValue("y"));
  379. block[x][y] = new Block(BlockType.valueOf(e.getAttributeValue("type")), x * BLOCK_SIZE, y * BLOCK_SIZE);
  380. }
  381. } catch (JDOMException e) {
  382. e.printStackTrace();
  383. } catch (IOException e) {
  384. e.printStackTrace();
  385. }
  386. }
  387.  
  388. public void setAt(int x, int y, BlockType b) {
  389. block[x][y] = new Block(b, x * BLOCK_SIZE, y * BLOCK_SIZE);
  390. }
  391.  
  392. public Block getAt(int x, int y) {
  393. return block[x][y];
  394. }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement