Android Open Source - RollOverSphere---a-simple-libgdx-game Game Helper






From Project

Back to project page RollOverSphere---a-simple-libgdx-game.

License

The source code is released under:

MIT License

If you think the Android project RollOverSphere---a-simple-libgdx-game listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.igorcrevar.rolloverchuck.utils;
//from  ww  w.  j  a  va  2 s . c o  m
import java.lang.reflect.Field;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;

public class GameHelper {
  public static float screenX2OtherX(float x, float worldX) {
    return worldX * x / Gdx.graphics.getWidth();
  }
  
  public static float screenY2OtherY(float y, float worldY) {
    return worldY * (Gdx.graphics.getHeight() - y) / Gdx.graphics.getHeight();
  }
  
  public static void clearScreen() {
    Gdx.graphics.getGL20().glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    Gdx.graphics.getGL20().glClearDepthf(1.0f);
    Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  }

  public static boolean tapPointInsideRectangle(float x, float y, float rectX, float rectY, float rectWidth, float rectHeight) {
    return x >= rectX && x <= rectX + rectWidth && y >= rectY - rectHeight && y <= rectY;
  }
  
  public static float getAlignedPosX(BitmapFont font, String txt, float sizeX) {
    return (sizeX - font.getBounds(txt).width) / 2.0f;
  }
  
  public static float getAlignedPosY(BitmapFont font, String txt, float sizeY) {
    return (sizeY - font.getBounds(txt).height) / 2.0f;
  }
  
  public static void setProjectionFor2D(SpriteBatch spriteBatch, float width, float height) {
    spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
  }
  
  private static Vector3 tmp1 = new Vector3();
  private static Vector3 tmp2 = new Vector3();
  private static Vector3 tmp3 = new Vector3();
  /**
   * populate vertex buffer with normals
   * @param vert vertex buffer
   * @param ind index buffer
   * @param vertexSize size of one vertex
   * @param posOffset offset of position data inside vertex
   * @param normalOffset offset of normal data inside vertex
   */
  public static void populateNormals(float[] vert, short[] ind, int vertexSize, int posOffset, int normalOffset) {
    for (int i = 0; i < ind.length; i += 3) {
      int off1 = vertexSize * ind[i];
      int off2 = vertexSize * ind[i + 1];
      int off3 = vertexSize * ind[i + 2];
      // set vertex positions
      tmp1.set(vert[off1 + posOffset], vert[off1 + posOffset + 1], vert[off1 + posOffset + 2]);
      tmp2.set(vert[off2 + posOffset], vert[off2 + posOffset + 1], vert[off2 + posOffset + 2]);
      tmp3.set(vert[off3 + posOffset], vert[off3 + posOffset + 1], vert[off3 + posOffset + 2]);
      // subs
      tmp2.sub(tmp1);
      tmp3.sub(tmp1);
      // cross
      tmp2.crs(tmp3).nor();
      // save normals
      vert[off1 + normalOffset] = tmp2.x;
      vert[off1 + normalOffset + 1] = tmp2.y;
      vert[off1 + normalOffset + 2] = tmp2.z;
    }
  }
  
  private static String lastStrValue = null;
  private static long lastPointsValue = Long.MIN_VALUE;
  public static String getStringPoints(long pointsScore) {
    if (pointsScore == lastPointsValue) {
      return lastStrValue;
    }
    else if (pointsScore <= 99999999l) {
      lastStrValue = String.valueOf(pointsScore);
    }
    else if (pointsScore <= 99999999999l) {
      lastStrValue = String.valueOf(pointsScore / 1000l) + "K";
    }
    else if (pointsScore <= 99999999999999l) {
      lastStrValue = String.valueOf(pointsScore / 1000000l) + "M";
    }
    else {
      lastStrValue = String.valueOf(pointsScore / 1000000000l) + "G";
    }
    
    lastPointsValue = pointsScore;
    return lastStrValue;
  }
  
