Adding floor based on image or how to optimize pre-existing method

Hay E :

I'm creating a game with Java for a school project. I currently have a Player class and I am creating the floor he will walk on inside it. Right now, I just made a Clamp method where I add different sections based on the image so he walks on the 'floor'. Eventually, I have platforms that he needs to be able to jump on, but fall if he misses. That code is currently not working and I'm not sure how to go about it. Is there an easier or more efficient way to do the floor? If not, how can I make the platforms work?

I've already tried using a variable at the end of the method to test if it is a platform or not.

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);
    }


}

The floor needs to work properly in all places where there are no platforms. He just walks on it. Otherwise, if he is landing on a platform he needs to stay on it until that platform ends, then he falls. Or, if he misses a platform then he falls.

Hay E :

Figured it out

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);
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=105971&siteId=1