Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.badlogic.gdx.Game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- 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.renderers.OrthogonalTiledMapRenderer;
- 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.BodyDef.BodyType;
- import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- public class GameLauncher extends Game {
- public SpriteBatch batch;
- private World world;
- private OrthographicCamera camera;
- private Box2DDebugRenderer debugRenderer;
- private Body player1, shadowPlayer1;
- private Body player2, shadowPlayer2;
- public void create() {
- this.batch = new SpriteBatch();
- this.world = new World(new Vector2(0, 0), true);
- this.camera = new OrthographicCamera();
- this.camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- this.camera.update();
- //this.mapRenderer = new OrthogonalTiledMapRenderer(this.map);
- BodyDef bodyDef = new BodyDef();
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.position.set(20, 20);
- player1 = this.world.createBody(bodyDef);
- PolygonShape collider = new PolygonShape();
- collider.setAsBox(16, 16);
- FixtureDef fDef = new FixtureDef();
- fDef.density = 0f;
- fDef.friction = 0f;
- fDef.restitution = 0f;
- fDef.filter.groupIndex = -1;
- fDef.shape = collider;
- player1.createFixture(fDef);
- bodyDef.type = BodyType.KinematicBody;
- bodyDef.position.set(20, 20);
- shadowPlayer1 = this.world.createBody(bodyDef);
- collider = new PolygonShape();
- collider.setAsBox(16, 16);
- fDef = new FixtureDef();
- fDef.density = 0f;
- fDef.friction = 0f;
- fDef.restitution = 0f;
- fDef.filter.groupIndex = -1;
- fDef.shape = collider;
- shadowPlayer1.createFixture(fDef);
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.position.set(60, 60);
- player2 = this.world.createBody(bodyDef);
- collider = new PolygonShape();
- collider.setAsBox(16, 16);
- fDef = new FixtureDef();
- fDef.density = 0f;
- fDef.friction = 0f;
- fDef.restitution = 0f;
- fDef.filter.groupIndex = -2;
- fDef.shape = collider;
- player2.createFixture(fDef);
- bodyDef.type = BodyType.KinematicBody;
- bodyDef.position.set(60, 60);
- shadowPlayer2 = this.world.createBody(bodyDef);
- collider = new PolygonShape();
- collider.setAsBox(16, 16);
- fDef = new FixtureDef();
- fDef.density = 0f;
- fDef.friction = 0f;
- fDef.restitution = 0f;
- fDef.filter.groupIndex = -2;
- fDef.shape = collider;
- shadowPlayer2.createFixture(fDef);
- collider.dispose();
- this.debugRenderer = new Box2DDebugRenderer();
- }
- public void render() {
- Gdx.gl.glClearColor(0, 0, 1, 1);
- Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- this.player1.setLinearVelocity(0, 0);
- this.shadowPlayer1.setLinearVelocity(0, 0);
- if(Gdx.input.isKeyPressed(Keys.Z)) {
- //this.player1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, 30));
- this.shadowPlayer1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, 30));
- }
- if(Gdx.input.isKeyPressed(Keys.S)) {
- //this.player1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, -30));
- this.shadowPlayer1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, -30));
- }
- if(Gdx.input.isKeyPressed(Keys.Q)) {
- //this.player1.setLinearVelocity(new Vector2(-30, player1.getLinearVelocity().y));
- this.shadowPlayer1.setLinearVelocity(new Vector2(-30, player1.getLinearVelocity().y));
- }
- if(Gdx.input.isKeyPressed(Keys.D)) {
- //this.player1.setLinearVelocity(new Vector2(30, player1.getLinearVelocity().y));
- this.shadowPlayer1.setLinearVelocity(new Vector2(30, player1.getLinearVelocity().y));
- }
- this.player1.setTransform(this.shadowPlayer1.getPosition(), 0);
- this.player2.setTransform(this.shadowPlayer2.getPosition(), 0);
- this.world.step(1/60f, 6, 2);
- debugRenderer.render(this.world, camera.combined);
- this.camera.update();
- }
- public void dispose() {
- batch.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement