Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.finn.pokemon.screen;
- import com.badlogic.gdx.Application;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.maps.tiled.TiledMap;
- import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
- import com.badlogic.gdx.maps.tiled.TmxMapLoader;
- import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.physics.box2d.*;
- import com.badlogic.gdx.utils.Array;
- import com.finn.pokemon.Pokemon;
- import com.finn.pokemon.util.TiledObjectUtil;
- import java.util.HashMap;
- public class GameScreen implements Screen {
- final Pokemon game;
- OrthographicCamera camera;
- OrthographicCamera guiCamera;
- HashMap<String, TiledMapRenderer> tiledRenderingHashMap;
- TiledMapRenderer tiledMapRenderer;
- String currentMap;
- World world;
- Body player;
- Box2DDebugRenderer b2dr;
- Texture playerSheet;
- TextureRegion[] runningLeft;
- TextureRegion[] runningRight;
- TextureRegion[] runningUp;
- TextureRegion[] runningDown;
- Animation leftAnimation;
- Animation rightAnimation;
- Animation upAnimation;
- Animation downAnimation;
- SpriteBatch batch;
- SpriteBatch hudBatch;
- String animationDirection;
- String lastAnimationDirection;
- float stateTime;
- int xSpeed;
- int ySpeed;
- int mapWidth;
- int mapHeight;
- int Scale;
- int[] backgroundLayers = { 0, 1,};
- int[] foregroundLayers = { 2 };
- Rectangle wleftBounds;
- Rectangle wrightBounds;
- Rectangle wupBounds;
- Rectangle wdownBounds;
- Vector3 touchPoint;
- Texture leftRegion;
- Texture rightRegion;
- Texture upRegion;
- Texture downRegion;
- boolean DEBUG = false;
- public GameScreen(final Pokemon game) {
- this.game = game;
- float w = Gdx.graphics.getWidth();
- float h = Gdx.graphics.getHeight();
- camera = new OrthographicCamera();
- camera.setToOrtho(false,w,h);
- camera.update();
- guiCamera = new OrthographicCamera();
- guiCamera.setToOrtho(false,w,h);
- guiCamera.update();
- TiledMap outsideMap = new TmxMapLoader().load("Tilemaps/outside.tmx");
- TiledMap insideMap = new TmxMapLoader().load("Tilemaps/inside.tmx");
- TiledMapRenderer rendererA = new OrthogonalTiledMapRenderer(outsideMap);
- TiledMapRenderer rendererB = new OrthogonalTiledMapRenderer(insideMap);
- tiledRenderingHashMap = new HashMap();
- tiledRenderingHashMap.put("outside_map", rendererA);
- tiledRenderingHashMap.put("room_map", rendererB);
- currentMap = "outside_map";
- mapWidth = (outsideMap.getProperties().get("width",Integer.class) * outsideMap.getProperties().get("tilewidth",Integer.class));
- mapHeight = (outsideMap.getProperties().get("height",Integer.class) * outsideMap.getProperties().get("tileheight",Integer.class));
- batch = new SpriteBatch();
- hudBatch = new SpriteBatch();
- Scale = 1;
- if(Gdx.app.getType() == Application.ApplicationType.Android) {
- Scale = 4;
- camera.zoom = 1 / Scale;
- wleftBounds = new Rectangle(0, 0, 120, 120);
- wrightBounds = new Rectangle(240, 0, 120, 120);
- wdownBounds = new Rectangle(120, 0, 120, 120);
- wupBounds = new Rectangle(120, 120, 120, 120);
- // STOP DEFINING THE TYPE OF OBJECT THINGS ARE, IT ALREADY KNOWS AND SO ASSUMES YOU ARE DEFINING ANOTHER!
- touchPoint = new Vector3();
- leftRegion = new Texture(Gdx.files.internal("Images/joystick_left2.png"));
- rightRegion = new Texture(Gdx.files.internal("Images/joystick_right2.png"));
- downRegion = new Texture(Gdx.files.internal("Images/joystick_down2.png"));
- upRegion = new Texture(Gdx.files.internal("Images/joystick_up2.png"));
- }
- playerSheet = new Texture(Gdx.files.internal("Images/playerNew32x64.png"));
- runningDown = new TextureRegion[3];
- runningUp = new TextureRegion[3];
- runningRight = new TextureRegion[3];
- runningLeft = new TextureRegion[3];
- for(int i=0;i<3;i++) {
- runningDown[i] = new TextureRegion(playerSheet, 32 * i, 0, 32, 64);
- runningLeft[i] = new TextureRegion(playerSheet, 32 * i, 64, 32, 64);
- runningRight[i] = new TextureRegion(playerSheet, 32 * i, 128, 32, 64);
- runningUp[i] = new TextureRegion(playerSheet, 32 * i, 192, 32, 64);
- }
- downAnimation = new Animation<TextureRegion>(0.1f, runningDown);
- leftAnimation = new Animation<TextureRegion>(0.1f, runningLeft);
- rightAnimation = new Animation<TextureRegion>(0.1f, runningRight);
- upAnimation = new Animation<TextureRegion>(0.1f, runningUp);
- stateTime = 0f;
- world = new World(new Vector2(0,0), false);
- b2dr = new Box2DDebugRenderer();
- //player = createBox(32,32,32,32,false);
- player = createCircle(32,32,16,false);
- // for(int i = 0;i<20;i++) {
- // createCircle(64 + (i * 32),64 + (i * 16),12,false).applyLinearImpulse(0,-500,1,1,false);
- //
- // }
- TiledObjectUtil.parseTiledObjectLater(world,outsideMap.getLayers().get("collision-layer").getObjects());
- }
- @Override
- public void render(float delta) {
- world.step(1 / 60f,6,2);
- processInput();
- updateCamera();
- draw();
- }
- public void processInput() {
- if((Math.round(player.getPosition().x + 16) % 32) == 0 && (Math.round(player.getPosition().y + 16) % 32) == 0) {
- xSpeed = ySpeed = 0;
- animationDirection = "null";
- //player.setTransform((float) Math.floor(player.getPosition().x),(float) Math.floor(player.getPosition().y),0);
- }
- if((Math.round(player.getPosition().x + 16) == 128 && (Math.round(player.getPosition().y + 16) == 160)) && currentMap == "outside_map") {
- changeMap("room_map","Tilemaps/inside.tmx");
- } else if((Math.round(player.getPosition().x + 16) == 224 && (Math.round(player.getPosition().y + 16) == 32)) && currentMap == "room_map") {
- changeMap("outside_map","Tilemaps/outside.tmx");
- }
- int tileX = ((Math.round(player.getPosition().x + 16) / 32));
- int tileY = ((Math.round(player.getPosition().y + 16) / 32));
- TiledMap insideMap = new TmxMapLoader().load("Tilemaps/inside.tmx");
- TiledMapTileLayer layer = (TiledMapTileLayer) insideMap.getLayers().get("background");
- //System.out.println(layer.getCell(tileX,tileY).getTile().getProperties().get("speed"));
- //System.out.println(tileX + " | " + tileY);
- updateInput();
- //player.setTransform((float) Math.round(player.getPosition().x),(float) Math.round(player.getPosition().y),0);
- player.setLinearVelocity(xSpeed * 128, ySpeed * 128);
- //System.out.println("Player x " + (Math.round(player.getPosition().x + 16)) + " | Player y " + (Math.round(player.getPosition().y + 16)));
- //System.out.println("Player y " + (Math.floor(player.getPosition().y) % 32));
- }
- public void updateCamera() {
- camera.position.set(player.getPosition().x + 16,player.getPosition().y + 16,0);
- if(currentMap == "outside_map") {
- camera.position.x = MathUtils.clamp(camera.position.x, camera.viewportWidth / Scale * 0.5f, mapWidth - camera.viewportWidth / Scale * 0.5f);
- camera.position.y = MathUtils.clamp(camera.position.y, camera.viewportHeight / Scale * 0.5f, mapHeight - camera.viewportHeight / Scale * 0.5f);
- }
- camera.update();
- }
- public void changeMap(String mapName,String directory) {
- currentMap = mapName;
- Array<Body> bodies = new Array<Body>();
- world.getBodies(bodies);
- for(int i = 0; i < bodies.size; i++) {
- world.destroyBody(bodies.get(i));
- }
- TiledMap Map = new TmxMapLoader().load(directory);
- System.out.println(mapWidth);
- TiledObjectUtil.parseTiledObjectLater(world,Map.getLayers().get("collision-layer").getObjects());
- player = createCircle(32,32,16,false);
- player.setTransform(208,48,0);
- }
- public void draw() {
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- //tiledMapRenderer.setView(camera);
- tiledRenderingHashMap.get(currentMap).setView(camera);
- stateTime += Gdx.graphics.getDeltaTime();
- //tiledMapRenderer.render(backgroundLayers);
- tiledRenderingHashMap.get(currentMap).render(backgroundLayers);
- boolean isMovementKeyDown = (Gdx.input.isKeyDown(Keys.A) || Gdx.input.isKeyDown(Keys.S) || Gdx.input.isKeyDown(Keys.D) || Gdx.input.isKeyDown(Keys.W));
- batch.setProjectionMatrix(camera.combined);
- batch.begin();
- if(animationDirection == "up") {
- batch.draw((TextureRegion) upAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true),player.getPosition().x - 16,player.getPosition().y - 16);
- } else if(animationDirection == "down") {
- batch.draw((TextureRegion) downAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true),player.getPosition().x - 16,player.getPosition().y - 16);
- } else if(animationDirection == "left") {
- batch.draw((TextureRegion) leftAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true),player.getPosition().x - 16,player.getPosition().y - 16);
- } else if(animationDirection == "right") {
- batch.draw((TextureRegion) rightAnimation.getKeyFrame(isMovementKeyDown ? stateTime : 0.3f, true)),player.getPosition().x - 16,player.getPosition().y - 16);
- }
- batch.end();
- tiledRenderingHashMap.get(currentMap).render(foregroundLayers);
- if(DEBUG) {
- b2dr.render(world,camera.combined);
- //System.out.println(player.getPosition().x + " | " + player.getPosition().y);
- }
- hudBatch.begin();
- if(Gdx.app.getType() == Application.ApplicationType.Android) {
- hudBatch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
- hudBatch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);
- hudBatch.draw(downRegion, wdownBounds.x, wdownBounds.y, wdownBounds.width, wdownBounds.height);
- hudBatch.draw(upRegion, wupBounds.x, wupBounds.y, wupBounds.width, wupBounds.height);
- }
- hudBatch.end();
- }
- public void updateInput() {
- if(Gdx.app.getType() == Application.ApplicationType.Android) {
- for (int i = 0; i < 5; i++) {
- if (!Gdx.input.isTouched(i)) continue;
- guiCamera.unproject(touchPoint.set(Gdx.input.getX(i), Gdx.input.getY(i), 0));
- if (wleftBounds.contains(touchPoint.x, touchPoint.y)) {
- if (ySpeed == 0) {
- animationDirection = "left";
- lastAnimationDirection = "left";
- xSpeed = -1;
- }
- } else if (wrightBounds.contains(touchPoint.x, touchPoint.y)) {
- if (ySpeed == 0) {
- xSpeed = 1;
- animationDirection = "right";
- lastAnimationDirection = "right";
- }
- } else if (wupBounds.contains(touchPoint.x, touchPoint.y)) {
- if (xSpeed == 0) {
- ySpeed = 1;
- animationDirection = "up";
- lastAnimationDirection = "up";
- }
- } else if (wdownBounds.contains(touchPoint.x, touchPoint.y)) {
- if (xSpeed == 0) {
- ySpeed = -1;
- animationDirection = "down";
- lastAnimationDirection = "down";
- }
- }
- }
- }
- if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
- if(ySpeed == 0) {
- animationDirection = "left";
- lastAnimationDirection = "left";
- xSpeed = -1;
- }
- }
- if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
- if(ySpeed == 0) {
- xSpeed = 1;
- animationDirection = "right";
- lastAnimationDirection = "right";
- }
- }
- if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
- if(xSpeed == 0) {
- ySpeed = 1;
- animationDirection = "up";
- lastAnimationDirection = "up";
- }
- }
- if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
- if(xSpeed == 0) {
- ySpeed = -1;
- animationDirection = "down";
- lastAnimationDirection = "down";
- }
- }
- if(Gdx.input.isKeyPressed(Input.Keys.NUM_1)) {
- changeMap("room_map","Tilemaps/inside.tmx");
- }
- }
- public Body createBox(int x, int y, int width, int height, boolean isStatic) {
- Body pBody;
- BodyDef def = new BodyDef();
- if(isStatic)
- def.type = BodyDef.BodyType.StaticBody;
- else
- def.type = BodyDef.BodyType.DynamicBody;
- def.position.set(x + 16,y + 16);
- def.fixedRotation = true;
- pBody = world.createBody(def);
- PolygonShape shape = new PolygonShape();
- shape.setAsBox(width / 2,height / 2);
- pBody.createFixture(shape,1.0f);
- shape.dispose();
- return pBody;
- }
- public Body createCircle(int x, int y, int radius, boolean isStatic) {
- Body pBody;
- BodyDef def = new BodyDef();
- if(isStatic)
- def.type = BodyDef.BodyType.StaticBody;
- else
- def.type = BodyDef.BodyType.DynamicBody;
- def.position.set(x + 16,y + 16);
- def.fixedRotation = false;
- pBody = world.createBody(def);
- CircleShape shape = new CircleShape();
- shape.setRadius(radius);
- pBody.createFixture(shape,1.0f);
- shape.dispose();
- return pBody;
- }
- @Override
- public void resize(int width, int height) {
- camera.setToOrtho(false, width, height);
- }
- @Override
- public void show() {
- }
- @Override
- public void hide() {
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement