Advertisement
fstorehaug

Untitled

Jan 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.63 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.math.Rectangle;
  9.  
  10. import java.util.Random;
  11.  
  12. public class BouncyHelicopter extends MyGdxGame {
  13.  
  14.     Helicopter[] helicopters;
  15.     int numberOfHelicopters;
  16.     Random random;
  17.     Helicopter toSpawn;
  18.  
  19.     public BouncyHelicopter(MyGdxGame newParent) {
  20.         parent = newParent;
  21.         batch = new SpriteBatch();
  22.         font = new BitmapFont();
  23.         random = new Random();
  24.        
  25.         // How manny choppers do we want to spawn
  26.         numberOfHelicopters = 2;
  27.         helicopters = new Helicopter[numberOfHelicopters];
  28.  
  29.         // Spawn chopper on position i in the matrix
  30.         for (int i = 0; i < numberOfHelicopters; i++) {
  31.  
  32.             //Give the new helicopter apropriate speed and adds it to the array
  33.             toSpawn = new Helicopter();
  34.             toSpawn.xspeed = 1;
  35.             toSpawn.yspeed = 1;
  36.             helicopters[i] = toSpawn;
  37.             //for use later
  38.             Helicopter prevCopter;
  39.  
  40.             // Now i have give it a position that is not inside the other choppers
  41.             boolean collision = true;
  42.             while (collision) {
  43.                 // Generate a suggested position
  44.                 int xvalue = random.nextInt((int) toSpawn.xmax + 1);
  45.                 int yvalue = random.nextInt((int) toSpawn.ymax + 1);
  46.  
  47.                 // See if the chopper is alone
  48.                 if (i > 0) {
  49.                     // Go trough all the previous choppers
  50.                     for (int j = 0; j < i; j++) {
  51.                         // Selects the chopper we are looking at
  52.                         prevCopter = helicopters[j];
  53.  
  54.                             //check if the chopper box is intersecting
  55.                         if (notColiding(xvalue + toSpawn.width, xvalue, prevCopter.xvalue, prevCopter.xvalue + prevCopter.width)
  56.                             || notColiding(yvalue + toSpawn.height, yvalue, prevCopter.yvalue, prevCopter.yvalue + prevCopter.height)) {
  57.                             //no colisions yay, update position of to spawn
  58.                             toSpawn.xvalue = xvalue;
  59.                             toSpawn.yvalue = yvalue;
  60.                             //break loop
  61.                             collision = false;
  62.                         } else {
  63.                             // redo, log incase endless loop
  64.                             Gdx.app.log("Colision", String.valueOf(i) + ":" + String.valueOf(xvalue) + ", " + String.valueOf(yvalue));
  65.                         }
  66.                     }
  67.                     // approves of the first one by default
  68.                 } else {
  69.                     helicopters[i].xvalue = xvalue;
  70.                     helicopters[i].yvalue = yvalue;
  71.                     Gdx.app.log("GoodValue", String.valueOf(i) + ":" + String.valueOf(xvalue) + ", " + String.valueOf(yvalue));
  72.                     collision = false;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     private void updatePosition() {
  79.  
  80.         //iterate over helicopters
  81.         for (Helicopter basicHelicopter: helicopters) {
  82.             //Do it again
  83.             for (Helicopter targetHelicopter: helicopters){
  84.                 // check if we are looking at ourselves
  85.                 if (!((basicHelicopter.xvalue == targetHelicopter.xvalue) && (basicHelicopter.yvalue == targetHelicopter.yvalue))){
  86.                     // ok, we have to choppers, now to see if they are colliding.
  87.  
  88.                         //is my ass in the same position as your face or vise versa?
  89.                     if (basicHelicopter.xvalue == targetHelicopter.xvalue + targetHelicopter.width
  90.                     || targetHelicopter.xvalue == basicHelicopter.xvalue + basicHelicopter.width){
  91.  
  92.                     // and are the the faces actually in the same y plane?
  93.                         if (!notColiding(
  94.                             basicHelicopter.yvalue + basicHelicopter.height,
  95.                             basicHelicopter.yvalue + 0,
  96.                             targetHelicopter.yvalue + 0,
  97.                             targetHelicopter.yvalue + targetHelicopter.height )){
  98.  
  99.                             //yes, well, i think everyone needs to back up here.
  100.                             basicHelicopter.xspeed = -basicHelicopter.xspeed;
  101.                             targetHelicopter.xspeed = -targetHelicopter.xspeed;
  102.                         }
  103.                     }
  104.                         // is my top in somones bottom, or vise versa?
  105.                     if (basicHelicopter.yvalue == targetHelicopter.yvalue + targetHelicopter.height
  106.                     || targetHelicopter.yvalue == basicHelicopter.yvalue + basicHelicopter.height){
  107.                         // are we in the same x plane?
  108.                     if  (!notColiding(
  109.                             basicHelicopter.xvalue+ basicHelicopter.width,
  110.                             basicHelicopter.xvalue+ 0,
  111.                             targetHelicopter.xvalue +0,
  112.                             targetHelicopter.xvalue+ targetHelicopter.width)){
  113.                             Gdx.app.log("Chage Dir", "we actually did it?");
  114.                             // yes, respect my personal space pleese.
  115.                             basicHelicopter.yspeed = -basicHelicopter.yspeed;
  116.                             targetHelicopter.yspeed = -targetHelicopter.yspeed;
  117.                         }
  118.                     }
  119.                 }
  120.                 else{
  121.                     // looking at ourselves, logged some shit, might need again.
  122.                 }
  123.             }
  124.             // move the helecopter acording to speed
  125.             basicHelicopter.xvalue += basicHelicopter.xspeed;
  126.             basicHelicopter.yvalue += basicHelicopter.yspeed;
  127.  
  128.             // chech colisions with walls
  129.             if (basicHelicopter.xvalue < basicHelicopter.xmin || basicHelicopter.xvalue > basicHelicopter.xmax) {
  130.                 basicHelicopter.xspeed = -basicHelicopter.xspeed;
  131.                 //basicHelicopter.img.flip(true, false);
  132.             }
  133.             if (basicHelicopter.yvalue < basicHelicopter.ymin || basicHelicopter.yvalue > basicHelicopter.ymax) {
  134.                 basicHelicopter.yspeed = -basicHelicopter.yspeed;
  135.             }// repeat of all helicopters
  136.         }
  137.     }
  138.  
  139.     // returnerer true hvis x og y ikke deler samme område
  140.     private boolean notColiding(float xmax, float xmin, float ymin, float ymax){
  141.         Gdx.app.log("notColiding", String.valueOf((xmax < ymin || xmin > ymax)));
  142.         return (xmax < ymin || xmin > ymax);
  143.     }
  144.  
  145.     @Override
  146.     public void currentRender() {
  147.         Gdx.gl.glClearColor(0f, 0f, 1f, 1);
  148.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  149.  
  150.         batch.begin();
  151.         font.draw(batch, "Space To Continue", 10f, Gdx.graphics.getHeight());
  152.         for (Helicopter helicopter: helicopters) {
  153.             batch.draw(helicopter.img, helicopter.xvalue, helicopter.yvalue);
  154.         }
  155.         batch.end();
  156.         stupidKeyFunction();
  157.         updatePosition();
  158.     }
  159.  
  160.     @Override
  161.     public void stupidKeyFunction(){
  162.         if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
  163.             Gdx.app.debug("space", "spacepress");
  164.         }
  165.         if (Gdx.input.isKeyJustPressed(Input.Keys.A)){
  166.             Gdx.app.debug("space", this.name);
  167.         }
  168.     }
  169.  
  170.     @Override
  171.     public void dispose() {
  172.         batch.dispose();
  173.         //helicopter.imgTest.dispose();
  174.         font.dispose();
  175.     }
  176.  
  177.     @Override
  178.     public void create() {
  179.         return;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement