Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class LevelScreen implements Screen{
- public LevelScreen(Outpost outpost) {
- this.outpost = outpost;
- }
- @Override
- public void show(){
- //add input controls
- Gdx.input.setInputProcessor(new PlayerInput(this));
- //create box2d world
- world = new World(new Vector2(0, 0), true);
- debugRenderer = new Box2DDebugRenderer();
- //run the setupLevel method in the child class
- this.setupLevel();
- };
- public void render(float delta) {
- //update player data
- player.update();
- //update box2d
- world.step(1/60f, 6, 2);
- //do graphical rendering in the WorldRenderer
- renderer.render();
- }
- public void hide() {
- dispose();
- }
- @Override
- public void dispose() {
- renderer.dispose();
- }
- public World getBox2DWorld(){
- return world;
- }
- public Box2DDebugRenderer getBox2DDebugRenderer(){
- return debugRenderer;
- }
- }
- public class Overworld extends LevelScreen{
- public Overworld(Outpost outpost){
- super(outpost);
- }
- public void setupLevel() {
- //set background music and start playing
- PlayAudio.playMusic(true);
- //create the player object
- startLocation = new Vector2(0,0);
- player = new Player(this, startLocation);
- //create map and renderer
- map = new TmxMapLoader().load("data/graphics/overworld.tmx");
- cameraLocation = new Vector3(player.getBody().getPosition().x, player.getBody().getPosition().y, 0);
- renderer = new WorldRenderer(this, this.map, cameraLocation);
- }
- }
- public class WorldRenderer {
- public WorldRenderer(LevelScreen screen, TiledMap map, Vector3 cameraPosition){
- this.screen = screen;
- screen.setRenderer(this);
- orthogonalTiledMapRenderer = new OrthogonalTiledMapRenderer(map,1f / 32f);
- width = Gdx.graphics.getWidth();
- height = Gdx.graphics.getHeight();
- cam = new OrthographicCamera();
- //make sure the two integers in setToOrtho are equal to make proportions match.
- //Larger integer means smaller scale images (camera further away from plane).
- cam.setToOrtho(false, width / height * 16, 16);
- cam.position.set(cameraPosition);
- cam.update();
- batch = new SpriteBatch();
- batch.setProjectionMatrix(cam.combined);
- }
- public void render(){
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- //get objects from screen
- player = screen.getPlayer();
- cam.update();
- //update Box2D debug renderer
- Box2DDebugRenderer debugRenderer = screen.getBox2DDebugRenderer();
- Matrix4 cameraCopy = cam.combined.cpy().scale(LevelScreen.BOX_TO_WORLD, LevelScreen.BOX_TO_WORLD, 1f);
- debugRenderer.render(screen.getBox2DWorld(), cameraCopy);
- //render the tile map
- orthogonalTiledMapRenderer.setView(cam);
- orthogonalTiledMapRenderer.render();
- //set batch matrix to camera matrix
- batch.setProjectionMatrix(cam.combined);
- //begin rendering
- batch.begin();
- //draw the ship
- batch.draw(
- player.getTexture(), //texture region
- player.getBody().getPosition().x, player.getBody().getPosition().y, //position
- -1 * (player.getTexture().getRegionWidth() / 2) * LevelScreen.WORLD_TO_BOX, -1 * (player.getTexture().getRegionWidth() / 2) * LevelScreen.WORLD_TO_BOX, //origin offset
- player.getTexture().getRegionWidth(), player.getTexture().getRegionHeight(), //width/height
- LevelScreen.WORLD_TO_BOX, LevelScreen.WORLD_TO_BOX, //scale
- player.getBody().getTransform().getRotation()
- );
- //done rendering
- batch.end();
- }
- public OrthographicCamera getCamera(){
- return cam;
- }
- public void dispose(){
- batch.dispose();
- }
- }
- public abstract class Entity {
- protected BodyDef bodyDef;
- protected Body body;
- protected TextureAtlas atlas;
- protected TextureRegion texture;
- public LevelScreen screen;
- public Entity(LevelScreen screen){
- this.screen = screen;
- }
- /*
- * Set up box2d body
- */
- protected void createBody(Vector2 position, float width, float height, float density, float friction, float restitution, boolean isMoveable){
- bodyDef = new BodyDef();
- bodyDef.position.set(position.scl(LevelScreen.WORLD_TO_BOX));
- body = screen.getBox2DWorld().createBody(bodyDef);
- body.setUserData(this);
- if(isMoveable){
- bodyDef.type = BodyType.DynamicBody;
- }else{
- bodyDef.type = BodyType.StaticBody;
- }
- PolygonShape shape = new PolygonShape();
- shape.setAsBox(width * LevelScreen.WORLD_TO_BOX, height * LevelScreen.WORLD_TO_BOX);
- FixtureDef fixtureDef = new FixtureDef();
- fixtureDef.shape = shape;
- fixtureDef.density = density;
- fixtureDef.friction = friction;
- fixtureDef.restitution = restitution;
- Fixture fixture = body.createFixture(fixtureDef);
- shape.dispose();
- }
- /**
- * @return the box2d body
- */
- public Body getBody(){
- return body;
- }
- }
- public abstract class ActiveEntity extends Entity {
- protected Vector2 startPosition;
- protected boolean isMovingUp;
- protected boolean isMovingDown;
- protected boolean isMovingLeft;
- protected boolean isMovingRight;
- public ActiveEntity(LevelScreen screen, Vector2 startPosition){
- super(screen);
- this.startPosition = startPosition;
- this.isMovingUp = false;
- this.isMovingDown = false;
- this.isMovingLeft = false;
- this.isMovingRight = false;
- this.defineBody();
- this.defineEntity();
- }
- public void update(){
- this.applyIntendedMovement();
- Gdx.app.log(Outpost.LOG, String.valueOf(this.body.getMass()));
- }
- public void applyIntendedMovement(){
- if(this.isMovingUp){
- this.body.applyForceToCenter(new Vector2(0,100).scl(this.SPEED_FACTOR), true);
- }
- }
- public void setMovingUp(boolean isMovingUp){
- this.isMovingUp = isMovingUp;
- }
- public void setMovingDown(boolean isMovingDown){
- this.isMovingDown = isMovingDown;
- }
- public void setMovingLeft(boolean isMovingLeft){
- this.isMovingLeft = isMovingLeft;
- }
- public void setMovingRight(boolean isMovingRight){
- this.isMovingRight = isMovingRight;
- }
- }
- public class Player extends ActiveEntity{
- public Player(LevelScreen screen, Vector2 startPosition) {
- super(screen, startPosition);
- }
- protected void defineBody(){
- float width = 0.5f;
- float height = 0.5f;
- float density = 10f;
- float friction = 0f;
- float restitution = 1f;
- boolean isMoveable = true;
- this.createBody(this.startPosition, width, height, density, friction, restitution, isMoveable);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement