aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/fr/umlv/java/wallj/viewer/Viewer.java')
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/Viewer.java104
1 files changed, 76 insertions, 28 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
index b9686b0..ea6f3d0 100644
--- a/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
+++ b/src/main/java/fr/umlv/java/wallj/viewer/Viewer.java
@@ -1,63 +1,111 @@
1package fr.umlv.java.wallj.viewer; 1package fr.umlv.java.wallj.viewer;
2 2
3import fr.umlv.java.wallj.board.Board; 3import fr.umlv.java.wallj.board.Board;
4import fr.umlv.java.wallj.board.TileVec2;
4import fr.umlv.java.wallj.context.Context; 5import fr.umlv.java.wallj.context.Context;
5import fr.umlv.java.wallj.context.Game; 6import fr.umlv.java.wallj.context.Game;
7import fr.umlv.java.wallj.context.GraphicsContext;
8import fr.umlv.java.wallj.event.*;
6import fr.umlv.java.wallj.event.Event; 9import fr.umlv.java.wallj.event.Event;
7import fr.umlv.zen5.ApplicationContext; 10import fr.umlv.zen5.ApplicationContext;
11import fr.umlv.zen5.ScreenInfo;
12import org.jbox2d.common.Vec2;
8 13
9import java.awt.*; 14import java.awt.*;
15import java.awt.geom.Point2D;
10import java.time.Duration; 16import java.time.Duration;
11import java.util.LinkedList; 17import java.util.LinkedList;
12import java.util.List; 18import java.util.List;
13import java.util.Objects; 19import java.util.Objects;
14import java.util.stream.Collectors; 20import java.util.stream.Collectors;
21import java.util.stream.Stream;
15 22
16/** 23/**
17 * Link between application and Zen 5 24 * Link between application and Zen 5
18 * 25 *
19 * @author Adam NAILI 26 * @author Adam NAILI
27 * @author Pacien TRAN-GIRARD
20 */ 28 */
21public final class Viewer { 29public final class Viewer {
22 public static final Color BACKGROUND_COLOR = Color.WHITE; 30 public static final Color BACKGROUND_COLOR = Color.WHITE;
31
23 private static final Duration FRAME_DURATION = Duration.ofMillis(1000 / 60); 32 private static final Duration FRAME_DURATION = Duration.ofMillis(1000 / 60);
33 private static int EXIT_OK = 0;
24 34
25 private final Game currentGame; 35 private final ApplicationContext appContext;
36 private final Game game;
26 37
27 /** 38 /**
28 * @param boards the valid list of boards charged in the application 39 * @param boards the valid list of boards charged in the application
29 */ 40 */
30 public Viewer(List<Board> boards) { 41 public Viewer(ApplicationContext appContext, List<Board> boards) {
31 this.currentGame = new Game(Objects.requireNonNull(boards)); 42 this.appContext = Objects.requireNonNull(appContext);
43 this.game = new Game(boards);
32 } 44 }
33 45
34 /** 46 public void run() {
35 * @param applicationContext the application context from Zen 5 47 List<Event> forwardEvents = new LinkedList<>();
36 */ 48 Duration lastExec = Duration.ZERO;
37 public void eventLoop(ApplicationContext applicationContext) { 49
38 List<Event> events = new LinkedList<>(); 50 while (!game.isOver()) {
39 Duration lastExecDuration = Duration.ZERO; 51 final Duration lastExecCopy = lastExec;
40 while (!currentGame.isOver()) {
41 Duration last = lastExecDuration;
42 StopWatch stopWatch = new StopWatch(); 52 StopWatch stopWatch = new StopWatch();
43 applicationContext.renderFrame(graphics2D -> { 53 List<Event> events = Stream.concat(forwardEvents.stream(), mapInputEvent()).collect(Collectors.toList());
44 InputHandler inputHandler = new InputHandler(applicationContext); 54 forwardEvents.clear();
45 ScreenManager screenManager = new ScreenManager(applicationContext, graphics2D); 55
46 events.addAll(inputHandler.getEvents()); 56 appContext.renderFrame(graphics2D -> {
47 Context context = new Context(currentGame, events, screenManager.clearScreen(), last); 57 GraphicsContext graphicsContext = new GraphicsContext(graphics2D, appContext.getScreenInfo());
48 List<Event> newEvents = currentGame.update(context).collect(Collectors.toList()); //return new events created from update(); 58 clearScreen(graphicsContext);
49 events.clear(); 59 Context context = new Context(game, events, graphicsContext, lastExecCopy);
50 events.addAll(newEvents); //add the new events returned by updates 60 game.update(context).forEach(forwardEvents::add);
51 }); 61 });
52 try { 62
53 Duration sleepDuration = FRAME_DURATION.minus(stopWatch.peek()); 63 sleep(FRAME_DURATION.minus(stopWatch.peek()));
54 if (!sleepDuration.isNegative()) Thread.sleep(sleepDuration.toMillis()); 64 lastExec = stopWatch.peek();
55 } catch (Exception e) {
56 applicationContext.exit(-1);
57 }
58 lastExecDuration = stopWatch.peek();
59 } 65 }
60 applicationContext.exit(0); 66
67 appContext.exit(EXIT_OK);
61 } 68 }
62}
63 69
70 private Stream<Event> mapInputEvent() {
71 fr.umlv.zen5.Event inputEvent = appContext.pollEvent();
72 if (inputEvent == null) return Stream.empty();
73
74 switch (inputEvent.getAction()) {
75 case POINTER_DOWN:
76 Point2D.Float clickLocation = inputEvent.getLocation();
77 TileVec2 tile = TileVec2.of(new Vec2(clickLocation.x, clickLocation.y));
78 return Stream.of(new MoveRobotOrder(tile));
79
80 case KEY_PRESSED:
81 switch (inputEvent.getKey()) {
82 case SPACE:
83 return Stream.of(new BombSetupOrder());
84 case R:
85 return Stream.of(new ConfirmOrder());
86 case Q:
87 return Stream.of(new QuitGameOrder());
88 case S:
89 return Stream.of(new SimulationStartOrder());
90 default:
91 return Stream.empty();
92 }
93
94 default:
95 return Stream.empty();
96 }
97 }
98
99 private void clearScreen(GraphicsContext graphicsContext) {
100 ScreenInfo window = graphicsContext.getScreenInfo();
101 graphicsContext.paintRectangle(BACKGROUND_COLOR, new Vec2(), window.getWidth(), window.getHeight());
102 }
103
104 private void sleep(Duration duration) {
105 try {
106 if (!duration.isNegative()) Thread.sleep(duration.toMillis());
107 } catch (InterruptedException e) {
108 throw new RuntimeException(e);
109 }
110 }
111}