Skip to content

Commit

Permalink
Add Shoot game
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalibo committed Nov 25, 2017
1 parent 8cf30c5 commit ce39c05
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ launch game

![](https://raw.githubusercontent.com/vitalibo/Brick-Game-9999-in-1/assets/docs/img/sR35V1.gif) ![](https://raw.githubusercontent.com/vitalibo/Brick-Game-9999-in-1/assets/docs/img/IFREtC.gif) ![](https://raw.githubusercontent.com/vitalibo/Brick-Game-9999-in-1/assets/docs/img/bUdlw7.gif) ![](https://raw.githubusercontent.com/vitalibo/Brick-Game-9999-in-1/assets/docs/img/x82Vbe.gif)

![](https://raw.githubusercontent.com/vitalibo/Brick-Game-9999-in-1/assets/docs/img/AwX9jY.gif)

- Board panel `capacity [20x10]`
- Preview panel `capacity [4x4]`
- Score `max 999999`
Expand All @@ -40,4 +42,5 @@ launch game
### Games
- `0001` Snake
- `0002` Race
- `0003` Tetris
- `0003` Tetris
- `0004` Shoot
4 changes: 3 additions & 1 deletion src/main/java/com/github/vitalibo/brickgame/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.vitalibo.brickgame.game.Game;
import com.github.vitalibo.brickgame.game.Menu;
import com.github.vitalibo.brickgame.game.race.RaceGame;
import com.github.vitalibo.brickgame.game.shoot.ShootGame;
import com.github.vitalibo.brickgame.game.snake.SnakeGame;
import com.github.vitalibo.brickgame.game.tetris.TetrisGame;

Expand All @@ -17,7 +18,8 @@ public class Run {
Class<? extends Game>[] GAMES = new Class[]{
SnakeGame.class,
RaceGame.class,
TetrisGame.class
TetrisGame.class,
ShootGame.class
};
Menu.setGames(GAMES);
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/github/vitalibo/brickgame/game/shoot/Army.java
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 src/main/java/com/github/vitalibo/brickgame/game/shoot/Gun.java
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);
}

}
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()));
}

}

0 comments on commit ce39c05

Please sign in to comment.