From fa2ee79f25b09fb14dbf35b710536230ecadf95f Mon Sep 17 00:00:00 2001 From: QuentinBeauchet Date: Sat, 27 Feb 2021 22:53:57 +0100 Subject: [PATCH] FusionRoom --- src/main/java/Main.java | 11 +- src/main/java/Model/BasicPlayer.java | 34 +--- src/main/java/Model/Cell.java | 4 +- src/main/java/Model/Entity.java | 26 ++- src/main/java/Model/Map.java | 253 ++++++++++++++------------- src/main/java/Model/Procedure.java | 12 +- src/main/java/Model/Room.java | 10 +- 7 files changed, 176 insertions(+), 174 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 6556d73..ff1e613 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,11 +14,14 @@ public static void main(String[] args) { //myMap.upgradeCellsWithEntitys(); //System.out.println(myMap.cheminFind(myMap.getRooms().get(0).getDoors().get(0),myMap.getRooms().get(1).getDoors().get(0))); //myMap.setPath(); - Procedure.setRamdomRooms(myMap); - Model.Procedure.setRamdomMob(myMap); + Procedure.setRandomRooms(myMap); + Model.Procedure.setRandomMob(myMap); - //System.out.println(Procedure.getRandomRoom()); - myMap.FustionRoom(); + //myMap.ligne(new Position(10,5),new Position(30,30)); + //myMap.ligneV2(new Position(10,5),new Position(30,30)); + + new Affichage(myMap); + myMap.RoomFusion(); new Affichage(myMap); } } diff --git a/src/main/java/Model/BasicPlayer.java b/src/main/java/Model/BasicPlayer.java index c0e88da..c29d58a 100644 --- a/src/main/java/Model/BasicPlayer.java +++ b/src/main/java/Model/BasicPlayer.java @@ -2,13 +2,11 @@ public class BasicPlayer extends Entity { - private Map CurrentMap; - - public BasicPlayer(Position position, Map map) { - super(position, map); - this.CurrentMap = map; + public BasicPlayer(Position position) { + super(position); } + /*TODO deplacer tout ça private boolean checkIfNewPositionAccessible(Position targetPosition) { return getCurrentMap().Cells.get(targetPosition.getY()).get(targetPosition.getX()).isAccesible(); } @@ -16,34 +14,10 @@ private boolean checkIfNewPositionAccessible(Position targetPosition) { public void move(Position newPosition) { if(checkIfNewPositionAccessible(newPosition)) setPosition(newPosition); - } + }*/ @Override public String toString() { return "\u001B[32m@"; } - - public void move(int targetX, int targetY) { - move(new Position(targetX, targetY)); - } - - public void moveLeft() { - move(new Position(getPositionX() - 1, getPositionY())); - } - - public void moveRight() { - move(new Position(getPositionX() + 1, getPositionY())); - } - - public void moveUp() { - move(new Position(getPositionX(), getPositionY() - 1)); - } - - public void moveDown() { - move(new Position(getPositionX(), getPositionY() + 1)); - } - - public Map getCurrentMap() { - return CurrentMap; - } } diff --git a/src/main/java/Model/Cell.java b/src/main/java/Model/Cell.java index 07272c4..8304a4c 100644 --- a/src/main/java/Model/Cell.java +++ b/src/main/java/Model/Cell.java @@ -2,7 +2,7 @@ public class Cell{ public enum CellType { - BORDER, NORMAL, DOOR, PATH, VOID, ANGLE, ; + BORDER, NORMAL, DOOR, PATH, VOID, ; } private final boolean isAccesible; private final CellType type; @@ -43,7 +43,7 @@ public String toString() { case PATH -> { return "\u001B[34m*"; } - case ANGLE, BORDER -> { + case BORDER -> { return "\u001B[31m*"; } case DOOR -> { diff --git a/src/main/java/Model/Entity.java b/src/main/java/Model/Entity.java index 3aa2738..4872667 100644 --- a/src/main/java/Model/Entity.java +++ b/src/main/java/Model/Entity.java @@ -3,11 +3,9 @@ public abstract class Entity { protected Position Position; - protected Map CurrentMap; - public Entity(Position position, Map currentMap) { + public Entity(Position position) { this.Position = position; - this.CurrentMap = currentMap; } public Position getPosition() { @@ -18,8 +16,6 @@ protected void setPosition(Position position) { this.Position = position; } - - /* Pour que ce soit moins chiant */ public int getPositionX() { return Position.getX(); @@ -36,4 +32,24 @@ protected void setPositionX(int x) { protected void setPositionY(int y) { setPosition(new Position(getPosition().getX(), y)); } + + public void move(Position pos) { + move(new Position(pos.getX(), pos.getY())); + } + + public void moveLeft() { + move(new Position(getPositionX() - 1, getPositionY())); + } + + public void moveRight() { + move(new Position(getPositionX() + 1, getPositionY())); + } + + public void moveUp() { + move(new Position(getPositionX(), getPositionY() - 1)); + } + + public void moveDown() { + move(new Position(getPositionX(), getPositionY() + 1)); + } } \ No newline at end of file diff --git a/src/main/java/Model/Map.java b/src/main/java/Model/Map.java index bc44bbc..55e1185 100644 --- a/src/main/java/Model/Map.java +++ b/src/main/java/Model/Map.java @@ -8,7 +8,7 @@ public class Map { - public final static int nbrMaxRooms=5; + public final static int nbrMaxRooms = 8; protected int SIZEX; protected int SIZEY; protected ArrayList> Cells; @@ -16,12 +16,12 @@ public class Map { protected ArrayList Entitys = new ArrayList<>(); public Map(int SIZEX, int SIZEY) { - this.SIZEX=SIZEX; - this.SIZEY=SIZEY; + this.SIZEX = SIZEX; + this.SIZEY = SIZEY; fillMap(Cell.CellType.VOID); } - public void fillMap(Cell.CellType type){ + public void fillMap(Cell.CellType type) { Cells = new ArrayList<>(); for (int i = 0; i < SIZEY; i++) { ArrayList line = new ArrayList<>(); @@ -36,47 +36,44 @@ public void fillMap(Cell.CellType type){ public String toString() { StringBuilder sb = new StringBuilder(); sb.append(" "); - for (int x = 0; x < this.SIZEX ; x++) { + for (int x = 0; x < this.SIZEX; x++) { sb.append("\u001B[0m").append(x); - if(x<10){ + if (x < 10) { sb.append(" "); - } - else{ + } else { sb.append(" "); } } sb.append("\n"); - for (int y = 0; y < this.SIZEY ; y++) { - if(y<10){ + for (int y = 0; y < this.SIZEY; y++) { + if (y < 10) { sb.append("\u001B[0m").append(" ").append(y).append(" "); - } - else{ + } else { sb.append("\u001B[0m").append(y).append(" "); } - for (int x = 0; x < this.SIZEX ; x++) { - sb.append(this.get(x,y)).append(" "); + for (int x = 0; x < this.SIZEX; x++) { + sb.append(this.get(x, y)).append(" "); } sb.append("\n"); } return sb.toString(); } - public void addRoom(Room r , Position p){ - if(!isCollision(r,p)){ - for (int y = 0; y < r.getSIZEY() ; y++) { - for (int x = 0; x < r.getSIZEX() ; x++) { + public void addRoom(Room r, Position p) { + if (!isCollision(r, p)) { + for (int y = 0; y < r.getSIZEY(); y++) { + for (int x = 0; x < r.getSIZEX(); x++) { this.set(p.getX() + x, p.getY() + y, r.get(x, y)); - } } + } //TODO a modifier / partie ArrayList Doors - ArrayList Doors=new ArrayList<>(); - for (Position pos : r.getDoors()){ + ArrayList Doors = new ArrayList<>(); + for (Position pos : r.getDoors()) { Doors.add(pos.somme(p)); } r.addDoors(Doors); Rooms.add(r); - } - else{ + } else { throw new CollisionRoom(r); } } @@ -86,33 +83,31 @@ public void addEntity(Entity e) { } public void upgradeCellsWithEntitys() { - for(Entity e : Entitys) { + for (Entity e : Entitys) { Cells.get(e.getPositionY()).get(e.getPositionX()).setEntity(e); } } - public boolean isCollision(Room r, Position p){ - for (int y = 0; y < r.getSIZEY() ; y++) { - for (int x = 0; x < r.getSIZEX() ; x++) { - if(this.get(p.getX()+x,p.getY()+y).getType() != Cell.CellType.VOID - )return true; + public boolean isCollision(Room r, Position p) { + for (int y = 0; y < r.getSIZEY(); y++) { + for (int x = 0; x < r.getSIZEX(); x++) { + if (this.get(p.getX() + x, p.getY() + y).getType() != Cell.CellType.VOID + ) return true; } } return false; } - public boolean suppX (int x, int y) - { + public boolean suppX(int x, int y) { if (Math.abs(x) > Math.abs(y)) return true; return false; } - public boolean sameRoom (Position Init, Position Dest) - { + public boolean sameRoom(Position Init, Position Dest) { return false; } - public ArrayList cheminFind (Position posInit, Position posDest) { + public ArrayList cheminFind(Position posInit, Position posDest) { int deltaX; int deltaY; @@ -189,24 +184,24 @@ public ArrayList cheminFind (Position posInit, Position posDest) { return positionList; } - public Cell get(int x , int y){ + public Cell get(int x, int y) { return Cells.get(y).get(x); } - public Cell get(Position pos){ + public Cell get(Position pos) { return get(pos.getX(), pos.getY()); } - public void set(int x , int y, Cell c){ - Cells.get(y).set(x,c); + public void set(int x, int y, Cell c) { + Cells.get(y).set(x, c); } - public void set(Position pos, Cell c){ - set(Cells,pos,c); + public void set(Position pos, Cell c) { + set(Cells, pos, c); } - public void set(ArrayList> map, Position pos, Cell c){ - map.get(pos.getX()).set(pos.getY(),c); + public void set(ArrayList> map, Position pos, Cell c) { + map.get(pos.getX()).set(pos.getY(), c); } public int getSIZEX() { @@ -217,98 +212,116 @@ public int getSIZEY() { return SIZEY; } - public ArrayList getRooms(){ + public ArrayList getRooms() { return Rooms; } - //TODO c'est moche tkt (les angles marchent pas encore parfaitement - public void FustionRoom(){ - ArrayList> arrayListCell = new ArrayList<>(); - for (int i = 0; i < SIZEY; i++) { - ArrayList line= new ArrayList<>(); - for (int j = 0; j < SIZEX; j++) { - line.add(get(j,i)); + public void ligne(Position pos1, Position pos2) { + //TODO svp quelqu'un peut me dire pourquoi il faut inverser ? + Position p1 = new Position(pos1.getY(), pos1.getX()); + Position p2 = new Position(pos2.getY(), pos2.getX()); + ArrayList chemin = new ArrayList<>(); + chemin.add(p1); + Position lastPos = chemin.get(chemin.size() - 1); + while (lastPos.getX() != p2.getX()) { + if (lastPos.getX() > p2.getX()) { + chemin.add(new Position(lastPos.getX() - 1, lastPos.getY())); + } else if (lastPos.getX() < p2.getX()) { + chemin.add(new Position(lastPos.getX() + 1, lastPos.getY())); } - arrayListCell.add(line); + lastPos = chemin.get(chemin.size() - 1); + } + while (lastPos.getY() != p2.getY()) { + lastPos = chemin.get(chemin.size() - 1); + if (lastPos.getY() > p2.getY()) { + chemin.add(new Position(lastPos.getX(), lastPos.getY() - 1)); + } else if (lastPos.getY() < p2.getY()) { + chemin.add(new Position(lastPos.getX(), lastPos.getY() + 1)); + } + } + for (Position p : chemin) { + set(p, new Cell(true, Cell.CellType.PATH)); } + } - for (int y = 0; y < SIZEY; y++) { - for (int x = 0; x < SIZEX; x++) { - Position THIS = new Position(x,y); - Position UP = THIS.somme(0,-1); - Position DOWN = THIS.somme(0,1); - Position RIGHT = THIS.somme(1,0); - Position LEFT = THIS.somme(-1,0); - Position DOWNofLEFT = THIS.somme(-1,1); - Position DOWNofRIGHT = THIS.somme(1,1); - Position UPofRIGHT = THIS.somme(1,-1); - Position UPofLEFT = THIS.somme(-1,-1); - - if(x==0){ - LEFT = THIS.somme(0,0); - DOWNofLEFT = THIS.somme(1,1); - UPofLEFT = LEFT.somme(1,-1); - } - if(x==(SIZEX-1)){ - RIGHT = THIS.somme(0,0); - DOWNofRIGHT = THIS.somme(-1,1); - UPofRIGHT = RIGHT.somme(-1,-1); + public void ligneV2(Position pos1, Position pos2) { + Position milieu = new Position((pos1.getX() + pos2.getX()) / 2, (pos1.getY() + pos2.getY()) / 2); + ligne(pos1, milieu); + ligne(milieu, pos2); + } + + //TODO a optimiser c'est affreux + public void RoomFusion(){ + for (int i = 0; i < getRooms().size(); i=i+2) { + ligneV2(Procedure.getRandomPosition(getRooms().get(i)),Procedure.getRandomPosition(getRooms().get(i+1))); + } + for (int i = 0; i < getRooms().size(); i=i+4) { + ligneV2(Procedure.getRandomPosition(getRooms().get(i)),Procedure.getRandomPosition(getRooms().get(i+3))); + } + + for (int y = 0; y < getSIZEY(); y++) { + for (int x = 0; x < getSIZEX(); x++) { + if(get(x,y).getType().equals(Cell.CellType.PATH)){ + set(x,y,new Cell(true, Cell.CellType.NORMAL)); } - if(y==0){ - UP = THIS.somme(0,0); - if(x==0){ - UPofLEFT = LEFT.somme(1,0); - UPofRIGHT = RIGHT.somme(2,0); - } - else if(x==(SIZEX-1)){ - UPofRIGHT = RIGHT.somme(-1,0); - UPofLEFT = LEFT.somme(-2,0); - } - else{ - UPofLEFT = LEFT.somme(-1,0); - UPofRIGHT = RIGHT.somme(1,0); + } + } + + for (int y = 0; y < getSIZEY(); y++) { + for (int x = 0; x < getSIZEX(); x++) { + if (get(x, y).getType().equals(Cell.CellType.VOID)) { + Position THIS = new Position(x, y); + ArrayList voisins = new ArrayList<>(); + voisins.add(THIS.somme(0, -1)); + voisins.add(THIS.somme(0, 1)); + voisins.add(THIS.somme(1, 0)); + voisins.add(THIS.somme(-1, 0)); + voisins.add(THIS.somme(-1, 1)); + voisins.add(THIS.somme(1, 1)); + voisins.add(THIS.somme(1, -1)); + voisins.add(THIS.somme(-1, -1)); + for (Position p : voisins) { + if (p.getX() >= 0 && p.getY() >= 0 && p.getX() < getSIZEX() && p.getY() < getSIZEY()) { + if (get(p).getType().equals(Cell.CellType.NORMAL)) { + set(x, y, new Cell(true, Cell.CellType.BORDER)); + } + } } } - if(y==(SIZEY-1)){ - DOWN = THIS.somme(0,0); - if(x==0){ - DOWNofLEFT = THIS.somme(1,0); - DOWNofRIGHT = THIS.somme(2,0); - } - else if(x==(SIZEX-1)){ - DOWNofLEFT = THIS.somme(-1,0); - DOWNofRIGHT = THIS.somme(-2,1); + } + } + + for (int y = 0; y < getSIZEY(); y++) { + for (int x = 0; x < getSIZEX(); x++) { + if(get(x,y).getType().equals(Cell.CellType.BORDER)){ + Position THIS = new Position(x,y); + ArrayList voisins = new ArrayList<>(); + voisins.add(THIS.somme(0,-1)); + voisins.add(THIS.somme(0,1)); + voisins.add(THIS.somme(1,0)); + voisins.add(THIS.somme(-1,0)); + voisins.add(THIS.somme(-1,1)); + voisins.add(THIS.somme(1,1)); + voisins.add(THIS.somme(1,-1)); + voisins.add(THIS.somme(-1,-1)); + int nbrVoidVoisins=0; + for(Position p : voisins){ + if(p.getX()>=0 && p.getY()>=0 && p.getX()0 && p.getX()0 && p.getY() 4; - } -} +} \ No newline at end of file diff --git a/src/main/java/Model/Procedure.java b/src/main/java/Model/Procedure.java index 8733614..ecf94a3 100644 --- a/src/main/java/Model/Procedure.java +++ b/src/main/java/Model/Procedure.java @@ -16,7 +16,7 @@ public static Room getRandomRoom() { int SIZEX = rand.nextInt(Room.MaxSize - Room.MinSize + 1) + Room.MinSize; int SIZEY = rand.nextInt(Room.MaxSize - Room.MinSize + 1) + Room.MinSize; Room r = new Room(SIZEX, SIZEY); - setRandomPorte(4, r); + //setRandomPorte(4, r); return r; } @@ -38,7 +38,7 @@ private static Position getRandomPosition(int width, int heigth) { //TODO optimiser ça pas ouf le while public static Position getRandomPosition(Room r) { Position pos = getRandomPosition(r.getSIZEX(), r.getSIZEY()); - while ((r.get(pos).getType().equals(Cell.CellType.BORDER)) || (r.get(pos).getType().equals(Cell.CellType.ANGLE)) || (r.get(pos).getType().equals(Cell.CellType.DOOR))) { + while ((r.get(pos).getType().equals(Cell.CellType.BORDER)) || (r.get(pos).getType().equals(Cell.CellType.DOOR))) { pos = getRandomPosition(r.getSIZEX(), r.getSIZEY()); } return pos.somme(r.getAbsolutePos()); @@ -78,7 +78,7 @@ public static void setRandomPorte(int NbrPorteMax, Room r) { * * @param map Map */ - public static void setRamdomRooms(Map map) { + public static void setRandomRooms(Map map) { int nbrRooms = 0; long startTime = System.currentTimeMillis(); long elapsedTime = 0; @@ -103,7 +103,9 @@ public static void setRamdomRooms(Map map) { private static void setRandomMob(Room r, Map map) { Position pos = getRandomPosition(r); Cell cell = map.get(pos); - cell.setEntity(new BasicPlayer(pos, r)); + Entity e=new BasicPlayer(pos); + cell.setEntity(e); + map.addEntity(e); } /** @@ -111,7 +113,7 @@ private static void setRandomMob(Room r, Map map) { * * @param map Map */ - public static void setRamdomMob(Map map) { + public static void setRandomMob(Map map) { for (Room r : map.getRooms()) { int nbrMobs = rand.nextInt(Room.nbrMaxMobPerRoom)+1; for (int i = 0; i < nbrMobs; i++) { diff --git a/src/main/java/Model/Room.java b/src/main/java/Model/Room.java index 387e9d0..bb4bbd2 100644 --- a/src/main/java/Model/Room.java +++ b/src/main/java/Model/Room.java @@ -24,14 +24,8 @@ private void setBorders(){ this.set(i,SIZEY-1,new Cell(false, Cell.CellType.BORDER)); if (i==0 || i==(SIZEX-1)){ for (int j = 0; j < SIZEY; j++) { - if(j==0 || j==(SIZEY-1)){ - this.set(i,j,new Cell(false, Cell.CellType.ANGLE)); - this.set(i,j,new Cell(false, Cell.CellType.ANGLE)); - } - else{ - this.set(i,j,new Cell(false, Cell.CellType.BORDER)); - this.set(i,j,new Cell(false, Cell.CellType.BORDER)); - } + this.set(i,j,new Cell(false, Cell.CellType.BORDER)); + this.set(i,j,new Cell(false, Cell.CellType.BORDER)); } } }