SHARE
TWEET

Untitled

a guest Jun 30th, 2017 144 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.sacred.andengine;
  2.  
  3. import org.anddev.andengine.engine.Engine;
  4. import org.anddev.andengine.engine.LimitedFPSEngine;
  5. import org.anddev.andengine.engine.camera.Camera;
  6. import org.anddev.andengine.engine.options.EngineOptions;
  7. import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
  8. import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
  9. import org.anddev.andengine.entity.primitive.Rectangle;
  10. import org.anddev.andengine.entity.scene.Scene;
  11. import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
  12. import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
  13. import org.anddev.andengine.entity.scene.Scene.ITouchArea;
  14. import org.anddev.andengine.entity.scene.background.ColorBackground;
  15. import org.anddev.andengine.entity.shape.Shape;
  16. import org.anddev.andengine.entity.sprite.Sprite;
  17. import org.anddev.andengine.entity.util.FPSLogger;
  18. import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
  19. import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
  20. import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
  21. import org.anddev.andengine.input.touch.TouchEvent;
  22. import org.anddev.andengine.ui.activity.BaseGameActivity;
  23.  
  24.  
  25.  
  26. import com.badlogic.gdx.math.Vector2;
  27. import com.badlogic.gdx.physics.box2d.FixtureDef;
  28. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  29.  
  30. /**
  31.  * @author Nicolas Gramlich
  32.  * @since 18:47:08 - 19.03.2010
  33.  */                                                         //IAccelerometerListener,
  34. public class AndEngine extends BaseGameActivity implements IOnSceneTouchListener, IOnAreaTouchListener {
  35.        
  36.         /* Constants */
  37.         private static final int CAMERA_WIDTH = 480; // 720
  38.         private static final int CAMERA_HEIGHT = 320; // 480
  39.  
  40.         /* Fields */
  41.         //private Texture mTexture;
  42.  
  43.         //private TextureRegion tmpTextureRegion;
  44.        // private TextureRegion tmp2TextureRegion;
  45.  
  46.         private PhysicsWorld mPhysicsWorld;
  47.         //
  48.        
  49.         // Managers
  50.         private ObjectManager objectManager;
  51.         private TexturesManager texturesManager;
  52.        
  53.         /* Methods */
  54.         @Override
  55.         public Engine onLoadEngine() {
  56.  
  57.            
  58.             final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  59. //              LimitedFPSEngine engine = new LimitedFPSEngine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera), 60);
  60.  
  61.             final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
  62.             engineOptions.getTouchOptions().setRunOnUpdateThread(true);
  63.  
  64.             return new Engine(engineOptions);
  65.            
  66.         }
  67.  
  68.         @Override
  69.         public void onLoadResources() {
  70.             /* initialize texture manager */
  71.             texturesManager = new TexturesManager( this, mEngine);
  72.         }
  73.  
  74.         @Override
  75.         public Scene onLoadScene() {
  76.                 this.mEngine.registerUpdateHandler(new FPSLogger());
  77.  
  78.                 final Scene scene = new Scene(2);
  79.                 scene.setBackground(new ColorBackground( 255, 255, 255 ) );
  80.                 scene.setOnSceneTouchListener(this);
  81.                
  82. //                this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 5), false);
  83.                 this.mPhysicsWorld = new FixedStepPhysicsWorld( 30, new Vector2(0, 5 ), false, 3, 2 );
  84.  
  85.  
  86.  
  87.                 final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
  88.                 final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
  89.                 final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
  90.                 final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
  91.  
  92.                 final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
  93.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
  94.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
  95.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  96.                 PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  97.  
  98.                 scene.getBottomLayer().addEntity(ground);
  99.                 scene.getBottomLayer().addEntity(roof);
  100.                 scene.getBottomLayer().addEntity(left);
  101.                 scene.getBottomLayer().addEntity(right);
  102.  
  103.                 scene.registerUpdateHandler(this.mPhysicsWorld);
  104.  
  105.                 scene.setOnAreaTouchListener(this);
  106.  
  107.                 return scene;
  108.         }
  109.  
  110.         public void onLoadComplete() {
  111.             /* initialize object manager */
  112.             objectManager = new ObjectManager();
  113.            
  114.             //
  115.             // []   []   []
  116.             // []   []   []
  117.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 100, 296 );
  118.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 100, 276 );
  119.            
  120.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 160, 296 );
  121.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 160, 276 );
  122.            
  123.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 220, 296 );
  124.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 220, 276 );
  125.            
  126.             //
  127.             // [         ]
  128.             //
  129.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("w_128x16"), 105, 260 );
  130.            
  131.             //  [ ]    [ ]
  132.             //  [ ]    [ ]
  133.             //  [ ]    [ ]
  134.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("w_16x128"), 130, 132 );
  135.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("w_16x128"), 190, 132 );
  136.            
  137.             //
  138.             // [         ]
  139.             //
  140.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("w_128x16"), 105, 116 );
  141.            
  142.             //
  143.             //
  144.             //
  145.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 130, 94 );
  146.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 150, 94 );
  147.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 170, 94 );
  148.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 190, 94 );
  149.            
  150.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 140, 74 );
  151.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 160, 74 );
  152.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 180, 74 );
  153.            
  154.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 150, 54 );
  155.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 170, 54 );
  156.            
  157.             objectManager.CreateBox( getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), 160, 34 );
  158.         }
  159.        
  160.         @Override
  161.         public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  162.                 if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
  163.                         objectManager.removeObject((Sprite)pTouchArea, mEngine, mPhysicsWorld );
  164.                         return true;
  165.                 }
  166.  
  167.                 return false;
  168.         }
  169.  
  170.         @Override
  171.         public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  172.                 if(this.mPhysicsWorld != null) {
  173.                         if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
  174.                                 objectManager.CreateBox( this.getEngine(), mPhysicsWorld, texturesManager.GetTextureRegion("s_20x20"), pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
  175.                                 return true;
  176.                         }
  177.                 }
  178.                 return false;
  179.         }
  180.  
  181. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top