Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package EntityManager; import Components.BodyComponent; import Components.BulletComponent; import Components.CollisionComponent; import Components.FlashlightComponent; import Components.HingeComponent; import Components.JointComponent; import Components.PlayerComponent; import Components.PositionComponent; import static Utils.MyConstants.PPM; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.EntitySystem; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.gdx.Gdx; 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.Body; import com.badlogic.gdx.physics.box2d.JointDef; import com.badlogic.gdx.physics.box2d.joints.DistanceJointDef; import com.badlogic.gdx.physics.box2d.joints.MouseJointDef; import com.badlogic.gdx.physics.box2d.joints.RopeJoint; import com.badlogic.gdx.physics.box2d.joints.RopeJointDef; import com.badlogic.gdx.scenes.scene2d.Event; import com.badlogic.gdx.scenes.scene2d.EventListener; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Button; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Touchpad; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.mygdx.game.Bullet; import static com.mygdx.game.MyGdxGame.world; /** * * @author Dalliny */ public class ScreenControlsSystem extends EntitySystem { public static Vector2 playerLoc = new Vector2(0, 0); public static Vector2 cameraLoc = new Vector2(0, 0); public static float playerDirection; static int opacity = 50; Touchpad touchpad; Touchpad touchpadRight; Button button; Button button2; Touchpad.TouchpadStyle touchpadStyle; Skin touchpadSkin; static Stage stage; private ImmutableArray<Entity> players; private ImmutableArray<Entity> doors; private ComponentMapper<PlayerComponent> playersC = ComponentMapper.getFor(PlayerComponent.class); private ComponentMapper<BodyComponent> bm = ComponentMapper.getFor(BodyComponent.class); private ComponentMapper<CollisionComponent> collisionM = ComponentMapper.getFor(CollisionComponent.class); // private ComponentMapper<HingeComponent> doorM = ComponentMapper.getFor(HingeComponent.class); // private ComponentMapper<FlashlightComponent> fm = ComponentMapper.getFor(FlashlightComponent.class); boolean button1press = false; boolean button2press = false; public ScreenControlsSystem() { touchpadStyle = new Touchpad.TouchpadStyle(); touchpadSkin = new Skin(); //Set background image touchpadSkin.add("touchBackground", new Texture("touchBackground.png")); //Set knob image touchpadSkin.add("touchKnob", new Texture("touchKnob.png")); touchpadStyle.background = touchpadSkin.getDrawable("touchBackground"); touchpadStyle.knob = touchpadSkin.getDrawable("touchKnob"); //Set background image // touchpadStyle.background = new Texture("data/touchBackground.png"); // //Set knob image // touchpadSkin.add("touchKnob", new Texture("data/touchKnob.png")); touchpad = new Touchpad(10, touchpadStyle); touchpadRight = new Touchpad(10, touchpadStyle); touchpad.setBounds(0, 0, Gdx.graphics.getWidth() / 4, Gdx.graphics.getWidth() / 4); touchpadRight.setBounds(Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4, 0, Gdx.graphics.getWidth() / 4, Gdx.graphics.getWidth() / 4); button = new Button(touchpadSkin.getDrawable("touchKnob")); button.setBounds(Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 9, Gdx.graphics.getWidth() / 4, Gdx.graphics.getWidth() / 10, Gdx.graphics.getWidth() / 10); button.addListener(new ClickListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { button1press = true; return true; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { button1press = false; } }); button2 = new Button(touchpadSkin.getDrawable("touchKnob")); button2.setBounds(Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4, Gdx.graphics.getWidth() / 4, Gdx.graphics.getWidth() / 10, Gdx.graphics.getWidth() / 10); button2.addListener(new ClickListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { button2press = true; return true; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { button2press = false; } }); stage = new Stage(); stage.addActor(touchpad); stage.addActor(touchpadRight); stage.addActor(button); stage.addActor(button2); // world.setContactListener(new ContactListener() { // // @Override // public void beginContact(Contact contact) { // contact.getFixtureA(); // if (contact.getFixtureB() == player.body.getFixtureList().get(0)){ // System.out.println("touch!"); // for(Door door:doors){ // if(contact.getFixtureA().equals(SceneBuilder.platform1.getFixtureList().get(0))){ // door.collidePlayer = true; // // } // } // } // } // // @Override // public void endContact(Contact contact) { // } // // @Override // public void preSolve(Contact contact, Manifold oldManifold) { // } // // @Override // public void postSolve(Contact contact, ContactImpulse impulse) { // } // }); Gdx.input.setInputProcessor(stage); // Gdx.input.setInputProcessor(inputM); } public static void draw(SpriteBatch batch) { // touchpad.draw(batch, opacity); stage.draw(); } @Override public void addedToEngine(Engine engine) { doors = engine .getEntitiesFor(Family.all(HingeComponent.class, BodyComponent.class, JointComponent.class).get()); players = engine.getEntitiesFor(Family.all(PlayerComponent.class).get()); } @Override public void update(float delta) { // System.out.println("Controls update. entities found:" + entities.size()); for (int i = 0; i < players.size(); i++) { Entity e = players.get(i); playerDirection = (float) Math.toDegrees(bm.get(e).body.getAngle()); BodyComponent body = bm.get(e); cameraLoc = body.body.getWorldCenter().cpy(); float angleR = body.body.getAngle(); Vector2 look = new Vector2((float) Math.cos(angleR), (float) Math.sin(angleR)); look.scl(3); cameraLoc.add(look); playerLoc = body.body.getWorldCenter().cpy(); // playerLoc.add(body.body.getAngle()) // FlashlightComponent flashlight = fm.get(e); // flashlight.flashlight.setDirection((float) Math.toDegrees(body.body.getAngle())); // flashlight.flashlight.setPosition(body.body.getWorldCenter()); if (touchpad.isTouched()) { Vector2 vel = new Vector2(touchpad.getKnobPercentX() * 400, touchpad.getKnobPercentY() * 400); // body.body.applyLinearForce(playerLoc, vel, true); body.body.applyForceToCenter(vel, true); // body.body.applyForceToCenter(touchpad.getKnobPercentX()*4, touchpad.getKnobPercentY()*4,true); // System.out.println("touched"); } // flashlight.flashlight.setDirection(body.body.getAngle()); if (touchpadRight.isTouched()) { double angle = Math.atan2(touchpadRight.getKnobPercentY(), touchpadRight.getKnobPercentX()); body.body.setTransform(playerLoc.x, playerLoc.y, (float) angle); } if (button1press) { button.setChecked(false); if (collisionM.has(e)) { for (Entity door : doors) if (collisionM.has(door) && playersC.get(players.first()).grab == null) { // door.getComponent(JointComponent.class).toggleLock(); If we want to lock the door; RopeJointDef grab = new RopeJointDef(); grab.bodyA = bm.get(door).body; grab.localAnchorA.set(collisionM.get(e).manifold.getPoints()[1].x, collisionM.get(e).manifold.getPoints()[1].y); grab.localAnchorB.set(collisionM.get(door).manifold.getPoints()[1].x, collisionM.get(e).manifold.getPoints()[1].y); grab.bodyB = bm.get(players.first()).body; grab.maxLength = 50 / PPM; grab.collideConnected = true; playersC.get(players.first()).grab(world.createJoint(grab)); } } } else { if (playersC.get(e).grab != null) { world.destroyJoint(playersC.get(e).grab); playersC.get(e).endGrab(); System.out.println("button release"); } } if (button2press) { button.setChecked(false); EntityManager .add(new Entity().add(new BodyComponent(Bullet.getBullet(bm.get(players.first()).body))) .add(new BulletComponent())); } } } }