Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Rectangle;
- import java.util.Random;
- public class BouncyHelicopter extends MyGdxGame {
- Helicopter[] helicopters;
- int numberOfHelicopters;
- Random random;
- Helicopter toSpawn;
- public BouncyHelicopter(MyGdxGame newParent) {
- parent = newParent;
- batch = new SpriteBatch();
- font = new BitmapFont();
- random = new Random();
- // How manny choppers do we want to spawn
- numberOfHelicopters = 2;
- helicopters = new Helicopter[numberOfHelicopters];
- // Spawn chopper on position i in the matrix
- for (int i = 0; i < numberOfHelicopters; i++) {
- //Give the new helicopter apropriate speed and adds it to the array
- toSpawn = new Helicopter();
- toSpawn.xspeed = 1;
- toSpawn.yspeed = 1;
- helicopters[i] = toSpawn;
- //for use later
- Helicopter prevCopter;
- // Now i have give it a position that is not inside the other choppers
- boolean collision = true;
- while (collision) {
- // Generate a suggested position
- int xvalue = random.nextInt((int) toSpawn.xmax + 1);
- int yvalue = random.nextInt((int) toSpawn.ymax + 1);
- // See if the chopper is alone
- if (i > 0) {
- // Go trough all the previous choppers
- for (int j = 0; j < i; j++) {
- // Selects the chopper we are looking at
- prevCopter = helicopters[j];
- //check if the chopper box is intersecting
- if (notColiding(xvalue + toSpawn.width, xvalue, prevCopter.xvalue, prevCopter.xvalue + prevCopter.width)
- || notColiding(yvalue + toSpawn.height, yvalue, prevCopter.yvalue, prevCopter.yvalue + prevCopter.height)) {
- //no colisions yay, update position of to spawn
- toSpawn.xvalue = xvalue;
- toSpawn.yvalue = yvalue;
- //break loop
- collision = false;
- } else {
- // redo, log incase endless loop
- Gdx.app.log("Colision", String.valueOf(i) + ":" + String.valueOf(xvalue) + ", " + String.valueOf(yvalue));
- }
- }
- // approves of the first one by default
- } else {
- helicopters[i].xvalue = xvalue;
- helicopters[i].yvalue = yvalue;
- Gdx.app.log("GoodValue", String.valueOf(i) + ":" + String.valueOf(xvalue) + ", " + String.valueOf(yvalue));
- collision = false;
- }
- }
- }
- }
- private void updatePosition() {
- //iterate over helicopters
- for (Helicopter basicHelicopter: helicopters) {
- //Do it again
- for (Helicopter targetHelicopter: helicopters){
- // check if we are looking at ourselves
- if (!((basicHelicopter.xvalue == targetHelicopter.xvalue) && (basicHelicopter.yvalue == targetHelicopter.yvalue))){
- // ok, we have to choppers, now to see if they are colliding.
- //is my ass in the same position as your face or vise versa?
- if (basicHelicopter.xvalue == targetHelicopter.xvalue + targetHelicopter.width
- || targetHelicopter.xvalue == basicHelicopter.xvalue + basicHelicopter.width){
- // and are the the faces actually in the same y plane?
- if (!notColiding(
- basicHelicopter.yvalue + basicHelicopter.height,
- basicHelicopter.yvalue + 0,
- targetHelicopter.yvalue + 0,
- targetHelicopter.yvalue + targetHelicopter.height )){
- //yes, well, i think everyone needs to back up here.
- basicHelicopter.xspeed = -basicHelicopter.xspeed;
- targetHelicopter.xspeed = -targetHelicopter.xspeed;
- }
- }
- // is my top in somones bottom, or vise versa?
- if (basicHelicopter.yvalue == targetHelicopter.yvalue + targetHelicopter.height
- || targetHelicopter.yvalue == basicHelicopter.yvalue + basicHelicopter.height){
- // are we in the same x plane?
- if (!notColiding(
- basicHelicopter.xvalue+ basicHelicopter.width,
- basicHelicopter.xvalue+ 0,
- targetHelicopter.xvalue +0,
- targetHelicopter.xvalue+ targetHelicopter.width)){
- Gdx.app.log("Chage Dir", "we actually did it?");
- // yes, respect my personal space pleese.
- basicHelicopter.yspeed = -basicHelicopter.yspeed;
- targetHelicopter.yspeed = -targetHelicopter.yspeed;
- }
- }
- }
- else{
- // looking at ourselves, logged some shit, might need again.
- }
- }
- // move the helecopter acording to speed
- basicHelicopter.xvalue += basicHelicopter.xspeed;
- basicHelicopter.yvalue += basicHelicopter.yspeed;
- // chech colisions with walls
- if (basicHelicopter.xvalue < basicHelicopter.xmin || basicHelicopter.xvalue > basicHelicopter.xmax) {
- basicHelicopter.xspeed = -basicHelicopter.xspeed;
- //basicHelicopter.img.flip(true, false);
- }
- if (basicHelicopter.yvalue < basicHelicopter.ymin || basicHelicopter.yvalue > basicHelicopter.ymax) {
- basicHelicopter.yspeed = -basicHelicopter.yspeed;
- }// repeat of all helicopters
- }
- }
- // returnerer true hvis x og y ikke deler samme område
- private boolean notColiding(float xmax, float xmin, float ymin, float ymax){
- Gdx.app.log("notColiding", String.valueOf((xmax < ymin || xmin > ymax)));
- return (xmax < ymin || xmin > ymax);
- }
- @Override
- public void currentRender() {
- Gdx.gl.glClearColor(0f, 0f, 1f, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- font.draw(batch, "Space To Continue", 10f, Gdx.graphics.getHeight());
- for (Helicopter helicopter: helicopters) {
- batch.draw(helicopter.img, helicopter.xvalue, helicopter.yvalue);
- }
- batch.end();
- stupidKeyFunction();
- updatePosition();
- }
- @Override
- public void stupidKeyFunction(){
- if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
- Gdx.app.debug("space", "spacepress");
- }
- if (Gdx.input.isKeyJustPressed(Input.Keys.A)){
- Gdx.app.debug("space", this.name);
- }
- }
- @Override
- public void dispose() {
- batch.dispose();
- //helicopter.imgTest.dispose();
- font.dispose();
- }
- @Override
- public void create() {
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement