Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.diesel.pinball.twod.model;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- 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.Fixture;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.MassData;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef;
- import com.diesel.pinball.controllers.AudioController;
- import com.diesel.pinball.controllers.PhysicsController;
- import com.diesel.pinball.controllers.PinballEngineController;
- import com.diesel.pinball.controllers.TextureManager;
- import com.diesel.pinball.twod.box2d.data.BodyData;
- import com.diesel.pinball.twod.enums.Enums.CameraRequestType;
- import com.diesel.pinball.twod.enums.Enums.PinballBodyType;
- import com.diesel.pinball.twod.events.CameraRequestEvent;
- import com.diesel.pinball.twod.events.CollisionEndedEvent;
- import com.diesel.pinball.twod.events.CollisionReceivedEvent;
- import com.diesel.pinball.twod.events.PhysicsSolverEvent;
- import com.diesel.pinball.twod.events.listeners.CollisionEndedEventListener;
- import com.diesel.pinball.twod.events.listeners.CollisionReceivedEventListener;
- import com.diesel.pinball.twod.events.listeners.PhysicsSolverEventListener;
- import com.diesel.pinball.twod.utils.measurement.AspectRatio;
- import com.diesel.pinball.twod.utils.measurement.Meter;
- import com.diesel.pinball.twod.utils.measurement.Pixel;
- import com.google.gson.annotations.Expose;
- public class Plunger implements PinballObject {
- @Expose private String id;
- @Expose private float heightPixels;
- @Expose private float widthPixels;
- private float heightMeters;
- private float widthMeters;
- @Expose private float pixelsX;
- @Expose private float pixelsY;
- @Expose private float rotationDegrees;
- private Vector2 position;
- @Expose private int plungerHandleTextureWidth = 39;
- @Expose private int plungerHandleTextureHeight = 60;
- @Expose private int plungerHandleTextureU = 0;
- @Expose private int plungerHandleTextureV = 0;
- @Expose private int plungerSpringTextureWidth = 46;
- @Expose private int plungerSpringTextureHeight = 155;
- @Expose private int plungerSpringTextureU = 0;
- @Expose private int plungerSpringTextureV = 65;
- @Expose private int plungerBGTextureWidth = 60;
- @Expose private int plungerBGTextureHeight = 220;
- @Expose private int plungerBGTextureU = 47;
- @Expose private int plungerBGTextureV = 0;
- @Expose private String soundUrl;
- @Expose private String plungerTextureUrl;
- private Body plungerAnchorBody;
- private Body plungerBody;
- private Body plungerGuardTopLeftBody;
- private Body plungerGuardTopRightBody;
- private Body plungerSensorBody;
- private Sprite plungerHandleSprite;
- private Sprite plungerSpringSprite;
- private Sprite plungerBGSprite;
- private Vector2 anchorPosition;
- private CollisionReceivedEventListener collisionReceivedEventListener;
- private CollisionEndedEventListener collisionEndedEventListener;
- //private Texture plungerTexture;
- public float getHeightPixels() {
- return AspectRatio.adjustY(this.heightPixels, PhysicsController.PHYSICS_SCALE_Y);
- }
- public float getWidthPixels() {
- return AspectRatio.adjustX(this.widthPixels, PhysicsController.PHYSICS_SCALE_X);
- }
- public float getPixelsX() {
- return AspectRatio.adjustX(this.pixelsX, PhysicsController.PHYSICS_SCALE_X);
- }
- public float getPixelsY() {
- return AspectRatio.adjustY(this.pixelsY, PhysicsController.PHYSICS_SCALE_Y);
- }
- public Plunger(String id, float posXPixels, float posYPixels, float heightPixels, float widthPixels, float rotDegrees, String plungerTextureUrl) {
- this.id = id;
- this.heightPixels = heightPixels;
- this.widthPixels = widthPixels;
- this.pixelsX = posXPixels;
- this.pixelsY = posYPixels;
- this.rotationDegrees = rotDegrees;
- this.plungerTextureUrl = plungerTextureUrl;
- initialize();
- }
- public Vector2 getPlungerBodyPosition() {
- if(plungerBody!=null) return plungerBody.getPosition();
- return null;
- }
- private void handleEndCollision(CollisionEndedEvent evt) {
- Fixture fixa = evt.contact.getFixtureA();
- Body body = fixa.getBody();
- Body body2 = evt.contact.getFixtureB().getBody();
- if(body.getUserData()!=null) {
- BodyData data = (BodyData) body.getUserData();
- if(data.getBodyType().equals(PinballBodyType.PLUNGER_SENSOR) && data.getBodyId().equals(this.id)) {
- BodyData b2data = (BodyData) body2.getUserData();
- if(b2data!=null && b2data.getBodyType().equals(PinballBodyType.BALL)) {
- //Gdx.app.log("UNFOCUS", "request unfocus");
- CameraRequestEvent crevt = new CameraRequestEvent(new Object());
- crevt.targetZoom = 1f;
- crevt.requestType = CameraRequestType.UNFOCUS;
- PinballEngineController.CURRENT_APPLICATION.fireCameraRequestEvent(crevt);
- }
- }
- }
- }
- private void handleCollision(CollisionReceivedEvent evt) {
- Fixture fixa = evt.contact.getFixtureA();
- Body body = fixa.getBody();
- Body body2 = evt.contact.getFixtureB().getBody();
- if(body.getUserData()!=null) {
- BodyData data = (BodyData) body.getUserData();
- //Gdx.app.log("COLLISION", ""+data.getBodyType());
- if(data.getBodyType().equals(PinballBodyType.PLUNGER_SENSOR) && data.getBodyId().equals(this.id)) {
- BodyData b2data = (BodyData) body2.getUserData();
- if(b2data!=null && b2data.getBodyType().equals(PinballBodyType.BALL)) {
- CameraRequestEvent crevt = new CameraRequestEvent(new Object());
- crevt.requestType = CameraRequestType.FOCUS;
- crevt.targetZoom = 0.5f;
- crevt.position = plungerBody.getPosition();
- PinballEngineController.CURRENT_APPLICATION.fireCameraRequestEvent(crevt);
- }
- }else if(data.getBodyType().equals(PinballBodyType.PLUNGER)) {
- //Gdx.app.log("COLLISION", body.getLinearVelocity().y+"");
- if(body.getLinearVelocity().y>0) {
- AudioController.playSound(soundUrl);
- }
- }
- }
- }
- @Override
- public void render() {
- /**/
- Vector2 plungerHandlePosition = this.plungerBody.getPosition(); // that's the box's center position
- float pixelX = plungerHandlePosition.x;// Meter.toPixel(position.x);//(this.facing == Facing.LEFT) ? Meter.toPixel(position.x) : -Meter.toPixel(position.x);
- float pixelY = plungerHandlePosition.y;//Meter.toPixel(position.y);
- //plungerHandleSprite.setPosition(pixelX, pixelY);
- //ballSprite.setColor(getBallColor(ballMode));
- //Gdx.app.log("PLUNGER", "pixY:"+pixelY+" posY:"+position.y);
- plungerBGSprite.setBounds(position.x, position.y+Pixel.toMeter(4), plungerBGTextureWidth, plungerBGTextureHeight);
- plungerHandleSprite.setBounds(pixelX-Pixel.toMeter(2), pixelY+Pixel.toMeter(1), this.plungerHandleTextureWidth, this.plungerHandleTextureHeight);// -(Pixel.toMeter(this.size)/2), position.y-(Pixel.toMeter(this.size)/2), this.size, this.size);
- plungerSpringSprite.setBounds(position.x, position.y-Pixel.toMeter(10), this.plungerSpringTextureWidth, this.plungerSpringTextureHeight+Meter.toPixel(pixelY)-100);//+Meter.toPixel(pixelY-position.y));
- plungerBGSprite.draw(PinballEngineController.SPRITE_BATCH);
- plungerSpringSprite.draw(PinballEngineController.SPRITE_BATCH);
- plungerHandleSprite.draw(PinballEngineController.SPRITE_BATCH);
- }
- @Override
- public void initialize() {
- initializeGraphics();
- initializePhysics();
- }
- @Override
- public void initializeGraphics() {
- //this.widthPixels = AspectRatio.adjustX(this.widthPixels, PhysicsController.PHYSICS_SCALE_X);
- //this.heightPixels = AspectRatio.adjustY(this.heightPixels, PhysicsController.PHYSICS_SCALE_Y);
- //this.pixelsX = AspectRatio.adjustX(this.pixelsX, PhysicsController.PHYSICS_SCALE_X);
- //this.pixelsY = AspectRatio.adjustY(this.pixelsY, PhysicsController.PHYSICS_SCALE_Y);
- this.soundUrl = "sound_launch_slide.ogg";
- //AudioController.addSound(soundUrl);
- float sin = (float) Math.sin(Math.toRadians(this.rotationDegrees));
- float cos = (float) Math.cos(Math.toRadians(this.rotationDegrees));
- float adjx = (getHeightPixels())*sin;
- float adjy = (getHeightPixels())*cos;
- this.widthMeters = Pixel.toMeter(getWidthPixels());
- this.heightMeters = Pixel.toMeter(getHeightPixels());
- this.anchorPosition = new Vector2(Pixel.toMeter(getPixelsX()), Pixel.toMeter(getPixelsY()));
- this.position = new Vector2(Pixel.toMeter(getPixelsX()+adjx+(sin*(getWidthPixels()))), Pixel.toMeter(getPixelsY()+adjy));
- collisionReceivedEventListener = new CollisionReceivedEventListener() {
- @Override
- public void collisionOccurred(CollisionReceivedEvent evt) {
- handleCollision(evt);
- }
- };
- PinballEngineController.CURRENT_APPLICATION.addCollisionReceivedEventListener(collisionReceivedEventListener);
- collisionEndedEventListener = new CollisionEndedEventListener() {
- @Override
- public void endCollisionOccurred(CollisionEndedEvent evt) {
- handleEndCollision(evt);
- }
- };
- PinballEngineController.CURRENT_APPLICATION.addCollisionEndedEventListener(collisionEndedEventListener);
- this.plungerHandleSprite = new Sprite();
- this.plungerHandleSprite.setScale(Meter.METERS_PER_PIXEL/2, Meter.METERS_PER_PIXEL/2);
- // plungerTexture = new Texture(Gdx.files.internal(this.plungerTextureUrl));
- // plungerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- //
- this.plungerHandleSprite.setTexture(TextureManager.getTexture(this.plungerTextureUrl));
- this.plungerHandleSprite.setOrigin(-Pixel.toMeter(this.plungerHandleTextureWidth/4), -Pixel.toMeter(this.plungerHandleTextureHeight/4));
- this.plungerHandleSprite.setRegion(this.plungerHandleTextureU, this.plungerHandleTextureV, this.plungerHandleTextureWidth, this.plungerHandleTextureHeight);
- this.plungerHandleSprite.setPosition(0, 0);
- this.plungerSpringSprite = new Sprite();
- this.plungerSpringSprite.setOrigin(-Pixel.toMeter(this.plungerSpringTextureWidth/4), -Pixel.toMeter((this.plungerSpringTextureHeight)/4));
- this.plungerSpringSprite.setScale(Meter.METERS_PER_PIXEL/2, Meter.METERS_PER_PIXEL/2);
- this.plungerSpringSprite.setTexture(TextureManager.getTexture(this.plungerTextureUrl));
- this.plungerSpringSprite.setRegion(this.plungerSpringTextureU, this.plungerSpringTextureV, this.plungerSpringTextureWidth, this.plungerSpringTextureHeight);
- this.plungerBGSprite = new Sprite();
- this.plungerBGSprite.setOrigin(-Pixel.toMeter(this.plungerBGTextureWidth/4), -Pixel.toMeter(this.plungerBGTextureHeight/4));
- this.plungerBGSprite.setScale(Meter.METERS_PER_PIXEL/2, Meter.METERS_PER_PIXEL/2);
- this.plungerBGSprite.setTexture(TextureManager.getTexture(this.plungerTextureUrl));
- this.plungerBGSprite.setRegion(this.plungerBGTextureU, this.plungerBGTextureV, this.plungerBGTextureWidth, this.plungerBGTextureHeight);
- //initializePhysics();
- }
- @Override
- public void initializePhysics() {
- float sin = (float) Math.sin(Math.toRadians(this.rotationDegrees));
- float cos = (float) Math.cos(Math.toRadians(this.rotationDegrees));
- PolygonShape plungerSensorShape = new PolygonShape();
- float sensorHeight = this.heightMeters;
- plungerSensorShape.setAsBox(this.widthMeters, sensorHeight);
- BodyDef plungerSensorBodyDef = new BodyDef();
- plungerSensorBodyDef.position.set(this.position.x, (this.position.y+this.heightMeters+sensorHeight));
- plungerSensorBodyDef.type = BodyType.StaticBody;
- plungerSensorBody = PhysicsController.CURRENT_WORLD.createBody(plungerSensorBodyDef);
- Fixture plungerSensorFixture = plungerSensorBody.createFixture(plungerSensorShape, 1f);
- plungerSensorFixture.setSensor(true);
- plungerSensorShape.dispose();
- BodyData bodydata = new BodyData(this.id, PinballBodyType.PLUNGER_SENSOR, 0, plungerSensorBody);
- plungerSensorBody.setUserData(bodydata);
- PolygonShape plungerTopGuardLeft = new PolygonShape();
- plungerTopGuardLeft.setAsBox(Pixel.toMeter(AspectRatio.adjustX(1, PhysicsController.PHYSICS_SCALE_X)), Pixel.toMeter(AspectRatio.adjustY(10, PhysicsController.PHYSICS_SCALE_Y)));
- BodyDef plungerTopGuardLeftBodyDef = new BodyDef();
- plungerTopGuardLeftBodyDef.position.set(this.position.x-Pixel.toMeter(AspectRatio.adjustX(8, PhysicsController.PHYSICS_SCALE_X)), (this.position.y+this.heightMeters-Pixel.toMeter(AspectRatio.adjustY(15, PhysicsController.PHYSICS_SCALE_Y))));
- plungerTopGuardLeftBodyDef.type = BodyType.StaticBody;
- plungerGuardTopLeftBody = PhysicsController.CURRENT_WORLD.createBody(plungerTopGuardLeftBodyDef);
- plungerGuardTopLeftBody.createFixture(plungerTopGuardLeft, 1f);
- plungerTopGuardLeft.dispose();
- PolygonShape plungerTopGuardRight = new PolygonShape();
- plungerTopGuardRight.setAsBox(Pixel.toMeter(AspectRatio.adjustX(1, PhysicsController.PHYSICS_SCALE_X)), Pixel.toMeter(AspectRatio.adjustY(10, PhysicsController.PHYSICS_SCALE_Y)));
- BodyDef plungerTopGuardRightBodyDef = new BodyDef();
- plungerTopGuardRightBodyDef.position.set(this.position.x+Pixel.toMeter(AspectRatio.adjustX(8, PhysicsController.PHYSICS_SCALE_X)), (this.position.y+this.heightMeters-Pixel.toMeter(AspectRatio.adjustY(15, PhysicsController.PHYSICS_SCALE_Y))));
- plungerTopGuardRightBodyDef.type = BodyType.StaticBody;
- plungerGuardTopRightBody = PhysicsController.CURRENT_WORLD.createBody(plungerTopGuardRightBodyDef);
- plungerGuardTopRightBody.createFixture(plungerTopGuardRight, 1f);
- plungerTopGuardRight.dispose();
- PolygonShape plungerShape = new PolygonShape();
- plungerShape.setAsBox(this.widthMeters/2, Pixel.toMeter(AspectRatio.adjustY(15, PhysicsController.PHYSICS_SCALE_Y)), new Vector2(0,0), (float)Math.toRadians(this.rotationDegrees));
- BodyDef plungerBodyDef = new BodyDef();
- plungerBodyDef.position.set(this.position);//.set(0,0);
- plungerBodyDef.type = BodyType.DynamicBody;
- plungerBody = PhysicsController.CURRENT_WORLD.createBody(plungerBodyDef);
- MassData data = new MassData();
- data.mass = 5f;
- plungerBody.setMassData(data);
- BodyData bodyData = new BodyData(this.id, PinballBodyType.PLUNGER, 20f);
- plungerBody.setUserData(bodyData);
- FixtureDef plungerFixDef = new FixtureDef();
- plungerFixDef.density = 1f;//2f;
- plungerFixDef.shape = plungerShape;
- Fixture plungerBodyFixture = plungerBody.createFixture(plungerFixDef);
- //plungerBodyFixture.setRestitution(0.25f);
- plungerShape.dispose();
- PolygonShape plungerAnchorShape = new PolygonShape();
- plungerAnchorShape.setAsBox(this.widthMeters, this.heightMeters/4, new Vector2(0,0), (float)Math.toRadians(this.rotationDegrees));
- BodyDef plungerAnchorBodyDef = new BodyDef();
- plungerAnchorBodyDef.position.set(this.anchorPosition);
- plungerAnchorBodyDef.type = BodyType.StaticBody;
- plungerAnchorBody = PhysicsController.CURRENT_WORLD.createBody(plungerAnchorBodyDef);
- //BodyData bodydata = new BodyData(this.id, PinballBodyType.UNIDIRECTIONAL_GATE, 0, gateBody);
- //plungerAnchorBody.setUserData(bodydata);
- FixtureDef plungerAnchorFixDef = new FixtureDef();
- plungerAnchorFixDef.density = 1f;//2f;
- plungerAnchorFixDef.shape = plungerAnchorShape;
- //plungerAnchorFixDef.isSensor = true;
- plungerAnchorBody.createFixture(plungerAnchorFixDef);
- plungerAnchorShape.dispose();
- PrismaticJointDef jointDef = new PrismaticJointDef();
- jointDef.initialize(plungerBody, plungerAnchorBody, plungerBody.getWorldCenter(), new Vector2(sin,-cos));//sin,cos));//sin,cos));
- jointDef.lowerTranslation = 0;//(float) Math.pow(sin, 2);//sin;//0;//Pixel.toMeter((float) (sin*(this.widthPixels)));
- jointDef.upperTranslation = Pixel.toMeter(AspectRatio.adjustY(50, PhysicsController.PHYSICS_SCALE_Y));//this.heightMeters);//(float) cos;//Pixel.toMeter(sin);//cos;//this.widthMeters;//Pixel.toMeter(this.widthPixels/4);//Pixel.toMeter((float) (cos*(this.widthPixels)));
- jointDef.enableLimit = true;
- jointDef.enableMotor = true;
- jointDef.maxMotorForce = 200f;
- jointDef.motorSpeed = 200f;
- jointDef.collideConnected = false;
- PhysicsController.CURRENT_WORLD.createJoint(jointDef);
- }
- @Override
- public void dispose() {
- if(collisionReceivedEventListener!=null) {
- PinballEngineController.CURRENT_APPLICATION.removeCollisionReceivedEventListener(collisionReceivedEventListener);
- collisionReceivedEventListener = null;
- }
- if(collisionEndedEventListener!=null) {
- PinballEngineController.CURRENT_APPLICATION.removeCollisionEndedEventListener(collisionEndedEventListener);
- collisionEndedEventListener = null;
- }
- }
- @Override
- public String[] getAudioReferences() {
- return new String[] { this.soundUrl };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement