Advertisement
bekovski

textureatlas

Mar 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. package com.badlogic.androidgames.gamedev2d;
  2.  
  3. import framework.DynamicGameObject;
  4. import framework.Game;
  5. import framework.GameObject;
  6. import framework.Input.*;
  7. import framework.Screen;
  8. import framework.SpatialHashGrid;
  9. import framework.gl.Camera2D;
  10. import framework.gl.Texture;
  11. import framework.gl.Vertices;
  12. import framework.impl.GLGame;
  13. import framework.impl.GLGraphics;
  14. import framework.math.OverlapTester;
  15. import framework.math.Vector2;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20. import javax.microedition.khronos.opengles.GL10;
  21.  
  22. /**
  23.  * Created by Kebir on 16.03.2018.
  24.  */
  25. public class TextureAtlasTest extends GLGame {
  26.  
  27.     @Override
  28.     public Screen getStartScreen() {
  29.         return new TextureAtlasScreen(this);
  30.     }
  31.  
  32.     class TextureAtlasScreen extends Screen {
  33.  
  34.         final int NUM_TARGETS = 20;
  35.         final float WORLD_WIDTH = 9.6f;
  36.         final float WORLD_HEIGHT = 4.8f;
  37.  
  38.         Camera2D camera2D;
  39.         Texture texture;
  40.  
  41.         GLGraphics glGraphics;
  42.         Cannon cannon;
  43.         DynamicGameObject cannonBall;
  44.         List<GameObject> targets;
  45.         SpatialHashGrid grid;
  46.  
  47.         Vertices cannonVertices;
  48.         Vertices cannonBallVertices;
  49.         Vertices targetVertices;
  50.  
  51.         Vector2 touchPoint = new Vector2();
  52.         Vector2 gravity = new Vector2(0, -4);
  53.  
  54.  
  55.         public TextureAtlasScreen(Game game) {
  56.             super(game);
  57.  
  58.             glGraphics = ((GLGame)game).getGLGraphics();
  59.             camera2D = new Camera2D(glGraphics, WORLD_WIDTH, WORLD_HEIGHT);
  60.  
  61.             cannon = new Cannon(0, 0, 1, 0.5f);
  62.             cannonBall = new DynamicGameObject(0, 0, 0.2f, 0.2f);
  63.             targets = new ArrayList<GameObject>(NUM_TARGETS);
  64.             grid = new SpatialHashGrid(WORLD_WIDTH, WORLD_HEIGHT, 2.5f);
  65.             for(int i=0; i < NUM_TARGETS; ++i) {
  66.                 GameObject target = new GameObject(
  67.                         (float)Math.random() * WORLD_WIDTH,
  68.                         (float)Math.random() * WORLD_HEIGHT,
  69.                         0.5f,
  70.                         0.5f);
  71.                 grid.insertStaticObject(target);
  72.                 targets.add(target);
  73.             }
  74.  
  75.             // the cannon
  76.             float[] vertices = new float[] {
  77.                     -0.5f, -0.25f, 0.0f, 0.5f,
  78.                      0.5f, -0.25f, 1.0f, 0.5f,
  79.                      0.5f,  0.25f, 1.0f, 0.0f
  80.                     -0.5f,  0.25f, 0.0f, 0.0f
  81.             };
  82.             short[] indices = new short[] {
  83.                     0, 1, 2,
  84.                     2, 3, 0
  85.             };
  86.             cannonVertices = new Vertices(glGraphics, 4, 6, false, true);
  87.             cannonVertices.setVertices(vertices, 0, vertices.length);
  88.             cannonVertices.setIndices(indices, 0, indices.length);
  89.  
  90.             // the cannon ball
  91.             vertices = new float[] {
  92.                     -0.1f, -0.1f, 0.0f,  0.75f,
  93.                      0.1f, -0.1f, 0.25f, 0.75f,
  94.                      0.1f,  0.1f, 0.25f, 0.5f,
  95.                     -0.1f,  0.1f, 0.0f,  0.5f
  96.             };
  97.             indices = new short[] {
  98.                     0, 1, 2,
  99.                     2, 3, 0
  100.             };
  101.             cannonBallVertices = new Vertices(glGraphics, 4, 6, false, true);
  102.             cannonBallVertices.setVertices(vertices, 0, vertices.length);
  103.             cannonBallVertices.setIndices(indices, 0, indices.length);
  104.  
  105.             // the target
  106.             vertices = new float[] {
  107.                     -0.25f, -0.25f, 0.5f, 1.0f,
  108.                      0.25f, -0.25f, 1.0f, 1.0f,
  109.                      0.25f,  0.25f, 1.0f, 0.5f,
  110.                     -0.25f,  0.25f, 0.5f, 0.5f
  111.             };
  112.             indices = new short[] {
  113.                     0, 1, 2,
  114.                     2, 3, 0
  115.             };
  116.             targetVertices = new Vertices(glGraphics, 4, 6, false, true);
  117.             targetVertices.setVertices(vertices, 0, vertices.length);
  118.             targetVertices.setIndices(indices, 0, indices.length);
  119.         }
  120.  
  121.         @Override
  122.         public void update(float deltaTime) {
  123.             List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
  124.             game.getInput().getKeyEvents();
  125.  
  126.             for(int i=0; i < touchEvents.size(); ++i) {
  127.                 TouchEvent event = touchEvents.get(i);
  128.  
  129.                 camera2D.touchToWorld(touchPoint.set(event.x, event.y));
  130.  
  131.                 cannon.angle = touchPoint.sub(cannon.position).angle();
  132.  
  133.                 if(event.type == TouchEvent.TOUCH_UP) {
  134.                     float radians = cannon.angle * Vector2.TO_RADIANS;
  135.                     float ballSpeed = touchPoint.dist(cannon.position);
  136.  
  137.                     cannonBall.position.set(cannon.position);
  138.                     cannonBall.velocity.x = (float)Math.cos(radians) * ballSpeed;
  139.                     cannonBall.velocity.y = (float)Math.sin(radians) * ballSpeed;
  140.                     cannonBall.bounds.lowerLeft.set(cannonBall.position.x - 0.1f, cannonBall.position.y - 0.1f);
  141.                 }
  142.             }
  143.  
  144.             cannonBall.velocity.add(gravity.x * deltaTime, gravity.y * deltaTime);
  145.             cannonBall.position.add(cannonBall.velocity.x * deltaTime, cannonBall.velocity.y * deltaTime);
  146.             cannonBall.bounds.lowerLeft.add(cannonBall.velocity.x * deltaTime, cannonBall.velocity.y * deltaTime);
  147.  
  148.             List<GameObject> colliders = grid.getPotentialColliders(cannonBall);
  149.             for(int i=0; i < colliders.size(); ++i) {
  150.                 GameObject collider = colliders.get(i);
  151.                 if(OverlapTester.overlapRectangles(cannonBall.bounds, collider.bounds)) {
  152.                     grid.removeObject(collider);
  153.                     targets.remove(collider);
  154.                 }
  155.             }
  156.  
  157.             // the cannon ball falls even if we don't shoot it (since we always apply gravity to it)
  158.             // hence we need to check for y <= 0
  159.             if(cannonBall.position.y <= 0) {
  160.                 camera2D.position.set(WORLD_WIDTH/2, WORLD_HEIGHT/2);
  161.                 camera2D.zoom = 1.0f;
  162.             } else {
  163.                 camera2D.position.set(cannonBall.position);
  164.                 camera2D.zoom = 1 + cannonBall.position.y / WORLD_HEIGHT;
  165.             }
  166.         }
  167.  
  168.         @Override
  169.         public void present(float deltaTime) {
  170.             GL10 gl = glGraphics.getGL();
  171.  
  172.             // clear the screen with black
  173.             gl.glClearColor(0, 0, 0, 1);
  174.             gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  175.  
  176.             // set up the camera
  177.             camera2D.setViewportAndMatrices();
  178.  
  179.             gl.glEnable(GL10.GL_BLEND);
  180.             gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  181.             gl.glEnable(GL10.GL_TEXTURE_2D);
  182.             texture.bind();
  183.  
  184.             // the target(s)
  185.             targetVertices.bind();
  186.             for(int i=0; i < targets.size(); ++i) {
  187.                 gl.glLoadIdentity();
  188.                 gl.glTranslatef(targets.get(i).position.x, targets.get(i).position.y, 0);
  189.                 targetVertices.draw(GL10.GL_TRIANGLES, 0, 6);
  190.             }
  191.             targetVertices.unbind();
  192.  
  193.             // the cannonBall
  194.             cannonBallVertices.bind();
  195.             gl.glLoadIdentity();
  196.             gl.glTranslatef(cannonBall.position.x, cannonBall.position.y, 0);
  197.             cannonBallVertices.draw(GL10.GL_TRIANGLES, 0, 6);
  198.             cannonBallVertices.unbind();
  199.  
  200.             // the cannon
  201.  
  202.             gl.glLoadIdentity();
  203.             gl.glTranslatef(cannon.position.x, cannon.position.y, 0);
  204.             gl.glRotatef(cannon.angle, 0, 0, 1);
  205.             cannonVertices.bind();
  206.             cannonVertices.draw(GL10.GL_TRIANGLES, 0, 6);
  207.             cannonVertices.unbind();
  208.         }
  209.  
  210.         @Override
  211.         public void pause() {
  212.  
  213.         }
  214.  
  215.         @Override
  216.         public void resume() {
  217.             texture = new Texture((GLGame)game, "atlas.png");
  218.         }
  219.  
  220.         @Override
  221.         public void dispose() {
  222.             texture.dispose();
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement