Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. package triblade;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. import com.badlogic.gdx.ApplicationAdapter;
  8. import com.badlogic.gdx.Gdx;
  9. import com.badlogic.gdx.Input.Keys;
  10. import com.badlogic.gdx.audio.Sound;
  11. import com.badlogic.gdx.graphics.GL20;
  12. import com.badlogic.gdx.graphics.OrthographicCamera;
  13. import com.badlogic.gdx.graphics.Texture;
  14. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  15. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  16. import com.badlogic.gdx.math.Vector2;
  17.  
  18. public class TribladeGame extends ApplicationAdapter {
  19. SpriteBatch batch;
  20. Texture img;
  21.  
  22. char[][] grid;
  23.  
  24. int gridSize = 64;
  25.  
  26. int x;
  27. int y;
  28. int accel = 2;
  29. int decel = 1;
  30. int maxXSpeed = 12;
  31. int maxYSpeed = 20;
  32.  
  33. int playerWidth = (int)(gridSize * 0.8f);
  34. int playerHeight = (int)(gridSize * 0.8f);
  35.  
  36. int xSpeed;
  37. int ySpeed;
  38.  
  39. int jumpForce;
  40. int gravity;
  41.  
  42. int launchpadForce = 40;
  43. int boostpadForce = 24;
  44.  
  45. boolean onGround;
  46. boolean facingRight;
  47.  
  48. char blankChar = '.';
  49. char wallChar = 'x';
  50. char playerChar = 'p';
  51. char launchpadChar = 'l';
  52. char boostpadChar = 'b';
  53. char fakewallChar = 'f';
  54.  
  55. OrthographicCamera cam;
  56.  
  57. int windowWidth;
  58. int windowHeight;
  59. Vector2 windowPosition;
  60.  
  61. BitmapFont font;
  62.  
  63. Sound launchpadSFX;
  64. Sound boostpadSFX;
  65.  
  66. @Override
  67. public void create() {
  68. batch = new SpriteBatch();
  69. img = new Texture("badlogic.jpg");
  70.  
  71. launchpadSFX = Gdx.audio.newSound(Gdx.files.internal("launchpad2.wav"));
  72. boostpadSFX = Gdx.audio.newSound(Gdx.files.internal("launchpad.wav"));
  73.  
  74. int odd = Gdx.graphics.getWidth();
  75.  
  76. float h = Gdx.graphics.getHeight();
  77. float w = Gdx.graphics.getWidth();
  78.  
  79. cam = new OrthographicCamera(odd, odd * (h / w));
  80.  
  81. xSpeed = 0;
  82. ySpeed = 0;
  83. jumpForce = 20;
  84. gravity = 1;
  85.  
  86. onGround = false;
  87. facingRight = true;
  88.  
  89. grid = new char[][] { // y, x
  90. /**/{ 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' }, //
  91. { 'x', '.', '.', '.', '.', '.', '.', 'x' }, //
  92. { 'x', '.', '.', '.', '.', 'x', 'x', 'x' }, //
  93. { 'x', '.', '.', '.', '.', '.', '.', 'x' }, //
  94. { 'x', 'x', 'x', '.', '.', '.', '.', 'x' }, //
  95. { 'x', '.', 'p', '.', '.', 'x', '.', 'x' }, //
  96. { 'x', '.', '.', '.', '.', '.', '.', 'x' }, //
  97. { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' }, //
  98. };
  99.  
  100. loadLevel("testlevel.csv");
  101.  
  102. x = gridSize * 2;
  103. y = gridSize * 2;
  104.  
  105. OUTER: for(int i = 0; i < grid.length; ++i) {
  106. for(int j = 0; j < grid[0].length; ++j) {
  107. if(grid[i][j] == playerChar) {
  108. x = j * gridSize;
  109. y = (grid.length - 1 - i) * gridSize;
  110. break OUTER;
  111. }
  112. }
  113. }
  114.  
  115. windowWidth = Gdx.graphics.getWidth();
  116. windowHeight = Gdx.graphics.getHeight();
  117.  
  118. font = new BitmapFont();
  119. }
  120.  
  121. public void loadLevel(String filename) {
  122. BufferedReader br;
  123. String line = "";
  124. String separator = ",";
  125.  
  126. ArrayList<char[]> lines = new ArrayList<char[]>();
  127.  
  128. try {
  129. br = Gdx.files.internal(filename).reader(256);
  130. while((line = br.readLine()) != null) {
  131. String[] tempLine = line.split(separator);
  132. char[] tempCharArray = new char[tempLine.length];
  133. for(int i = 0; i < tempLine.length; ++i)
  134. tempCharArray[i] = tempLine[i].charAt(0);
  135. lines.add(tempCharArray);
  136. }
  137.  
  138. if(!lines.isEmpty()) grid = lines.toArray(new char[0][0]);
  139. }
  140. catch(IOException ioex) {
  141. // System.out.format("\"%1$s\" threw an error when reading!\n", filename);
  142. }
  143. }
  144.  
  145. public void update() {
  146. if(Gdx.input.isKeyPressed(Keys.ALT_LEFT) && Gdx.input.isKeyJustPressed(Keys.ENTER)) {
  147. if(Gdx.graphics.isFullscreen()) {
  148. Gdx.graphics.setDisplayMode(windowWidth, windowHeight, false);
  149. }
  150. else {
  151. windowWidth = Gdx.graphics.getWidth();
  152. windowHeight = Gdx.graphics.getHeight();
  153. Gdx.graphics.setDisplayMode(Gdx.graphics.getDesktopDisplayMode().width,
  154. Gdx.graphics.getDesktopDisplayMode().height, true);
  155. }
  156. }
  157.  
  158. // if(Gdx.input.isKeyPressed(Keys.UP)) y += speed;
  159. // if(Gdx.input.isKeyPressed(Keys.DOWN)) y -= speed;
  160. if(Gdx.input.isKeyPressed(Keys.LEFT) && Gdx.input.isKeyPressed(Keys.RIGHT)) {
  161. // do nothing
  162. }
  163. else if(Gdx.input.isKeyPressed(Keys.LEFT)) {
  164. facingRight = false;
  165. if(xSpeed > -maxXSpeed) xSpeed -= accel;
  166. else if(onGround) xSpeed -= Math.signum(xSpeed) * decel;
  167. // if(Math.abs(xSpeed) > maxXSpeed) xSpeed = (int)(maxXSpeed * Math.signum(xSpeed));
  168. }
  169. else if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
  170. facingRight = true;
  171. if(xSpeed < maxXSpeed) xSpeed += accel;
  172. else if(onGround) xSpeed -= Math.signum(xSpeed) * decel;
  173. // if(Math.abs(xSpeed) > maxXSpeed) xSpeed = (int)(maxXSpeed * Math.signum(xSpeed));
  174. }
  175. else xSpeed -= Math.signum(xSpeed) * decel;
  176.  
  177. if(Gdx.input.isKeyPressed(Keys.ESCAPE)) Gdx.app.exit();
  178.  
  179. if(Gdx.input.isKeyJustPressed(Keys.SPACE) && onGround) {
  180. ySpeed = jumpForce;
  181.  
  182. onGround = false;
  183. }
  184.  
  185. if(collides(x, y, launchpadChar)) {
  186. ySpeed = launchpadForce;
  187. launchpadSFX.play();
  188. onGround = false;
  189. }
  190.  
  191. if(collides(x, y, boostpadChar)) {
  192. xSpeed = boostpadForce * (facingRight ? 1 : -1);
  193. boostpadSFX.play();
  194. }
  195.  
  196. if(ySpeed < -maxYSpeed) ySpeed = -maxYSpeed;
  197.  
  198. ySpeed -= gravity;
  199.  
  200. for(int i = Math.abs(xSpeed); i != 0; i--) {
  201. int newX = (int)(x + Math.signum(xSpeed));
  202. if(collides(newX, y, wallChar)) {
  203. xSpeed = 0;
  204. break;
  205. }
  206. x += Math.signum(xSpeed);
  207. }
  208.  
  209. for(int i = Math.abs(ySpeed); i != 0; i--) {
  210. int newY = (int)(y + Math.signum(ySpeed));
  211. if(collides(x, newY, wallChar)) {
  212. ySpeed = 0;
  213. break;
  214. }
  215. y += Math.signum(ySpeed);
  216. }
  217.  
  218. onGround = collides(x, y - 1, wallChar);
  219. }
  220.  
  221. public boolean collides(int x, int y, char collider) {
  222. return grid[grid.length - 1 - y / gridSize][x / gridSize] == collider || //
  223. grid[grid.length - 1 - y / gridSize][(x + playerWidth) / gridSize] == collider || //
  224. grid[grid.length - 1 - (y + playerHeight) / gridSize][x / gridSize] == collider || //
  225. grid[grid.length - 1 - (y + playerHeight) / gridSize][(x + playerWidth) / gridSize] == collider;
  226. }
  227.  
  228. public void updateCamera() {
  229. // float h = Gdx.graphics.getHeight();
  230. // float w = Gdx.graphics.getWidth();
  231.  
  232. cam.position.set(x + playerWidth / 2, y + playerHeight / 2, 0);
  233.  
  234. cam.update();
  235. }
  236.  
  237. @Override
  238. public void render() {
  239. update();
  240. updateCamera();
  241.  
  242. Gdx.gl.glClearColor(0, 0.1f, 0.2f, 1);
  243. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  244. batch.begin();
  245. // batch.draw(img, 0, 0);
  246.  
  247. batch.setProjectionMatrix(cam.combined);
  248.  
  249. // batch.draw(img, 0, 0, 1024, 1024);
  250. batch.setColor(0, 0, 0, 1);
  251. batch.draw(img, 0, 0, grid[0].length * gridSize, grid.length * gridSize);
  252. batch.setColor(1, 1, 1, 1);
  253.  
  254. for(int i = 0; i < grid.length; ++i) {
  255. for(int j = 0; j < grid[0].length; ++j) {
  256. char currentChar = grid[grid.length - 1 - i][j];
  257. if(currentChar != blankChar) {
  258. if(currentChar == wallChar) batch.setColor(1, 1, 1, 1);
  259. else if(currentChar == fakewallChar) batch.setColor(0.7f, 0.7f, 0.7f, 1);
  260. else if(currentChar == launchpadChar) batch.setColor(1, 1, 0, 1);
  261. else if(currentChar == boostpadChar) batch.setColor(1, 0, 1, 1);
  262. else if(currentChar == playerChar) continue;
  263. batch.draw(img, j * gridSize, i * gridSize, gridSize, gridSize);
  264. }
  265. }
  266. }
  267.  
  268. batch.setColor(0, 1, 0, 1);
  269. batch.draw(img, x, y, playerWidth, playerHeight, 0, 0, img.getWidth(), img.getHeight(), !facingRight, false);
  270.  
  271. String message = "Speed: " + Math.abs(xSpeed);
  272.  
  273. font.draw(batch, message, cam.position.x - cam.viewportWidth / 2, cam.position.y - cam.viewportHeight / 2
  274. + font.getLineHeight());
  275.  
  276. batch.end();
  277. }
  278.  
  279. @Override
  280. public void resize(int width, int height) {
  281. cam.viewportWidth = width;
  282. cam.viewportHeight = height;
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement