Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Component {
- public static int width = 640, height = 480;
- private String title = "Side Scrolling Magyk!";
- private BlockGrid grid;
- private BlockType selection = BlockType.STONE;
- private Texture sky;
- private Player player;
- long lastFrame, lastFPS;
- int fps;
- public Component() {
- try {
- Display.setDisplayMode(new DisplayMode(width, height));
- Display.setTitle(title);
- Display.create();
- } catch(LWJGLException e) {
- e.printStackTrace();
- Display.destroy();
- System.exit(0);
- }
- sky = loadTexture("sky");
- Entity player = new Player((width / 2) - 16, (height / 2) - 32, 16, 32);
- grid = new BlockGrid();
- new Camera();
- initGL();
- getDelta();
- lastFPS = getTime();
- while(!Display.isCloseRequested()) {
- int delta = getDelta();
- glClear(GL_COLOR_BUFFER_BIT);
- input();
- render();
- Player.player = Player.loadTexture("player_outline");
- player.render();
- player.update(delta);
- grid.generateWorld();
- Display.update();
- Display.sync(60);
- }
- }
- private void input() {
- int mousex = Mouse.getX();
- int mousey = height - Mouse.getY() - 1;
- int grid_x = Math.round(mousex / World.BLOCK_SIZE);
- int grid_y = Math.round(mousey / World.BLOCK_SIZE);
- boolean mouseClicked = Mouse.isButtonDown(0);
- //Input
- if(mouseClicked) {
- grid.setAt(grid_x, grid_y, BlockType.AIR);
- }
- if(Mouse.isButtonDown(1)) {
- grid.setAt(grid_x, grid_y, selection);
- }
- while(Keyboard.next()) {
- //Saving and Loading//
- if(Keyboard.getEventKey() == Keyboard.KEY_S) {
- grid.save(new File("save.xml"));
- }
- if(Keyboard.getEventKey() == Keyboard.KEY_L) {
- grid.load(new File("save.xml"));
- }
- //Selection//
- if(Keyboard.getEventKey() == Keyboard.KEY_1) {
- selection = BlockType.STONE;
- }
- if(Keyboard.getEventKey() == Keyboard.KEY_2) {
- selection = BlockType.DIRT;
- }
- if(Keyboard.getEventKey() == Keyboard.KEY_3) {
- selection = BlockType.GRASS;
- }
- //Closing//
- if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
- Display.destroy();
- System.exit(0);
- }
- }
- updateFPS();
- }
- public void render() {
- sky.bind();
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);
- glVertex2f(0, 0);
- glTexCoord2f(1, 0);
- glVertex2f(width + 400, 0);
- glTexCoord2f(1, 1);
- glVertex2f(width + 400, height + 400);
- glTexCoord2f(0, 1);
- glVertex2f(0, height + 400);
- glEnd();
- }
- public void initGL() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, width, height, 0, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
- public long getTime() {
- return (Sys.getTime() * 1000) / Sys.getTimerResolution();
- }
- public int getDelta() {
- long time = getTime();
- int delta = (int) (time - lastFrame);
- lastFrame = time;
- return delta;
- }
- public void updateFPS() {
- if(getTime() - lastFPS > 1000) {
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- }
- private Texture loadTexture(String key) {
- try {
- return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/images/" + key + ".png")));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static int getWidth() {
- return width;
- }
- public static int getHeight() {
- return height;
- }
- public static void main(String[] args) {
- new Component();
- }
- }
- // NEXT CLASS //
- public class Player extends AbstractMoveableEntity {
- public float vertSpeed = 0;
- public float moveSpeed = 0.2f,
- translateX = 0;
- public static Texture player;
- private boolean canMove = true;
- BlockGrid grid;
- Block block;
- Camera cam;
- public Player(double x, double y, int width, int height) {
- super(x, y, width, height);
- }
- @Override
- public void render() {
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);
- glVertex2f((float) x, (float) y);
- glTexCoord2f(1, 0);
- glVertex2f((float) (x + width), (float) y);
- glTexCoord2f(1, 1);
- glVertex2f((float) (x + width), (float) (y + height));
- glTexCoord2f(0, 1);
- glVertex2f((float) x, (float) (y + height));
- glEnd();
- }
- @Override
- public void update(int delta) {
- if(Keyboard.isKeyDown(Keyboard.KEY_LEFT) && canMove) {
- x -= moveSpeed * delta;
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && canMove) {
- x += moveSpeed * delta;
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_UP) && canMove) {
- y -= moveSpeed * delta;
- }
- if(Keyboard.isKeyDown(Keyboard.KEY_DOWN) && canMove) {
- y += moveSpeed * delta;
- }
- glTranslatef((int) x, (int) y, 0);
- }
- public static Texture loadTexture(String path) {
- try {
- return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/images/" + path + ".png")));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public Rectangle getBounds() {
- return new Rectangle((int) x, (int) y, (int) width, (int) height);
- }
- }
- // NEXT CLASS //
- public class BlockGrid {
- public Block[][] block = new Block[worldW][worldH];
- public BlockGrid(File loadFile) {
- }
- public BlockGrid() {
- Random rand = new Random();
- for(int x = 0; x < worldW; x++) {
- for(int y = 0; y < worldH; y++) {
- block[x][y] = new Block(BlockType.AIR, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- }
- for(int y = 0; y < worldW; y++) {
- for(int x = 0; x < worldH; x++) {
- //Random dirt (world) generation//
- if(y > worldH / 6) {
- if(rand.nextInt(100) > 10) {
- try {
- if(block[x - 1][y - 1].getType() == BlockType.DIRT) {
- block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch(Exception e) { }
- }
- if(rand.nextInt(100) > 10) {
- try {
- if(block[x + 1][y - 1].getType() == BlockType.DIRT) {
- block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch(Exception e) { }
- }
- try {
- if(block[x][y - 1].getType() == BlockType.DIRT) {
- block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch(Exception e) { }
- if(rand.nextInt(100) < 10) {
- block[x][y] = new Block(BlockType.DIRT, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- }
- //Random stone generation//
- if(y > worldH / 4) {
- if(rand.nextInt(100) > 20) {
- try {
- if(block[x - 1][y - 1].getType() == BlockType.STONE) {
- block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch(Exception e) { }
- }
- if(rand.nextInt(100) > 20) {
- try {
- if(block[x + 1][y - 1].getType() == BlockType.STONE) {
- block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch(Exception e) { }
- }
- try {
- if(block[x][y - 1].getType() == BlockType.STONE) {
- block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch(Exception e) { }
- if(rand.nextInt(100) < 5) {
- block[x][y] = new Block(BlockType.STONE, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- }
- }
- }
- //Placing grass blocks//
- for(int y = 0; y < block.length; y++) {
- for(int x = 0; x < block[0].length; x++) {
- if(block[x][y].getType() == BlockType.DIRT && block[x][y - 1].getType() == BlockType.AIR) {
- block[x][y] = new Block(BlockType.GRASS, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- }
- }
- }
- public void generateWorld() {
- for(int x = 0; x < worldW - 1; x++) {
- for(int y = 0; y < worldH - 1; y++) {
- block[x][y].render();
- }
- }
- }
- public void save(File saveFile) {
- Document document = new Document();
- Element root = new Element("blocks");
- document.setRootElement(root);
- for(int x = 0; x < worldW - 1; x++) {
- for(int y = 0; y < worldH - 1; y++) {
- Element blocks = new Element("blocks");
- blocks.setAttribute("x", String.valueOf((int) (block[x][y].getX() / BLOCK_SIZE)));
- blocks.setAttribute("y", String.valueOf((int) (block[x][y].getY() / BLOCK_SIZE)));
- blocks.setAttribute("type", String.valueOf(block[x][y].getType()));
- root.addContent(blocks);
- }
- XMLOutputter output = new XMLOutputter();
- try {
- output.output(document, new FileOutputStream(saveFile));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void load(File loadFile) {
- try {
- SAXBuilder builder = new SAXBuilder();
- Document document = builder.build(loadFile);
- Element root = document.getRootElement();
- for(Object blocks : root.getChildren()) {
- Element e = (Element) blocks;
- int x = Integer.parseInt(e.getAttributeValue("x"));
- int y = Integer.parseInt(e.getAttributeValue("y"));
- block[x][y] = new Block(BlockType.valueOf(e.getAttributeValue("type")), x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void setAt(int x, int y, BlockType b) {
- block[x][y] = new Block(b, x * BLOCK_SIZE, y * BLOCK_SIZE);
- }
- public Block getAt(int x, int y) {
- return block[x][y];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement