public static boolean outOfArea(Point pos) {
return pos.x < 0 || pos.y < 0 || pos.x > WIDTH - 1
|| pos.y > HEIGHT - 1;
}
* This source code was highlighted with Source Code Highlighter .
private LaserLine mLaserLine;
mLaserLine = new LaserLine(new Point(posX, posY), angle);
scene.getChild(GameObjectsMap.LASER_LAYER).attachChild(mLaserLine);
public void buildLaser(GameObject[][] map) {
mLaserLine.setAngle(getAngle());
mLaserLine.build(map);
}
* This source code was highlighted with Source Code Highlighter .
public void build(GameObject[][] map) {
sBuilder.buildPath( this , map);
buildLines();
}
* This source code was highlighted with Source Code Highlighter .
private static LaserLineBuilder sBuilder = new LaserLineBuilder();
public class LaserLineBuilder {
private static final int MAX_STEPS = 200;
public void buildPath(LaserLine laserLine, GameObject[][] map) {
laserLine.clearPoints();
int step = 0;
int angle = laserLine.getAngle();
Point position = new Point(laserLine.getStartPosition());
GameObject gameObject;
while (step < MAX_STEPS) {
nextPosition(position, angle);
if (GameObjectsMap.outOfArea(position)) {
laserLine.addPoint(position);
return ;
}
gameObject = map[position.x][position.y];
if (gameObject == null ) {
continue ;
} else {
laserLine.addPoint(position);
int reflection = gameObject.onLaser(angle);
if (reflection < 0) {
return ;
} else {
angle = reflection;
}
}
}
laserLine.addPoint(position);
}
private void nextPosition(Point position, final int angle) {
switch (angle) {
case DynamicGameObject.DEG_0:
position.x++;
break ;
case DynamicGameObject.DEG_90:
position.y++;
break ;
case DynamicGameObject.DEG_180:
position.x--;
break ;
case DynamicGameObject.DEG_270:
position.y--;
break ;
case DynamicGameObject.DEG_45:
position.x++;
position.y++;
break ;
case DynamicGameObject.DEG_135:
position.x--;
position.y++;
break ;
case DynamicGameObject.DEG_225:
position.x--;
position.y--;
break ;
case DynamicGameObject.DEG_315:
position.x++;
position.y--;
break ;
default :
break ;
}
}
}
* This source code was highlighted with Source Code Highlighter .
public class Mirror extends DynamicGameObject {
public Mirror(final int posX, final int posY, final int angle,
final TextureRegion region) {
super(posX, posY, angle, region);
}
@Override
void attachTo(Scene scene) {
scene.getChild(GameObjectsMap.GAME_OBJECTS_LAYER).attachChild(
getSprite());
}
@Override
int onLaser( int angle) {
int a = getAngle() % 4;
if (angle == (a + 1) % 8) return (a + 7) % 8;
if (angle == (a + 7) % 8) return (a + 1) % 8;
if (angle == (a + 3) % 8) return (a + 5) % 8;
if (angle == (a + 5) % 8) return (a + 3) % 8;
return -1;
}
}
* This source code was highlighted with Source Code Highlighter .
public void add(Type type, final int posH, final int posW, final int angle) {
GameObject object = null ;
switch (type) {
case lasergun:
LaserGun l = new LaserGun(posH, posW, angle, mTextures
.getLaserGun());
mLaserGuns.add(l);
object = l;
break ;
case mirror:
object = new Mirror(posH, posW, angle, mTextures.getMirror());
break ;
case target:
break ;
default :
break ;
}
mMap[posH][posW] = object ;
}
* This source code was highlighted with Source Code Highlighter .
private LinkedList mLaserGuns;
andpublic void buildLasers() {
for (LaserGun gun : mLaserGuns) {
gun.buildLaser(mMap);
}
}
* This source code was highlighted with Source Code Highlighter .
private void initMap() {
mGameObjectsMap = new GameObjectsMap(mTextures);
mGameObjectsMap.add(Type.lasergun, 0, 0, 3);
mGameObjectsMap.add(Type.lasergun, 3, 2, 7);
mGameObjectsMap.add(Type.lasergun, 3, 4, 4);
mGameObjectsMap.add(Type.mirror, 3, 3, 3);
mGameObjectsMap.addToScene(mEngine.getScene());
mGameObjectsController = new GameObjectsController(mGameObjectsMap, this );
final Scene scene = getEngine().getScene();
mGameObjectsMap.buildLasers();
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/123516/
All Articles