Advertisement
ChaoticCactus

UPDATED - 2D Minecraft

Apr 21st, 2013
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. public class Player extends AbstractMoveableEntity {
  2.  
  3. public float vertSpeed = 0;
  4. public float moveSpeed = 0.2f,
  5. translateX = 0;
  6. public static Texture player;
  7.  
  8. private boolean canMove = true;
  9.  
  10. BlockGrid grid;
  11. Block block;
  12.  
  13. Camera cam;
  14.  
  15. public Player(float x, float y, int width, int height) {
  16. super(x, y, width, height);
  17. }
  18.  
  19. @Override
  20. public void render() {
  21. glBegin(GL_QUADS);
  22. glTexCoord2f(0, 0);
  23. glVertex2f((float) ((Component.width / 2) - 16), (float) ((Component.height / 2) - 32));
  24. glTexCoord2f(1, 0);
  25. glVertex2f((float) ((Component.width / 2) - 16 + width), (float) ((Component.height / 2) - 32));
  26. glTexCoord2f(1, 1);
  27. glVertex2f((float) ((Component.width / 2) - 16 + width), (float) ((Component.height / 2) - 32 + height));
  28. glTexCoord2f(0, 1);
  29. glVertex2f((float) ((Component.width / 2) - 16), (float) ((Component.height / 2) - 32 + height));
  30. glEnd();
  31. }
  32.  
  33. @Override
  34. public void update(int delta) {
  35. if (Keyboard.isKeyDown(Keyboard.KEY_LEFT) && canMove) {
  36. x -= (moveSpeed * delta);
  37. }
  38. if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && canMove) {
  39. x += (moveSpeed * delta);
  40. }
  41. if (Keyboard.isKeyDown(Keyboard.KEY_UP) && canMove) {
  42. y -= (moveSpeed * delta);
  43. }
  44. if (Keyboard.isKeyDown(Keyboard.KEY_DOWN) && canMove) {
  45. y += (moveSpeed * delta);
  46. }
  47. }
  48.  
  49. @Override
  50. public void translateToPlayer() {
  51. glTranslatef((int) x, (int) y, 0);
  52. }
  53. }
  54.  
  55. //* NEXT CLASS *//
  56.  
  57. public class Block {
  58.  
  59. private BlockType type = BlockType.AIR;
  60. private float x, y;
  61. private Texture texture;
  62. Camera cam;
  63. Player player;
  64.  
  65. public Block(BlockType type, float x, float y) {
  66. this.type = type;
  67. this.x = x;
  68. this.y = y;
  69.  
  70. try {
  71. this.texture = TextureLoader.getTexture("PNG", new FileInputStream(new File(type.location)));
  72. } catch (FileNotFoundException e) {
  73. e.printStackTrace();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78.  
  79. public void render() {
  80. texture.bind();
  81.  
  82. glTranslatef(x, y, 0);
  83. glBegin(GL_QUADS);
  84. glTexCoord2f(0, 0);
  85. glVertex2f(0, 0);
  86. glTexCoord2f(1, 0);
  87. glVertex2f(BLOCK_SIZE, 0);
  88. glTexCoord2f(1, 1);
  89. glVertex2f(BLOCK_SIZE, BLOCK_SIZE);
  90. glTexCoord2f(0, 1);
  91. glVertex2f(0, BLOCK_SIZE);
  92. glEnd();
  93. }
  94. }
  95.  
  96. //* NEXT CLASS *//
  97.  
  98. public class BlockGrid {
  99.  
  100. public Block[][] block = new Block[worldW][worldH];
  101.  
  102. Player player;
  103.  
  104. public BlockGrid(File loadFile) {
  105.  
  106. }
  107.  
  108. public BlockGrid() {
  109. Random rand = new Random();
  110.  
  111. for (int x = 0; x < worldW; x++) {
  112. for (int y = 0; y < worldH; y++) {
  113. block[x][y] = new Block(BlockType.AIR, x * BLOCK_SIZE, y * BLOCK_SIZE);
  114. }
  115. }
  116.  
  117. for (int y = 0; y < worldW; y++) {
  118. for (int x = 0; x < worldH; x++) {
  119.  
  120. //Random dirt (world) generation//
  121. if (y > worldH / 6) {
  122. if (rand.nextInt(100) > 10) {
  123. try {
  124. if (block[x - 1][y - 1].getType() == BlockType.DIRT) {
  125. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  126. }
  127. } catch (Exception e) {
  128. }
  129. }
  130.  
  131. if (rand.nextInt(100) > 10) {
  132. try {
  133. if (block[x + 1][y - 1].getType() == BlockType.DIRT) {
  134. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  135. }
  136. } catch (Exception e) {
  137. }
  138. }
  139.  
  140. try {
  141. if (block[x][y - 1].getType() == BlockType.DIRT) {
  142. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  143. }
  144. } catch (Exception e) {
  145. }
  146. if (rand.nextInt(100) < 10) {
  147. block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
  148. }
  149. }
  150.  
  151. //Random stone generation//
  152. if (y > worldH / 4) {
  153. if (rand.nextInt(100) > 20) {
  154. try {
  155. if (block[x - 1][y - 1].getType() == BlockType.STONE) {
  156. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  157. }
  158. } catch (Exception e) {
  159. }
  160. }
  161.  
  162. if (rand.nextInt(100) > 20) {
  163. try {
  164. if (block[x + 1][y - 1].getType() == BlockType.STONE) {
  165. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  166. }
  167. } catch (Exception e) {
  168. }
  169. }
  170.  
  171. try {
  172. if (block[x][y - 1].getType() == BlockType.STONE) {
  173. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  174. }
  175. } catch (Exception e) {
  176. }
  177.  
  178. if (rand.nextInt(100) < 5) {
  179. block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
  180. }
  181. }
  182. }
  183. }
  184.  
  185. //Placing grass blocks//
  186. for (int y = 0; y < block.length; y++) {
  187. for (int x = 0; x < block[0].length; x++) {
  188. if (block[x][y].getType() == BlockType.DIRT && block[x][y - 1].getType() == BlockType.AIR) {
  189. block[x][y] = new Block(BlockType.GRASS, x * BLOCK_SIZE, y * BLOCK_SIZE);
  190. }
  191. }
  192. }
  193. }
  194.  
  195. public void generateWorld() {
  196. for (int x = 0; x < worldW - 1; x++) {
  197. for (int y = 0; y < worldH - 1; y++) {
  198. block[x][y].render();
  199. }
  200. }
  201. }
  202. }
  203.  
  204. //* NEXT CLASS *//
  205.  
  206. public class Component {
  207.  
  208. public static float width = 640, height = 480;
  209. private String title = "Side Scrolling Magyk!";
  210.  
  211. private BlockGrid grid;
  212. private BlockType selection = BlockType.STONE;
  213.  
  214. private Texture sky;
  215.  
  216. long lastFrame, lastFPS;
  217. int fps;
  218.  
  219. public Component() {
  220. try {
  221. Display.setDisplayMode(new DisplayMode((int) width, (int) height));
  222. Display.setTitle(title);
  223. Display.create();
  224. } catch (LWJGLException e) {
  225. e.printStackTrace();
  226. Display.destroy();
  227. System.exit(0);
  228. }
  229.  
  230. sky = loadTexture("sky");
  231.  
  232. Entity player = new Player((width / 2) - 16, (height / 2) - 32, 16, 32);
  233. grid = new BlockGrid();
  234.  
  235. initGL();
  236. getDelta();
  237. lastFPS = getTime();
  238.  
  239.  
  240. while (!Display.isCloseRequested()) {
  241. int delta = getDelta();
  242.  
  243. glClear(GL_COLOR_BUFFER_BIT);
  244.  
  245. input();
  246. player.update(delta);
  247.  
  248. glLoadIdentity();
  249.  
  250. Player.player = Player.loadTexture("player_outline");
  251. player.render();
  252.  
  253. player.translateToPlayer();
  254.  
  255. render();
  256. grid.generateWorld();
  257.  
  258. Display.update();
  259. Display.sync(60);
  260. }
  261. }
  262.  
  263. public void render() {
  264.  
  265. sky.bind();
  266. glBegin(GL_QUADS);
  267. glTexCoord2f(0, 0);
  268. glVertex2f(0, 0);
  269. glTexCoord2f(1, 0);
  270. glVertex2f(width + 400, 0);
  271. glTexCoord2f(1, 1);
  272. glVertex2f(width + 400, height + 400);
  273. glTexCoord2f(0, 1);
  274. glVertex2f(0, height + 400);
  275. glEnd();
  276. }
  277.  
  278. public void initGL() {
  279. glMatrixMode(GL_PROJECTION);
  280. glLoadIdentity();
  281. glOrtho(0, width, height, 0, 1, -1);
  282. glMatrixMode(GL_MODELVIEW);
  283. glEnable(GL_TEXTURE_2D);
  284.  
  285. glEnable(GL_BLEND);
  286. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement