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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
package sample3d; import java.util.Arrays; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import my3d._math3d; import my3d._math3d.*; import my3d._shape3d2; import my3d._shape3d2.*; import my3d._slider_dialog.*; import my3d._viewer3d7; import my3d._viewport3d; import my3d._viewport3d3; public abstract class _labyrinth_view extends _viewer3d7 { protected _labyrinth lab; private _shape3d2 wall, goal; private _matrix m_front, m_left, m_right; private Color WALL_COLOR = Color.GRAY, FLOOR_COLOR = Color.WHITE, START_COLOR = Color.RED, GOAL_COLOR = Color.CYAN; @Override public void init_scene(_viewport3d vp) { _slider.set_tick_response(true); _viewport3d3 vp3 = (_viewport3d3)vp; vp3.set_light_balance(0.3, 0, 1.0); vp3.set_zsort_enable(true); lab = new _labyrinth(LAB_W, LAB_D); lab.iter((x, z) -> lab.set_wall(x, z, x % 2 == z % 2)); vp.scene_transform.position.set(0, 0, LEN); wall = new _shape3d2(); _vector[] va = new _vector[4]; for (int i = 0; i < 4; i++) va[i] = new _vector( (i == 0 || i == 1) ? 0.5 : -0.5, (i == 0 || i == 3) ? 0.5 : -0.5, 0); wall.vertices = va; wall.add_face(0, 1, 2, 3); wall.transform(new _transform(new _vector(0, 0, -0.5))); ((_face2)wall.faces.get(0)).set_texture_coordinates( new _point(1, 0), new _point(1, 1), new _point(0, 1), new _point(0, 0)); int c_wall = _math3d.to_argb(WALL_COLOR); _texture tex = new _texture((x, y) -> { y *= 10; x *= 6; x += ((int)(y) % 2 == 0) ? 0.55 : 0.05; return ((y - (int)y) < 0.25 || (x - (int)x) < 0.15) ? 0xffffffff : c_wall; }); wall.material = new _material(WALL_COLOR, 0, tex); set_target_texture(tex); goal = _shape3d2.create_corn(0.15, 0.3, 8); goal.transform(new _transform(new _vector(0, -0.2, 0))); goal.material = new _material(GOAL_COLOR, 10); goal.center = new _vector(0, 0, -0.25); m_front = new _matrix(); m_left = new _matrix(); m_right = new _matrix(); _matrix.rot_y(-Math.PI / 2, m_left); _matrix.rot_y(Math.PI / 2, m_right); } private _transform tf = new _transform(); @Override public void render(_viewport3d vp) { if (view_mode != EXPLORE_MODE) return; render_lab((x, z, obj) -> { tf.position.set(x, 0, z); switch (obj) { case FRONT_WALL: case GOAL: tf.orientation = m_front; break; case LEFT_WALL: tf.orientation = m_left; break; case RIGHT_WALL: tf.orientation = m_right; break; } vp.push_transform(tf); switch (obj) { case FRONT_WALL: case LEFT_WALL: case RIGHT_WALL: vp.render_shape(wall); break; case GOAL: vp.render_shape(goal); break; } vp.pop_transform(); }); } @Override public void draw(GraphicsContext gc) { if (view_mode == EXPLORE_MODE) return; double prev_w = gc.getLineWidth(); if (view_mode == EDIT_MODE) draw_lab(gc); else if (view_mode == TEST_MODE) { // 座標を設定して gc.setTransform(1, 0, 0, -1, (VIEW_W - WALL_SIZE) / 2, BOTTOM); for (int z = 0; z <= LAB_D; z++) { for (int x = -LAB_D / 2 - 1; x <= LAB_D / 2 + 1; x++) { draw_wall(x, z, is_wall(x, z), gc); if (is_goal(x, z)) draw_goal(x, z, true, gc); } } gc.setLineWidth(2); render_lab((x, z, obj) -> { switch (obj) { case FRONT_WALL: draw_line(x, z, x + 1, z, gc); break; case LEFT_WALL: draw_line(x + 1, z, x + 1, z + 1, gc); break; case RIGHT_WALL: draw_line(x, z, x, z + 1, gc); break; case GOAL: draw_goal(x, z, false, gc); break; } }); draw_start(0, 0, 0, gc); } gc.setLineWidth(prev_w); gc.setTransform(1, 0, 0, 1, 0, 0); } @Override public int get_view_width() { return VIEW_W; } @Override public int get_view_height() { return VIEW_H; } private static final int VIEW_W = 400, VIEW_H = 300; private static final int LAB_D = 9, LAB_W = 9; private static final int WALL_SIZE = 20, LEFT = (VIEW_W - WALL_SIZE * LAB_W) / 2, BOTTOM = (VIEW_H + WALL_SIZE * LAB_D) / 2; protected static final int EDIT_MODE = 1, EXPLORE_MODE = 2, TEST_MODE = 3; private int view_mode = EDIT_MODE; protected void set_view_mode(int view_mode) { this.view_mode = view_mode; } private void draw_lab(GraphicsContext gc) { // 壁 gc.setTransform(1, 0, 0, -1, LEFT, BOTTOM); gc.setLineWidth(1); lab.iter((x, z) -> draw_wall(x, z, lab.is_wall(x, z), gc)); // 区画 for (int x = 0; x <= LAB_W; x++) draw_line(x, 0, x, LAB_D, gc); for (int z = 0; z <= LAB_D; z++) draw_line(0, z, LAB_W, z, gc); // ゴール地点 int pos = lab.get_goal_pos(); if (pos != -1) draw_goal(pos % LAB_W, pos / LAB_W, true, gc); // スタート地点 pos = lab.get_start_pos(); if (pos != -1) draw_start(pos % LAB_W, pos / LAB_W, lab.get_start_dir(), gc); } private int[][] dir_matrices = { { 1, 0, 0, 1 }, { 0, -1, 1, 0 }, { -1, 0, 0, -1 }, { 0, 1, -1, 0 } }; private void draw_line(int x1, int y1, int x2, int y2, GraphicsContext gc) { gc.strokeLine( x1 * WALL_SIZE, y1 * WALL_SIZE, x2 * WALL_SIZE, y2 * WALL_SIZE); } private void draw_wall(int x, int z, boolean b_wall, GraphicsContext gc) { gc.setFill(b_wall ? WALL_COLOR : FLOOR_COLOR); gc.fillRect(x * WALL_SIZE, z * WALL_SIZE, WALL_SIZE, WALL_SIZE); } private void draw_start(int x, int z, int dir, GraphicsContext gc) { gc.setFill(START_COLOR); int[] m = dir_matrices[dir]; double k = WALL_SIZE / 3.0; gc.transform(m[0], m[1], m[2], m[3], (x + 0.5) * WALL_SIZE, (z + 0.5) * WALL_SIZE); gc.beginPath(); gc.moveTo(0, k); gc.lineTo(k, -k); gc.lineTo(-k, -k); gc.fill(); } private void draw_goal(int x, int z, boolean b_fill, GraphicsContext gc) { gc.setFill(GOAL_COLOR); int r = WALL_SIZE / 3; gc.beginPath(); gc.arc((x + 0.5) * WALL_SIZE, (z + 0.5) * WALL_SIZE, r, r, 0, 360); if (b_fill) gc.fill(); else gc.stroke(); } public int get_edit_pos(int x, int z) { if (view_mode != EDIT_MODE) return -1; x -= LEFT; if (x > 0) x /= WALL_SIZE; z = BOTTOM - z; if (z > 0) z /= WALL_SIZE; return lab.get_pos(x, z); } private int sign = 1; private void render_lab(_render_listener lis) { do { sign = -sign; render_half(1, 0, 0, 0.5, lis); } while (sign < 0); } private void render_half( int start_x, int z, double left, double right, _render_listener lis) { double lt, rt; // 側面の壁 lt = get_left(start_x, z + 1); rt = right; if (lt < rt) { if (is_wall(start_x, z)) { // 壁を描画して視界を狭める lis.render(sign * start_x, z, (sign < 0) ? LEFT_WALL : RIGHT_WALL); rt = lt; } else { // 視界を広げる start_x++; lt = rt; } } // 正面の壁 z++; for (int x = start_x - 1; lt > left; x--) { if (is_wall(x, z)) { if (x != start_x - 1) // ひとつ右が壁でなければ render_half(start_x, z, lt, rt, lis); lt = get_left(x, z); // 壁を描画 if (x != 0 || sign > 0) lis.render(sign * x, z, FRONT_WALL); start_x = x; } else { lt = get_left(x, z); if (lt <= left) // 左端ならば render_half(start_x, z, left, rt, lis); } // ゴールを描画 if (is_goal(x, z - 1) && (x != 0 || sign > 0)) lis.render(sign * x, z - 1, GOAL); } } // 壁の右位置 private static final double LEN = 1.2; private double get_left(int x, int z) { return (x - 0.5) / (LEN + z - 0.5); } private static final int FRONT_WALL = 1, LEFT_WALL = 2, RIGHT_WALL = 3, GOAL = 4; private static interface _render_listener { public void render(int x, int z, int obj); } private int p_dir, px, pz; private int get_pos(int x, int z) { int[] m = dir_matrices[p_dir]; return lab.get_pos( m[0] * x + m[2] * z + px, m[1] * x + m[3] * z + pz); } private boolean is_wall(int x, int z) { return lab.is_wall(get_pos(sign * x, z)); } private boolean is_goal(int x, int z) { int pos = get_pos(sign * x, z); return pos != -1 && pos == lab.get_goal_pos(); } protected void ready() { int pos = lab.get_start_pos(); if (pos != -1) { px = pos % LAB_W; pz = pos / LAB_W; p_dir = lab.get_start_dir(); } } protected boolean go() { if (!is_wall(0, 1)) { int[] m = dir_matrices[p_dir]; px += m[2]; pz += m[3]; if (is_goal(0, 0)) return true; } return false; } protected void turn_left() { p_dir = (p_dir + 3) % 4; } protected void turn_right() { p_dir = (p_dir + 1) % 4; } }