Adición de piso, según la imagen o la forma de optimizar el método de pre-existente

Hay E:

Estoy creando un juego con Java para un proyecto escolar. Actualmente tengo una clase de jugador y estoy creando el suelo que va a caminar en su interior. En este momento, acabo de hacer un método Clamp donde agrego diferentes secciones sobre la base de la imagen de modo que pisa el 'piso'. Con el tiempo, tengo plataformas que tiene que ser capaz de saltar, pero caer si se pierde. Ese código actualmente no está funcionando y no estoy seguro de cómo hacerlo. ¿Hay una manera más fácil o más eficiente de hacer el suelo? Si no es así, ¿cómo puedo hacer el trabajo de las plataformas?

Ya he intentado usar una variable al final del método a prueba de si se trata de una plataforma o no.

package com.sewd.sophomorevisitation.objects;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.sewd.sophomorevisitation.Background;
import com.sewd.sophomorevisitation.KeyListener;
import com.sewd.sophomorevisitation.Main;
import com.sewd.sophomorevisitation.sprites.SpriteAnimation;
import com.sewd.sophomorevisitation.sprites.SpriteSheet;

public class Player extends GameObject {

    Handler handler;
    static int times = 0;

    private static BufferedImage[] shreccRun = {SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(1, 0, 128, 106, 0, 0), SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(2, 0, 128, 106, 0, 0)};
    private static BufferedImage[] shreccJump = {SpriteSheet.getSprite(3, 0, 128, 106, 0, 0), SpriteSheet.getSprite(4, 0, 128, 106, 0, 0)};
    public static SpriteAnimation shreccRunAnimation = new SpriteAnimation(shreccRun, 5);
    public static SpriteAnimation shreccJumpAnimation = new SpriteAnimation(shreccJump, 5);
    public static SpriteAnimation animation = shreccRunAnimation;


    public Player(int x, int y, ID id) {
        super(x, y, id);
    }

    public void tick() {
        y += velY;

        if(x < 0) {
            death();
        }

        if(Main.started) {
            /*floor(1076, 1215, -500, Main.screenSize.width, 0, 972);
            floor(1215, 1374, -500, Main.screenSize.width, 0, 969);
            floor(1374, 1538, -500, Main.screenSize.width, 0, 936);
            floor(1538, 1667, -500, Main.screenSize.width, 0, 925);
            floor(1667, 3711, -500, Main.screenSize.width, 0, 919);*/
            floor(0, 473, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 40);
            floor(473, 600, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 70);
            floor(600, 775, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 90);
            floor(775, 2900, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 100);
            floor(2900, 2980, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 140);
            floor(2980, 3060, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 190);
            floor(2980, 3060, -500, Main.screenSize.width, 0, 873);
            floor(3060, 3073, -500, Main.screenSize.width, 0, 843);
            floor(3073, 3827, -500, Main.screenSize.width, 0, 813);
            floor(3827, 3857, -500, Main.screenSize.width, 0, 784);
            floor(3827, 10000, -500, Main.screenSize.width, 0, 784);

        }

        if(animation != shreccJumpAnimation) {
            animation.update();
        }else {
            if(times <= 5)animation.update();
            times++;
        }
    }

    public void death() {
        System.out.println("Dead");
    }

    public static void floor(int start, int stop, int minX, int maxX, int minY, int maxY) {
        if( start <= Background.x && Background.x < stop) Player.clamp(minX, maxX, minY, maxY);
    }

    public static void clamp(int minX, int maxX, int minY, int maxY) {
        if(x <= minX) x = minX;
        else if(x >= maxX) x = maxX;
        else if(y <= minY) {
            y = minY;
            KeyListener.isInAir = true;
            velY = -velY;
            if(velY == 0) velY = 10;
        }else if(y >= maxY) {
            y = maxY;
            KeyListener.isInAir = false;
            if(times > 0) {
                times = 0;
                animation.stop();
                animation.restart();
                animation = shreccRunAnimation;
                animation.start();
            }

        }else return;
    }

    public void render(Graphics g) {
        g.drawImage(animation.getSprite(), x - shreccRun[0].getWidth() - 50, y, shreccRun[0].getWidth() * 2, shreccRun[0].getHeight() * 2, null);
    }


}

La planta necesita para funcionar correctamente en todos los lugares donde no hay plataformas. Él camina sobre ella. De lo contrario, si él está aterrizando en una plataforma que necesita para permanecer en él hasta que los extremos de la plataforma, y ​​luego cae. O, si se pierde una plataforma luego se cae.

Hay E:

Lo averigué

public static boolean onFloor;


public void tick() {
    if(Main.isStarted()) {

        floor();

        if(!onFloor) {
            Player.setVelY(20);
        }else {
            Player.setVelY(0);
        }
    }
}

private void floor() {
    clamp(0, 5000, 824, "f");
    clamp(5000, 10000, 750, "p");
}

private void clamp(int start, int stop, int minY, String type) {
    if(x > start && x < stop) {
        if(Player.getY() != minY) {
            if(Player.getY() > minY) {
                if(type != "p") {
                    Player.setY(minY);
                    Player.setVelY(0);
                    onFloor = true;
                    return;
                }
            }
            if(!Player.jumping) {
                Player.setVelY(10);
                onFloor = false;
            }
        }else if(Player.getY() == minY) {
            Player.setY(minY);
            Player.setVelY(0);
            onFloor = true;
        }else if(Player.getY() > minY) {
            if(type != "p") {
                Player.setY(minY);
                Player.setVelY(0);
            }
        }
    }
}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=215218&siteId=1
Recomendado
Clasificación