Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.17 KB | None | 0 0
  1. package com.finn.pokemon.screen;
  2.  
  3. import com.badlogic.gdx.Application;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.Screen;
  7. import com.badlogic.gdx.graphics.GL20;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.Animation;
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  12. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  13. import com.badlogic.gdx.maps.tiled.TiledMap;
  14. import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
  15. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  16. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  17. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  18. import com.badlogic.gdx.math.MathUtils;
  19. import com.badlogic.gdx.math.Rectangle;
  20. import com.badlogic.gdx.math.Vector2;
  21. import com.badlogic.gdx.math.Vector3;
  22. import com.badlogic.gdx.physics.box2d.*;
  23. import com.badlogic.gdx.utils.Array;
  24. import com.finn.pokemon.Pokemon;
  25. import com.finn.pokemon.util.TiledObjectUtil;
  26.  
  27. import java.util.HashMap;
  28.  
  29. public class GameScreen implements Screen {
  30. final Pokemon game;
  31. OrthographicCamera camera;
  32. OrthographicCamera guiCamera;
  33. HashMap<String, TiledMapRenderer> tiledRenderingHashMap;
  34. TiledMapRenderer tiledMapRenderer;
  35. String currentMap;
  36. World world;
  37. Body player;
  38. Box2DDebugRenderer b2dr;
  39. Texture playerSheet;
  40. TextureRegion[] runningLeft;
  41. TextureRegion[] runningRight;
  42. TextureRegion[] runningUp;
  43. TextureRegion[] runningDown;
  44. Animation leftAnimation;
  45. Animation rightAnimation;
  46. Animation upAnimation;
  47. Animation downAnimation;
  48. SpriteBatch batch;
  49. SpriteBatch hudBatch;
  50. String animationDirection;
  51. String lastAnimationDirection;
  52. float stateTime;
  53. int xSpeed;
  54. int ySpeed;
  55. int mapWidth;
  56. int mapHeight;
  57. int Scale;
  58. int[] backgroundLayers = { 0, 1,};
  59. int[] foregroundLayers = { 2 };
  60. Rectangle wleftBounds;
  61. Rectangle wrightBounds;
  62. Rectangle wupBounds;
  63. Rectangle wdownBounds;
  64. Vector3 touchPoint;
  65. Texture leftRegion;
  66. Texture rightRegion;
  67. Texture upRegion;
  68. Texture downRegion;
  69. boolean DEBUG = false;
  70.  
  71. public GameScreen(final Pokemon game) {
  72. this.game = game;
  73.  
  74. float w = Gdx.graphics.getWidth();
  75. float h = Gdx.graphics.getHeight();
  76.  
  77. camera = new OrthographicCamera();
  78. camera.setToOrtho(false,w,h);
  79. camera.update();
  80.  
  81. guiCamera = new OrthographicCamera();
  82. guiCamera.setToOrtho(false,w,h);
  83. guiCamera.update();
  84.  
  85. TiledMap outsideMap = new TmxMapLoader().load("Tilemaps/outside.tmx");
  86. TiledMap insideMap = new TmxMapLoader().load("Tilemaps/inside.tmx");
  87. TiledMapRenderer rendererA = new OrthogonalTiledMapRenderer(outsideMap);
  88. TiledMapRenderer rendererB = new OrthogonalTiledMapRenderer(insideMap);
  89.  
  90. tiledRenderingHashMap = new HashMap();
  91. tiledRenderingHashMap.put("outside_map", rendererA);
  92. tiledRenderingHashMap.put("room_map", rendererB);
  93. currentMap = "outside_map";
  94.  
  95. mapWidth = (outsideMap.getProperties().get("width",Integer.class) * outsideMap.getProperties().get("tilewidth",Integer.class));
  96. mapHeight = (outsideMap.getProperties().get("height",Integer.class) * outsideMap.getProperties().get("tileheight",Integer.class));
  97.  
  98. batch = new SpriteBatch();
  99. hudBatch = new SpriteBatch();
  100.  
  101. Scale = 1;
  102. if(Gdx.app.getType() == Application.ApplicationType.Android) {
  103. Scale = 4;
  104. camera.zoom = 1 / Scale;
  105. wleftBounds = new Rectangle(0, 0, 120, 120);
  106. wrightBounds = new Rectangle(240, 0, 120, 120);
  107. wdownBounds = new Rectangle(120, 0, 120, 120);
  108. wupBounds = new Rectangle(120, 120, 120, 120);
  109. // STOP DEFINING THE TYPE OF OBJECT THINGS ARE, IT ALREADY KNOWS AND SO ASSUMES YOU ARE DEFINING ANOTHER!
  110. touchPoint = new Vector3();
  111. leftRegion = new Texture(Gdx.files.internal("Images/joystick_left2.png"));
  112. rightRegion = new Texture(Gdx.files.internal("Images/joystick_right2.png"));
  113. downRegion = new Texture(Gdx.files.internal("Images/joystick_down2.png"));
  114. upRegion = new Texture(Gdx.files.internal("Images/joystick_up2.png"));
  115. }
  116.  
  117. playerSheet = new Texture(Gdx.files.internal("Images/playerNew32x64.png"));
  118.  
  119. runningDown = new TextureRegion[3];
  120. runningUp = new TextureRegion[3];
  121. runningRight = new TextureRegion[3];
  122. runningLeft = new TextureRegion[3];
  123.  
  124.  
  125. for(int i=0;i<3;i++) {
  126. runningDown[i] = new TextureRegion(playerSheet, 32 * i, 0, 32, 64);
  127. runningLeft[i] = new TextureRegion(playerSheet, 32 * i, 64, 32, 64);
  128. runningRight[i] = new TextureRegion(playerSheet, 32 * i, 128, 32, 64);
  129. runningUp[i] = new TextureRegion(playerSheet, 32 * i, 192, 32, 64);
  130. }
  131.  
  132. downAnimation = new Animation<TextureRegion>(0.1f, runningDown);
  133. leftAnimation = new Animation<TextureRegion>(0.1f, runningLeft);
  134. rightAnimation = new Animation<TextureRegion>(0.1f, runningRight);
  135. upAnimation = new Animation<TextureRegion>(0.1f, runningUp);
  136.  
  137. stateTime = 0f;
  138. world = new World(new Vector2(0,0), false);
  139. b2dr = new Box2DDebugRenderer();
  140.  
  141. //player = createBox(32,32,32,32,false);
  142. player = createCircle(32,32,16,false);
  143. // for(int i = 0;i<20;i++) {
  144. // createCircle(64 + (i * 32),64 + (i * 16),12,false).applyLinearImpulse(0,-500,1,1,false);
  145. //
  146. // }
  147. TiledObjectUtil.parseTiledObjectLater(world,outsideMap.getLayers().get("collision-layer").getObjects());
  148. }
  149.  
  150. @Override
  151. public void render(float delta) {
  152. world.step(1 / 60f,6,2);
  153. processInput();
  154. updateCamera();
  155. draw();
  156. }
  157.  
  158. public void processInput() {
  159. if((Math.round(player.getPosition().x + 16) % 32) == 0 && (Math.round(player.getPosition().y + 16) % 32) == 0) {
  160. xSpeed = ySpeed = 0;
  161. animationDirection = "null";
  162. //player.setTransform((float) Math.floor(player.getPosition().x),(float) Math.floor(player.getPosition().y),0);
  163. }
  164. if((Math.round(player.getPosition().x + 16) == 128 && (Math.round(player.getPosition().y + 16) == 160)) && currentMap == "outside_map") {
  165. changeMap("room_map","Tilemaps/inside.tmx");
  166. } else if((Math.round(player.getPosition().x + 16) == 224 && (Math.round(player.getPosition().y + 16) == 32)) && currentMap == "room_map") {
  167. changeMap("outside_map","Tilemaps/outside.tmx");
  168. }
  169. int tileX = ((Math.round(player.getPosition().x + 16) / 32));
  170. int tileY = ((Math.round(player.getPosition().y + 16) / 32));
  171.  
  172. TiledMap insideMap = new TmxMapLoader().load("Tilemaps/inside.tmx");
  173.  
  174. TiledMapTileLayer layer = (TiledMapTileLayer) insideMap.getLayers().get("background");
  175.  
  176. //System.out.println(layer.getCell(tileX,tileY).getTile().getProperties().get("speed"));
  177.  
  178. //System.out.println(tileX + " | " + tileY);
  179. updateInput();
  180. //player.setTransform((float) Math.round(player.getPosition().x),(float) Math.round(player.getPosition().y),0);
  181. player.setLinearVelocity(xSpeed * 128, ySpeed * 128);
  182. //System.out.println("Player x " + (Math.round(player.getPosition().x + 16)) + " | Player y " + (Math.round(player.getPosition().y + 16)));
  183. //System.out.println("Player y " + (Math.floor(player.getPosition().y) % 32));
  184. }
  185.  
  186. public void updateCamera() {
  187. camera.position.set(player.getPosition().x + 16,player.getPosition().y + 16,0);
  188. if(currentMap == "outside_map") {
  189. camera.position.x = MathUtils.clamp(camera.position.x, camera.viewportWidth / Scale * 0.5f, mapWidth - camera.viewportWidth / Scale * 0.5f);
  190. camera.position.y = MathUtils.clamp(camera.position.y, camera.viewportHeight / Scale * 0.5f, mapHeight - camera.viewportHeight / Scale * 0.5f);
  191. }
  192. camera.update();
  193. }
  194.  
  195. public void changeMap(String mapName,String directory) {
  196. currentMap = mapName;
  197. Array<Body> bodies = new Array<Body>();
  198. world.getBodies(bodies);
  199. for(int i = 0; i < bodies.size; i++) {
  200. world.destroyBody(bodies.get(i));
  201. }
  202. TiledMap Map = new TmxMapLoader().load(directory);
  203. System.out.println(mapWidth);
  204. TiledObjectUtil.parseTiledObjectLater(world,Map.getLayers().get("collision-layer").getObjects());
  205. player = createCircle(32,32,16,false);
  206. player.setTransform(208,48,0);
  207. }
  208.  
  209. public void draw() {
  210. Gdx.gl.glClearColor(0, 0, 0, 1);
  211. Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  212. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  213.  
  214. //tiledMapRenderer.setView(camera);
  215. tiledRenderingHashMap.get(currentMap).setView(camera);
  216.  
  217. stateTime += Gdx.graphics.getDeltaTime();
  218.  
  219. //tiledMapRenderer.render(backgroundLayers);
  220. tiledRenderingHashMap.get(currentMap).render(backgroundLayers);
  221.  
  222. boolean isMovementKeyDown = (Gdx.input.isKeyDown(Keys.A) || Gdx.input.isKeyDown(Keys.S) || Gdx.input.isKeyDown(Keys.D) || Gdx.input.isKeyDown(Keys.W));
  223.  
  224. batch.setProjectionMatrix(camera.combined);
  225. batch.begin();
  226. if(animationDirection == "up") {
  227. batch.draw((TextureRegion) upAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true),player.getPosition().x - 16,player.getPosition().y - 16);
  228. } else if(animationDirection == "down") {
  229. batch.draw((TextureRegion) downAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true),player.getPosition().x - 16,player.getPosition().y - 16);
  230. } else if(animationDirection == "left") {
  231. batch.draw((TextureRegion) leftAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true),player.getPosition().x - 16,player.getPosition().y - 16);
  232. } else if(animationDirection == "right") {
  233. batch.draw((TextureRegion) rightAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true)),player.getPosition().x - 16,player.getPosition().y - 16);
  234. }
  235. batch.end();
  236.  
  237. tiledRenderingHashMap.get(currentMap).render(foregroundLayers);
  238. if(DEBUG) {
  239. b2dr.render(world,camera.combined);
  240. //System.out.println(player.getPosition().x + " | " + player.getPosition().y);
  241. }
  242.  
  243. hudBatch.begin();
  244. if(Gdx.app.getType() == Application.ApplicationType.Android) {
  245. hudBatch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
  246. hudBatch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);
  247. hudBatch.draw(downRegion, wdownBounds.x, wdownBounds.y, wdownBounds.width, wdownBounds.height);
  248. hudBatch.draw(upRegion, wupBounds.x, wupBounds.y, wupBounds.width, wupBounds.height);
  249. }
  250. hudBatch.end();
  251. }
  252.  
  253. public void updateInput() {
  254. if(Gdx.app.getType() == Application.ApplicationType.Android) {
  255. for (int i = 0; i < 5; i++) {
  256. if (!Gdx.input.isTouched(i)) continue;
  257. guiCamera.unproject(touchPoint.set(Gdx.input.getX(i), Gdx.input.getY(i), 0));
  258. if (wleftBounds.contains(touchPoint.x, touchPoint.y)) {
  259. if (ySpeed == 0) {
  260. animationDirection = "left";
  261. lastAnimationDirection = "left";
  262. xSpeed = -1;
  263. }
  264. } else if (wrightBounds.contains(touchPoint.x, touchPoint.y)) {
  265. if (ySpeed == 0) {
  266. xSpeed = 1;
  267. animationDirection = "right";
  268. lastAnimationDirection = "right";
  269. }
  270. } else if (wupBounds.contains(touchPoint.x, touchPoint.y)) {
  271. if (xSpeed == 0) {
  272. ySpeed = 1;
  273. animationDirection = "up";
  274. lastAnimationDirection = "up";
  275. }
  276. } else if (wdownBounds.contains(touchPoint.x, touchPoint.y)) {
  277. if (xSpeed == 0) {
  278. ySpeed = -1;
  279. animationDirection = "down";
  280. lastAnimationDirection = "down";
  281. }
  282.  
  283. }
  284. }
  285. }
  286. if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
  287. if(ySpeed == 0) {
  288. animationDirection = "left";
  289. lastAnimationDirection = "left";
  290. xSpeed = -1;
  291. }
  292. }
  293. if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
  294. if(ySpeed == 0) {
  295. xSpeed = 1;
  296. animationDirection = "right";
  297. lastAnimationDirection = "right";
  298. }
  299. }
  300. if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
  301. if(xSpeed == 0) {
  302. ySpeed = 1;
  303. animationDirection = "up";
  304. lastAnimationDirection = "up";
  305. }
  306. }
  307. if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
  308. if(xSpeed == 0) {
  309. ySpeed = -1;
  310. animationDirection = "down";
  311. lastAnimationDirection = "down";
  312. }
  313. }
  314. if(Gdx.input.isKeyPressed(Input.Keys.NUM_1)) {
  315. changeMap("room_map","Tilemaps/inside.tmx");
  316. }
  317. }
  318.  
  319. public Body createBox(int x, int y, int width, int height, boolean isStatic) {
  320. Body pBody;
  321. BodyDef def = new BodyDef();
  322.  
  323. if(isStatic)
  324. def.type = BodyDef.BodyType.StaticBody;
  325. else
  326. def.type = BodyDef.BodyType.DynamicBody;
  327. def.position.set(x + 16,y + 16);
  328. def.fixedRotation = true;
  329. pBody = world.createBody(def);
  330.  
  331. PolygonShape shape = new PolygonShape();
  332. shape.setAsBox(width / 2,height / 2);
  333.  
  334. pBody.createFixture(shape,1.0f);
  335. shape.dispose();
  336.  
  337. return pBody;
  338. }
  339.  
  340. public Body createCircle(int x, int y, int radius, boolean isStatic) {
  341. Body pBody;
  342. BodyDef def = new BodyDef();
  343.  
  344. if(isStatic)
  345. def.type = BodyDef.BodyType.StaticBody;
  346. else
  347. def.type = BodyDef.BodyType.DynamicBody;
  348. def.position.set(x + 16,y + 16);
  349. def.fixedRotation = false;
  350. pBody = world.createBody(def);
  351.  
  352. CircleShape shape = new CircleShape();
  353. shape.setRadius(radius);
  354.  
  355. pBody.createFixture(shape,1.0f);
  356. shape.dispose();
  357.  
  358. return pBody;
  359. }
  360.  
  361. @Override
  362. public void resize(int width, int height) {
  363. camera.setToOrtho(false, width, height);
  364. }
  365.  
  366. @Override
  367. public void show() {
  368.  
  369. }
  370.  
  371. @Override
  372. public void hide() {
  373.  
  374. }
  375.  
  376. @Override
  377. public void pause() {
  378.  
  379. }
  380.  
  381. @Override
  382. public void resume() {
  383.  
  384. }
  385.  
  386. @Override
  387. public void dispose() {
  388.  
  389. }
  390.  
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement