Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.badlogic.androidgames.gamedev2d;
- import framework.DynamicGameObject;
- import framework.Game;
- import framework.GameObject;
- import framework.Input.*;
- import framework.Screen;
- import framework.SpatialHashGrid;
- import framework.gl.Camera2D;
- import framework.gl.Texture;
- import framework.gl.Vertices;
- import framework.impl.GLGame;
- import framework.impl.GLGraphics;
- import framework.math.OverlapTester;
- import framework.math.Vector2;
- import java.util.ArrayList;
- import java.util.List;
- import javax.microedition.khronos.opengles.GL10;
- /**
- * Created by Kebir on 16.03.2018.
- */
- public class TextureAtlasTest extends GLGame {
- @Override
- public Screen getStartScreen() {
- return new TextureAtlasScreen(this);
- }
- class TextureAtlasScreen extends Screen {
- final int NUM_TARGETS = 20;
- final float WORLD_WIDTH = 9.6f;
- final float WORLD_HEIGHT = 4.8f;
- Camera2D camera2D;
- Texture texture;
- GLGraphics glGraphics;
- Cannon cannon;
- DynamicGameObject cannonBall;
- List<GameObject> targets;
- SpatialHashGrid grid;
- Vertices cannonVertices;
- Vertices cannonBallVertices;
- Vertices targetVertices;
- Vector2 touchPoint = new Vector2();
- Vector2 gravity = new Vector2(0, -4);
- public TextureAtlasScreen(Game game) {
- super(game);
- glGraphics = ((GLGame)game).getGLGraphics();
- camera2D = new Camera2D(glGraphics, WORLD_WIDTH, WORLD_HEIGHT);
- cannon = new Cannon(0, 0, 1, 0.5f);
- cannonBall = new DynamicGameObject(0, 0, 0.2f, 0.2f);
- targets = new ArrayList<GameObject>(NUM_TARGETS);
- grid = new SpatialHashGrid(WORLD_WIDTH, WORLD_HEIGHT, 2.5f);
- for(int i=0; i < NUM_TARGETS; ++i) {
- GameObject target = new GameObject(
- (float)Math.random() * WORLD_WIDTH,
- (float)Math.random() * WORLD_HEIGHT,
- 0.5f,
- 0.5f);
- grid.insertStaticObject(target);
- targets.add(target);
- }
- // the cannon
- float[] vertices = new float[] {
- -0.5f, -0.25f, 0.0f, 0.5f,
- 0.5f, -0.25f, 1.0f, 0.5f,
- 0.5f, 0.25f, 1.0f, 0.0f
- -0.5f, 0.25f, 0.0f, 0.0f
- };
- short[] indices = new short[] {
- 0, 1, 2,
- 2, 3, 0
- };
- cannonVertices = new Vertices(glGraphics, 4, 6, false, true);
- cannonVertices.setVertices(vertices, 0, vertices.length);
- cannonVertices.setIndices(indices, 0, indices.length);
- // the cannon ball
- vertices = new float[] {
- -0.1f, -0.1f, 0.0f, 0.75f,
- 0.1f, -0.1f, 0.25f, 0.75f,
- 0.1f, 0.1f, 0.25f, 0.5f,
- -0.1f, 0.1f, 0.0f, 0.5f
- };
- indices = new short[] {
- 0, 1, 2,
- 2, 3, 0
- };
- cannonBallVertices = new Vertices(glGraphics, 4, 6, false, true);
- cannonBallVertices.setVertices(vertices, 0, vertices.length);
- cannonBallVertices.setIndices(indices, 0, indices.length);
- // the target
- vertices = new float[] {
- -0.25f, -0.25f, 0.5f, 1.0f,
- 0.25f, -0.25f, 1.0f, 1.0f,
- 0.25f, 0.25f, 1.0f, 0.5f,
- -0.25f, 0.25f, 0.5f, 0.5f
- };
- indices = new short[] {
- 0, 1, 2,
- 2, 3, 0
- };
- targetVertices = new Vertices(glGraphics, 4, 6, false, true);
- targetVertices.setVertices(vertices, 0, vertices.length);
- targetVertices.setIndices(indices, 0, indices.length);
- }
- @Override
- public void update(float deltaTime) {
- List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
- game.getInput().getKeyEvents();
- for(int i=0; i < touchEvents.size(); ++i) {
- TouchEvent event = touchEvents.get(i);
- camera2D.touchToWorld(touchPoint.set(event.x, event.y));
- cannon.angle = touchPoint.sub(cannon.position).angle();
- if(event.type == TouchEvent.TOUCH_UP) {
- float radians = cannon.angle * Vector2.TO_RADIANS;
- float ballSpeed = touchPoint.dist(cannon.position);
- cannonBall.position.set(cannon.position);
- cannonBall.velocity.x = (float)Math.cos(radians) * ballSpeed;
- cannonBall.velocity.y = (float)Math.sin(radians) * ballSpeed;
- cannonBall.bounds.lowerLeft.set(cannonBall.position.x - 0.1f, cannonBall.position.y - 0.1f);
- }
- }
- cannonBall.velocity.add(gravity.x * deltaTime, gravity.y * deltaTime);
- cannonBall.position.add(cannonBall.velocity.x * deltaTime, cannonBall.velocity.y * deltaTime);
- cannonBall.bounds.lowerLeft.add(cannonBall.velocity.x * deltaTime, cannonBall.velocity.y * deltaTime);
- List<GameObject> colliders = grid.getPotentialColliders(cannonBall);
- for(int i=0; i < colliders.size(); ++i) {
- GameObject collider = colliders.get(i);
- if(OverlapTester.overlapRectangles(cannonBall.bounds, collider.bounds)) {
- grid.removeObject(collider);
- targets.remove(collider);
- }
- }
- // the cannon ball falls even if we don't shoot it (since we always apply gravity to it)
- // hence we need to check for y <= 0
- if(cannonBall.position.y <= 0) {
- camera2D.position.set(WORLD_WIDTH/2, WORLD_HEIGHT/2);
- camera2D.zoom = 1.0f;
- } else {
- camera2D.position.set(cannonBall.position);
- camera2D.zoom = 1 + cannonBall.position.y / WORLD_HEIGHT;
- }
- }
- @Override
- public void present(float deltaTime) {
- GL10 gl = glGraphics.getGL();
- // clear the screen with black
- gl.glClearColor(0, 0, 0, 1);
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- // set up the camera
- camera2D.setViewportAndMatrices();
- gl.glEnable(GL10.GL_BLEND);
- gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
- gl.glEnable(GL10.GL_TEXTURE_2D);
- texture.bind();
- // the target(s)
- targetVertices.bind();
- for(int i=0; i < targets.size(); ++i) {
- gl.glLoadIdentity();
- gl.glTranslatef(targets.get(i).position.x, targets.get(i).position.y, 0);
- targetVertices.draw(GL10.GL_TRIANGLES, 0, 6);
- }
- targetVertices.unbind();
- // the cannonBall
- cannonBallVertices.bind();
- gl.glLoadIdentity();
- gl.glTranslatef(cannonBall.position.x, cannonBall.position.y, 0);
- cannonBallVertices.draw(GL10.GL_TRIANGLES, 0, 6);
- cannonBallVertices.unbind();
- // the cannon
- gl.glLoadIdentity();
- gl.glTranslatef(cannon.position.x, cannon.position.y, 0);
- gl.glRotatef(cannon.angle, 0, 0, 1);
- cannonVertices.bind();
- cannonVertices.draw(GL10.GL_TRIANGLES, 0, 6);
- cannonVertices.unbind();
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- texture = new Texture((GLGame)game, "atlas.png");
- }
- @Override
- public void dispose() {
- texture.dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement