aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/board/TileVec2.java
blob: 6ccd1166327be36bb2e58465ba9ca2c2b101340d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package fr.umlv.java.wallj.board;

import org.jbox2d.common.Vec2;

import java.util.Objects;

/**
 * A typed immutable tile coordinate vector containing the coordinates of a Tile in a Board.
 *
 * @author Pacien TRAN-GIRARD
 */
public final class TileVec2 {

  private static final int TILE_DIM = 20;

  /**
   * @param row the row
   * @param col the column
   * @return a corresponding tile vector
   */
  public static TileVec2 of(int row, int col) {
    return new TileVec2(row, col);
  }

  /**
   * @param v a JBox2D Vec2 vector
   * @return the coordinates of the tile containing the given point
   */
  public static TileVec2 of(Vec2 v) {
    return new TileVec2((int) (v.x / TILE_DIM), (int) (v.y / TILE_DIM));
  }

  private final int row, col;

  private TileVec2(int row, int col) {
    this.row = row;
    this.col = col;
  }

  /**
   * @return the row
   */
  public int getRow() {
    return row;
  }

  /**
   * @return the column
   */
  public int getCol() {
    return col;
  }

  /**
   * @return the corresponding JBox2D coordinates of the top-left corner of the tile
   */
  public Vec2 toVec2() {
    return new Vec2(row * TILE_DIM, col * TILE_DIM);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof TileVec2)) return false;
    TileVec2 tileVec2 = (TileVec2) o;
    return row == tileVec2.row &&
           col == tileVec2.col;
  }

  @Override
  public int hashCode() {
    return Objects.hash(row, col);
  }

  @Override
  public String toString() {
    return "TileVec2{" +
           "row=" + row +
           ", col=" + col +
           '}';
  }

}