Advertisement
Guest User

Untitled

a guest
May 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. package com.example.emilnielsen.cloudhopper.CloudHopperGame;
  2.  
  3. import android.util.Log;
  4.  
  5. import com.example.emilnielsen.cloudhopper.CollisionListener;
  6. import com.example.emilnielsen.cloudhopper.Game;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11.  
  12. /**
  13.  * Created by emilnielsen on 03/05/16.
  14.  */
  15. public class World
  16. {
  17.  
  18.     //removed game game
  19.     public static final float min_X = 0;
  20.     public static final float max_X = 319;
  21.     public static final float min_Y = 0;
  22.     public static final float max_Y = 479;
  23.  
  24.     Random rand;
  25.     int points = 0;
  26.     int lives = 3;
  27.     int spawnRate = 0;
  28.     boolean gameover = false;
  29.  
  30.     Balloon balloon = new Balloon();
  31.  
  32.     BlackCloud blackCloud;
  33.  
  34.     CollisionListener listener;
  35.  
  36.  
  37.     List<BlackCloud> blackClouds = new ArrayList<>();
  38.  
  39.     public World(CollisionListener listener)
  40.     {
  41.         this.listener = listener;
  42.     }
  43.  
  44.  
  45.  
  46.     public void update(Game game, float deltaTime, float accelX, float accelY)
  47.     {
  48.         points = points + 2;
  49.         spawnRate = (int)Math.ceil(points/1000);
  50.  
  51.         if(balloon.y + Balloon.HEIGHT + 5 > max_Y || balloon.y < min_Y + 24)
  52.         {
  53.             gameover = true;
  54.             return;
  55.         }
  56.  
  57.  
  58.         balloon.x = balloon.x - (accelX * 50 * deltaTime);
  59.         balloon.y = balloon.y + (accelY * 50 * deltaTime);
  60.  
  61.         if(balloon.x < min_X)
  62.         {
  63.             balloon.x = min_X;
  64.         }
  65.         if(balloon.x + balloon.WIDTH > max_X)
  66.         {
  67.             balloon.x = max_X - balloon.WIDTH;
  68.         }
  69.  
  70.  
  71.  
  72.         spawnClouds();
  73.         moveClouds(deltaTime);
  74.         collideBalloonBlackCloud(deltaTime);
  75.  
  76.  
  77.     }
  78.  
  79.     private void moveClouds(float deltaTime){
  80.         int stop = blackClouds.size();
  81.         for(int i = 0; i < stop; i++){
  82.             try
  83.             {
  84.                 blackCloud = blackClouds.get(i);
  85.  
  86.                 blackCloud.x = blackCloud.x + (blackCloud.velocityX * 10 * deltaTime);
  87.  
  88.                 if (blackCloud.x < min_X - BlackCloud.WIDTH)
  89.                 {
  90.                     blackClouds.remove(i);
  91.                 }
  92.             }catch (IndexOutOfBoundsException e){
  93.                 Log.d("ArrayOutOfBounds", "Cloud was removed");
  94.             }
  95.         }
  96.     }
  97.  
  98.  
  99.     private void constructClouds(int amount){
  100.  
  101.         for(int i = 0; i < amount; i++)
  102.         {
  103.             rand = new Random();
  104.             float newBlackCloudX = (float) rand.nextInt((400 - 320) + 1) + 320;
  105.             float newBlackCloudY = (float) rand.nextInt((400 - 30) + 1) + 30;
  106.  
  107.             //To increase difficulty subtract the spawnrate from the velocity. This will make the clouds move faster as the screen gets filled with clouds.
  108.             float newBlackCloudVelocityX = -10;
  109.  
  110.             blackClouds.add(new BlackCloud(newBlackCloudX, newBlackCloudY, newBlackCloudVelocityX));
  111.         }
  112.     }
  113.  
  114.     private void spawnClouds()
  115.     {
  116.         if(blackClouds.size() == 0)
  117.         {
  118.             constructClouds(1);
  119.         }
  120.  
  121.         if(blackClouds.size() < spawnRate)
  122.         {
  123.             constructClouds(spawnRate);
  124.         }
  125.  
  126.  
  127.     }
  128.  
  129.  
  130.  
  131.     private void collideBalloonBlackCloud(float deltaTime)
  132.     {
  133.         int stop = blackClouds.size();
  134.         BlackCloud blackCloud;
  135.  
  136.         for(int i = 0; i < stop; i++)
  137.         {
  138.             blackCloud = blackClouds.get(i);
  139.  
  140.             if(collideRects(balloon.x, balloon.y, Balloon.WIDTH, Balloon.HEIGHT, blackCloud.x, blackCloud.y, BlackCloud.WIDTH, BlackCloud.HEIGHT))
  141.             {
  142.  
  143.                 stop--;
  144.                 listener.collisionBlackCloud();
  145.  
  146.  
  147.  
  148.                 lives--;
  149.  
  150.                 blackClouds.remove(i);
  151.                 return;
  152.  
  153.  
  154.             }
  155.         }
  156.     }
  157.  
  158.     private boolean collideRects(float x, float y, float width, float height, float x2, float y2, float width2, float height2)
  159.     {
  160.         if(x < x2 + width2 &&
  161.                 x + width > x2 &&
  162.                 y < y2 + height2 &&
  163.                 y + height > y2)
  164.         {
  165.             return true;
  166.         }
  167.  
  168.         return false;
  169.     }
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement