coordinate.java

来自「monkeymahjongg,一个JME3D游戏的源代码」· Java 代码 · 共 73 行

JAVA
73
字号
package jmetest.monkeymahjongg.game;
 
 import java.io.Serializable;
 import java.util.Map;
 import java.util.HashMap;
 
 /**
 * Integer coordinate specifying the "logical" position of a tile.
 *
 * @author Gronau
 */
 public class Coordinate implements Serializable {
 
 private final static long serialVersionUID = 1; 
 
 private static transient Map<Integer, Map<Integer, Map<Integer, Coordinate>>> map;
 
 private final int x;
 private final int y;
 private final int z;
 
 private Coordinate(int x, int y, int z) {
 this.x = x;
 this.y = y;
 this.z = z;
 }
 
 public int getX() {
 return x;
 }
 
 public int getY() {
 return y;
 }
 
 public int getZ() {
 return z;
 }
 
 public Coordinate add(int dx, int dy, int dz) {
 return at(x+dx, y+dy, z+dz);
 }
 
 public static Coordinate at(int x, int y, int z) {
 if (map == null) {
 map = new HashMap<Integer, Map<Integer, Map<Integer, Coordinate>>>();
 }
 Map<Integer, Map<Integer, Coordinate>> mapX = map.get(x);
 if (mapX == null) {
 mapX = new HashMap<Integer, Map<Integer, Coordinate>>();
 map.put(x, mapX);
 }
 Map<Integer, Coordinate> mapY = mapX.get(y);
 if (mapY == null) {
 mapY = new HashMap<Integer, Coordinate>();
 mapX.put(y, mapY);
 }
 if (mapY.containsKey(z)) {
 return mapY.get(z);
 } else {
 Coordinate c = new Coordinate(x,y,z);
 mapY.put(z, c);
 return c;
 }
 }
 
 @Override
 public String toString() {
 return String.format("[%d,%d,%d]", x, y, z);
 }
 
 }
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?