Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.55 KB | None | 0 0
  1. package gameworld;
  2.  
  3. import com.badlogic.gdx.graphics.Color;
  4. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  5. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  6. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  7. import com.badlogic.gdx.math.Intersector;
  8. import com.badlogic.gdx.math.MathUtils;
  9. import com.badlogic.gdx.math.Rectangle;
  10. import com.badlogic.gdx.math.Vector2;
  11. import com.badlogic.gdx.utils.Array;
  12.  
  13. import Tweens.Value;
  14. import Tweens.ValueAccessor;
  15. import aurelienribon.tweenengine.Tween;
  16. import aurelienribon.tweenengine.TweenManager;
  17. import configuration.Configuration;
  18. import gameobjects.Background;
  19. import gameobjects.Hero;
  20. import gameobjects.Menu;
  21. import gameobjects.Orbit;
  22. import gameobjects.Star;
  23. import helpers.AssetLoader;
  24. import helpers.ColorManager;
  25. import helpers.Rumble;
  26. import orbits.ActionResolver;
  27. import orbits.OrbitalsGame;
  28. import ui.Banner;
  29. import ui.Button;
  30. import ui.ScoreBanner;
  31. import ui.TimerBar;
  32.  
  33. public class GameWorld {
  34.  
  35.     public final float w;
  36.     //GENERAL VARIABLES
  37.     public float gameWidth;
  38.     public float gameHeight;
  39.     public float worldWidth;
  40.     public float worldHeight;
  41.  
  42.     public ActionResolver actionResolver;
  43.     public OrbitalsGame game;
  44.     public GameWorld world = this;
  45.  
  46.  
  47.     //GAME OBJECTS
  48.     //private Hero hero;
  49.     private Menu menu;
  50.     private GameCam camera;
  51.     public GameState gameState;
  52.     public ColorManager colorManager;
  53.     private Orbit orbit, orbit1, orbit2, orbit3, orbit4, orbit5, orbit6;
  54.     private Hero hero;
  55.     private Background background;
  56.     private TimerBar timerBar;
  57.  
  58.     private Button pauseButton;
  59.     private final int numberOfStars;
  60.     private final int numberOfEnemies;
  61.     private Array<Star> stars = new Array<Star>();
  62.     private Array<Orbit> orbits = new Array<Orbit>();
  63.     private Array<Vector2> points = new Array<Vector2>();
  64.  
  65.     public Banner banner;
  66.  
  67.     private int score;
  68.  
  69.     //TWEENS
  70.     private TweenManager manager;
  71.     public Rumble rumble;
  72.     private Rectangle bounds;
  73.     private float distance;
  74.     private Orbit currentOrbit;
  75.     private float iORadius = 120;
  76.     private int numOfOrbits = 6;
  77.  
  78.  
  79.     private final ScoreBanner scoreBanner;
  80.  
  81.     public GameWorld(OrbitalsGame game, ActionResolver actionResolver, float gameWidth,
  82.                      float gameHeight, float worldWidth, float worldHeight) {
  83.  
  84.         this.gameWidth = gameWidth;
  85.         this.w = gameHeight / 100;
  86.         this.gameHeight = gameHeight;
  87.         this.worldWidth = worldWidth;
  88.         this.worldHeight = worldHeight;
  89.         this.game = game;
  90.         this.actionResolver = actionResolver;
  91.         this.numberOfStars = 110;
  92.         this.numberOfEnemies = 0;
  93.  
  94.  
  95.         colorManager = new ColorManager();
  96.         camera = new GameCam(world, worldWidth / 2, worldHeight / 2, worldWidth, worldHeight);
  97.  
  98.         gameState = GameState.MENU;
  99.  
  100.         for (int i = 0; i < numberOfStars; i++) {
  101.             stars.add(new Star(world));
  102.         }
  103.  
  104.         createPoints(0);
  105.  
  106.  
  107.         Tween.registerAccessor(Value.class, new ValueAccessor());
  108.         manager = new TweenManager();
  109.  
  110.         this.rumble = new Rumble(this);
  111.         banner = new Banner(this, "Pause", world.gameHeight / 2 - 100, parseColor("#34495e", 1f));
  112.         pauseButton = new Button(this, 50, world.gameHeight - 150,
  113.                 100, 100,
  114.                 AssetLoader.square, AssetLoader.pauseButton, Configuration.COLOR_PAUSE_BUTTON,
  115.                 .05f);
  116.  
  117.         bounds = new Rectangle(-50, -50, worldWidth + 100, worldHeight + 100);
  118.  
  119.         orbit = new Orbit(this, world.worldWidth / 2, w * 25, iORadius);
  120.         orbit1 = new Orbit(this, world.worldWidth / 2 + 300, w * 60, iORadius);
  121.         orbit2 = new Orbit(this, world.worldWidth / 2 - 200, w * 90, iORadius);
  122.         orbit3 = new Orbit(this, MathUtils.random(iORadius + 30, world.gameWidth - iORadius - 30),
  123.                 w * 120, iORadius);
  124.         orbit4 = new Orbit(this, MathUtils.random(iORadius + 30, world.gameWidth - iORadius - 30),
  125.                 w * 150, iORadius);
  126.         orbit5 = new Orbit(this, MathUtils.random(iORadius + 30, world.gameWidth - iORadius - 30),
  127.                 w * 180, iORadius);
  128.  
  129.         orbit6 = new Orbit(this, MathUtils.random(iORadius + 30, world.gameWidth - iORadius - 30),
  130.                 w * 210, iORadius);
  131.  
  132.         currentOrbit = orbit;
  133.         orbits.add(orbit);
  134.         orbits.add(orbit1);
  135.         orbits.add(orbit2);
  136.         orbits.add(orbit3);
  137.         orbits.add(orbit4);
  138.  
  139.         orbits.add(orbit5);
  140.         orbits.add(orbit6);
  141.         hero = new Hero(this, orbit, 20);
  142.         background = new Background(this);
  143.         menu = new Menu(this);
  144.         menu.start();
  145.  
  146.         pauseButton = new Button(this, 50, w * 85,
  147.                 100, 100,
  148.                 AssetLoader.button, AssetLoader.pauseButton, Configuration.COLOR_PAUSE_BUTTON,
  149.                 .05f);
  150.         scoreBanner = new ScoreBanner(this, score + "", w * 85,
  151.                 parseColor(Configuration.COLOR_BANNER_WITH_SCORE, 1f));
  152.  
  153.         timerBar = new TimerBar(this, "", w * 85, Color.WHITE);
  154.     }
  155.  
  156.  
  157.     public void update(float delta) {
  158.         manager.update(delta);
  159.         background.update(delta);
  160.         colorManager.update(delta);
  161.         menu.update(delta);
  162.         banner.update(delta);
  163.         pauseButton.update(delta);
  164.         scoreBanner.update(delta);
  165.         if (!isPaused()) {
  166.             for (int i = 0; i < numberOfStars; i++) {
  167.                 stars.get(i).update(delta);
  168.             }
  169.             for (int i = 0; i < orbits.size; i++) {
  170.                 orbits.get(i).update(delta);
  171.             }
  172.             hero.update(delta);
  173.             timerBar.update(delta);
  174.         }
  175.  
  176.         if (hero.getHeroState() == Hero.HeroState.FLYING) {
  177.             camera.setPoint(hero.getPosition());
  178.         }
  179.  
  180.         if (isRunning()) {
  181.             collisions();
  182.         } else if (isMenu()) {
  183.         } else if (isGameOver()) {
  184.         } else if (isPaused()) {
  185.         }
  186.     }
  187.  
  188.     private void collisions() {
  189.  
  190.         for (int i = 0; i < orbits.size; i++) {
  191.             if (hero.getHeroState() != Hero.HeroState.ORBIT) {
  192.                 if (Intersector.overlaps(hero.circle, orbits.get(i).circle)) {
  193.                     AssetLoader.sound.play();
  194.                     distance = w * 25 - orbits.get(i).circle.y;
  195.                     collisionBetweenOrbitAndHero(orbits.get(i));
  196.                     score++;
  197.                     scoreBanner.setText(score + "");
  198.                     orbits.get(i).effect();
  199.                     timerBar.reset();
  200.                     for (int j = 0; j < orbits.size; j++) {
  201.                         if (i != j) {
  202.                             //orbits.get(j).finish();
  203.                         }
  204.                     }
  205.  
  206.                 }
  207.             }
  208.         }
  209.  
  210.         for (int i = 0; i < orbits.size; i++) {
  211.             if (orbits.get(i).getPosition().y < -orbits.get(i).circle.radius * 4) {
  212.                 if (i == 0) {
  213.                     orbits.get(i).reset(orbits.get(orbits.size - 1).getPosition().y);
  214.                 } /*else if (i == 1) {
  215.                     orbits.get(i).reset(orbits.get(0).getPosition().y);
  216.                 } else if (i == 2) {
  217.                     orbits.get(i).reset(orbits.get(1).getPosition().y);
  218.                 } else if (i == 3) {
  219.                     orbits.get(i).reset(orbits.get(2).getPosition().y);
  220.                 } else if (i == 4) {
  221.                     orbits.get(i).reset(orbits.get(3).getPosition().y);
  222.                 } else if (i == 6) {
  223.                     orbits.get(i).reset(orbits.get(4).getPosition().y);
  224.                 } */ else {
  225.                     orbits.get(i).reset(orbits.get(i - 1).getPosition().y);
  226.                 }
  227.             }
  228.         }
  229.  
  230.  
  231.     }
  232.  
  233.     private void collisionBetweenOrbitAndHero(Orbit or) {
  234.  
  235.         currentOrbit = or;
  236.         or.startTimer();
  237.         camera.setPoint(or.getPosition());
  238.         hero.setCurrentOrbit(or);
  239.         hero.getAngle2Vecs(hero.getPosition(), or.getPosition());
  240.         hero.setAngle();
  241.         hero.setHeroState(Hero.HeroState.ORBIT);
  242.  
  243.         for (int i = 0; i < orbits.size; i++) {
  244.             orbits.get(i).scrollTo(distance);
  245.         }
  246.     }
  247.  
  248.     public void finish() {
  249.  
  250.         pauseButton.finish();
  251.         menu.startGameOver();
  252.         scoreBanner.dissapear();
  253.         saveScoresLogic();
  254.         hero.finish();
  255.         timerBar.dissapear();
  256.  
  257.         for (int i = 0; i < orbits.size; i++) {
  258.             orbits.get(i).finish();
  259.         }
  260.     }
  261.  
  262.     private void saveScoresLogic() {
  263.         AssetLoader.addGamesPlayed();
  264.         int gamesPlayed = AssetLoader.getGamesPlayed();
  265.         // GAMES PLAYED ACHIEVEMENTS!
  266.         actionResolver.submitScore(score);
  267.         actionResolver.submitGamesPlayed(gamesPlayed);
  268.  
  269.         if (score > AssetLoader.getHighScore()) {
  270.             AssetLoader.setHighScore(score);
  271.         }
  272.         //checkAchievements();
  273.     }
  274.  
  275.     private void checkAchievements() {
  276.         if (actionResolver.isSignedIn()) {
  277.  
  278.         }
  279.     }
  280.  
  281.  
  282.     public void pause() {
  283.         banner.appear();
  284.         gameState = GameState.PAUSE;
  285.     }
  286.  
  287.  
  288.     public ColorManager getColorManager() {
  289.         return colorManager;
  290.     }
  291.  
  292.     public boolean isRunning() {
  293.         return gameState == GameState.RUNNING;
  294.     }
  295.  
  296.     public void renewMap() {
  297.         //menu.finishGameOver(10);
  298.         renewOrbits();
  299.         currentOrbit = orbit;
  300.         pauseButton.start();
  301.         hero.reset();
  302.         hero.setCurrentOrbit(currentOrbit);
  303.         hero.start();
  304.         for (int i = 0; i < orbits.size; i++) {
  305.             orbits.get(i).start();
  306.         }
  307.         score = 0;
  308.         scoreBanner.appear();
  309.         timerBar.appear();
  310.  
  311.         if (AssetLoader.getAds()) {
  312.             world.actionResolver.viewAd(false);
  313.         } else {
  314.             world.actionResolver.viewAd(true);
  315.         }
  316.  
  317.     }
  318.  
  319.     private void renewOrbits() {
  320.         orbit = new Orbit(this, world.worldWidth / 2, w * 25, iORadius);
  321.         orbits.clear();
  322.         orbits.add(orbit);
  323.  
  324.         for (int i = 0; i < numOfOrbits - 1; i++) {
  325.             orbit1 = new Orbit(this,
  326.                     MathUtils.random(iORadius + 30, world.gameWidth - iORadius - 30),
  327.                     w * (60 + i * 30), iORadius);
  328.             orbits.add(orbit1);
  329.         }
  330.  
  331.     }
  332.  
  333.     public static Color parseColor(String hex, float alpha) {
  334.         String hex1 = hex;
  335.         if (hex1.indexOf("#") != -1) {
  336.             hex1 = hex1.substring(1);
  337.             // Gdx.app.log("Hex", hex1);
  338.         }
  339.         Color color = Color.valueOf(hex1);
  340.         color.a = alpha;
  341.         return color;
  342.     }
  343.  
  344.     public void createPoints(int numOfS) {
  345.         /*for (int i = 0; i < numOfS; i++) {
  346.             float margin = 0;
  347.             float distance = ((world.gameWidth - (margin * 2)) / (numOfS - 1));
  348.             points.add(new Vector2(margin + (distance * i), (worldHeight)));
  349.             points.add(new Vector2(margin + (distance * i), (0)));
  350.         }
  351.  
  352.         for (int i = 0; i < numOfS; i++) {
  353.             float margin = 0;
  354.             float distance = ((world.gameHeight - (margin * 2)) / (numOfS - 1));
  355.             points.add(new Vector2((worldWidth), margin + (distance * i)));
  356.             points.add(new Vector2((0), margin + (distance * i)));
  357.         }*/
  358.  
  359.         for (int i = 0; i < numOfS; i++) {
  360.             float angle = MathUtils.random(0, 360);
  361.             float dist = MathUtils.random(0, worldHeight / 2);
  362.             float x = MathUtils.cos(angle) * dist + worldWidth / 2;
  363.             float y = MathUtils.sin(angle) * dist + worldHeight / 2;
  364.             points.add(new Vector2(x, y));
  365.         }
  366.     }
  367.  
  368.     public Array<Vector2> getPoints() {
  369.         return points;
  370.     }
  371.  
  372.     public Array<Star> getStars() {
  373.         return stars;
  374.     }
  375.  
  376.     public Array<Orbit> getBalls() {
  377.         return orbits;
  378.     }
  379.  
  380.     public void addScore() {
  381.         score++;
  382.     }
  383.  
  384.     public int getScore() {
  385.         return score;
  386.     }
  387.  
  388.     public boolean isPaused() {
  389.         return gameState == GameState.PAUSE;
  390.     }
  391.  
  392.  
  393.     public GameCam getCamera() {
  394.         return camera;
  395.     }
  396.  
  397.     public Banner getBanner() {
  398.         return banner;
  399.     }
  400.  
  401.     public boolean isGameOver() {
  402.         return gameState == GameState.GAMEOVER;
  403.     }
  404.  
  405.     public boolean isMenu() {
  406.         return gameState == GameState.MENU;
  407.     }
  408.  
  409.     public Menu getMenu() {
  410.         return menu;
  411.     }
  412.  
  413.  
  414.     public boolean isTransition() {
  415.         return gameState == GameState.TRANSITION;
  416.     }
  417.  
  418.     public Button getPauseButton() {
  419.         return pauseButton;
  420.     }
  421.  
  422.     public void setPauseMode() {
  423.         if (gameState == GameState.RUNNING) {
  424.             gameState = GameState.PAUSE;
  425.             banner.appearPause();
  426.         }
  427.     }
  428.  
  429.     public Vector2 getRandomPoint() {
  430.         return points.get((int) Math.floor(Math.random() * points.size));
  431.     }
  432.  
  433.     public Rectangle getRectangle() {
  434.         return bounds;
  435.     }
  436.  
  437.     public void render(SpriteBatch batcher, ShapeRenderer shapeRenderer, ShaderProgram fontShader) {
  438.  
  439.         background.render(batcher, shapeRenderer);
  440.         pauseButton.draw(batcher);
  441.         for (int i = 0; i < orbits.size; i++) {
  442.             orbits.get(i).render(batcher, shapeRenderer);
  443.         }
  444.         scoreBanner.render(batcher, shapeRenderer, fontShader);
  445.  
  446.         hero.render(batcher, shapeRenderer);
  447.         banner.render(batcher, shapeRenderer, fontShader);
  448.         timerBar.render(batcher, shapeRenderer, fontShader);
  449.  
  450.     }
  451.  
  452.     public Hero getHero() {
  453.         return hero;
  454.     }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement