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






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;
/* w  w w.  j a va 2  s .c  o  m*/
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.Disposable;
import com.igorcrevar.rolloverchuck.GameType.BaseGameType;
import com.igorcrevar.rolloverchuck.GameType.Difficulty;
import com.igorcrevar.rolloverchuck.mesh.CubeMesh;
import com.igorcrevar.rolloverchuck.mesh.CubeMeshWithNormals;
import com.igorcrevar.rolloverchuck.mesh.FieldMesh;
import com.igorcrevar.rolloverchuck.mesh.IMesh;
import com.igorcrevar.rolloverchuck.mesh.SphereMesh;
import com.igorcrevar.rolloverchuck.objects.StarsObject;
import com.igorcrevar.rolloverchuck.utils.ShaderAssetLoader;

public class GameManager implements Disposable {
  private AssetManager assetManager = new AssetManager();
  
  private boolean isSoundOn;
  
  private HashMap<String, IMesh> meshMap = new HashMap<String, IMesh>(8);  
  private GameData gameData = new GameData();
  private Preferences preferences;
  private StarsObject stars;

  private Sprite overlaySprite;
  
  public GameManager() {
    //load top score, etc
    preferences = Gdx.app.getPreferences("crewprefs");
    isSoundOn = preferences.getBoolean("sound_on", true);    
    
    assetManager.setLoader(ShaderProgram.class, new ShaderAssetLoader(new InternalFileHandleResolver()));
    
    // add all needed assets to queue
    assetManager.load("fonts/font.fnt", BitmapFont.class);
    assetManager.load("atlases/widgets.atlas", TextureAtlas.class);
    assetManager.load("atlases/base.atlas", TextureAtlas.class);
    assetManager.load("sounds/intro.ogg", Music.class);
    assetManager.load("sounds/game.ogg", Music.class);
    assetManager.load("light", ShaderProgram.class);
    assetManager.load("field", ShaderProgram.class);
    assetManager.load("simple", ShaderProgram.class);
    assetManager.load("shadow", ShaderProgram.class);
    assetManager.load("region", ShaderProgram.class);
  }
  
  public void setSoundOn(boolean value) {
    isSoundOn = value;
    preferences.putBoolean("sound_on", value);
    preferences.flush();
    if (isSoundOn) {
      playIntroMusic();
    }
    else {
      stopIntroMusic();
    }
  }
  
  public void saveScore(GameType gameType, long score) {
    // games played by player
    String gamesPlayedKey = "games_played" + gameType.toString();
    preferences.putLong(gamesPlayedKey, preferences.getLong(gamesPlayedKey, 0) + 1);
    // save top score
    String topScoreKey = "top_score" + gameType.toString();
    long topScore = preferences.getLong(topScoreKey, 0);
    if (topScore < score) {
      preferences.putLong(topScoreKey, score);
    }
    
    preferences.flush();
  }
  
  public long getTopScore(GameType gameType) {
    return preferences.getLong("top_score" + gameType.toString(), 0);
  }
    
  public long getTotalGamesPlayed(GameType gameType) {
    return preferences.getLong("games_played" + gameType.toString(), 0);
  }
  
  public long getAllGamesPlayed() {
    long res = 0;
    for (BaseGameType baseType : BaseGameType.values()) {
      for (Difficulty difficulty : Difficulty.values()) {
        String key = String.format("games_played%d-%d", baseType.ordinal(), difficulty.ordinal());
        res += preferences.getLong(key, 0);
      }
    }
    
    return res;
  }
  
  public boolean getIsSoundOn() {
    return isSoundOn;
  }
  
  /**
   * Update loading of assets
   * @return true if loading is finished
   */
  public boolean updateLoading() {
    if (assetManager.update()) {
      // add meshes
      addMesh("plane", new FieldMesh(gameData));
      addMesh("sphere", new SphereMesh(gameData));
      addMesh("cube", new CubeMesh(gameData));
      addMesh("cube_with_normals", new CubeMeshWithNormals(gameData));
      return true;
    }    
    return false;
  }
  
  @Override
  public void dispose() {
    assetManager.dispose();    
    for (IMesh m : meshMap.values()) {
      m.dispose();
    }
  }
  
  public TextureAtlas getTextureAtlas(String fileName) {
    String fullName = "atlases/" + fileName + ".atlas";
    return assetManager.get(fullName, TextureAtlas.class);
  }
  
  public Texture getTexture(String fileName) {
    return assetManager.get("images/" + fileName, Texture.class);
  }
  
  public Sound getSound(String fileName) {
    return assetManager.get("sounds/" + fileName, Sound.class);
  }
  
  public ShaderProgram getShader(final String fileName) {
    return assetManager.get(fileName, ShaderProgram.class);
  }
  
  public void addMesh(final String name, IMesh mesh) {
    meshMap.put(name, mesh);
  }
  
  public IMesh getMesh(final String name) {
    return meshMap.get(name);
  }
  
  public void playIntroMusic() {
    if (isSoundOn && assetManager.isLoaded("sounds/intro.ogg", Music.class)) {
      Music music = assetManager.get("sounds/intro.ogg", Music.class);
      music.setLooping(true);
      music.play();
    }
  }
  
  public void stopIntroMusic() {
    if (assetManager.isLoaded("sounds/intro.ogg", Music.class)) {
      Music music = assetManager.get("sounds/intro.ogg", Music.class);
      music.stop();
    }
  }
  
  public void playGameMusic() {
    if (isSoundOn && assetManager.isLoaded("sounds/game.ogg", Music.class)) {
      Music music = assetManager.get("sounds/game.ogg", Music.class);
      music.setLooping(false);
      music.play();
    }
  }
  
  public void stopGameMusic() {
    if (assetManager.isLoaded("sounds/game.ogg", Music.class)) {
      Music music = assetManager.get("sounds/game.ogg", Music.class);
      music.stop();
    }
  }
  
  public void playCoinSound() {
    if (isSoundOn) {
      getSound("coin.wav").play();
    }
  }
  
  public void playMoveSound() {
    if (isSoundOn) {
      getSound("move.wav").play();
    }
  }
  
  public void playDieSound() {
    if (isSoundOn) {
      getSound("die.wav").play();
    }
  }
  
  public BitmapFont getBitmapFont() {
    return assetManager.get("fonts/font.fnt", BitmapFont.class);
  }
  
  public boolean isBitmapFontLoaded() {
    return assetManager.isLoaded("fonts/font.fnt", BitmapFont.class);
  }
  
  public GameData getGameData() {
    return gameData;
  }
  
  public StarsObject getStars() {
    if (stars == null) {
      stars = new StarsObject();
    }
    
    return stars;
  }
  
  public Sprite getOverlaySprite(float rgbFactor, float alphaFactor) {
    if (overlaySprite == null) {
      overlaySprite = new Sprite();
      overlaySprite.setBounds(0, 0, 1920, 1080);
      overlaySprite.setOrigin(960, 540);
      overlaySprite.setRegion(getTextureAtlas("base").findRegion("myfont"));
    }
        
    overlaySprite.setColor(rgbFactor, rgbFactor, rgbFactor, alphaFactor);
    return overlaySprite;
  }
}




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