  public static void load(String fileName, Object object) {
    try {
      FileHandle fh = Gdx.files.external(fileName);
      Field[] fields = object.getClass().getFields();      
      if (fh.exists()) {
        String content = fh.readString();
        String[] rows = content.split("\n");
        for (String r : rows) {
          String[] data = r.split("=");
          if (data.length != 2) continue; 
          for (Field f : fields) {
            Class<?> type = f.getType();            
            if (f.getName().equals(data[0])) {
              if (type.isPrimitive()) {
                if (type.equals(boolean.class)) {
                  f.setBoolean(object, Boolean.parseBoolean(data[1]));
                }
                else if (type.equals(float.class)) {
                  f.setFloat(object, Float.parseFloat(data[1]));
                }
                else if (type.equals(int.class)) {
                  f.setInt(object, Integer.parseInt(data[1]));
                }
                else if (type.equals(long.class)) {
                  f.setLong(object, Long.parseLong(data[1]));
                }
              }
              else if (type.getName().equals("com.badlogic.gdx.math.Vector3")) {
                String[] tmp = data[1].split(",");
                f.set(object, new Vector3(Float.parseFloat(tmp[0]), Float.parseFloat(tmp[1]), Float.parseFloat(tmp[2])));
              }
              break;
            }
          }
        }
      }
      else {
        StringBuilder content = new StringBuilder();
        for (Field f : fields) {
          content.append(f.getName()).append('=');
          Class<?> type = f.getType();
          String tmp = "";
          if (type.equals(boolean.class)) {
            tmp = Boolean.toString(f.getBoolean(object));
          }
          else if (type.equals(float.class)) {
            tmp = Float.toString(f.getFloat(object));
          }
          else if (type.equals(int.class)) {
            tmp = Integer.toString(f.getInt(object));
          }
          else if (type.equals(long.class)) {
            tmp = Long.toString(f.getLong(object));
          }
          else if (type.getName().equals("com.badlogic.gdx.math.Vector3")) {
            Vector3 v = (Vector3)f.get(object);
            tmp = v.x + "," + v.y + "," + v.z;            
          }
          
          content.append(tmp).append("\n");
        }

        fh.writeString(content.toString(), false);
      }
    }
    catch (Exception ex) {      
    }    
  }
}




Java Source Code List

com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.igorcrevar.rolloverchuck.DesktopRunner.java
com.igorcrevar.rolloverchuck.GameConsts.java
com.igorcrevar.rolloverchuck.GameData.java
com.igorcrevar.rolloverchuck.GameListener.java
com.igorcrevar.rolloverchuck.GameManager.java
com.igorcrevar.rolloverchuck.GameType.java
com.igorcrevar.rolloverchuck.IActivityRequestHandler.java
com.igorcrevar.rolloverchuck.ISceneManager.java
com.igorcrevar.rolloverchuck.IScene.java
com.igorcrevar.rolloverchuck.mesh.CubeMeshWithNormals.java
com.igorcrevar.rolloverchuck.mesh.CubeMesh.java
com.igorcrevar.rolloverchuck.mesh.FieldMesh.java
com.igorcrevar.rolloverchuck.mesh.IMesh.java
com.igorcrevar.rolloverchuck.mesh.SphereMesh.java
com.igorcrevar.rolloverchuck.objects.BoxObject.java
com.igorcrevar.rolloverchuck.objects.ChuckObject.java
com.igorcrevar.rolloverchuck.objects.ChuckSpriteObject.java
com.igorcrevar.rolloverchuck.objects.EndGameButtonsObject.java
com.igorcrevar.rolloverchuck.objects.FieldObject.java
com.igorcrevar.rolloverchuck.objects.GameButton.java
com.igorcrevar.rolloverchuck.objects.IntroSceneButtonsObject.java
com.igorcrevar.rolloverchuck.objects.MainWallObject.java
com.igorcrevar.rolloverchuck.objects.StarsObject.java
com.igorcrevar.rolloverchuck.objects.TextButton.java
com.igorcrevar.rolloverchuck.objects.TrailObject.java
com.igorcrevar.rolloverchuck.objects.boxes.BoxManager.java
com.igorcrevar.rolloverchuck.objects.boxes.BoxRegionManager.java
com.igorcrevar.rolloverchuck.objects.boxes.BoxRegion.java
com.igorcrevar.rolloverchuck.objects.boxes.BoxTypeFactory.java
com.igorcrevar.rolloverchuck.objects.boxes.IBoxRegion.java
com.igorcrevar.rolloverchuck.objects.boxes.IBoxType.java
com.igorcrevar.rolloverchuck.physics.CollisionSolver.java
com.igorcrevar.rolloverchuck.physics.ICollisionIterationHandler.java
com.igorcrevar.rolloverchuck.physics.SphereMoving.java
com.igorcrevar.rolloverchuck.points.PlayerPoint.java
com.igorcrevar.rolloverchuck.points.PointsManager.java
com.igorcrevar.rolloverchuck.scenes.GameLoadingScene.java
com.igorcrevar.rolloverchuck.scenes.GameScene.java
com.igorcrevar.rolloverchuck.scenes.IntroScene.java
com.igorcrevar.rolloverchuck.scenes.GameMode.ArcadeGameMode.java
com.igorcrevar.rolloverchuck.scenes.GameMode.IGameMode.java
com.igorcrevar.rolloverchuck.scenes.GameMode.StressFreeGameMode.java
com.igorcrevar.rolloverchuck.utils.CollisionHelper.java
com.igorcrevar.rolloverchuck.utils.GameHelper.java
com.igorcrevar.rolloverchuck.utils.IMyFontDrawerFont.java
com.igorcrevar.rolloverchuck.utils.Mathf.java
com.igorcrevar.rolloverchuck.utils.MyFontDrawerBatch.java
com.igorcrevar.rolloverchuck.utils.MyFontDrawerDefaultFont.java
com.igorcrevar.rolloverchuck.utils.MyFontDrawer.java
com.igorcrevar.rolloverchuck.utils.ShaderAssetLoader.java
com.wayilookatgames.rolloverball.MainActivity.java