diff --git a/serverlogic/src/main/java/com/ljsd/jieling/handler/map/MapLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/handler/map/MapLogic.java index 712098f7f..7e5839f39 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/handler/map/MapLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/handler/map/MapLogic.java @@ -10,6 +10,7 @@ import com.ljsd.jieling.network.session.ISession; import com.ljsd.jieling.protocols.CommonProto; import com.ljsd.jieling.protocols.MapInfoProto; import com.ljsd.jieling.protocols.MessageTypeProto; +import com.ljsd.jieling.util.CellUtil; import com.ljsd.jieling.util.MessageUtil; import com.ljsd.jieling.util.ToolsUtil; import org.slf4j.Logger; @@ -105,7 +106,7 @@ public class MapLogic { int randomIndex = random.nextInt(scMap1.getGroups().length); int x = scMap1.getGroups()[randomIndex][0]; int y = scMap1.getGroups()[randomIndex][1]; - int xy = ToolsUtil.xy2Pos(x, y); + int xy = CellUtil.xy2Pos(x, y); SCMapEventsConfig scMapEvent = SCMapEventsConfig.scMapEventMap.get(scMap1.getEvent()); if (scMapEvent.getStyle() == EventType.enter) { mapManager.setCurXY(xy); diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/CellUtil.java b/serverlogic/src/main/java/com/ljsd/jieling/util/CellUtil.java new file mode 100644 index 000000000..08fd06ebd --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/CellUtil.java @@ -0,0 +1,92 @@ +package com.ljsd.jieling.util; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class CellUtil { + + + public static int xy2Pos(int x, int y) { + if (x < 0 || y < 0){ + return 0; + } + return (x << 8) | y; + } + + public static int[] pos2XY(int pos) { + int[] xy = new int[2]; + xy[0] = (pos >> 8) & 0xff; + xy[1] = pos & 0xff; + return xy; + } + + + + public static void main(String[] args) { + int xx = 19; + int yy = 14; + int pos = xy2Pos(xx,yy); + System.out.println("======> pos : "+pos); + int[] xy = pos2XY(pos); + System.out.println("=======> nX : "+xy[0]+" , nY : "+xy[1]); + System.out.println("====================================="); + Set cells = getOneOpenCell(21,18, pos); + for(Integer i : cells){ + int[] new_xy = pos2XY(i); + System.out.println("=======> nX : "+new_xy[0]+" , nY : "+new_xy[1]); + } + } + + + public static Set getOneOpenCell(int width, int height, int pos){ + int[] xy = pos2XY(pos); + int x = xy[0]; + int y = xy[1]; + Set openCells = new HashSet<>(); + for (int i = -2; i <= 2; i++) { + if (0 <= x+i && x+i < width && i!=0){ + openCells.add(xy2Pos(x+i,y)); +// System.out.println("=======> nX : "+(x+i)+" , nY : "+y); + } + if (0 <= y+i && y+i < height && i!=0){ + openCells.add(xy2Pos(x,y+i)); +// System.out.println("=======> nX : "+x+" , nY : "+(y+i)); + } + } + if (x+1 < width && y+1 < height){ + openCells.add(xy2Pos(x+1,y+1)); +// System.out.println("===============> nX : "+(x+1)+" , nY : "+(y+1)); + } + if (x+1 < width && y-1 >= 0){ + openCells.add(xy2Pos(x+1,y-1)); +// System.out.println("===============> nX : "+(x+1)+" , nY : "+(y-1)); + } + if (x-1 >= 0 && y+1 < height){ + openCells.add(xy2Pos(x-1,y+1)); +// System.out.println("===============> nX : "+(x-1)+" , nY : "+(y+1)); + } + if (x-1 >= 0 && y-1 >= 0){ + openCells.add(xy2Pos(x-1,y-1)); +// System.out.println("===============> nX : "+(x-1)+" , nY : "+(y-1)); + } + System.out.println("====================================="); + return openCells; + } + + + /** + * 做归并-将较小集合归并到较大集合 + * @param s1 较大集合 + * @param s2 较小集合 + * @return + */ + public static Set union(Set s1 , Set s2){ + for(Integer s : s2){ + if(!s1.contains(s)) + s1.add(s); + } + return s1 ; + } +} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/ToolsUtil.java b/serverlogic/src/main/java/com/ljsd/jieling/util/ToolsUtil.java index 3563f9f1e..28fec8dd0 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/util/ToolsUtil.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/ToolsUtil.java @@ -3,26 +3,6 @@ package com.ljsd.jieling.util; public class ToolsUtil { - public static int xy2Pos(int x, int y) { - return (x << 8) | y; - } - public static int[] pos2XY(int pos) { - int[] xy = new int[2]; - xy[0] = (pos >> 8) & 0xff; - xy[1] = pos & 0xff; - return xy; - } - - - - public static void main(String[] args) { - int xx = 12; - int yy = 5; - int pos = xy2Pos(xx,yy); - System.out.println("======> pos : "+pos); - int[] xy = pos2XY(pos); - System.out.println("=======> nX : "+xy[0]+" , nY : "+xy[1]); - } }