让玩家走向鼠标 - Slick& Java的

时间:2013-06-29 20:14:54

标签: java lwjgl slick2d

所以这是我的问题:我是一个(新的)java程序员,我需要帮助我用Slick和LWJGL开发的游戏。我想要做的事情非常简单:按住鼠标按钮后,让播放器(或代表播放器的图像)朝向鼠标。我不希望它向玩家旋转。这是我的“Play”课程的样子:

package com.stuntmania.game;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Play extends BasicGameState implements ComponentListener {

    Animation player, movingUp, movingDown, movingLeft, movingRight;
    Image worldMap, fog;
    int[] duration = { 200, 200 };

    float playerPositionX = 0, playerPositionY = 0;
    float shiftX = playerPositionX + (Game.WIDTH / 2), shiftY = playerPositionY + (Game.HEIGHT / 2) - 200;
    float fogPercent = 0.0f;
    int targetX, targetY;

    private MouseOverArea mainMenu, quitGame, resume;

    boolean escape = false, goToMainMenuState = false;
    static boolean debugMode = false;

    public Play(int state) {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        worldMap = new Image("res/world.png");
        fog = new Image("res/fog.png");

        Image[] walkUp = { new Image("res/buckysBack.png"), new Image("res/buckysBack.png") };
        Image[] walkDown = { new Image("res/buckysFront.png"), new Image("res/buckysBack.png") };
        Image[] walkLeft = { new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png") };
        Image[] walkRight = { new Image("res/buckysRight.png"), new Image("res/buckysRight.png") };
        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);
        player = movingDown;

        mainMenu = new MouseOverArea(gc, new Image("res/MainMenu1.png"), gc.getWidth() / 2 - 35, 200, this);
        quitGame = new MouseOverArea(gc, new Image("res/QuitGame1.png"), gc.getWidth() / 2 - 35, 250, this);
        resume = new MouseOverArea(gc, new Image("res/Resume1.png"), gc.getWidth() / 2 - 35, 300, this);

        mainMenu.setMouseOverImage(new Image("res/MainMenu2.png"));
        quitGame.setMouseOverImage(new Image("res/QuitGame2.png"));
        resume.setMouseOverImage(new Image("res/Resume2.png"));
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        worldMap.draw(playerPositionX - 80, playerPositionY);

        g.setColor(new Color(0.0f, 0.0f, 0.0f, fogPercent));
        g.drawImage(fog, shiftX - 481, shiftY - 760, (new Color(1f, 1f, 1f, fogPercent)));
        player.draw(shiftX, shiftY);
        g.setColor(Color.darkGray);
        g.drawString("Y: " + playerPositionY + " X: " + playerPositionX, 100, 200);

        if (escape == true) {
            mainMenu.render(gc, g);
            quitGame.render(gc, g);
            resume.render(gc, g);
            if (escape == false) {
                g.clear();
            }
        }
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        // //////////////////////////////////// CALCS. /////////////////////////////////////////////////
        if (playerPositionY > -1000) {
            fogPercent = playerPositionY / -1000;
        }

        // //////////////////////////////////// INPUT GESTION //////////////////////////////////////////////////

        // Everything here is not applicable when you're in pause menu
        if (escape == false) {

            // move towards mouse


            if (input.isKeyDown(Input.KEY_W)) {
                player = movingUp;
                playerPositionY += delta * .15f;
                if (playerPositionY > 0 /* si peux pas monter plus haut ICI */) {
                    playerPositionY -= delta * .15f;
                }
            }

            if (input.isKeyDown(Input.KEY_S)) {
                player = movingDown;
                playerPositionY -= delta * .1f;
            }

            if (input.isKeyDown(Input.KEY_A)) {
                player = movingLeft;
                playerPositionX += delta * .1f;
            }

            if (input.isKeyDown(Input.KEY_D)) {
                player = movingRight;
                playerPositionX -= delta * .1f;
            }
        }
            if (input.isKeyPressed(Input.KEY_ESCAPE)) {
            escape = !escape;
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // activate debugmode
        if (input.isKeyPressed(Input.KEY_EQUALS)) {
            debugMode = !debugMode;
        }

        // Menu pause
        if (escape == true) {
            if (input.isKeyDown(Input.KEY_R)) {
                escape = false;
            }

            if (input.isKeyDown(Input.KEY_Q)) {
                System.exit(0);
            }

            if (input.isKeyDown(Input.KEY_M) || goToMainMenuState == true) {
                try {
                    Thread.sleep(150);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sbg.enterState(Game.MENU);
                goToMainMenuState = false;
                escape = false;
            }
        }
    }

    // button gestion
    public void componentActivated(AbstractComponent source) {
        if (source == quitGame) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.exit(0);
        }

        if (source == resume) {
            escape = false;
        }

        if (source == mainMenu) {
            goToMainMenuState = true;
        }
    }

    public int getID() {
        return Game.PLAY;
    }

    public static boolean getDebugMode() {
        return debugMode;
    }
}

不要担心雾/调试模式,我仍然在努力。

是的,我搜索过这已经存在了,不,我找不到任何我想要的东西。

那么,谁能告诉我一个简单的方法呢?我想详细解释复杂的数学。谢谢:))

1 个答案:

答案 0 :(得分:1)

如果您使用LWJGL Display渲染对象,则可以使用org.lwjgl.input.Mouse来获取当前鼠标位置和按钮状态。
你会有类似的东西:

if (Mouse.isButtonDown(0)) {
    Vec2d mousePos = translateToWorldCoordinates(new Vec2d(Mouse.getX(), Mouse.getY()));
    player.getPosition().add(mousePos.sub(player.getPosition()).normalize().mult(player.getSpeed()));
}

在你的周期中。

您可以使用org.newdawn.slick.Input - 它有getMouseX()getMouseY方法。