路径绘图。在android工作室

时间:2016-08-25 19:21:33

标签: java android

我正在尝试为牧群游戏实现路径绘图。

目前,当我在PC上运行游戏时,我使用鼠标绘制牧民将要经过的路径,但是我无法让它画出一条线。

package com.mygdx.herdergame.Sprites;

import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.mygdx.herdergame.HerderGame;
import com.mygdx.herdergame.Screens.PlayScreen;
import java.util.ArrayList;

public class Herder extends Sprite implements InputProcessor {
    HerderGame game;
    public World world;
    public Body b2body;
    private PlayScreen screen;

    //States
    private enum State {
        RUNNINGRIGHT, RUNNINGLEFT, RUNNINGUP, RUNNINGDOWN, STANDINGRIGHT, STANDINGLEFT, STANDINGUP, STANDINGDOWN
    }

    private State currentState;
    private State previousState;
    private float stateTimer;

    //Textures
    private Animation runningRight;
    private Animation runningLeft;
    private Animation runningUp;
    private Animation runningDown;
    private TextureRegion standRight;
    private TextureRegion standLeft;
    private TextureRegion standUp;
    private TextureRegion standDown;

    private float destinationX;
    private float destinationY;

    //Velocity
    private static final float MAX_SPEED = 500;
    private float speedLimit = 1000;
    private Vector2 velocity = new Vector2(0, 0);

    private static float ACCURACY = 50;

    //Touch Screen
    private Vector3 touchpoint = new Vector3();
    private Vector2 targetPosition;
    private ArrayList<Vector2> herderPath = new ArrayList<Vector2>();
    boolean dragging;
    boolean followingPath = false;

    // death variables
    private boolean isInTimedDeathZone;
    private float deathTimer;

    //this is the constructor.
    public Herder(World world, PlayScreen screen) {
        super(screen.getAtlas().findRegion("herderRight"));
        this.screen = screen;
        game = screen.getGame();

        this.world = world;
        defBody();
        setTextures();

        currentState = State.STANDINGRIGHT;
        previousState = State.STANDINGRIGHT;
        stateTimer = 0;

        isInTimedDeathZone = false;
        deathTimer = 0;
    }


    public void defBody() {
        BodyDef bdef = new BodyDef();
        bdef.position.set(500, 500);
        bdef.type = BodyDef.BodyType.DynamicBody;

        b2body = world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(10);

        fdef.shape = shape;

        b2body.createFixture(fdef).setUserData(this);

    }

    public void setTextures() {

        int herderUpStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderUp().getStartX();
        int herderUpStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderUp().getStartY();
        int herderDownStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderDown().getStartX();
        int herderDownStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderDown().getStartY();
        int herderLeftStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderLeft().getStartX();
        int herderLeftStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderLeft().getStartY();
        int herderRightStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getStartX();
        int herderRightStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getStartY();
        int numberOfFrame = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getNumberOfFrame();
        int width = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getWidth();
        int height = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getHeight();


        //this enables the sprite to be drawn.
        Array<TextureRegion> frames = new Array<TextureRegion>();
        for (int i = 0; i < numberOfFrame; i++) {
            frames.add(new TextureRegion(getTexture(), herderRightStartX + width * i, herderRightStartY, width, height));
        }
        runningRight = new Animation(0.1f, frames);
        frames.clear();

        for (int i = 0; i < numberOfFrame; i++) {
            frames.add(new TextureRegion(getTexture(), herderLeftStartX + width * i, herderLeftStartY, width, height));
        }
        runningLeft = new Animation(0.1f, frames);
        frames.clear();

        for (int i = 0; i < numberOfFrame; i++) {
            frames.add(new TextureRegion(getTexture(), herderDownStartX + width * i, herderDownStartY, width, height));
        }
        runningDown = new Animation(0.1f, frames);
        frames.clear();

        for (int i = 0; i < numberOfFrame; i++) {
            frames.add(new TextureRegion(getTexture(), herderUpStartX + width * i, herderUpStartY, width, height));
        }
        runningUp = new Animation(0.1f, frames);



        standRight = new TextureRegion(getTexture(), herderRightStartX, herderRightStartY, width, height);
        setBounds(0, 0, 32, 32);
        setRegion(standRight);

        standLeft = new TextureRegion(getTexture(), herderLeftStartX, herderLeftStartY, width, height);
        setBounds(0, 0, 32, 32);
        setRegion(standLeft);

        standUp = new TextureRegion(getTexture(), herderUpStartX, herderUpStartY, width, height);
        setBounds(0, 0, 32, 32);
        setRegion(standUp);

        standDown = new TextureRegion(getTexture(), herderDownStartX, herderDownStartY, width, height);
        setBounds(0, 0, 32, 32);
        setRegion(standDown);

        setSize(32, 32);
    }

    public TextureRegion getFrame(float dt) {
        currentState = getState();

        TextureRegion region;

        switch (currentState) {
            case RUNNINGRIGHT:
                region = runningRight.getKeyFrame(stateTimer, true);
                break;
            case RUNNINGLEFT:
                region = runningLeft.getKeyFrame(stateTimer, true);
                break;
            case RUNNINGUP:
                region = runningUp.getKeyFrame(stateTimer, true);
                break;
            case RUNNINGDOWN:
                region = runningDown.getKeyFrame(stateTimer, true);
                break;
            case STANDINGLEFT:
                region = standLeft;
                break;
            case STANDINGUP:
                region = standUp;
                break;
            case STANDINGDOWN:
                region = standDown;
                break;
            case STANDINGRIGHT:
            default:
                region = standRight;
                break;
        }

        stateTimer = currentState == previousState ? stateTimer + dt : 0;
        previousState = currentState;
        return region;

    }

