diff options
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.java | 104 |
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 @@ | |||
1 | package fr.umlv.java.wallj.viewer; | 1 | package fr.umlv.java.wallj.viewer; |
2 | 2 | ||
3 | import fr.umlv.java.wallj.board.Board; | 3 | import fr.umlv.java.wallj.board.Board; |
4 | import fr.umlv.java.wallj.board.TileVec2; | ||
4 | import fr.umlv.java.wallj.context.Context; | 5 | import fr.umlv.java.wallj.context.Context; |
5 | import fr.umlv.java.wallj.context.Game; | 6 | import fr.umlv.java.wallj.context.Game; |
7 | import fr.umlv.java.wallj.context.GraphicsContext; | ||
8 | import fr.umlv.java.wallj.event.*; | ||
6 | import fr.umlv.java.wallj.event.Event; | 9 | import fr.umlv.java.wallj.event.Event; |
7 | import fr.umlv.zen5.ApplicationContext; | 10 | import fr.umlv.zen5.ApplicationContext; |
11 | import fr.umlv.zen5.ScreenInfo; | ||
12 | import org.jbox2d.common.Vec2; | ||
8 | 13 | ||
9 | import java.awt.*; | 14 | import java.awt.*; |
15 | import java.awt.geom.Point2D; | ||
10 | import java.time.Duration; | 16 | import java.time.Duration; |
11 | import java.util.LinkedList; | 17 | import java.util.LinkedList; |
12 | import java.util.List; | 18 | import java.util.List; |
13 | import java.util.Objects; | 19 | import java.util.Objects; |
14 | import java.util.stream.Collectors; | 20 | import java.util.stream.Collectors; |
21 | import 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 | */ |
21 | public final class Viewer { | 29 | public 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 | } | ||