Code Refactor

Made some utilities
This commit is contained in:
Ash Reynolds 2016-02-09 18:33:12 +00:00
parent 71f048e3ca
commit 71ffec00ab
13 changed files with 233 additions and 64 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@ -37,12 +37,8 @@ public abstract class Base {
this.size = size;
this.bodyType = bodyType;
this.curColour = curColour;
makeBody();
}
protected abstract void makeBody();
// Accessors
public Vector2 getPos() { return body.getPosition(); }
public Vector2 getSize() { return size; }

View File

@ -0,0 +1,53 @@
package com.game.Actor;
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.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import static com.game.Misc.Vars.PPM;
/**
* Created by Ash on 09/02/2016.
*/
public class Box2DSprite {
protected Body body;
protected Animation animation;
protected float width, height;
protected Vector2 pos;
protected TextureRegion currentFrame;
public Box2DSprite(Body body)
{
this.body = body;
//setAnimation();
}
public void update(float dt)
{
pos = body.getPosition();
currentFrame = animation.getKeyFrame(dt, true);
}
public void render(SpriteBatch sb)
{
sb.begin();
sb.draw(currentFrame,
(pos.x * PPM) - width / 2,
(pos.y * PPM) - height / 2
);
sb.end();
}
/*public void setAnimation(TextureRegion[] reg, float delay)
{
setAnimation(reg, delay);
}*/
}

View File

@ -0,0 +1,36 @@
package com.game.Actor.Object;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.game.Misc.Vars;
/**
* Created by Ash on 09/02/2016.
*/
public class Background {
private Vector2 pos;
private Texture texture;
public Background(String path)
{
loadTexture(path);
}
public void update(float dt, Vector2 pos)
{
this.pos = pos;
}
public void render(SpriteBatch sb)
{
sb.draw(texture, pos.x, pos.y, Vars.SCREEN_WIDTH, Vars.SCREEN_HEIGHT);
}
private void loadTexture(String path)
{
this.texture = new Texture(path);
}
}

View File

@ -2,6 +2,7 @@ package com.game.Actor;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.game.Misc.Box2dUtils;
import com.game.Misc.Vars;
import static com.game.Misc.Vars.PPM;
@ -14,38 +15,19 @@ public class Platform extends Base {
public Platform(World world, Vector2 pos, Colours curColour) {
super(world, pos, new Vector2(TILESIZE, TILESIZE), "STATIC", curColour);
}
@Override
protected void makeBody() {
bd = new BodyDef();
if(bodyType.equals("STATIC")) { bd.type = BodyDef.BodyType.StaticBody; } // Doesn't move, isn't affected by forces
else if(bodyType.equals("KINEMATIC")) { bd.type = BodyDef.BodyType.KinematicBody; } // Can move, isn't affected by forces
else { bd.type = BodyDef.BodyType.DynamicBody; } // Can move, is affected by forces
bd.position.set(pos.x / PPM, pos.y / PPM);
body = world.createBody(bd);
FixtureDef fd = new FixtureDef();
// Create the box (platform)
PolygonShape polygon = new PolygonShape();
polygon.setAsBox(TILESIZE / 2 / PPM, TILESIZE / 2 / PPM);
fd.shape = polygon;
body.createFixture(fd);
// Create a sensor used for jumping above each box
ChainShape chain = new ChainShape();
Vector2[] v = new Vector2[2];
v[0] = new Vector2(
(-TILESIZE / 2 + 5) / PPM, (TILESIZE / 2 + 5) / PPM);
v[1] = new Vector2(
(TILESIZE / 2 - 5) / PPM, (TILESIZE / 2 + 5) / PPM);
chain.createChain(v);
fd.shape = chain;
fd.isSensor = true;
body.createFixture(fd).setUserData("PLATFORM");
body = Box2dUtils.makeBody(world,
BodyDef.BodyType.StaticBody,
pos
);
Box2dUtils.makePolygon(body, size, "", false);
Box2dUtils.makeChain(body,
new Vector2[]{
new Vector2((-TILESIZE / 2 + 5) / PPM, (TILESIZE / 2 + 5) / PPM),
new Vector2((TILESIZE / 2 - 5) / PPM, (TILESIZE / 2 + 5) / PPM)
},
"PLATFORM",
true
);
}
}

View File

