Advertisement
Guest User

Pinball Engine Class - Android / Box2D

a guest
Jan 8th, 2013
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.04 KB | None | 0 0
  1. package com.diesel.pinball.twod.model;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.g2d.Sprite;
  5. import com.badlogic.gdx.math.Vector2;
  6. import com.badlogic.gdx.physics.box2d.Body;
  7. import com.badlogic.gdx.physics.box2d.BodyDef;
  8. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  9. import com.badlogic.gdx.physics.box2d.Fixture;
  10. import com.badlogic.gdx.physics.box2d.FixtureDef;
  11. import com.badlogic.gdx.physics.box2d.MassData;
  12. import com.badlogic.gdx.physics.box2d.PolygonShape;
  13. import com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef;
  14. import com.diesel.pinball.controllers.AudioController;
  15. import com.diesel.pinball.controllers.PhysicsController;
  16. import com.diesel.pinball.controllers.PinballEngineController;
  17. import com.diesel.pinball.controllers.TextureManager;
  18. import com.diesel.pinball.twod.box2d.data.BodyData;
  19. import com.diesel.pinball.twod.enums.Enums.CameraRequestType;
  20. import com.diesel.pinball.twod.enums.Enums.PinballBodyType;
  21. import com.diesel.pinball.twod.events.CameraRequestEvent;
  22. import com.diesel.pinball.twod.events.CollisionEndedEvent;
  23. import com.diesel.pinball.twod.events.CollisionReceivedEvent;
  24. import com.diesel.pinball.twod.events.PhysicsSolverEvent;
  25. import com.diesel.pinball.twod.events.listeners.CollisionEndedEventListener;
  26. import com.diesel.pinball.twod.events.listeners.CollisionReceivedEventListener;
  27. import com.diesel.pinball.twod.events.listeners.PhysicsSolverEventListener;
  28. import com.diesel.pinball.twod.utils.measurement.AspectRatio;
  29. import com.diesel.pinball.twod.utils.measurement.Meter;
  30. import com.diesel.pinball.twod.utils.measurement.Pixel;
  31. import com.google.gson.annotations.Expose;
  32.  
  33. public class Plunger implements PinballObject {
  34.  
  35.  
  36.     @Expose private String id;
  37.     @Expose private float heightPixels;
  38.     @Expose private float widthPixels;
  39.     private float heightMeters;
  40.     private float widthMeters;
  41.     @Expose private float pixelsX;
  42.     @Expose private float pixelsY;
  43.     @Expose private float rotationDegrees;
  44.     private Vector2 position;
  45.    
  46.     @Expose private int plungerHandleTextureWidth = 39;
  47.     @Expose private int plungerHandleTextureHeight = 60;
  48.     @Expose private int plungerHandleTextureU = 0;
  49.     @Expose private int plungerHandleTextureV = 0;
  50.    
  51.     @Expose private int plungerSpringTextureWidth = 46;
  52.     @Expose private int plungerSpringTextureHeight = 155;
  53.     @Expose private int plungerSpringTextureU = 0;
  54.     @Expose private int plungerSpringTextureV = 65;
  55.  
  56.     @Expose private int plungerBGTextureWidth = 60;
  57.     @Expose private int plungerBGTextureHeight = 220;
  58.     @Expose private int plungerBGTextureU = 47;
  59.     @Expose private int plungerBGTextureV = 0;
  60.    
  61.     @Expose private String soundUrl;
  62.     @Expose private String plungerTextureUrl;
  63.    
  64.     private Body plungerAnchorBody;
  65.     private Body plungerBody;
  66.     private Body plungerGuardTopLeftBody;
  67.     private Body plungerGuardTopRightBody;
  68.     private Body plungerSensorBody;
  69.    
  70.     private Sprite plungerHandleSprite;
  71.     private Sprite plungerSpringSprite;
  72.     private Sprite plungerBGSprite;
  73.    
  74.     private Vector2 anchorPosition;
  75.     private CollisionReceivedEventListener collisionReceivedEventListener;
  76.     private CollisionEndedEventListener collisionEndedEventListener;
  77.     //private Texture plungerTexture;
  78.    
  79.     public float getHeightPixels() {
  80.         return AspectRatio.adjustY(this.heightPixels, PhysicsController.PHYSICS_SCALE_Y);
  81.     }
  82.  
  83.     public float getWidthPixels() {
  84.         return AspectRatio.adjustX(this.widthPixels, PhysicsController.PHYSICS_SCALE_X);
  85.     }
  86.  
  87.     public float getPixelsX() {
  88.         return  AspectRatio.adjustX(this.pixelsX, PhysicsController.PHYSICS_SCALE_X);
  89.     }
  90.  
  91.     public float getPixelsY() {
  92.         return AspectRatio.adjustY(this.pixelsY, PhysicsController.PHYSICS_SCALE_Y);
  93.     }
  94.  
  95.     public Plunger(String id, float posXPixels, float posYPixels, float heightPixels, float widthPixels, float rotDegrees, String plungerTextureUrl) {
  96.  
  97.         this.id = id;
  98.         this.heightPixels = heightPixels;
  99.         this.widthPixels = widthPixels;
  100.        
  101.         this.pixelsX = posXPixels;
  102.         this.pixelsY = posYPixels;
  103.         this.rotationDegrees = rotDegrees;
  104.        
  105.         this.plungerTextureUrl = plungerTextureUrl;
  106.        
  107.         initialize();
  108.        
  109.     }
  110.    
  111.     public Vector2 getPlungerBodyPosition() {
  112.         if(plungerBody!=null) return plungerBody.getPosition();
  113.         return null;
  114.     }
  115.    
  116.     private void handleEndCollision(CollisionEndedEvent evt) {
  117.         Fixture fixa = evt.contact.getFixtureA();
  118.         Body body = fixa.getBody();
  119.         Body body2 = evt.contact.getFixtureB().getBody();
  120.  
  121.         if(body.getUserData()!=null) {
  122.             BodyData data = (BodyData) body.getUserData();
  123.             if(data.getBodyType().equals(PinballBodyType.PLUNGER_SENSOR) && data.getBodyId().equals(this.id)) {
  124.                 BodyData b2data = (BodyData) body2.getUserData();
  125.                 if(b2data!=null && b2data.getBodyType().equals(PinballBodyType.BALL)) {
  126.                     //Gdx.app.log("UNFOCUS", "request unfocus");
  127.                     CameraRequestEvent crevt = new CameraRequestEvent(new Object());
  128.                     crevt.targetZoom = 1f;
  129.                     crevt.requestType = CameraRequestType.UNFOCUS;
  130.                     PinballEngineController.CURRENT_APPLICATION.fireCameraRequestEvent(crevt);
  131.                 }
  132.             }
  133.         }
  134.     }
  135.    
  136.     private void handleCollision(CollisionReceivedEvent evt) {
  137.         Fixture fixa = evt.contact.getFixtureA();
  138.         Body body = fixa.getBody();
  139.         Body body2 = evt.contact.getFixtureB().getBody();
  140.  
  141.         if(body.getUserData()!=null) {
  142.             BodyData data = (BodyData) body.getUserData();
  143.             //Gdx.app.log("COLLISION", ""+data.getBodyType());
  144.             if(data.getBodyType().equals(PinballBodyType.PLUNGER_SENSOR) && data.getBodyId().equals(this.id)) {
  145.                 BodyData b2data = (BodyData) body2.getUserData();
  146.                 if(b2data!=null && b2data.getBodyType().equals(PinballBodyType.BALL)) {
  147.                     CameraRequestEvent crevt = new CameraRequestEvent(new Object());
  148.                     crevt.requestType = CameraRequestType.FOCUS;
  149.                     crevt.targetZoom = 0.5f;
  150.                     crevt.position = plungerBody.getPosition();
  151.                     PinballEngineController.CURRENT_APPLICATION.fireCameraRequestEvent(crevt);
  152.                 }
  153.             }else if(data.getBodyType().equals(PinballBodyType.PLUNGER)) {
  154.                 //Gdx.app.log("COLLISION", body.getLinearVelocity().y+"");
  155.                 if(body.getLinearVelocity().y>0) {
  156.                     AudioController.playSound(soundUrl);
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.    
  163.     @Override
  164.     public void render() {
  165.         /**/
  166.         Vector2 plungerHandlePosition = this.plungerBody.getPosition(); // that's the box's center position
  167.        
  168.         float pixelX = plungerHandlePosition.x;// Meter.toPixel(position.x);//(this.facing == Facing.LEFT) ? Meter.toPixel(position.x) : -Meter.toPixel(position.x);
  169.         float pixelY = plungerHandlePosition.y;//Meter.toPixel(position.y);
  170.        
  171.         //plungerHandleSprite.setPosition(pixelX, pixelY);
  172.         //ballSprite.setColor(getBallColor(ballMode));
  173.        
  174.         //Gdx.app.log("PLUNGER", "pixY:"+pixelY+" posY:"+position.y);
  175.        
  176.         plungerBGSprite.setBounds(position.x, position.y+Pixel.toMeter(4), plungerBGTextureWidth, plungerBGTextureHeight);
  177.         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);
  178.         plungerSpringSprite.setBounds(position.x, position.y-Pixel.toMeter(10), this.plungerSpringTextureWidth, this.plungerSpringTextureHeight+Meter.toPixel(pixelY)-100);//+Meter.toPixel(pixelY-position.y));
  179.          
  180.         plungerBGSprite.draw(PinballEngineController.SPRITE_BATCH);
  181.         plungerSpringSprite.draw(PinballEngineController.SPRITE_BATCH);
  182.         plungerHandleSprite.draw(PinballEngineController.SPRITE_BATCH);
  183.        
  184.     }
  185.    
  186.     @Override
  187.     public void initialize() {
  188.         initializeGraphics();
  189.         initializePhysics();
  190.     }
  191.  
  192.     @Override
  193.     public void initializeGraphics() {
  194.  
  195.         //this.widthPixels = AspectRatio.adjustX(this.widthPixels, PhysicsController.PHYSICS_SCALE_X);
  196.         //this.heightPixels = AspectRatio.adjustY(this.heightPixels, PhysicsController.PHYSICS_SCALE_Y);
  197.         //this.pixelsX = AspectRatio.adjustX(this.pixelsX, PhysicsController.PHYSICS_SCALE_X);
  198.         //this.pixelsY = AspectRatio.adjustY(this.pixelsY, PhysicsController.PHYSICS_SCALE_Y);
  199.        
  200.         this.soundUrl = "sound_launch_slide.ogg";
  201.         //AudioController.addSound(soundUrl);
  202.        
  203.         float sin = (float) Math.sin(Math.toRadians(this.rotationDegrees));
  204.         float cos = (float) Math.cos(Math.toRadians(this.rotationDegrees));
  205.        
  206.         float adjx = (getHeightPixels())*sin;
  207.         float adjy = (getHeightPixels())*cos;
  208.  
  209.         this.widthMeters = Pixel.toMeter(getWidthPixels());
  210.         this.heightMeters = Pixel.toMeter(getHeightPixels());
  211.         this.anchorPosition = new Vector2(Pixel.toMeter(getPixelsX()), Pixel.toMeter(getPixelsY()));
  212.        
  213.         this.position = new Vector2(Pixel.toMeter(getPixelsX()+adjx+(sin*(getWidthPixels()))), Pixel.toMeter(getPixelsY()+adjy));
  214.        
  215.         collisionReceivedEventListener = new CollisionReceivedEventListener() {
  216.             @Override
  217.             public void collisionOccurred(CollisionReceivedEvent evt) {
  218.                 handleCollision(evt);
  219.             }
  220.         };
  221.        
  222.         PinballEngineController.CURRENT_APPLICATION.addCollisionReceivedEventListener(collisionReceivedEventListener);
  223.        
  224.         collisionEndedEventListener = new CollisionEndedEventListener() {
  225.             @Override
  226.             public void endCollisionOccurred(CollisionEndedEvent evt) {
  227.                 handleEndCollision(evt);
  228.             }
  229.         };
  230.        
  231.         PinballEngineController.CURRENT_APPLICATION.addCollisionEndedEventListener(collisionEndedEventListener);
  232.  
  233.        
  234.         this.plungerHandleSprite = new Sprite();
  235.         this.plungerHandleSprite.setScale(Meter.METERS_PER_PIXEL/2, Meter.METERS_PER_PIXEL/2);
  236.        
  237. //      plungerTexture = new Texture(Gdx.files.internal(this.plungerTextureUrl));
  238. //      plungerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  239. //     
  240.         this.plungerHandleSprite.setTexture(TextureManager.getTexture(this.plungerTextureUrl));
  241.         this.plungerHandleSprite.setOrigin(-Pixel.toMeter(this.plungerHandleTextureWidth/4), -Pixel.toMeter(this.plungerHandleTextureHeight/4));
  242.         this.plungerHandleSprite.setRegion(this.plungerHandleTextureU, this.plungerHandleTextureV, this.plungerHandleTextureWidth, this.plungerHandleTextureHeight);
  243.         this.plungerHandleSprite.setPosition(0, 0);
  244.  
  245.         this.plungerSpringSprite = new Sprite();
  246.         this.plungerSpringSprite.setOrigin(-Pixel.toMeter(this.plungerSpringTextureWidth/4), -Pixel.toMeter((this.plungerSpringTextureHeight)/4));
  247.         this.plungerSpringSprite.setScale(Meter.METERS_PER_PIXEL/2, Meter.METERS_PER_PIXEL/2);
  248.         this.plungerSpringSprite.setTexture(TextureManager.getTexture(this.plungerTextureUrl));
  249.         this.plungerSpringSprite.setRegion(this.plungerSpringTextureU, this.plungerSpringTextureV, this.plungerSpringTextureWidth, this.plungerSpringTextureHeight);
  250.        
  251.         this.plungerBGSprite = new Sprite();
  252.         this.plungerBGSprite.setOrigin(-Pixel.toMeter(this.plungerBGTextureWidth/4), -Pixel.toMeter(this.plungerBGTextureHeight/4));
  253.         this.plungerBGSprite.setScale(Meter.METERS_PER_PIXEL/2, Meter.METERS_PER_PIXEL/2);
  254.         this.plungerBGSprite.setTexture(TextureManager.getTexture(this.plungerTextureUrl));
  255.         this.plungerBGSprite.setRegion(this.plungerBGTextureU, this.plungerBGTextureV, this.plungerBGTextureWidth, this.plungerBGTextureHeight);
  256.  
  257.         //initializePhysics();
  258.     }
  259.    
  260.     @Override
  261.     public void initializePhysics() {
  262.            
  263.         float sin = (float) Math.sin(Math.toRadians(this.rotationDegrees));
  264.         float cos = (float) Math.cos(Math.toRadians(this.rotationDegrees));
  265.        
  266.         PolygonShape plungerSensorShape = new PolygonShape();
  267.         float sensorHeight = this.heightMeters;
  268.         plungerSensorShape.setAsBox(this.widthMeters, sensorHeight);
  269.         BodyDef plungerSensorBodyDef = new BodyDef();
  270.         plungerSensorBodyDef.position.set(this.position.x, (this.position.y+this.heightMeters+sensorHeight));
  271.         plungerSensorBodyDef.type = BodyType.StaticBody;
  272.        
  273.         plungerSensorBody = PhysicsController.CURRENT_WORLD.createBody(plungerSensorBodyDef);
  274.         Fixture plungerSensorFixture = plungerSensorBody.createFixture(plungerSensorShape, 1f);
  275.         plungerSensorFixture.setSensor(true);
  276.         plungerSensorShape.dispose();
  277.        
  278.         BodyData bodydata = new BodyData(this.id, PinballBodyType.PLUNGER_SENSOR, 0, plungerSensorBody);
  279.         plungerSensorBody.setUserData(bodydata);
  280.  
  281.         PolygonShape plungerTopGuardLeft = new PolygonShape();
  282.         plungerTopGuardLeft.setAsBox(Pixel.toMeter(AspectRatio.adjustX(1, PhysicsController.PHYSICS_SCALE_X)), Pixel.toMeter(AspectRatio.adjustY(10, PhysicsController.PHYSICS_SCALE_Y)));
  283.         BodyDef plungerTopGuardLeftBodyDef = new BodyDef();
  284.         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))));
  285.         plungerTopGuardLeftBodyDef.type = BodyType.StaticBody;
  286.         plungerGuardTopLeftBody = PhysicsController.CURRENT_WORLD.createBody(plungerTopGuardLeftBodyDef);
  287.         plungerGuardTopLeftBody.createFixture(plungerTopGuardLeft, 1f);
  288.         plungerTopGuardLeft.dispose();
  289.        
  290.         PolygonShape plungerTopGuardRight = new PolygonShape();
  291.         plungerTopGuardRight.setAsBox(Pixel.toMeter(AspectRatio.adjustX(1, PhysicsController.PHYSICS_SCALE_X)), Pixel.toMeter(AspectRatio.adjustY(10, PhysicsController.PHYSICS_SCALE_Y)));
  292.         BodyDef plungerTopGuardRightBodyDef = new BodyDef();
  293.         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))));
  294.         plungerTopGuardRightBodyDef.type = BodyType.StaticBody;
  295.         plungerGuardTopRightBody = PhysicsController.CURRENT_WORLD.createBody(plungerTopGuardRightBodyDef);
  296.         plungerGuardTopRightBody.createFixture(plungerTopGuardRight, 1f);
  297.         plungerTopGuardRight.dispose();
  298.        
  299.        
  300.         PolygonShape plungerShape = new PolygonShape();
  301.         plungerShape.setAsBox(this.widthMeters/2, Pixel.toMeter(AspectRatio.adjustY(15, PhysicsController.PHYSICS_SCALE_Y)), new Vector2(0,0), (float)Math.toRadians(this.rotationDegrees));
  302.        
  303.         BodyDef plungerBodyDef = new BodyDef();
  304.         plungerBodyDef.position.set(this.position);//.set(0,0);
  305.         plungerBodyDef.type = BodyType.DynamicBody;
  306.        
  307.         plungerBody = PhysicsController.CURRENT_WORLD.createBody(plungerBodyDef);
  308.         MassData data = new MassData();
  309.         data.mass = 5f;
  310.         plungerBody.setMassData(data);
  311.         BodyData bodyData = new BodyData(this.id, PinballBodyType.PLUNGER, 20f);
  312.         plungerBody.setUserData(bodyData);
  313.        
  314.         FixtureDef plungerFixDef = new FixtureDef();
  315.         plungerFixDef.density = 1f;//2f;
  316.         plungerFixDef.shape = plungerShape;
  317.        
  318.         Fixture plungerBodyFixture = plungerBody.createFixture(plungerFixDef);
  319.         //plungerBodyFixture.setRestitution(0.25f);
  320.         plungerShape.dispose();
  321.  
  322.         PolygonShape plungerAnchorShape = new PolygonShape();
  323.         plungerAnchorShape.setAsBox(this.widthMeters, this.heightMeters/4, new Vector2(0,0), (float)Math.toRadians(this.rotationDegrees));
  324.        
  325.         BodyDef plungerAnchorBodyDef = new BodyDef();
  326.         plungerAnchorBodyDef.position.set(this.anchorPosition);
  327.         plungerAnchorBodyDef.type = BodyType.StaticBody;
  328.        
  329.         plungerAnchorBody = PhysicsController.CURRENT_WORLD.createBody(plungerAnchorBodyDef);
  330.        
  331.         //BodyData bodydata = new BodyData(this.id, PinballBodyType.UNIDIRECTIONAL_GATE, 0, gateBody);
  332.         //plungerAnchorBody.setUserData(bodydata);
  333.        
  334.         FixtureDef plungerAnchorFixDef = new FixtureDef();
  335.         plungerAnchorFixDef.density = 1f;//2f;
  336.         plungerAnchorFixDef.shape = plungerAnchorShape;
  337.         //plungerAnchorFixDef.isSensor = true;
  338.        
  339.         plungerAnchorBody.createFixture(plungerAnchorFixDef);
  340.         plungerAnchorShape.dispose();
  341.  
  342.         PrismaticJointDef jointDef = new PrismaticJointDef();
  343.         jointDef.initialize(plungerBody, plungerAnchorBody, plungerBody.getWorldCenter(), new Vector2(sin,-cos));//sin,cos));//sin,cos));
  344.         jointDef.lowerTranslation = 0;//(float) Math.pow(sin, 2);//sin;//0;//Pixel.toMeter((float) (sin*(this.widthPixels)));
  345.         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)));
  346.         jointDef.enableLimit = true;
  347.         jointDef.enableMotor = true;
  348.         jointDef.maxMotorForce = 200f;
  349.         jointDef.motorSpeed = 200f;
  350.         jointDef.collideConnected = false;
  351.        
  352.         PhysicsController.CURRENT_WORLD.createJoint(jointDef);
  353.     }
  354.    
  355.     @Override
  356.     public void dispose() {
  357.         if(collisionReceivedEventListener!=null) {
  358.             PinballEngineController.CURRENT_APPLICATION.removeCollisionReceivedEventListener(collisionReceivedEventListener);
  359.             collisionReceivedEventListener = null;
  360.         }  
  361.         if(collisionEndedEventListener!=null) {
  362.             PinballEngineController.CURRENT_APPLICATION.removeCollisionEndedEventListener(collisionEndedEventListener);
  363.             collisionEndedEventListener = null;
  364.         }
  365.  
  366.     }
  367.  
  368.     @Override
  369.     public String[] getAudioReferences() {
  370.         return new String[] { this.soundUrl };
  371.     }
  372.    
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement