From d7d8b71a1e5b606e5cbf3ae928b3bb2eb78de841 Mon Sep 17 00:00:00 2001 From: Solargale Date: Thu, 26 Mar 2020 14:23:01 -0600 Subject: [PATCH] Decided to delete code from previous game and made a bunch of original code --- core/src/com/mygdx/game/Block.java | 56 ++++++++++ core/src/com/mygdx/game/Collision.java | 95 ---------------- core/src/com/mygdx/game/Dungeon.java | 12 -- core/src/com/mygdx/game/Environment.java | 112 ------------------- core/src/com/mygdx/game/Input_Handler.java | 122 --------------------- core/src/com/mygdx/game/Level.java | 34 ------ core/src/com/mygdx/game/MyGdxGame.java | 18 ++- core/src/com/mygdx/game/Player.java | 81 -------------- core/src/com/mygdx/game/World.java | 17 +++ core/src/com/mygdx/game/WorldRenderer.java | 34 ++++++ 10 files changed, 119 insertions(+), 462 deletions(-) create mode 100644 core/src/com/mygdx/game/Block.java delete mode 100644 core/src/com/mygdx/game/Collision.java delete mode 100644 core/src/com/mygdx/game/Dungeon.java delete mode 100644 core/src/com/mygdx/game/Environment.java delete mode 100644 core/src/com/mygdx/game/Input_Handler.java delete mode 100644 core/src/com/mygdx/game/Level.java delete mode 100644 core/src/com/mygdx/game/Player.java create mode 100644 core/src/com/mygdx/game/World.java create mode 100644 core/src/com/mygdx/game/WorldRenderer.java diff --git a/core/src/com/mygdx/game/Block.java b/core/src/com/mygdx/game/Block.java new file mode 100644 index 0000000..8c749bd --- /dev/null +++ b/core/src/com/mygdx/game/Block.java @@ -0,0 +1,56 @@ +package com.mygdx.game; + +import com.badlogic.gdx.graphics.Color; + +public class Block { + int x,y,width,height; + Color color; + + public Block(int tempX, int tempY,int tempWidth, int tempHeight, Color tempColor) { + x = tempX; + y = tempY; + color = tempColor; + width = tempWidth; + height = tempHeight; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } +} diff --git a/core/src/com/mygdx/game/Collision.java b/core/src/com/mygdx/game/Collision.java deleted file mode 100644 index 1b68ffa..0000000 --- a/core/src/com/mygdx/game/Collision.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.mygdx.game; - -import com.badlogic.gdx.ApplicationAdapter; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.maps.MapObject; -import com.badlogic.gdx.maps.MapObjects; -import com.badlogic.gdx.maps.objects.RectangleMapObject; -import com.badlogic.gdx.maps.tiled.TiledMap; -import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.physics.box2d.*; - - -public class Collision extends ApplicationAdapter { - private World world; - public Body body; - private CircleShape shape; - private Player player; - private float accumulator; - private float timeStep = 1/60f; - - Collision(Player player) { - this.player = player; - } - - @Override - public void create(){ - world = new World(new Vector2(0, 0), false); - - BodyDef bdef = new BodyDef(); - bdef.position.set(player.getX(), player.getY()); - bdef.type = BodyDef.BodyType.DynamicBody; - bdef.fixedRotation = true; - body = world.createBody(bdef); - body.setLinearDamping(10f); - shape = new CircleShape(); - shape.setRadius(1/3f); - body.createFixture(shape, 0.1f); - accumulator = 0; - } - - - - @Override - public void render(){ - - Environment.batch.begin(); - Environment.batch.end(); - player.setX(body.getPosition().x); - player.setY(body.getPosition().y); - Environment.getCam().position.set(player.getX(),player.getY(),0); - accumulator += Math.min(Gdx.graphics.getDeltaTime(), 0.25f); - while (accumulator>= timeStep) { - world.step(Gdx.graphics.getRawDeltaTime(), 8, 8); - accumulator -= timeStep; - } - } - - @Override - public void dispose(){ - shape.dispose(); - } - - void buildBuildingsBodies(TiledMap tiledMap){ - MapObjects objects = tiledMap.getLayers().get("Object Layer 1").getObjects(); - for (MapObject object: objects) { - Rectangle rectangle = ((RectangleMapObject) object).getRectangle(); - - //create a dynamic within the world body (also can be KinematicBody or StaticBody - BodyDef bodyDef = new BodyDef(); - bodyDef.type = BodyDef.BodyType.StaticBody; - Body body = world.createBody(bodyDef); - - PolygonShape shape = new PolygonShape(); - shape.setAsBox(0.4f,0.4f); - //create a fixture for each body from the shape - Fixture fixture = body.createFixture(shape,0.01f); - fixture.setFriction(0.1F); - - Vector2 center = new Vector2(); - rectangle.getCenter(center); - - //Scaling the center of the body with the rest of the map - body.setTransform(center.scl(1/40f),0); - } - } - - World getWorld() { - return world; - } - - Body getBody() { - return body; - } -} diff --git a/core/src/com/mygdx/game/Dungeon.java b/core/src/com/mygdx/game/Dungeon.java deleted file mode 100644 index a5ee610..0000000 --- a/core/src/com/mygdx/game/Dungeon.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mygdx.game; - -public class Dungeon extends Level{ - int x1,x2,y1,y2 = 0; - - public Dungeon(int x1, int x2, int y1, int y2) { - this.x1 = x1; - this.x2 = x2; - this.y1 = y1; - this.y2 = y2; - } -} diff --git a/core/src/com/mygdx/game/Environment.java b/core/src/com/mygdx/game/Environment.java deleted file mode 100644 index c077498..0000000 --- a/core/src/com/mygdx/game/Environment.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.mygdx.game; - -import com.badlogic.gdx.Game; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.FPSLogger; -import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.maps.tiled.TiledMap; -import com.badlogic.gdx.maps.tiled.TiledMapRenderer; -import com.badlogic.gdx.maps.tiled.TmxMapLoader; -import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; -import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; - -public class Environment extends Game { - - static SpriteBatch batch; - - private TiledMap map; - private TiledMapRenderer tiledMapRenderer; - private float elapsedTime = 0f; - private float time = 0; - private float tick = 1/60f; - private int maxUpdatesPerFrame = 1; - private int tileSize = 32; - private Collision collision; - private Box2DDebugRenderer box2DDebugRenderer; - public static float VIEWPORT_HEIGHT = 9/2f; - public static float VIEWPORT_WIDTH = 16/2f; - public static OrthographicCamera cam; - private static Player player; - private FPSLogger fpsLogger; - - @Override - public void create () { - //Instantiating the player character with their sprite and location - player = new Player(); - collision = new Collision(player); - collision.create(); - - cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT); - box2DDebugRenderer = new Box2DDebugRenderer(); - - map = new TmxMapLoader().load(String.valueOf(Gdx.files.internal("Tilesets/"+ "island" +".tmx"))); - tiledMapRenderer = new OrthogonalTiledMapRenderer(map,1/40f); - - cam.setToOrtho(false,VIEWPORT_WIDTH,VIEWPORT_HEIGHT); - cam.zoom = 2.5f; - - fpsLogger = new FPSLogger(); - } - - @Override - public void render () { - Gdx.gl.glClearColor(0, 0, 0, 0); - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - - elapsedTime += Gdx.graphics.getDeltaTime(); - batch = new SpriteBatch(); - - batch.setProjectionMatrix(getCam().combined); - tiledMapRenderer.setView(getCam()); - tiledMapRenderer.render(); - collision.render(); - - batch.begin(); - batch.draw(player.getAnimation().getKeyFrame(elapsedTime, true), player.getX()-0.5f, player.getY()-0.5f,1,1); - - tick =1/60f; - batch.end(); - - //box2DDebugRenderer.render(collision.getWorld(),cam.combined); - getCam().update(); - - - //collision.buildBuildingsBodies(map); - - - time += Gdx.graphics. getDeltaTime(); - Input_Handler input_handler = new Input_Handler(collision,player); - Gdx.input.setInputProcessor(input_handler); - int updatesThisFrame = 0; - while (time >= tick && updatesThisFrame < maxUpdatesPerFrame) { - updatesThisFrame++; - time -= tick; - } - System.gc(); - fpsLogger.log(); - } - - //Clears the screen of the following textures - @Override - public void dispose() { - map.dispose(); - batch.dispose(); - player.getTextureAtlas().dispose(); - collision.dispose(); - } - - public static OrthographicCamera getCam() { - return cam; - } - - void zoomCam(float zoom) { - cam.zoom -= zoom; - } - - float getZoomCam(){ - return cam.zoom; - } - -} diff --git a/core/src/com/mygdx/game/Input_Handler.java b/core/src/com/mygdx/game/Input_Handler.java deleted file mode 100644 index 6125ec1..0000000 --- a/core/src/com/mygdx/game/Input_Handler.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.mygdx.game; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; -import com.badlogic.gdx.InputProcessor; -import com.badlogic.gdx.graphics.g2d.Animation; -import com.badlogic.gdx.graphics.g2d.TextureAtlas; - -public class Input_Handler implements InputProcessor { - - private float playerSpeed = 0.04f; - private Player player; - private Animation animation; - - Input_Handler(Collision collision, Player player) { - this.player = player; - Environment environment = (Environment) Gdx.app.getApplicationListener(); - - //Zooming Keybinds - if (Gdx.input.isKeyPressed(Input.Keys.EQUALS) && environment.getZoomCam() >= 1) { - environment.zoomCam(0.1f); - } - if (Gdx.input.isKeyPressed(Input.Keys.MINUS) && environment.getZoomCam() <= 3) { - environment.zoomCam(-0.1f); - } - - //4 Cardinal directions - if (Gdx.input.isKeyPressed(Input.Keys.UP)) { - up(collision); - } - if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - right(collision); - } - if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) { - down(collision); - } - if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - left(collision); - } - //4 Diagonal directions - if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - directionAnimation("UpRight"); - } - if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - directionAnimation("UpLeft"); - } - if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - directionAnimation("DownRight"); - } - if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - directionAnimation("DownLeft"); - } - } - - private void up(Collision collision) { - collision.getBody().applyLinearImpulse(0, playerSpeed,collision.getBody().getPosition().x, collision.getBody().getPosition().y, false); - directionAnimation("Up"); - } - - private void down(Collision collision) { - collision.getBody().applyLinearImpulse(0, -playerSpeed, collision.getBody().getPosition().x, collision.getBody().getPosition().y, false); - directionAnimation("Down"); - } - - private void right(Collision collision) { - collision.getBody().applyLinearImpulse(playerSpeed, 0, collision.getBody().getPosition().x, collision.getBody().getPosition().y, false); - directionAnimation("Right"); - } - - private void left(Collision collision) { - collision.getBody().applyLinearImpulse(-playerSpeed, 0, collision.getBody().getPosition().x, collision.getBody().getPosition().y, false); - directionAnimation("Left"); - } - - @Override - public boolean keyDown(int keycode) { - return false; - } - - @Override - public boolean keyUp(int keycode) { - return false; - } - - - @Override - public boolean keyTyped(char character) { - return false; - } - - @Override - public boolean touchDown(int screenX, int screenY, int pointer, int button) { - return false; - } - - @Override - public boolean touchUp(int screenX, int screenY, int pointer, int button) { - return false; - } - - @Override - public boolean touchDragged(int screenX, int screenY, int pointer) { - return false; - } - - @Override - public boolean mouseMoved(int screenX, int screenY) { - return false; - } - - @Override - public boolean scrolled(int amount) { - return false; - } - - public void directionAnimation(String direction) { - player.setAnimation( new Animation<>(1/4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + direction))); - } -} - - - diff --git a/core/src/com/mygdx/game/Level.java b/core/src/com/mygdx/game/Level.java deleted file mode 100644 index b802cfe..0000000 --- a/core/src/com/mygdx/game/Level.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.mygdx.game; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.maps.tiled.TiledMap; -import com.badlogic.gdx.maps.tiled.TmxMapLoader; -import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; - -public class Level extends Environment { - - private OrthogonalTiledMapRenderer tiledMapRenderer; - - @Override - public void create(){ - super.create(); - String nameOfMap = "island"; - TiledMap map = new TmxMapLoader().load(String.valueOf(Gdx.files.internal("Tilesets/" + nameOfMap + ".tmx"))); - tiledMapRenderer = new OrthogonalTiledMapRenderer(map,1/40f); - } - - @Override - public void render(){ - super.render(); - tiledMapRenderer.setView(cam); - tiledMapRenderer.render(); - cam.update(); - batch.setProjectionMatrix(cam.combined); - } - - @Override - public void dispose(){ - super.dispose(); - tiledMapRenderer.dispose(); - } -} diff --git a/core/src/com/mygdx/game/MyGdxGame.java b/core/src/com/mygdx/game/MyGdxGame.java index 6121184..3812959 100644 --- a/core/src/com/mygdx/game/MyGdxGame.java +++ b/core/src/com/mygdx/game/MyGdxGame.java @@ -2,6 +2,7 @@ package com.mygdx.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; @@ -9,20 +10,25 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class MyGdxGame extends ApplicationAdapter { SpriteBatch batch; Texture img; + World world; + WorldRenderer worldRenderer; @Override public void create () { - batch = new SpriteBatch(); - img = new Texture("badlogic.jpg"); + Block[] blocks = new Block[4]; + blocks[0] = new Block(0,0, 6,1,new Color(0,1,0,1)); + blocks[1] = new Block(1,0, 1,1,new Color(0,1,0,1)); + blocks[2] = new Block(2,0, 1,1,new Color(0,1,0,1)); + blocks[3] = new Block(3,0, 1,1,new Color(0,1,0,1)); + world = new World(blocks); } @Override public void render () { - Gdx.gl.glClearColor(1, 0, 0, 1); + Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - batch.begin(); - batch.draw(img, 0, 0); - batch.end(); + worldRenderer = new WorldRenderer(world.getBlocks()); + worldRenderer.render(); } @Override diff --git a/core/src/com/mygdx/game/Player.java b/core/src/com/mygdx/game/Player.java deleted file mode 100644 index 8f53a16..0000000 --- a/core/src/com/mygdx/game/Player.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.mygdx.game; - -import com.badlogic.gdx.graphics.g2d.Animation; -import com.badlogic.gdx.graphics.g2d.TextureAtlas; -import com.badlogic.gdx.utils.Array; - -public class Player { - private Array keyFrames; - private float x, y; - private String spriteName; - private TextureAtlas textureAtlas; - private Animation animation; - private float frameDuration; - - Player() { - this.x = 10; - this.y = 10; - this.frameDuration = 1/4f; - this.spriteName = "man"; - this.textureAtlas = new TextureAtlas("NewSprites/" + spriteName + ".atlas"); - this.keyFrames = textureAtlas.findRegions(spriteName + "_Down"); - this.animation = new Animation<>(frameDuration, keyFrames); - } - - public float getX() { - return x; - } - - public void setX(float x) { - this.x = x; - } - - public float getY() { - return y; - } - - public void setY(float y) { - this.y = y; - } - - String getSpriteName() { - return spriteName; - } - - public void setSpriteName(String tempSpriteName) { - spriteName = tempSpriteName; - } - - TextureAtlas getTextureAtlas() { - return textureAtlas; - } - - - Animation getAnimation() { - return animation; - } - - void setAnimation(Animation animation) { - this.animation = animation; - } - - public float getFrameDuration() { - return frameDuration; - } - - public void setFrameDuration(float frameDuration) { - this.frameDuration = frameDuration; - } - - public Array getKeyFrames() { - return keyFrames; - } - - public void setKeyFrames(Array keyFrames) { - this.keyFrames = keyFrames; - } - - public void setTextureAtlas(TextureAtlas textureAtlas) { - this.textureAtlas = textureAtlas; - } -} diff --git a/core/src/com/mygdx/game/World.java b/core/src/com/mygdx/game/World.java new file mode 100644 index 0000000..e881c11 --- /dev/null +++ b/core/src/com/mygdx/game/World.java @@ -0,0 +1,17 @@ +package com.mygdx.game; + +public class World { + Block[] blocks; + + public World(Block[] blocks) { + this.blocks = blocks; + } + + public Block[] getBlocks() { + return blocks; + } + + public void setBlocks(Block[] blocks) { + this.blocks = blocks; + } +} diff --git a/core/src/com/mygdx/game/WorldRenderer.java b/core/src/com/mygdx/game/WorldRenderer.java new file mode 100644 index 0000000..71eb26e --- /dev/null +++ b/core/src/com/mygdx/game/WorldRenderer.java @@ -0,0 +1,34 @@ +package com.mygdx.game; + +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; + +public class WorldRenderer { + private OrthographicCamera cam; + private World world; + private ShapeRenderer debugRenderer = new ShapeRenderer(); + + public WorldRenderer(World world) { + this.cam = new OrthographicCamera(10,7); + this.cam.position.set(5,3.5f,0); + this.cam.update(); + this.world = world; + } + + public WorldRenderer(Block[] blocks) { + this.cam = new OrthographicCamera(10,7); + this.cam.position.set(5,3.5f,0); + this.cam.update(); + this.world = new World(blocks); + } + + public void render(){ + debugRenderer.setProjectionMatrix(cam.combined); + debugRenderer.begin(ShapeRenderer.ShapeType.Filled); + for (Block block : world.getBlocks()) { + debugRenderer.setColor(block.getColor()); + debugRenderer.rect(block.getX()+0.5f, block.getY()+0.5f, block.getWidth(), block.getHeight()); + } + debugRenderer.end(); + } +}