Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. public class Game extends ApplicationAdapter implements InputProcessor {
  2. final int TILE_SIZE_IN_PX = 128;
  3. TiledMap tiledMap;
  4. OrthographicCamera camera;
  5. TiledMapRenderer tiledMapRenderer;
  6. SpriteBatch sb;
  7. Texture texture;
  8. Player player;
  9. Direction startDirection;
  10. public TileGrid grid;
  11.  
  12.  
  13. @Override
  14. public void create() {
  15. float w = Gdx.graphics.getWidth();
  16. float h = Gdx.graphics.getHeight();
  17. grid = new TileGrid((int)h, (int)w, TILE_SIZE_IN_PX);
  18.  
  19. camera = new OrthographicCamera();
  20. camera.setToOrtho(false, w, h);
  21. camera.update();
  22. tiledMap = new TmxMapLoader().load("RoboRally.tmx");
  23. tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
  24. Gdx.input.setInputProcessor(this);
  25.  
  26. tiledMap.getLayers().get("walls");
  27.  
  28. sb = new SpriteBatch();
  29. texture = new Texture(Gdx.files.internal("car.jpg"));
  30.  
  31. startDirection = Direction.West;
  32. player = new Player(texture, startDirection);
  33. player.setPosition(5,40);
  34. grid.getTile(0,0).addSprite(player);
  35.  
  36. }
  37.  
  38. @Override
  39. public void render() {
  40. Gdx.gl.glClearColor(1,0,0,1);
  41. Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  42. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  43. camera.update();
  44. tiledMapRenderer.setView(camera);
  45. tiledMapRenderer.render();
  46. sb.setProjectionMatrix(camera.combined);
  47. sb.begin();
  48. drawSprites();
  49. sb.end();
  50. }
  51.  
  52. public void drawSprites() {
  53. for (Sprite sprite : grid.getAllSpritesOnMap()) {
  54. sprite.draw(sb);
  55. }
  56. }
  57.  
  58.  
  59. @Override
  60. public boolean keyDown(int keycode) {
  61. float x = player.getX();
  62. float y = player.getY();
  63.  
  64. //Viktig å fjerne spilleren fra sin nåværende tile
  65. Tile currentTile = grid.getTileFromCoordinates(y, x);
  66.  
  67. int moveDistance = TILE_SIZE_IN_PX;
  68.  
  69. if (keycode == Input.Keys.RIGHT) {
  70. player.turnRight();
  71. }
  72.  
  73. if (keycode == Input.Keys.LEFT) {
  74. player.turnLeft();
  75. }
  76.  
  77. if (keycode == Input.Keys.valueOf("1")) {
  78. player.moveForward(1, moveDistance, this, currentTile);
  79. }
  80.  
  81. if (keycode == Input.Keys.valueOf("2")) {
  82. player.moveForward(2, moveDistance, this, currentTile);
  83. }
  84.  
  85. if (keycode == Input.Keys.valueOf("3")) {
  86. player.moveForward(3, moveDistance, this, currentTile);
  87. }
  88.  
  89. if (keycode == Input.Keys.U) {
  90. player.uTurn();
  91. }
  92.  
  93.  
  94. return false;
  95. }
  96.  
  97. public void updatePlayerPositionInGrid(Tile currentTile) {
  98. float x = player.getX();
  99. float y = player.getY();
  100.  
  101. currentTile.getSprites().remove(player);
  102. grid.getTileFromCoordinates(y, x).addSprite(player);
  103.  
  104. System.out.println(grid.getTileFromCoordinates(y,x));
  105.  
  106. }
  107.  
  108. @Override
  109. public boolean keyUp(int i) {
  110. return false;
  111. }
  112.  
  113. @Override
  114. public boolean keyTyped(char c) {
  115. return false;
  116. }
  117.  
  118. @Override
  119. public boolean touchDown(int i, int i1, int i2, int i3) {
  120. return false;
  121. }
  122.  
  123. @Override
  124. public boolean touchUp(int i, int i1, int i2, int i3) {
  125. return false;
  126. }
  127.  
  128. @Override
  129. public boolean touchDragged(int i, int i1, int i2) {
  130. return false;
  131. }
  132.  
  133. @Override
  134. public boolean mouseMoved(int i, int i1) {
  135. return false;
  136. }
  137.  
  138. @Override
  139. public boolean scrolled(int i) {
  140. return false;
  141. }
  142.  
  143. public enum Direction{
  144. North, East, South, West
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement