samedi 15 août 2015

Random generation of textures in libGDX in android

I have class Scrollable. All classes with obstacles extends scrollable. I want to do one class Obstacles, whose random generated all obstacles.(Textures = obstacles without collision)

class Scrollabel

public class Scrollable {

protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledDown;

public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
    position = new Vector2(x, y);
    velocity = new Vector2(0, scrollSpeed);
    this.width = width;
    this.height = height;
    isScrolledDown = false;
}

public void update(float delta) {
    position.add(velocity.cpy().scl(delta));

    if (position.y - height > 0) {
        isScrolledDown = true;
    }
}

// Reset
public void reset(float newY) {
    position.y = newY;
    isScrolledDown = false;
}

public boolean isScrolledDown() {
    return isScrolledDown;
}

public float getTailY() {
    return (position.y - height);
}

public float getX() {
    return position.x;
}

public float getY() {
    return position.y;
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}}

Class ScrollHandler

public class ScrollHandler {

private Grass frontGrass1, backGrass1, frontGrass2, backGrass2;
private Pipe pipe1, pipe2, pipe3;
public static final int SCROLL_SPEED = 59;
public static final int PIPE_GAP = 49;

public ScrollHandler(float yPos) {
    frontGrass1 = new Grass(0, yPos, 22, 800, SCROLL_SPEED);
    backGrass1 = new Grass(0, frontGrass1.getTailY(), 22, 800,
            SCROLL_SPEED);
    frontGrass2 = new Grass(460, yPos, 22, 800, SCROLL_SPEED);
    backGrass2 = new Grass(460, frontGrass2.getTailY(), 22, 800,
            SCROLL_SPEED);

    pipe1 = new Pipe(210, 0, 22, 60, SCROLL_SPEED, yPos);
    pipe2 = new Pipe(0, pipe1.getTailY() + PIPE_GAP, 22, 70, SCROLL_SPEED, yPos);
    pipe3 = new Pipe(0, pipe2.getTailY() + PIPE_GAP, 22, 60, SCROLL_SPEED, yPos);
}

public void update(float delta) {
    // обновляем все объекты
    frontGrass1.update(delta);
    backGrass1.update(delta);
    frontGrass2.update(delta);
    backGrass2.update(delta);
    pipe1.update(delta);
    pipe2.update(delta);
    pipe3.update(delta);
    // проверяем кто из объектов за левой границей
    // и соответственно сбрасываем параметры этого объекта
    if (pipe1.isScrolledDown()) {
        pipe1.reset(pipe3.getTailY() + PIPE_GAP);
    } else if (pipe2.isScrolledDown()) {
        pipe2.reset(pipe1.getTailY() + PIPE_GAP);

    } else if (pipe3.isScrolledDown()) {
        pipe3.reset(pipe2.getTailY() + PIPE_GAP);
    }
    if (frontGrass1.isScrolledDown()) {
        frontGrass1.reset(backGrass1.getTailY());

    } else if (backGrass1.isScrolledDown()) {
        backGrass1.reset(frontGrass1.getTailY());
    }

    if (frontGrass2.isScrolledDown()) {
        frontGrass2.reset(backGrass2.getTailY());

    } else if (backGrass2.isScrolledDown()) {
        backGrass2.reset(frontGrass2.getTailY());
    }
}

public Grass getFrontGrass1() {
    return frontGrass1;
}

public Grass getBackGrass1() {
    return backGrass1;
}

public Grass getFrontGrass2() {
    return frontGrass2;
}

public Grass getBackGrass2() {
    return backGrass2;
}

public Pipe getPipe1() {
    return pipe1;
}

public Pipe getPipe2() {
    return pipe2;
}

public Pipe getPipe3() {
    return pipe3;
}}

Example class Pipe

public class ScrollHandler {

private Grass frontGrass1, backGrass1, frontGrass2, backGrass2;
private Pipe pipe1, pipe2, pipe3;
public static final int SCROLL_SPEED = 59;
public static final int PIPE_GAP = 49;

public ScrollHandler(float yPos) {
    frontGrass1 = new Grass(0, yPos, 22, 800, SCROLL_SPEED);
    backGrass1 = new Grass(0, frontGrass1.getTailY(), 22, 800,
            SCROLL_SPEED);
    frontGrass2 = new Grass(460, yPos, 22, 800, SCROLL_SPEED);
    backGrass2 = new Grass(460, frontGrass2.getTailY(), 22, 800,
            SCROLL_SPEED);

    pipe1 = new Pipe(210, 0, 22, 60, SCROLL_SPEED, yPos);
    pipe2 = new Pipe(0, pipe1.getTailY() + PIPE_GAP, 22, 70, SCROLL_SPEED, yPos);
    pipe3 = new Pipe(0, pipe2.getTailY() + PIPE_GAP, 22, 60, SCROLL_SPEED, yPos);
}

public void update(float delta) {
    frontGrass1.update(delta);
    backGrass1.update(delta);
    frontGrass2.update(delta);
    backGrass2.update(delta);
    pipe1.update(delta);
    pipe2.update(delta);
    pipe3.update(delta);
    if (pipe1.isScrolledDown()) {
        pipe1.reset(pipe3.getTailY() + PIPE_GAP);
    } else if (pipe2.isScrolledDown()) {
        pipe2.reset(pipe1.getTailY() + PIPE_GAP);

    } else if (pipe3.isScrolledDown()) {
        pipe3.reset(pipe2.getTailY() + PIPE_GAP);
    }
    if (frontGrass1.isScrolledDown()) {
        frontGrass1.reset(backGrass1.getTailY());

    } else if (backGrass1.isScrolledDown()) {
        backGrass1.reset(frontGrass1.getTailY());
    }

    if (frontGrass2.isScrolledDown()) {
        frontGrass2.reset(backGrass2.getTailY());

    } else if (backGrass2.isScrolledDown()) {
        backGrass2.reset(frontGrass2.getTailY());
    }
}

public Grass getFrontGrass1() {
    return frontGrass1;
}

public Grass getBackGrass1() {
    return backGrass1;
}

public Grass getFrontGrass2() {
    return frontGrass2;
}

public Grass getBackGrass2() {
    return backGrass2;
}

public Pipe getPipe1() {
    return pipe1;
}

public Pipe getPipe2() {
    return pipe2;
}

public Pipe getPipe3() {
    return pipe3;
}}




Aucun commentaire:

Enregistrer un commentaire