Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package triblade;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Vector2;
- public class TribladeGame extends ApplicationAdapter {
- SpriteBatch batch;
- Texture img;
- char[][] grid;
- int gridSize = 64;
- int x;
- int y;
- int accel = 2;
- int decel = 1;
- int maxXSpeed = 12;
- int maxYSpeed = 20;
- int playerWidth = (int)(gridSize * 0.8f);
- int playerHeight = (int)(gridSize * 0.8f);
- int xSpeed;
- int ySpeed;
- int jumpForce;
- int gravity;
- int launchpadForce = 40;
- int boostpadForce = 24;
- boolean onGround;
- boolean facingRight;
- char blankChar = '.';
- char wallChar = 'x';
- char playerChar = 'p';
- char launchpadChar = 'l';
- char boostpadChar = 'b';
- char fakewallChar = 'f';
- OrthographicCamera cam;
- int windowWidth;
- int windowHeight;
- Vector2 windowPosition;
- BitmapFont font;
- Sound launchpadSFX;
- Sound boostpadSFX;
- @Override
- public void create() {
- batch = new SpriteBatch();
- img = new Texture("badlogic.jpg");
- launchpadSFX = Gdx.audio.newSound(Gdx.files.internal("launchpad2.wav"));
- boostpadSFX = Gdx.audio.newSound(Gdx.files.internal("launchpad.wav"));
- int odd = Gdx.graphics.getWidth();
- float h = Gdx.graphics.getHeight();
- float w = Gdx.graphics.getWidth();
- cam = new OrthographicCamera(odd, odd * (h / w));
- xSpeed = 0;
- ySpeed = 0;
- jumpForce = 20;
- gravity = 1;
- onGround = false;
- facingRight = true;
- grid = new char[][] { // y, x
- /**/{ 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' }, //
- { 'x', '.', '.', '.', '.', '.', '.', 'x' }, //
- { 'x', '.', '.', '.', '.', 'x', 'x', 'x' }, //
- { 'x', '.', '.', '.', '.', '.', '.', 'x' }, //
- { 'x', 'x', 'x', '.', '.', '.', '.', 'x' }, //
- { 'x', '.', 'p', '.', '.', 'x', '.', 'x' }, //
- { 'x', '.', '.', '.', '.', '.', '.', 'x' }, //
- { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' }, //
- };
- loadLevel("testlevel.csv");
- x = gridSize * 2;
- y = gridSize * 2;
- OUTER: for(int i = 0; i < grid.length; ++i) {
- for(int j = 0; j < grid[0].length; ++j) {
- if(grid[i][j] == playerChar) {
- x = j * gridSize;
- y = (grid.length - 1 - i) * gridSize;
- break OUTER;
- }
- }
- }
- windowWidth = Gdx.graphics.getWidth();
- windowHeight = Gdx.graphics.getHeight();
- font = new BitmapFont();
- }
- public void loadLevel(String filename) {
- BufferedReader br;
- String line = "";
- String separator = ",";
- ArrayList<char[]> lines = new ArrayList<char[]>();
- try {
- br = Gdx.files.internal(filename).reader(256);
- while((line = br.readLine()) != null) {
- String[] tempLine = line.split(separator);
- char[] tempCharArray = new char[tempLine.length];
- for(int i = 0; i < tempLine.length; ++i)
- tempCharArray[i] = tempLine[i].charAt(0);
- lines.add(tempCharArray);
- }
- if(!lines.isEmpty()) grid = lines.toArray(new char[0][0]);
- }
- catch(IOException ioex) {
- // System.out.format("\"%1$s\" threw an error when reading!\n", filename);
- }
- }
- public void update() {
- if(Gdx.input.isKeyPressed(Keys.ALT_LEFT) && Gdx.input.isKeyJustPressed(Keys.ENTER)) {
- if(Gdx.graphics.isFullscreen()) {
- Gdx.graphics.setDisplayMode(windowWidth, windowHeight, false);
- }
- else {
- windowWidth = Gdx.graphics.getWidth();
- windowHeight = Gdx.graphics.getHeight();
- Gdx.graphics.setDisplayMode(Gdx.graphics.getDesktopDisplayMode().width,
- Gdx.graphics.getDesktopDisplayMode().height, true);
- }
- }
- // if(Gdx.input.isKeyPressed(Keys.UP)) y += speed;
- // if(Gdx.input.isKeyPressed(Keys.DOWN)) y -= speed;
- if(Gdx.input.isKeyPressed(Keys.LEFT) && Gdx.input.isKeyPressed(Keys.RIGHT)) {
- // do nothing
- }
- else if(Gdx.input.isKeyPressed(Keys.LEFT)) {
- facingRight = false;
- if(xSpeed > -maxXSpeed) xSpeed -= accel;
- else if(onGround) xSpeed -= Math.signum(xSpeed) * decel;
- // if(Math.abs(xSpeed) > maxXSpeed) xSpeed = (int)(maxXSpeed * Math.signum(xSpeed));
- }
- else if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
- facingRight = true;
- if(xSpeed < maxXSpeed) xSpeed += accel;
- else if(onGround) xSpeed -= Math.signum(xSpeed) * decel;
- // if(Math.abs(xSpeed) > maxXSpeed) xSpeed = (int)(maxXSpeed * Math.signum(xSpeed));
- }
- else xSpeed -= Math.signum(xSpeed) * decel;
- if(Gdx.input.isKeyPressed(Keys.ESCAPE)) Gdx.app.exit();
- if(Gdx.input.isKeyJustPressed(Keys.SPACE) && onGround) {
- ySpeed = jumpForce;
- onGround = false;
- }
- if(collides(x, y, launchpadChar)) {
- ySpeed = launchpadForce;
- launchpadSFX.play();
- onGround = false;
- }
- if(collides(x, y, boostpadChar)) {
- xSpeed = boostpadForce * (facingRight ? 1 : -1);
- boostpadSFX.play();
- }
- if(ySpeed < -maxYSpeed) ySpeed = -maxYSpeed;
- ySpeed -= gravity;
- for(int i = Math.abs(xSpeed); i != 0; i--) {
- int newX = (int)(x + Math.signum(xSpeed));
- if(collides(newX, y, wallChar)) {
- xSpeed = 0;
- break;
- }
- x += Math.signum(xSpeed);
- }
- for(int i = Math.abs(ySpeed); i != 0; i--) {
- int newY = (int)(y + Math.signum(ySpeed));
- if(collides(x, newY, wallChar)) {
- ySpeed = 0;
- break;
- }
- y += Math.signum(ySpeed);
- }
- onGround = collides(x, y - 1, wallChar);
- }
- public boolean collides(int x, int y, char collider) {
- return grid[grid.length - 1 - y / gridSize][x / gridSize] == collider || //
- grid[grid.length - 1 - y / gridSize][(x + playerWidth) / gridSize] == collider || //
- grid[grid.length - 1 - (y + playerHeight) / gridSize][x / gridSize] == collider || //
- grid[grid.length - 1 - (y + playerHeight) / gridSize][(x + playerWidth) / gridSize] == collider;
- }
- public void updateCamera() {
- // float h = Gdx.graphics.getHeight();
- // float w = Gdx.graphics.getWidth();
- cam.position.set(x + playerWidth / 2, y + playerHeight / 2, 0);
- cam.update();
- }
- @Override
- public void render() {
- update();
- updateCamera();
- Gdx.gl.glClearColor(0, 0.1f, 0.2f, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- // batch.draw(img, 0, 0);
- batch.setProjectionMatrix(cam.combined);
- // batch.draw(img, 0, 0, 1024, 1024);
- batch.setColor(0, 0, 0, 1);
- batch.draw(img, 0, 0, grid[0].length * gridSize, grid.length * gridSize);
- batch.setColor(1, 1, 1, 1);
- for(int i = 0; i < grid.length; ++i) {
- for(int j = 0; j < grid[0].length; ++j) {
- char currentChar = grid[grid.length - 1 - i][j];
- if(currentChar != blankChar) {
- if(currentChar == wallChar) batch.setColor(1, 1, 1, 1);
- else if(currentChar == fakewallChar) batch.setColor(0.7f, 0.7f, 0.7f, 1);
- else if(currentChar == launchpadChar) batch.setColor(1, 1, 0, 1);
- else if(currentChar == boostpadChar) batch.setColor(1, 0, 1, 1);
- else if(currentChar == playerChar) continue;
- batch.draw(img, j * gridSize, i * gridSize, gridSize, gridSize);
- }
- }
- }
- batch.setColor(0, 1, 0, 1);
- batch.draw(img, x, y, playerWidth, playerHeight, 0, 0, img.getWidth(), img.getHeight(), !facingRight, false);
- String message = "Speed: " + Math.abs(xSpeed);
- font.draw(batch, message, cam.position.x - cam.viewportWidth / 2, cam.position.y - cam.viewportHeight / 2
- + font.getLineHeight());
- batch.end();
- }
- @Override
- public void resize(int width, int height) {
- cam.viewportWidth = width;
- cam.viewportHeight = height;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement