使播放器趋向于鼠标-Slick&爪哇 [英] Make the player go towards the mouse - Slick & Java

查看:91
本文介绍了使播放器趋向于鼠标-Slick&爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的问题:我是一名(新)java程序员,我需要与Slick和LWJGL一起开发的游戏的帮助.我想做的事情非常简单:按住鼠标按钮后,使播放器(或代表播放器的图像)朝向鼠标.我不希望它向播放器旋转.这是我的播放"类的样子:

So here is my problem: I'm a (new) java programmer and I need help for the game I am developing with Slick and LWJGL. What I want to do is really simple: make the player (or the image representing the player) head towards the mouse once I hold down the mouse button. I don't want it to rotate towards the player. Here's what my "Play" class looks like:

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

不用担心雾化/调试模式,我仍在努力.

Don't worry about fog/debug mode, I'm still working on those.

是的,我已经搜索过它是否已经存在,不,我找不到要搜索的内容.

Yes, I've searched is this already existed and no, I couldn't find anything I was searching for.

那么,有人可以告诉我一种简单的方法吗?我想详细解释复杂的数学.谢谢:)

So, can anyone tell me a simple way to do it? I'd like detailed explanation about complicated maths. Thanks :)

推荐答案

如果使用LWJGL Display渲染对象,则可以简单地使用org.lwjgl.input.Mouse获取当前鼠标位置和按钮状态.
您将看到类似这样的内容:

If you use a LWJGL Display to render objects, you can simply use org.lwjgl.input.Mouse to get current mouse position and button state.
You will have something like:

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方法.

And you can use org.newdawn.slick.Input - it has getMouseX() and getMouseY methods.

这篇关于使播放器趋向于鼠标-Slick&爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