Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.emilnielsen.cloudhopper.CloudHopperGame;
- import android.util.Log;
- import com.example.emilnielsen.cloudhopper.CollisionListener;
- import com.example.emilnielsen.cloudhopper.Game;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- /**
- * Created by emilnielsen on 03/05/16.
- */
- public class World
- {
- //removed game game
- public static final float min_X = 0;
- public static final float max_X = 319;
- public static final float min_Y = 0;
- public static final float max_Y = 479;
- Random rand;
- int points = 0;
- int lives = 3;
- int spawnRate = 0;
- boolean gameover = false;
- Balloon balloon = new Balloon();
- BlackCloud blackCloud;
- CollisionListener listener;
- List<BlackCloud> blackClouds = new ArrayList<>();
- public World(CollisionListener listener)
- {
- this.listener = listener;
- }
- public void update(Game game, float deltaTime, float accelX, float accelY)
- {
- points = points + 2;
- spawnRate = (int)Math.ceil(points/1000);
- if(balloon.y + Balloon.HEIGHT + 5 > max_Y || balloon.y < min_Y + 24)
- {
- gameover = true;
- return;
- }
- balloon.x = balloon.x - (accelX * 50 * deltaTime);
- balloon.y = balloon.y + (accelY * 50 * deltaTime);
- if(balloon.x < min_X)
- {
- balloon.x = min_X;
- }
- if(balloon.x + balloon.WIDTH > max_X)
- {
- balloon.x = max_X - balloon.WIDTH;
- }
- spawnClouds();
- moveClouds(deltaTime);
- collideBalloonBlackCloud(deltaTime);
- }
- private void moveClouds(float deltaTime){
- int stop = blackClouds.size();
- for(int i = 0; i < stop; i++){
- try
- {
- blackCloud = blackClouds.get(i);
- blackCloud.x = blackCloud.x + (blackCloud.velocityX * 10 * deltaTime);
- if (blackCloud.x < min_X - BlackCloud.WIDTH)
- {
- blackClouds.remove(i);
- }
- }catch (IndexOutOfBoundsException e){
- Log.d("ArrayOutOfBounds", "Cloud was removed");
- }
- }
- }
- private void constructClouds(int amount){
- for(int i = 0; i < amount; i++)
- {
- rand = new Random();
- float newBlackCloudX = (float) rand.nextInt((400 - 320) + 1) + 320;
- float newBlackCloudY = (float) rand.nextInt((400 - 30) + 1) + 30;
- //To increase difficulty subtract the spawnrate from the velocity. This will make the clouds move faster as the screen gets filled with clouds.
- float newBlackCloudVelocityX = -10;
- blackClouds.add(new BlackCloud(newBlackCloudX, newBlackCloudY, newBlackCloudVelocityX));
- }
- }
- private void spawnClouds()
- {
- if(blackClouds.size() == 0)
- {
- constructClouds(1);
- }
- if(blackClouds.size() < spawnRate)
- {
- constructClouds(spawnRate);
- }
- }
- private void collideBalloonBlackCloud(float deltaTime)
- {
- int stop = blackClouds.size();
- BlackCloud blackCloud;
- for(int i = 0; i < stop; i++)
- {
- blackCloud = blackClouds.get(i);
- if(collideRects(balloon.x, balloon.y, Balloon.WIDTH, Balloon.HEIGHT, blackCloud.x, blackCloud.y, BlackCloud.WIDTH, BlackCloud.HEIGHT))
- {
- stop--;
- listener.collisionBlackCloud();
- lives--;
- blackClouds.remove(i);
- return;
- }
- }
- }
- private boolean collideRects(float x, float y, float width, float height, float x2, float y2, float width2, float height2)
- {
- if(x < x2 + width2 &&
- x + width > x2 &&
- y < y2 + height2 &&
- y + height > y2)
- {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement