Android Open Source - proteus-sidescroller Game World






From Project

Back to project page proteus-sidescroller.

License

The source code is released under:

GNU General Public License

If you think the Android project proteus-sidescroller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bartholomew.proteus;
/*from   ww w  .ja  v a2  s .  co  m*/
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.ChainShape;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class GameWorld implements Screen {
  
  // Constants
  private static final float TIMESTEP = 1/60f;
  private static final int VELOCITY_ITERATIONS = 8, POSITION_ITERATIONS = 3;
  
  
  // Box2d and necessary constants
  private World m_world;  
  private Box2DDebugRenderer m_debugRenderer;
  private OrthographicCamera m_camera;
  private SpriteBatch m_batch;
  private int m_widthScaled, m_heightScaled;
  
  // Body variables
  private Body m_box;
  private float m_boxSpeed = 500;
  private Vector2 m_boxMovement = new Vector2();
  private float m_boxTorque = 0;
    
  @Override
  public void render(float delta) {
    //Put new color as background and set the camera
    Gdx.gl.glClearColor(0.1f, 0.4f, 0.5f, 0.1f);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    m_debugRenderer.render(m_world, m_camera.combined); // Render the world  
    m_world.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); // Step in the world
    
    m_camera.position.set(m_box.getPosition().x, m_box.getPosition().y, 0);
    m_camera.update();
    m_box.applyForceToCenter(m_boxMovement, true);
  }

  @Override
  public void resize(int width, int height) {
    m_widthScaled = width/30;
    m_heightScaled = height/30;
    m_camera.viewportWidth = m_widthScaled;
    m_camera.viewportHeight = m_heightScaled;
    m_camera.update();

  }

  @Override
  public void show() {
    
    // Setup the world
    m_widthScaled = Gdx.graphics.getWidth()/30;
    m_heightScaled = Gdx.graphics.getHeight()/30;
    
    // Init the world with gravity values
    m_world = new World(new Vector2(0f, -9.8f), true);
    m_debugRenderer = new Box2DDebugRenderer();
    m_camera = new OrthographicCamera();

    // Set InputProcessor
    Gdx.input.setInputProcessor(new InputController() {
      @Override
      public boolean keyDown(int keycode) {
        switch (keycode){
        case (Keys.W):
          m_boxMovement.y = m_boxSpeed;
          break;
        case (Keys.A):
          m_boxMovement.x = - m_boxSpeed;
          break;
        case (Keys.S):
          m_boxMovement.y = - m_boxSpeed;
          break;
        case (Keys.D):
          m_boxMovement.x = m_boxSpeed;
          break;
        case (Keys.SPACE):
          //m_boxTorque = 200;
          break;
        default:
          return false; // unknown input
        }
        return true;
      }
      @Override
      public boolean keyUp(int keycode) {
        switch (keycode){
        case (Keys.W):
        case (Keys.S):
          m_boxMovement.y = 0;
          break;
        case (Keys.A):
        case (Keys.D):
          m_boxMovement.x = 0;
          break;
        case (Keys.SPACE):
          //m_boxTorque = -150;
          break;
        default:
          return false; // unknown input
        }
        return true;
      }
      
      @Override
      public boolean scrolled(int amount) {
        m_camera.zoom += amount/30f;
        return true;
      }
    });
    
    BodyDef bodyDef = new BodyDef();
    FixtureDef fixtureDef = new FixtureDef();
    
//BOX
    // Body definition
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(2.25f, 10);
    
    // Box shape
    PolygonShape boxShape = new PolygonShape();
    boxShape.setAsBox(.5f, 1f);
    
    // Fixture definition
    fixtureDef.shape = boxShape;
    fixtureDef.friction = 0.75f;
    fixtureDef.restitution = 0.1f;
    fixtureDef.density = 0.1f;
    
    m_box = m_world.createBody(bodyDef);
    m_box.createFixture(fixtureDef);
    boxShape.dispose();
    
    m_box.applyForce(new Vector2(10, 20),new Vector2(0,0), true);
    
// BALL    
    // Ball Shape
    CircleShape circle = new CircleShape();
    circle.setPosition(new Vector2(0, 1.5f));
    circle.setRadius(0.5f);
    
    // Fixture definition
    fixtureDef.shape = circle;
    fixtureDef.density = 2.5f; // 2.5 kg per 1 meter squared
    fixtureDef.friction = 0.25f; // from 0-1, 1 is max friction, 0 is no friction
    fixtureDef.restitution = 0.75f; // from 0-1, 1 is not lose any energy at collisions, 0 is lose all energy
  
    m_box.createFixture(fixtureDef);
    circle.dispose();
    
// PLATFORM
    // Body definition
    bodyDef.type = BodyType.StaticBody;
    bodyDef.position.set(0,0);
    
    // Platform shape
    ChainShape platformShape = new ChainShape();
    platformShape.createChain(new Vector2[] {new Vector2(-50, 0), new Vector2(50, 0), new Vector2(50, 50), new Vector2(-50, 50), new Vector2(-50, 0)});
    
    // Fixture definition
    fixtureDef.shape = platformShape;
    fixtureDef.friction = 0.5f;
    fixtureDef.restitution = 0f;
    
    m_world.createBody(bodyDef).createFixture(fixtureDef);
    
    platformShape.dispose();
    
  }

  @Override
  public void hide() {
    dispose();
  }

  @Override
  public void pause() {

  }

  @Override
  public void resume() {

  }

  @Override
  public void dispose() {
    m_world.dispose();
    m_debugRenderer.dispose();
    
    
  }

}




Java Source Code List

com.bartholomew.proteus.GameWorld.java
com.bartholomew.proteus.InputController.java
com.bartholomew.proteus.MainActivity.java
com.bartholomew.proteus.MainMenu.java
com.bartholomew.proteus.Main.java
com.bartholomew.proteus.Proteus.java
com.bartholomew.proteus.Splash.java
com.bartholomew.proteus.client.GwtLauncher.java
com.bartholomew.proteus.tween.ActorAccessor.java
com.bartholomew.proteus.tween.SpriteAccessor.java