1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sample3d;
import java.util.ArrayList;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import my3d._viewport3d;
public class _sample14 extends _labyrinth_view {
public static void main(String... args) {
Application.launch(args);
}
private static final int WALL = 1, START = 2, GOAL = 3;
int edit_obj = WALL;
private static final int TIME_LIMIT = 30;
int time = -1;
AnimationTimer timer;
@Override
public void init_scene(_viewport3d vp) {
super.init_scene(vp);
set_vert_slider_range(0, 0);
set_horz_slider_range(0, 0);
ArrayList<MenuItem> items = add_app_menu(
(index) -> {
if (time > 0) {
timer.stop();
time = 0;
}
switch (index) {
case 0: // 編集ビュー
set_view_mode(1);
time = -1;
repaint();
break;
case 1: // 探索ビュー
set_view_mode(2);
time = 0;
ready();
repaint();
break;
case 2: // テスト・ビュー
set_view_mode(3);
time = -1;
ready();
repaint();
break;
case 3: edit_obj = WALL; break;
case 4: edit_obj = START; break;
case 5: edit_obj = GOAL; break;
case 6: // 全て壁
case 7: // 全て床
lab.iter((x, z) -> lab.set_wall(x, z, index == 6));
repaint();
break;
case 8: // 迷宮自動作成
lab.create();
ready();
repaint();
break;
}
},
"r編集ビュー", "r探索ビュー", "rテスト・ビュー",
"---", "r壁 / 床", "rスタート地点", "rゴール地点",
"---", "全部壁", "全部床",
"---", "迷宮自動作成"
);
((RadioMenuItem)items.get(0)).setSelected(true);
((RadioMenuItem)items.get(3)).setSelected(true);
view.setOnMouseClicked((evt) -> {
int pos = get_edit_pos((int)evt.getX(), (int)evt.getY());
if (pos == -1) return;
switch (edit_obj) {
case WALL:
lab.set_wall(pos, !lab.is_wall(pos));
break;
case START:
if (pos == lab.get_start_pos())
lab.set_start_dir(lab.get_start_dir() + 1);
else
lab.set_start_pos(pos);
break;
case GOAL:
lab.set_goal_pos(pos);
break;
}
repaint();
});
stage.getScene().setOnKeyPressed((evt) -> {
KeyCode key = evt.getCode();
if (key == KeyCode.ENTER) {
if (time > 0) {
timer.stop();
time = 0;
}
else {
ready();
if (time == 0)
timer.start();
}
repaint();
return;
}
if (time == 0)
return;
switch (key) {
case UP:
if (go())
timer.stop();
break;
case LEFT: turn_left(); break;
case RIGHT: turn_right(); break;
}
repaint();
});
timer = new AnimationTimer() {
long prev_t;
@Override
public void handle(long t) {
if (time == 0) {
time = TIME_LIMIT;
prev_t = t;
repaint();
}
else {
if ((t - prev_t) >= 1.0e+9) {
prev_t += 1.0e+9;
time--;
if (time == 0)
this.stop();
repaint();
}
}
}
};
}
@Override
public void draw(GraphicsContext gc) {
super.draw(gc);
if (time == -1)
return;
gc.setFill(Color.BLACK);
gc.setTextAlign(TextAlignment.CENTER);
gc.fillText(String.format("TIME: %02d", time),
get_view_width() / 2, 20);
}
}