Java 启动(停机); 设置大小(32,32); } 公共纹理区域getFrame(浮点dt){ currentState=getState(); 结构区; 开关(当前状态){ 案例运行权: region=runningRight.getKeyFrame(stat
启动(停机); 设置大小(32,32); } 公共纹理区域getFrame(浮点dt){ currentState=getState(); 结构区; 开关(当前状态){ 案例运行权: region=runningRight.getKeyFrame(stateTimer,true); 打破 案例左侧: region=runningLeft.getKeyFrame(stateTimer,true); 打破 案件审理: region=runningUp.getKeyFrame(stateTimer,true); 打破 个案摘要: region=runningDown.getKeyFrame(stateTimer,true); 打破 案例左侧: 区域=左侧; 打破 案例标准: 区域=站立; 打破 案件搁置: 地区=停摆; 打破 案件审理权: 违约: 区域=右侧; 打破 } stateTimer=currentState==previousState?stateTimer+dt:0; previousState=当前状态; 返回区; } 公共状态getState(){ Vector2方向=b2body.getLinearVelocity(); if(direction.isZero()){ 开关(先前状态){ 案件审理: 返回状态.STANDINGUP; 案例左侧: 返回状态。STANDINGLEFT; 个案摘要: 返回状态。停机; 案例运行权: 返回状态。立场正确; 违约: 返回先前状态; } }如果(direction.x>=0&&direction.y>=0),则为else{ 如果(方向x>方向y){ 返回State.RUNNINGRIGHT; }else返回State.RUNNINGUP; }如果(direction.x>=0&&direction.y-direction.y),则为else{ 返回State.RUNNINGRIGHT; }else返回State.RUNNINGDOWN; }else if(方向x=0){ 如果(-direction.x>direction.y){ 返回State.RUNNINGLEFT; }else返回State.RUNNINGUP; }否则如果(方向x 2f){ screen.gameOver(); } } 公共无效设置IntimedDeathZone(布尔b){ 此.isInTimedDeathZone=b; } @凌驾 公共布尔键关闭(int-keycode){ 开关(钥匙代码){ case Input.Keys.UP: 速度y=速度限制; 打破 case Input.Keys.DOWN: 速度y=-速度限制; 打破 case Input.Keys.LEFT: 速度x=-速度限制; 打破 case Input.Keys.RIGHT: 速度x=速度限制; 打破 违约: 返回true; } followPath=false; 返回true; } @凌驾 公共布尔键控(int-keycode){ 开关(钥匙代码){ case Input.Keys.UP: case Input.Keys.DOWN: 速度y=0; 打破 case Input.Keys.LEFT: case Input.Keys.RIGHT: 速度x=0; 打破 } 返回true; } @凌驾 公共布尔键类型(字符字符){ 返回false; } @凌驾 公共布尔接地(整数屏幕X、整数屏幕Y、整数指针、整数按钮){ 浮动扩散; 漂浮困难; //如果不是鼠标左键或第一个触摸指针,则忽略 如果(button!=Input.Buttons.LEFT | | pointer>0)返回false; screen.getCamera().unproject(touchpoint.set(screenX,screenY,0)); //如果第一个点不靠近牧人,则忽略 diffX=touchpoint.x-b2body.getPosition().x; diffY=touchpoint.y-b2body.getPosition().y; 如果(diffX>25 | | diffX<-25 | | diffY>25 | | diffY<-25)返回false; 拖动=真; herderPath.clear(); targetPosition=b2body.getPosition(); herderPath.add(targetPosition); 返回true; } @凌驾 公共布尔值(int-screenX、int-screenY、int-pointer){ Vector2 vec2=新的Vector2(); 如果(!拖动)返回false; screen.getCamera().unproject(touchpoint.set(screenX,screenY,0)); vec2.set(接触点x,接触点y); herderPath.add(vec2); followPath=true; 返回true; } @凌驾 公共布尔补色(整数屏幕X、整数屏幕Y、整数指针、整数按钮){ 如果(button!=Input.Buttons.LEFT | | pointer>0)返回false; screen.getCamera().unproject(touchpoint.set(screenX,screenY,0)); 拖动=假; 返回true; } 专用向量2计算级别(向量2当前位置,向量2目标位置){ Vector2 TENTP=新Vector2().set(targetPosition); 返回TENTP.sub(当前位置).nor().scl(速度限制); } 公共布尔值isXValid(int x){ 返回b2body.getPosition().x=x-精度; } 公共布尔值isYValid(整数y){ 返回b2body.getPosition().y=y-精度; } 公共布尔值isXStapable(){ 返回isXValid((int)destinationX); } 公共布尔值isYStapable(){ 返回isYValid((int)目的地); } 公共停车场(){ 速度设置(0,0); } @凌驾 公共布尔mouseMoved(int-screenX,int-screenY){ 返回false; } @凌驾 公共布尔滚动(整数金额){ 返回false; } 公共无效设置速度限制(浮点乘数){ 如果(速度限制>最大速度){ 此.speedLimit=最大速度; }否则{ 这个.speedLimit=乘数; } } } 出了什么问题?WheJava 启动(停机); 设置大小(32,32); } 公共纹理区域getFrame(浮点dt){ currentState=getState(); 结构区; 开关(当前状态){ 案例运行权: region=runningRight.getKeyFrame(stat,java,android,Java,Android,启动(停机); 设置大小(32,32); } 公共纹理区域getFrame(浮点dt){ currentState=getState(); 结构区; 开关(当前状态){ 案例运行权: region=runningRight.getKeyFrame(stateTimer,true); 打破 案例左侧: region=runningLeft.getKeyFrame(stateTimer,true); 打破 案件审理: region=runningUp.getKeyFrame(stateTimer,t
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;
}
}
}