refactoring, deploy Position and XYGridIterator

- try to use more Position instead of (x,y)
  - create PositionWithDpeth for its specific usage in path finding
- create a XYGridITerator that walk cells from grid X then Y.
This commit is contained in:
philippe lhardy
2020-12-28 19:12:35 +01:00
parent 0c886f0cd1
commit 02fda1fc2e
15 changed files with 311 additions and 162 deletions

View File

@@ -1,6 +1,7 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.Position;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import java.awt.*;
@@ -20,15 +21,15 @@ extends MazeCellRenderer
super(model, frame, x, y);
}
void drawLine(Graphics g, int refx, int refy, int x, int y, int a, int b) {
void drawLine(Graphics g, Position ref, int x, int y, int a, int b) {
g.drawLine(
(int) ((refx + x) * width / SUBCELL), (int) ((refy + y) * height / SUBCELL),
(int) ((refx + a) * width / SUBCELL),(int) ((refy + b) * height / SUBCELL));
(int) ((ref.getX() + x) * width / SUBCELL), (int) ((ref.getY() + y) * height / SUBCELL),
(int) ((ref.getX() + a) * width / SUBCELL),(int) ((ref.getY() + b) * height / SUBCELL));
}
@Override
public void drawBackground(Graphics g, int pX, int pY, short walls) {
UV uv= new UV(pX,pY);
public void drawBackground(Graphics g, Position cell, short walls) {
UV uv= new UV(cell);
int x = offsetX + (int) (uv.getX() * width / SUBCELL);
int y = offsetY + (int) (uv.getY() * height / SUBCELL);
Color savecolor = g.getColor();
@@ -39,47 +40,39 @@ extends MazeCellRenderer
}
class UV
extends Position
{
int u;
int v;
UV(int u, int v)
UV(Position p)
{
this.u=u;
this.v=v;
}
int getX()
{
return u*4 + 2*(v%2);
}
int getY()
{
return 3*v;
super(p);
u=p.getX();
v=p.getY();
setX(u*4 + 2*(v%2));
setY(3*v);
}
}
@Override
public void drawWalls(Graphics g, int pX, int pY, short walls) {
drawHalfNW(g, pX, pY, walls);
public void drawWalls(Graphics g, Position cell, short walls) {
drawHalfNW(g, cell, walls);
}
void drawHalfNW(Graphics g, int refx, int refy, short walls)
void drawHalfNW(Graphics g, Position cell, short walls)
{
UV uv= new UV(refx,refy);
int x = uv.getX();
int y = uv.getY();
UV uv= new UV(cell);
// NW : (0,1) - (2,0)
if (Brick.isFlagSet( Brick.UP, walls))
drawLine(g,x, y, 0,1,2,0);
drawLine(g,uv, 0,1,2,0);
// W : (0,1) - (0,3)
if (Brick.isFlagSet( Brick.LEFT, walls))
drawLine(g,x,y,0,1,0,3);
drawLine(g,uv,0,1,0,3);
// NE : (2,0) - (4,1)
// if (Brick.isFlagSet( Brick.NE, walls))
drawLine(g,x,y,2,0,4,1);
drawLine(g,uv,2,0,4,1);
}
void drawHalfSW(Graphics g)