/** size of a tile in pixel (for one dimension)*/
int TILE_SIZE_IN_PIXEL = 4;
/** size of a piece in tiles (for one dimension)*/
int PIECE_SIZE_IN_TILE = 5;
public int tileToPixel(int positionInTiles){
return TILE_SIZE_IN_PIXEL * positionInTiles;
}
/** returns the tile coordinate to which the given tile coordinate belongs
Note: tileToPixel(pixelToTile(x)) only returns x if x is the upper or left edge of a tile
*/
public int pixelToTile(int positionInPixel){
return positionInPixel/TILE_SIZE_IN_PIXEL;
}
아마도 두 개의 인수 (x와 y)로 연산하는 메소드를 원할 것입니다.
ID-> 조각 변환과 그 반대의 경우에는 다양한 방법을 사용할 수 있습니다. 선택할 수있는 것은 정확한 요구 사항 (속도, 게임 크기 ...)에 달려 있습니다. 따라서 구현 세부 사항을 숨기고 나중에 변경할 수 있도록하십시오.
나는 정말 쉬운 솔루션을 시작했으면합니다 : 당신이 시작 도착
public class Piece{
/** x position measured in tiles */
private int x;
/** y position measured in tiles */
private int y;
/** I don't think you need this, but you asked for it. I'd pass around Piece instances instead */
private final Long id;
public void getX(){
return x;
}
public void getY(){
return y;
}
public void getID(){
return id;
}
}
public class Board(){
private Set<Long,Piece> pieces = new HashMap<Piece>(pieces);
public Piece getPieceOnTile(int tileX, int tileY){
for(Piece piece:pieces){
if (isPieceOnTile(piece, tileX, tileY)) return piece;
}
}
private boolean isPieceOnTile(piece, tileX, tileY){
if (piece.getX() < tileX) return false;
if (piece.getX() > tileX + PIECE_SIZE_IN_TILE) return false;
if (piece.getY() < tileY) return false;
if (piece.getY() > tileY + PIECE_SIZE_IN_TILE) return false;
return true;
}
}
희망을. 컴파일러가 없으면 모든 코드가 작성되므로 오타 및 물론 버그가 포함될 수 있습니다.이 버그는 크리에이티브 커먼즈 라이선스에 따라 배포 될 수 있습니다.
세트에 조각을 보관하는 방식은 조각이 많지 않으면 잘 작동합니다. 대부분의 보드 영역에 조각이 포함되어 있지 않으면 2D 배열보다 효과적입니다. 모든 것은 현재 중첩 된 부분이 없다고 가정합니다. 필요한 경우 getPieceOnTile은 조각 컬렉션을 반환해야합니다. 순서가 중요하지 않은 경우 집합, 존재하는 경우 목록입니다.
그 getPixelPosition()에 대한 코드와 나는 당신의 대답을 받아 들일 것입니다 : –
Geez ClickUpvote는 힌트를 얻습니다. 화면을 모델로 사용하는 것은 좋은 디자인이 아닙니다. 메모리 모델을 사용하십시오. 색상 및 위치는 독립적 인 속성입니다. 메모리 모델을 사용하여 다른 속성을 저장할 수도 있습니다. 예 : http://www.fastgraph.com/qfrip.html – jim
x, y 속성을 모르는 상태에서 스프라이트를 화면에 그려서 한 타일에서 다른 타일로 이동했음을 나타낼 수 있습니까? –