-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piece.java
114 lines (99 loc) · 3.23 KB
/
Piece.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Point;
import java.util.ArrayList;
import javax.swing.*;
public abstract class Piece {
private static final double TRACKING_MULTIPLIER = 1.15; //relative size of a selected piece
private final Board board; //stores the board reference
private Player player; //stores the player reference
protected Image fullSizeImg; //the unscaled image of the piece
private Image img; //the sized image of the piece
private boolean currentlyTracking = false; //if the piece is tracking the mouse
private boolean isAlive = true;
private Point mouse; //last known coordinate of the mouse
private Point tile; //location of the piece
private Point pos; //x,y position of the top left corner of the piece
private int w; //width and height of the piece
private int h;
public Piece(Board aBoard, Player aPlayer, int xTile, int yTile) {
board = aBoard;
player = aPlayer;
tile = new Point(xTile, yTile);
pos = new Point();
mouse = new Point();
}
public void draw(Graphics2D g2) {
if(isAlive) {
if(currentlyTracking) {
pos.x = (int)(mouse.x - (board.getTileWidth() * TRACKING_MULTIPLIER) / 2);
pos.y = (int)(mouse.y - (board.getTileWidth() * TRACKING_MULTIPLIER) / 2);
} else {
pos.x = (int)(board.getPos().x + (tile.x * board.getTileWidth()));
pos.y = (int)(board.getPos().y + (tile.y * board.getTileHeight()));
}
g2.drawImage(img, pos.x, pos.y, null);
} else {
//TODO: if you want to make a graveyard, draw the piece's location here. move the g2.drawImage (from above) down below the else.
}
}
//TODO: assert dimensions are non-zero
public void resize() {
if(currentlyTracking) {
w = (int)(board.getTileWidth() * TRACKING_MULTIPLIER);
h = (int)(board.getTileHeight() * TRACKING_MULTIPLIER);
} else {
w = (int)board.getTileWidth();
h = (int)board.getTileHeight();
}
img = fullSizeImg.getScaledInstance(w, h, Image.SCALE_SMOOTH);
}
//setter methods
public void setCurrentlyTracking(boolean setTo) {
currentlyTracking = setTo;
}
public void setPosition(Point aTile) {
tile = aTile;
}
public void setMousePos(Point aMouse) {
mouse = aMouse;
}
public void setIsAlive(boolean setTo) {
isAlive = setTo;
}
public void setPlayer(Player aPlayer) {
player = aPlayer;
}
//getter methods
public Point getTile() {
return tile;
}
public Board getBoard() {
return board;
}
public Player getPlayer() {
return player;
}
public Player getOpponent() {
return player.getOpponent();
}
public int getTeam() {
return player.getTeam();
}
public int getStartPos() {
return player.getStartPos();
}
public boolean onBoard(Point p) {
return player.onBoard(p);
}
public Piece pieceAt(Point p) {
return player.pieceAt(p);
}
public abstract ArrayList<Point> validTiles();
public abstract void setImg();
public abstract Piece copy();
public abstract int getValue();
}