Videogioco Java, problema salto

Pubblicità

CuzzinoSan

Nuovo Utente
Messaggi
30
Reazioni
10
Punteggio
31
Ciao a tutti, sto programmando un gioco 2d platform ( stile mario ). Sono fermo da un pò di giorni ormai sull'implementazione del salto. Il concetto del salto penso di averlo capito, però non riesco ad implementarlo. Se qualcuno riesce a darmi una mano gli sarei molto grato.

Allego la classe player, dove è presente un mio tentativo non funzionante, e la classe keyManager.
Grazie in anticipo.

Java:
package game.entity;

import game.CollisionManager;
import game.GamePanel;
import game.KeyManager;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Player extends Entity {

    GamePanel gamePanel;
    KeyManager keyManager;
    CollisionManager collisionManager;
    public int screenX;
    public int screenY;
    public int maxHeight;

    public Player(GamePanel gamePanel, KeyManager keyManager) {
        this.gamePanel = gamePanel;
        this.keyManager = keyManager;
        this.collisionManager = new CollisionManager(gamePanel);

        screenX = gamePanel.screenWidth / 2 - (gamePanel.tileSize / 2);
        screenY = gamePanel.screenHeight / 2 - (gamePanel.tileSize / 2);

        solidArea = new Rectangle(20, 0, 15, gamePanel.tileSize);

        setDefaultValue();
        getPlayerImage();
    }

    public Player() {
    }


    public void setDefaultValue() {
        worldX = gamePanel.tileSize;
        worldY = gamePanel.tileSize * 7;
        maxHeight = (int) worldY;

        velY = 0;
        jumpVelocity = 50;
        speed = 3;
        direction = "idle";
    }

    public void getPlayerImage() {
        //TODO: Get player image
        try {
            up = ImageIO.read(getClass().getResourceAsStream("/game/textures/idle.png"));
            left1 = ImageIO.read(getClass().getResourceAsStream("/game/textures/left1.png"));
            left2 = ImageIO.read(getClass().getResourceAsStream("/game/textures/left2.png"));
            right1 = ImageIO.read(getClass().getResourceAsStream("/game/textures/right1.png"));
            right2 = ImageIO.read(getClass().getResourceAsStream("/game/textures/right2.png"));
            down = ImageIO.read(getClass().getResourceAsStream("/game/textures/down.png"));
            idle = ImageIO.read(getClass().getResourceAsStream("/game/textures/idle.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void update() {
        //worldY -= velY;
        if (keyManager.spacePressed || keyManager.downPressed || keyManager.leftPressed || keyManager.rightPressed) {

            if (keyManager.spacePressed) {
                //if(isOnPlatform()){
                setGravity(10.0);

                gravity -= 0.5;
                setVelY(10);
                worldY -= velY;
                velY -= 0.5;
                if (gravity <= 0) {
                    falling = true;
                    jumping = false;
                }
                if (falling) {
                    System.out.println("falling");
                    velY += 0.1;
                    worldY += velY;
                }
            }



            //}
        }
        if (keyManager.downPressed) {
            direction = "down";

        }
        if (keyManager.leftPressed) {

            direction = "left";
        }
        if (keyManager.rightPressed) {
            direction = "right";
        }
        //Check collision
        collisionOn = false;
        gamePanel.collisionManager.checkTile(this);
        if (!collisionOn) {
            switch (direction) {
                case "left":
                    worldX -= speed;
                    break;
                case "right":
                    worldX += speed;
                    break;
                case "down":
                    worldY += speed;
                    break;
            }
        }

        // Animation changer
        spriteCounter++;
        if (spriteCounter > 15) {
            spriteNumber = (spriteNumber == 1) ? 2 : 1;
            spriteCounter = 0;
        }
    }


    public void draw(Graphics2D g2) {
        //g2.setColor(Color.WHITE);
        //g2.fillRect(x, y, gamePanel.tileSize, gamePanel.tileSize);
        BufferedImage image = null;
        switch (direction) {
            case "space":
                image = idle;
                break;
            case "down":
                image = down;
                break;
            case "left":
                if (spriteNumber == 1)
                    image = left1;
                if (spriteNumber == 2)
                    image = left2;
                break;
            case "right":
                if (spriteNumber == 1)
                    image = right1;
                if (spriteNumber == 2)
                    image = right2;
                break;

            case "idle":
                image = idle;
        }

        g2.drawImage(image, screenX, screenY, gamePanel.tileSize, gamePanel.tileSize, null);
    }

    void jump() {
        if (!jumping) {
            jumping = true;
            gravity -= 0.1;
            setVelY(-gravity);
            worldY -= gravity;
            System.out.println(jumping);
        }
        if (gravity <= 0) {
            System.out.println(jumping);
            jumping = false;
            falling = true;
            worldY += gravity;
        }
    }

    @Override
    public boolean isOnPlatform() {
        gamePanel.collisionManager.entityBottomRow = (gamePanel.collisionManager.entityBottomWorldY + speed) / gamePanel.tileSize;
        gamePanel.collisionManager.tileNum1 = gamePanel.tileManager.mapTileNum[gamePanel.collisionManager.entityLeftCol][gamePanel.collisionManager.entityBottomRow];
        gamePanel.collisionManager.tileNum2 = gamePanel.tileManager.mapTileNum[gamePanel.collisionManager.entityRightCol][gamePanel.collisionManager.entityBottomRow];
        if ((gamePanel.tileManager.tiles[gamePanel.collisionManager.tileNum1].collision || gamePanel.tileManager.tiles[gamePanel.collisionManager.tileNum2].collision)) {
            onPlatform = true;
        } else {
            onPlatform = false;
        }

        return onPlatform;
    }


}
Java:
package game;

import game.entity.Entity;
import game.entity.Player;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyManager implements KeyListener {

    public boolean spacePressed,downPressed,leftPressed,rightPressed;
    public Player player= new Player();



    @Override
    public void keyTyped(KeyEvent e) {
        //Nothing
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();

        if(code == KeyEvent.VK_SPACE){
            //player.setJumping(true);
            spacePressed = true;
        } else if(code == KeyEvent.VK_A){
            leftPressed = true;
        } else if (code == KeyEvent.VK_D) {
            rightPressed = true;
        } else if (code == KeyEvent.VK_S){
            downPressed = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();

        if(code == KeyEvent.VK_W || code == KeyEvent.VK_SPACE){
            spacePressed = false;
        }
        if(code == KeyEvent.VK_A){
            leftPressed = false;
        }
        if(code == KeyEvent.VK_S){
            downPressed = false;
        }
        if(code == KeyEvent.VK_D){
            rightPressed = false;
        }
    }
}
 
Vorrei poterti aiutare ma di programmazione, anche a livelli basilari, non ci capisco una mazza! Però ti sono vicino, faccio il tifo per te!
 
Pubblicità
Pubblicità
Indietro
Top