public abstract class GameObject extends Entity {
protected Sprite mSprite;
public GameObject(final int posX, final int posY, final TextureRegion region) {
mSprite = new Sprite(posX * GameObjectsMap.CELL_SIZE, posY
* GameObjectsMap.CELL_SIZE, region);
attachChild(mSprite);
}
protected Sprite getSprite() {
return mSprite;
}
public static enum Type {
lasergun, mirror, target
}
abstract int onLaser( int angle);
abstract void attachTo(Scene scene);
}
* This source code was highlighted with Source Code Highlighter .
public abstract class DynamicGameObject extends GameObject {
public static final int DEG_0 = 0;
public static final int DEG_45 = 1;
public static final int DEG_90 = 2;
public static final int DEG_135 = 3;
public static final int DEG_180 = 4;
public static final int DEG_225 = 5;
public static final int DEG_270 = 6;
public static final int DEG_315 = 7;
private static final float ANGLE = 45f;
private int mAngle;
public DynamicGameObject(final int posX, final int posY, final int angle,
final TextureRegion region) {
super(posX, posY, region);
setAngle(angle);
}
public int getAngle() {
return mAngle;
}
public void setAngle( int angle) {
this .mAngle = angle;
rotateSprite();
}
public void rotateLeft() {
mAngle--;
if (mAngle < DEG_0) {
mAngle = DEG_315;
}
rotateSprite();
}
public void rotateRigth() {
mAngle++;
if (mAngle > DEG_315) {
mAngle = DEG_0;
}
rotateSprite();
}
public float getRotationAngle() {
return ANGLE * mAngle;
}
protected void rotateSprite() {
mSprite.setRotation(getRotationAngle());
}
}
* This source code was highlighted with Source Code Highlighter .
public class LaserLine extends Entity {
private static final float LINE_WIDTH = 3f;
LinkedList<Point> mPoints;
private final Point mStartPosition;
private int mAngle;
public LaserLine(Point startPosition, int angle) {
init();
mStartPosition = startPosition;
mPoints.add(mStartPosition);
mAngle = angle;
}
public void init() {
mPoints = new LinkedList<Point>();
}
public void clearPoints() {
mPoints.clear();
mPoints.add(mStartPosition);
}
public void addPoint(final int cellNumX, final int cellNumY) {
mPoints.add( new Point(cellNumX, cellNumY));
}
public void addPoint(Point point) {
mPoints.add( new Point(point));
}
public void buildLines() {
detachChildren();
Point previousPoint = null ;
for (Point point : mPoints) {
Ln.i(point.x + " " + point.y);
if (previousPoint != null ) {
attachChild(makeLine(previousPoint, point));
}
previousPoint = point;
}
Ln.i( "-------------" );
}
public final Line makeLine(final Point start, final Point end) {
final Line line = new Line(
(start.x + 0.5f) * GameObjectsMap.CELL_SIZE_X,
(start.y + 0.5f) * GameObjectsMap.CELL_SIZE_Y,
(end.x + 0.5f) * GameObjectsMap.CELL_SIZE_X,
(end.y + 0.5f) * GameObjectsMap.CELL_SIZE_Y);
line.setLineWidth(LINE_WIDTH);
line.setColor(1f, 0.1f, 0.1f, 0.75f);
return line;
}
public Point getStartPosition() {
return mStartPosition;
}
public void setAngle( int angle) {
mAngle = angle;
}
public int getAngle() {
return mAngle;
}
}
* This source code was highlighted with Source Code Highlighter .
public class LaserGun extends DynamicGameObject {
public LaserGun(final int posX, final int posY, final int angle,
final TextureRegion region) {
super(posX, posY, angle, region);
}
@Override
int onLaser( int angle) {
return -1;
}
@Override
void attachTo(Scene scene) {
scene.getChild(GameObjectsMap.GAME_OBJECTS_LAYER).attachChild(getSprite());
}
}
* This source code was highlighted with Source Code Highlighter .
public Scene onLoadScene() {
this .mEngine.registerUpdateHandler( new FPSLogger());
final Scene scene = new Scene(NUMBER_OF_LAYERS);
scene.setBackground( new SpriteBackground( new Sprite(0, 0, mTextures
.getBackground())));
LaserLine line = new LaserLine( new Point(3, 3), 0);
line.addPoint( new Point(4, 4));
line.addPoint( new Point(5, 2));
line.buildLines();
scene.getChild(GameObjectsMap.LASER_LAYER).attachChild(line);
LaserGun gun = new LaserGun(3, 3, 4, mTextures.getLaserGun());
gun.attachTo(scene);
return scene;
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/120863/
All Articles