@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.game.Misc.Box2dUtils;
import com.game.Misc.Vars;
import static com.game.Misc.Vars.PPM;
@ -13,6 +14,8 @@ import static com.game.Misc.Vars.PPM;
*/
public class Player extends Base {
Vector2 curVel;
// TODO, remove
private Texture texture = new Texture("textures/player.png");
@ -28,26 +31,12 @@ public class Player extends Base {
public Player(World world, Vector2 pos, Vector2 size, Colours curColour) {
super(world, pos, size, "", curColour);
curAction = Action.IDLE;
}
@Override
public void makeBody() {
bd = new BodyDef();
bd.type = BodyDef.BodyType.DynamicBody;
bd.position.set(pos.x / PPM, pos.y / PPM);
body = world.createBody(bd);
FixtureDef fd = new FixtureDef();
CircleShape circle = new CircleShape();
circle.setRadius((size.x / 2) / PPM);
fd.shape = circle;
fd.density = 1f;
fd.friction = 0.9f;
body.createFixture(fd).setUserData("PLAYER");
body = Box2dUtils.makeBody(world,
BodyDef.BodyType.DynamicBody,
pos
);
Box2dUtils.makeCircle(body, size.x, "PLAYER", false);
}
public void update(float dt)
@ -58,7 +47,7 @@ public class Player extends Base {
curAction = Action.FALLING;
}
Vector2 curVel = body.getLinearVelocity();
curVel = body.getLinearVelocity();
curVel.x = Vars.SCROLLSPEED.x * dt;
body.setLinearVelocity(curVel);
pos = body.getPosition();

View File

@ -0,0 +1,74 @@
package com.game.Misc;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import static com.game.Misc.Vars.PPM;
/**
* Created by Ash on 09/02/2016.
*/
public class Box2dUtils {
private static float density = 1f;
private static float friction = 0.9f;
public static Body makeBody(World world, BodyDef.BodyType bodyType, Vector2 pos)
{
BodyDef bd = new BodyDef();
bd.type = bodyType;
bd.position.set(pos.x / PPM,
pos.y / PPM
);
return world.createBody(bd);
}
public static void makePolygon(Body body, Vector2 size, String userData, boolean isSensor)
{
FixtureDef fd = new FixtureDef();
PolygonShape shape = new PolygonShape();
shape.setAsBox((size.x / 2) / PPM,
(size.y / 2) / PPM
);
fd.shape = shape;
fd.density = density;
fd.friction = friction;
fd.isSensor = isSensor;
if(userData.equals("")) { body.createFixture(fd); }
else { body.createFixture(fd).setUserData(userData); }
}
public static void makeCircle(Body body, float diameter, String userData, boolean isSensor)
{
FixtureDef fd = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius((diameter / 2) / PPM);
fd.shape = shape;
fd.density = density;
fd.friction = friction;
fd.isSensor = isSensor;
if(userData.equals("")) { body.createFixture(fd); }
else { body.createFixture(fd).setUserData(userData); }
}
public static void makeChain(Body body, Vector2[] v, String userData, boolean isSensor)
{
FixtureDef fd = new FixtureDef();
ChainShape shape = new ChainShape();
shape.createChain(v);
fd.shape = shape;
fd.density = density;
fd.friction = friction;
fd.isSensor = isSensor;
if(userData.equals("")) { body.createFixture(fd); }
else { body.createFixture(fd).setUserData(userData); }
}
}

View File

@ -0,0 +1,36 @@
package com.game.Misc;
import com.badlogic.gdx.math.Vector2;
import com.game.Actor.Object.Background;
/**
* Created by Ash on 09/02/2016.
*/
public class ParralaxBackground {
private Background back;
private Background middle;
private Background front;
public ParralaxBackground(String back, String middle, String front)
{
this.back = new Background(back);
createBackground(middle);
createBackground(front);
}
public void update(float dt)
{
Vector2 movementAmount = new Vector2(.1f, 0);
back.update(dt, movementAmount);
middle.update(dt, movementAmount);
front.update(dt, movementAmount);
}
private void createBackground(String path)
{
}
}

View File

@ -13,14 +13,14 @@ public class Vars {
public static final int SCREEN_WIDTH = 1280;
public static final int SCREEN_HEIGHT = 720;
public static final boolean RESIZABLE = false;
public static final float FRAMERATE = 60f;
public static final float FRAMERATE = 60;
// Physics related
public static final float STEP = 1 / FRAMERATE;
public static final Vector2 GRAVITY = new Vector2(0, -9.81f);
public static final float PPM = 100f; // Pixels per meter
public static final float TILESIZE = 64f;
public static final Vector2 SCROLLSPEED = new Vector2(150, 0);
public static final Vector2 SCROLLSPEED = new Vector2(150f, 0);
// Filter bits
public static final short BIT_RED = 2;

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.objects.PolylineMapObject;
import com.badlogic.gdx.maps.objects.TextureMapObject;
@ -30,7 +31,7 @@ import java.util.ArrayList;
public class Play extends State {
// TODO, remove
public boolean isDebug = false;
public boolean isDebug = true;
// Physics related
private World world;
@ -104,7 +105,7 @@ public class Play extends State {
cameraUpdate(dt);
b2dCam.position.x = player.getPos().x / PPM;
b2dCam.position.x = player.getPos().x;
b2dCam.update();
player.update(dt);
@ -117,12 +118,13 @@ public class Play extends State {
if(!isDebug)
{
tmr.setView(cam);
tmr.render();
sb.begin();
player.render(sb);
sb.end();
tmr.setView(cam);
tmr.render();
}
else
{
@ -151,7 +153,7 @@ public class Play extends State {
Vector3 camPos = cam.position;
camPos.x = cam.position.x + ((player.getPos().x * PPM) - cam.position.x) * .2f;
camPos.y = cam.position.y + ((player.getPos().y * PPM) - cam.position.y) * .2f;
//camPos.y = cam.position.y + ((player.getPos().y * PPM) - cam.position.y) * .2f;
cam.position.set(camPos);
cam.update();
}

View File

@ -16,5 +16,6 @@ public class DesktopLauncher {
config.height = Vars.SCREEN_HEIGHT;
config.resizable = Vars.RESIZABLE;
config.addIcon("spritesheets/icon.jpg", Files.FileType.Internal);
config.foregroundFPS = (int)Vars.FRAMERATE;
}
}