Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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(float x, float y, int width, int height) {
- super(x, y, width, height);
- }
- @Override
- public void render() {
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);
- glVertex2f((float) ((Component.width / 2) - 16), (float) ((Component.height / 2) - 32));
- glTexCoord2f(1, 0);
- glVertex2f((float) ((Component.width / 2) - 16 + width), (float) ((Component.height / 2) - 32));
- glTexCoord2f(1, 1);
- glVertex2f((float) ((Component.width / 2) - 16 + width), (float) ((Component.height / 2) - 32 + height));
- glTexCoord2f(0, 1);
- glVertex2f((float) ((Component.width / 2) - 16), (float) ((Component.height / 2) - 32 + 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);
- }
- }
- @Override
- public void translateToPlayer() {
- glTranslatef((int) x, (int) y, 0);
- }
- }
- //* NEXT CLASS *//
- public class Block {
- private BlockType type = BlockType.AIR;
- private float x, y;
- private Texture texture;
- Camera cam;
- Player player;
- public Block(BlockType type, float x, float y) {
- this.type = type;
- this.x = x;
- this.y = y;
- try {
- this.texture = TextureLoader.getTexture("PNG", new FileInputStream(new File(type.location)));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void render() {
- texture.bind();
- glTranslatef(x, y, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);
- glVertex2f(0, 0);
- glTexCoord2f(1, 0);
- glVertex2f(BLOCK_SIZE, 0);
- glTexCoord2f(1, 1);
- glVertex2f(BLOCK_SIZE, BLOCK_SIZE);
- glTexCoord2f(0, 1);
- glVertex2f(0, BLOCK_SIZE);
- glEnd();
- }
- }
- //* NEXT CLASS *//
- public class BlockGrid {
- public Block[][] block = new Block[worldW][worldH];
- Player player;
- 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();
- }
- }
- }
- }
- //* NEXT CLASS *//
- public class Component {
- public static float width = 640, height = 480;
- private String title = "Side Scrolling Magyk!";
- private BlockGrid grid;
- private BlockType selection = BlockType.STONE;
- private Texture sky;
- long lastFrame, lastFPS;
- int fps;
- public Component() {
- try {
- Display.setDisplayMode(new DisplayMode((int) width, (int) 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();
- initGL();
- getDelta();
- lastFPS = getTime();
- while (!Display.isCloseRequested()) {
- int delta = getDelta();
- glClear(GL_COLOR_BUFFER_BIT);
- input();
- player.update(delta);
- glLoadIdentity();
- Player.player = Player.loadTexture("player_outline");
- player.render();
- player.translateToPlayer();
- render();
- grid.generateWorld();
- Display.update();
- Display.sync(60);
- }
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement