如何让我的形状“点”我的玩家

时间:2017-07-24 01:03:52

标签: java android libgdx

我正在尝试制作一个从顶部产生敌人的游戏(如垂直滚动游戏),并且一种敌人在下降时基本上会遵循玩家的X坐标。敌人类被称为追随者,现在它确实指向玩家(参见追随者的更新功能),但它并不像我需要的那样准确。例如,如果玩家加速,则跟随者将无法一直看到他。

一种看待它的方法是,我希望玩家的位置成为辐射系统中的坐标,并使我的追随者的顶点准确地旋转并创建一条直线,每一帧都看着它

这是跟随者类:

public class Follower {

Player target; //follow this

//position
private Vector2 position;
private float x;
private float y;

//speed
private Vector2 velocity;
private float speed;
private float radians;
private float faceTarget;

//dimensions
private float[] shapeX;
private float[] shapeY;
private int numPoints; //vertices for the shape

private boolean remove; //to remove from the game

public Follower(float x,float y, Player target){
    this.target = target;
    this.x = x;
    this.y = y;

    velocity = new Vector2(0, 0);

    numPoints = 4;
    speed = 200;

    shapeX = new float[numPoints];
    shapeY = new float[numPoints];
    radians = 3.1415f / 2;

    setShape();
}

public void setShape(){
    //top vertice
    shapeX[0] = x + MathUtils.cos(radians) * 30;
    shapeY[0] = y + MathUtils.sin(radians) * 30;

    //left vertice
    shapeX[1] = x + MathUtils.cos(radians - 4 * 3.1415f / 10) * 30;
    shapeY[1] = y + MathUtils.sin(radians - 4 * 3.1415f / 10) * 30;

    //bottom vertice
    shapeX[2] = x + MathUtils.cos(radians + 3.1415f) * 60;
    shapeY[2] = y + MathUtils.sin(radians + 3.1415f) * 60;

    //left vertice
    shapeX[3] = x + MathUtils.cos(radians + 4 * 3.1415f / 10) * 30;
    shapeY[3] = y + MathUtils.sin(radians + 4 * 3.1415f / 10) * 30;
}

public boolean shouldRemove() {
    return remove;
}

public void update(float dt) {
    float angle = (float) Math.atan2(target.getPosition().y - y, target.getPosition().x - x); //angle between the follower and target
    velocity.set((float) Math.cos(angle) * speed , -speed); //setting direction to follow the target

    radians +=  Math.cos(angle) * dt; //THIS HERE IS MAKING IT ROTATE

    x += velocity.x * dt;
    y += velocity.y * dt;

    setShape();

    if(y <= 0 - 60)
        remove = true;
    else
        remove = false;


}

public void draw(ShapeRenderer sp){
    sp.setColor(1, 1, 1 ,1);
    sp.begin(ShapeRenderer.ShapeType.Line);
    for(int i = 0, j = shapeX.length - 1;
        i < shapeX.length;
        j = i++) {

        sp.line(shapeX[i], shapeY[i], shapeX[j], shapeY[j]);

    }
    sp.end();
}

}

我没有添加GameScreen,因为我认为不需要显示它们的呈现方式,无论哪种方式,它都会保持不变。

另外,使用代码行,我使用Follower指向底部顶点为“眼睛”的玩家

感谢您的回答!

0 个答案:

没有答案