-
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
41 changed files
with
1,362 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,25 @@ | ||
package com.github.vitalibo.brickgame; | ||
|
||
|
||
import com.github.vitalibo.brickgame.core.Controller; | ||
import com.github.vitalibo.brickgame.core.Kernel; | ||
import com.github.vitalibo.brickgame.core.ui.BrickGameFrame; | ||
import com.github.vitalibo.brickgame.game.Game; | ||
import com.github.vitalibo.brickgame.game.Menu; | ||
|
||
public class Run { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
BrickGameFrame frame = new BrickGameFrame(); | ||
boolean[][] canvas = new boolean[20][10]; | ||
int x = 0, y = -1; | ||
|
||
while (true) { | ||
if (++y >= 20) { | ||
y = 0; | ||
if (++x >= 10) { | ||
x = 0; | ||
} | ||
} | ||
|
||
canvas[y][x] = true; | ||
frame.getFrame().draw(canvas); | ||
canvas[y][x] = false; | ||
frame.getScore().inc(); | ||
static { | ||
@SuppressWarnings("unchecked") | ||
Class<? extends Game>[] GAMES = new Class[]{ | ||
}; | ||
Menu.setGames(GAMES); | ||
} | ||
|
||
Thread.sleep(50); | ||
} | ||
public static void main(String[] args) { | ||
BrickGameFrame frame = new BrickGameFrame(); | ||
Controller controller = new Controller(frame, new Kernel()); | ||
controller.init(Menu.class); | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
.../github/vitalibo/brickgame/core/Grid.java → ...ithub/vitalibo/brickgame/core/Canvas.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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
package com.github.vitalibo.brickgame.core; | ||
|
||
@FunctionalInterface | ||
public interface Grid { | ||
public interface Canvas { | ||
|
||
void draw(boolean[][] src); | ||
|
||
boolean[][] get(); | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/vitalibo/brickgame/core/Context.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,30 @@ | ||
package com.github.vitalibo.brickgame.core; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
public class Context { | ||
|
||
@Getter | ||
private final Canvas board; | ||
@Getter | ||
private final Canvas preview; | ||
@Getter | ||
private final Number score; | ||
@Getter | ||
private final Number speed; | ||
@Getter | ||
private final Number level; | ||
@Getter | ||
private final Number life; | ||
@Getter | ||
private final State sound; | ||
@Getter | ||
private final State pause; | ||
@Getter | ||
private final Kernel kernel; | ||
@Getter | ||
private final Controller controller; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/github/vitalibo/brickgame/core/Controllable.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,12 @@ | ||
package com.github.vitalibo.brickgame.core; | ||
|
||
public interface Controllable { | ||
|
||
void doDown(); | ||
void doLeft(); | ||
void doRight(); | ||
void doUp(); | ||
|
||
void doRotate(); | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/com/github/vitalibo/brickgame/core/Controller.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,104 @@ | ||
package com.github.vitalibo.brickgame.core; | ||
|
||
import com.github.vitalibo.brickgame.core.ui.BrickGameFrame; | ||
import com.github.vitalibo.brickgame.game.Game; | ||
import com.github.vitalibo.brickgame.game.Life; | ||
import com.github.vitalibo.brickgame.game.Menu; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import lombok.Synchronized; | ||
import lombok.experimental.Delegate; | ||
|
||
import java.awt.event.KeyAdapter; | ||
import java.awt.event.KeyEvent; | ||
import java.lang.reflect.Constructor; | ||
|
||
public class Controller extends KeyAdapter implements Controllable { | ||
|
||
@Delegate(types = Controllable.class) | ||
private Game game; | ||
@Getter | ||
private boolean locked; | ||
|
||
private Context context; | ||
|
||
public Controller(BrickGameFrame frame, Kernel kernel) { | ||
frame.addKeyListener(this); | ||
this.context = new Context( | ||
frame.getBoard(), frame.getPreview(), | ||
frame.getScore(), frame.getSpeed(), frame.getLevel(), | ||
Life.of(frame.getPreview()), | ||
frame.getSound(), frame.getPause(), | ||
kernel, this); | ||
} | ||
|
||
@Synchronized | ||
@SneakyThrows(Exception.class) | ||
public void init(Class<? extends Game> cls) { | ||
Constructor<? extends Game> constructor = cls.getConstructor(Context.class); | ||
game = constructor.newInstance(context); | ||
|
||
game.init(); | ||
} | ||
|
||
@Synchronized | ||
public void reinit() { | ||
game.init(); | ||
} | ||
|
||
@Synchronized | ||
public void lock() { | ||
locked = true; | ||
} | ||
|
||
@Synchronized | ||
public void unlock() { | ||
locked = false; | ||
} | ||
|
||
@Override | ||
public void keyPressed(KeyEvent event) { | ||
switch (event.getKeyCode()) { | ||
case KeyEvent.VK_S: | ||
context.getSound().change(); | ||
// TODO: on/off sound | ||
break; | ||
case KeyEvent.VK_P: | ||
boolean isPause = context.getPause().change(); | ||
context.getKernel().values() | ||
.forEach(job -> job.setPause(isPause)); | ||
break; | ||
case KeyEvent.VK_R: | ||
context.getKernel().values() | ||
.forEach(Kernel.Job::kill); | ||
// TODO: clean up | ||
init(Menu.class); | ||
context.getLife().set(0); | ||
this.unlock(); | ||
break; | ||
} | ||
|
||
if (context.getPause().get() || isLocked()) { | ||
return; | ||
} | ||
|
||
switch (event.getKeyCode()) { | ||
case KeyEvent.VK_UP: | ||
this.doUp(); | ||
break; | ||
case KeyEvent.VK_DOWN: | ||
this.doDown(); | ||
break; | ||
case KeyEvent.VK_LEFT: | ||
this.doLeft(); | ||
break; | ||
case KeyEvent.VK_RIGHT: | ||
this.doRight(); | ||
break; | ||
case KeyEvent.VK_SPACE: | ||
this.doRotate(); | ||
break; | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/vitalibo/brickgame/core/GameException.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,33 @@ | ||
package com.github.vitalibo.brickgame.core; | ||
|
||
import com.github.vitalibo.brickgame.game.Point; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@ToString(callSuper = true) | ||
public class GameException extends RuntimeException { | ||
|
||
@Getter | ||
private final Point point; | ||
|
||
public GameException(Point point) { | ||
super(); | ||
this.point = point; | ||
} | ||
|
||
public GameException(Point point, String message) { | ||
super(message); | ||
this.point = point; | ||
} | ||
|
||
public GameException(Point point, String message, Throwable cause) { | ||
super(message, cause); | ||
this.point = point; | ||
} | ||
|
||
public GameException(Point point, Throwable cause) { | ||
super(cause); | ||
this.point = point; | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/com/github/vitalibo/brickgame/core/Kernel.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,99 @@ | ||
package com.github.vitalibo.brickgame.core; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.Delegate; | ||
|
||
import java.util.Map; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Consumer; | ||
|
||
public class Kernel extends Timer implements Map<String, Kernel.Job> { | ||
|
||
@Delegate | ||
private final Map<String, Job> jobs; | ||
|
||
public Kernel() { | ||
jobs = new ConcurrentHashMap<>(); | ||
} | ||
|
||
public Kernel.Job job(Object o, long milliseconds, Consumer<Job> action) { | ||
return new Job(o, milliseconds, action); | ||
} | ||
|
||
public Kernel.Job job(Object o, long milliseconds, Consumer<CountdownJob> action, | ||
int countdown, Consumer<CountdownJob> finalize) { | ||
return new CountdownJob(o, milliseconds, action, countdown, finalize); | ||
} | ||
|
||
public class Job extends TimerTask { | ||
|
||
@Getter | ||
private final String name; | ||
|
||
private final Consumer<? super Job> action; | ||
|
||
@Getter | ||
@Setter | ||
private boolean pause; | ||
@Getter | ||
private boolean killed; | ||
|
||
Job(Object o, long milliseconds, Consumer<? super Job> action) { | ||
this.name = o.getClass().getName(); | ||
this.action = action; | ||
jobs.put(name, this); | ||
schedule(this, 0, milliseconds); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (pause) { | ||
return; | ||
} | ||
|
||
action.accept(this); | ||
} | ||
|
||
public boolean kill() { | ||
jobs.remove(name); | ||
killed = true; | ||
return cancel(); | ||
} | ||
|
||
} | ||
|
||
public class CountdownJob extends Job { | ||
|
||
private final Consumer<CountdownJob> finalize; | ||
|
||
@Getter | ||
private int countdown = 100; | ||
|
||
CountdownJob(Object o, long milliseconds, Consumer<? super CountdownJob> action, | ||
int countdown, Consumer<CountdownJob> finalize) { | ||
super(o, milliseconds, job -> action.accept((CountdownJob) job)); | ||
this.countdown = countdown; | ||
this.finalize = finalize; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (isPause()) { | ||
return; | ||
} | ||
|
||
if (--countdown < 0) { | ||
this.kill(); | ||
finalize.accept(this); | ||
return; | ||
} | ||
|
||
super.run(); | ||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.