If you think the Android project ProjectSierra listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
Copyright 2014 Seth Traverse/*www.java2s.com*/
This file is part of Project Sierra.
Project Sierra is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Project Sierra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Project Sierra. If not, see <http://www.gnu.org/licenses/>.
*/package ca.viaware.game.entity;
import ca.viaware.game.entity.events.MoveEvent;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.util.ArrayList;
publicclass MovingEntity extends Entity {
privatefloat dx;
privatefloat dy;
privatefloat moveTime;
private Entity target;
privateboolean moving;
private ArrayList<MoveEvent> moveEventHandlers;
public MovingEntity(float x, float y, float width, float height, TextureRegion texture) {
super(x, y, width, height, texture);
moveEventHandlers = new ArrayList<MoveEvent>();
}
@Override
publicvoid update(float delta) {
if (moving) {
moveTime -= delta;
if (moveTime <= 0) {
moving = false;
setDX(0);
setDY(0);
setX(target.getX());
setY(target.getY());
sendMoveEvent(MoveEvent.ON_COMPLETED);
} else {
startMovingTo(target, moveTime / delta);
}
}
float moveX = dx * Gdx.graphics.getDeltaTime();
float moveY = dy * Gdx.graphics.getDeltaTime();
if (moveX != 0 || moveY != 0) {
sendMoveEvent(MoveEvent.ON_MOVE);
}
setX(getX() + moveX);
setY(getY() + moveY);
}
@Override
publicvoid render(SpriteBatch batch) {
super.render(batch);
}
publicfloat getDX() {
return dx;
}
publicvoid setDX(float dx) {
this.dx = dx;
}
publicfloat getDY() {
return dy;
}
publicvoid setDY(float dy) {
this.dy = dy;
}
publicvoid addMoveEventHandler(MoveEvent e) {
moveEventHandlers.add(e);
}
privatevoid sendMoveEvent(int type) {
for (MoveEvent e : moveEventHandlers) {
switch (type) {
case MoveEvent.ON_START:
e.onStart();
break;
case MoveEvent.ON_MOVE:
e.onMove();
break;
case MoveEvent.ON_COMPLETED:
e.onCompleted();
break;
}
}
}
publicvoid stopAllMovement() {
setDX(0);
setDY(0);
sendMoveEvent(MoveEvent.ON_COMPLETED);
}
publicvoid startMovingTo(Entity other, float speed) {
if (!moving) sendMoveEvent(MoveEvent.ON_START);
float xSpeed = (float) (other.getCenteredX() - getCenteredX());
float ySpeed = (float) (other.getCenteredY() - getCenteredY());
float factor = (xSpeed * xSpeed) + (ySpeed * ySpeed);
factor = (float) Math.sqrt(factor);
factor = speed / factor;
xSpeed = xSpeed * factor;
ySpeed = ySpeed * factor;
setDX(xSpeed);
setDY(ySpeed);
}
publicvoid moveTo(Entity other, float time) {
sendMoveEvent(MoveEvent.ON_START);
this.moveTime = time;
this.target = other;
moving = true;
}
}