    public State getState() {
        Vector2 direction = b2body.getLinearVelocity();

        if (direction.isZero()) {
            switch (previousState) {
                case RUNNINGUP:
                    return State.STANDINGUP;
                case RUNNINGLEFT:
                    return State.STANDINGLEFT;
                case RUNNINGDOWN:
                    return State.STANDINGDOWN;
                case RUNNINGRIGHT:
                    return State.STANDINGRIGHT;
                default:
                    return previousState;
            }
        } else if (direction.x >= 0 && direction.y >= 0) {

            if (direction.x > direction.y) {
                return State.RUNNINGRIGHT;
            } else return State.RUNNINGUP;

        } else if (direction.x >= 0 && direction.y <= 0) {

            if (direction.x > -direction.y) {
                return State.RUNNINGRIGHT;
            } else return State.RUNNINGDOWN;

        } else if (direction.x <= 0 && direction.y >= 0) {

            if (-direction.x > direction.y) {
                return State.RUNNINGLEFT;
            } else return State.RUNNINGUP;

        } else if (direction.x <= 0 && direction.y <= 0) {

            if (-direction.x > -direction.y) {
                return State.RUNNINGLEFT;
            } else return State.RUNNINGDOWN;

        } else return currentState = previousState;
    }

    public void update(float dt) {

        if (isXStapable() || isYStapable()) {
            stop();
        }

        if (followingPath) {

            if (!herderPath.iterator().hasNext()) {
                followingPath = false;
                herderPath.clear();
                stop();
            } else if (targetPosition.dst(b2body.getPosition()) < 1.5) {
                targetPosition = herderPath.get(0);
                herderPath.remove(0);
                velocity = calculateVelocity(b2body.getPosition(), targetPosition);
            } else {
                velocity = calculateVelocity(b2body.getPosition(), targetPosition);
            }

        }

        b2body.setLinearVelocity(velocity);
        setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
        setRegion(getFrame(dt));

        if (isInTimedDeathZone) {
            deathTimer += dt;
        } else {
            deathTimer = 0;
        }

        if (deathTimer > 2f) {
            screen.gameOver();
        }

    }

    public void setInTimedDeathZone(boolean b) {
        this.isInTimedDeathZone = b;
    }

    @Override
    public boolean keyDown(int keycode) {
        switch (keycode) {
            case Input.Keys.UP:
                velocity.y = speedLimit;
                break;
            case Input.Keys.DOWN:
                velocity.y = -speedLimit;
                break;
            case Input.Keys.LEFT:
                velocity.x = -speedLimit;
                break;
            case Input.Keys.RIGHT:
                velocity.x = speedLimit;
                break;
            default:
                return true;
        }

        followingPath = false;
        return true;
    }

    @Override
    public boolean keyUp(int keycode) {
        switch (keycode) {
            case Input.Keys.UP:
            case Input.Keys.DOWN:
                velocity.y = 0;
                break;
            case Input.Keys.LEFT:
            case Input.Keys.RIGHT:
                velocity.x = 0;
                break;
        }
        return true;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        float diffX;
        float diffY;

        // ignore if its not left mouse button or first touch pointer
        if (button != Input.Buttons.LEFT || pointer > 0) return false;

        screen.getCamera().unproject(touchpoint.set(screenX, screenY, 0));

        //ignore if the first point is not close to the herder
        diffX = touchpoint.x - b2body.getPosition().x;
        diffY = touchpoint.y - b2body.getPosition().y;
        if (diffX > 25 || diffX < -25 || diffY > 25 || diffY < -25) return false;

        dragging = true;
        herderPath.clear();
        targetPosition = b2body.getPosition();
        herderPath.add(targetPosition);
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        Vector2 vec2 = new Vector2();
        if (!dragging) return false;
        screen.getCamera().unproject(touchpoint.set(screenX, screenY, 0));
        vec2.set(touchpoint.x, touchpoint.y);
        herderPath.add(vec2);
        followingPath = true;

        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (button != Input.Buttons.LEFT || pointer > 0) return false;
        screen.getCamera().unproject(touchpoint.set(screenX, screenY, 0));
        dragging = false;
        return true;
    }

    private Vector2 calculateVelocity(Vector2 currentPosition, Vector2 targetPosition) {
        Vector2 tempTP = new Vector2().set(targetPosition);

        return tempTP.sub(currentPosition).nor().scl(speedLimit);
    }

    public boolean isXValid(int x) {
        return b2body.getPosition().x <= x + ACCURACY && b2body.getPosition().x >= x - ACCURACY;

    }

    public boolean isYValid(int y) {
        return b2body.getPosition().y <= y + ACCURACY && b2body.getPosition().y >= y - ACCURACY;

    }

    public boolean isXStapable() {
        return isXValid((int) destinationX);
    }

    public boolean isYStapable() {
        return isYValid((int) destinationY);
    }

    public void stop() {
        velocity.set(0, 0);
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

    public void setSpeedLimit(float multiplier) {
        if (speedLimit > MAX_SPEED) {
            this.speedLimit = MAX_SPEED;
        } else {
            this.speedLimit = multiplier;
        }

    }
}

0 个答案:

没有答案