-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/vitalibo/brickgame/game/shoot/Army.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.vitalibo.brickgame.game.shoot; | ||
|
||
import com.github.vitalibo.brickgame.game.Point; | ||
import lombok.experimental.Delegate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.stream.IntStream; | ||
|
||
class Army implements List<Point> { | ||
|
||
private static final Random random = new Random(); | ||
|
||
@Delegate | ||
private final List<Point> points; | ||
|
||
Army() { | ||
this.points = new CopyOnWriteArrayList<>(); | ||
} | ||
|
||
boolean killEnemy(List<Point> shoots) { | ||
List<Point> o = new ArrayList<>(shoots); | ||
shoots.removeAll(points); | ||
return points.removeAll(o); | ||
} | ||
|
||
void makeAttack() { | ||
points.forEach(Point::doDown); | ||
|
||
IntStream.range(0, 10) | ||
.filter(i -> random.nextBoolean()) | ||
.forEach(i -> add(Point.of(0, i))); | ||
} | ||
|
||
boolean hasWin() { | ||
return points.stream() | ||
.anyMatch(o -> o.getY() > 19); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/github/vitalibo/brickgame/game/shoot/Gun.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.github.vitalibo.brickgame.game.shoot; | ||
|
||
import com.github.vitalibo.brickgame.game.Point; | ||
import lombok.Getter; | ||
|
||
class Gun { | ||
|
||
@Getter | ||
private final Point point; | ||
|
||
Gun() { | ||
point = Point.of(19, 4); | ||
} | ||
|
||
public void doLeft() { | ||
if (point.getX() < 1) { | ||
return; | ||
} | ||
|
||
point.doLeft(); | ||
} | ||
|
||
public void doRight() { | ||
if (point.getX() > 8) { | ||
return; | ||
} | ||
|
||
point.doRight(); | ||
} | ||
|
||
Point fire() { | ||
return Point.of(point); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/github/vitalibo/brickgame/game/shoot/ShootGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.github.vitalibo.brickgame.game.shoot; | ||
|
||
import com.github.vitalibo.brickgame.core.Context; | ||
import com.github.vitalibo.brickgame.game.Game; | ||
import com.github.vitalibo.brickgame.game.Point; | ||
import com.github.vitalibo.brickgame.util.CanvasTranslator; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.stream.Stream; | ||
|
||
public class ShootGame extends Game { | ||
|
||
@Getter | ||
private Army army; | ||
@Getter | ||
private Gun gun; | ||
@Getter | ||
private List<Point> shoots; | ||
|
||
public ShootGame(Context context) { | ||
super(context); | ||
setUp(); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
army = new Army(); | ||
gun = new Gun(); | ||
shoots = new CopyOnWriteArrayList<>(); | ||
|
||
kernel.job(this, 1000 - speed.get() * 40, job -> doAttack()); | ||
kernel.job("shoots", 25, job -> doShoot()); | ||
} | ||
|
||
private void doShoot() { | ||
shoots.forEach(Point::doUp); | ||
if (army.killEnemy(shoots)) { | ||
score.inc(10); | ||
} | ||
|
||
repaint(); | ||
} | ||
|
||
@Override | ||
public void doDown() { | ||
|
||
} | ||
|
||
@Override | ||
public void doLeft() { | ||
gun.doLeft(); | ||
repaint(); | ||
} | ||
|
||
@Override | ||
public void doRight() { | ||
gun.doRight(); | ||
repaint(); | ||
} | ||
|
||
@Override | ||
public void doUp() { | ||
doRotate(); | ||
} | ||
|
||
@Override | ||
public void doRotate() { | ||
shoots.add(gun.fire()); | ||
repaint(); | ||
} | ||
|
||
private void setUp() { | ||
life.set(3); | ||
score.set(0); | ||
} | ||
|
||
private void doAttack() { | ||
army.makeAttack(); | ||
repaint(); | ||
|
||
if (army.hasWin()) { | ||
crash(gun.getPoint()); | ||
} | ||
} | ||
|
||
private void repaint() { | ||
board.draw(CanvasTranslator.from( | ||
Stream.of(gun.getPoint()), shoots.stream(), army.stream())); | ||
} | ||
|
||
} |