-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
124 lines (98 loc) · 4.21 KB
/
Player.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
115
116
117
118
119
120
121
122
123
124
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import java.awt.Image;
public class Player extends JLabel{
private static final int ifFocused = JComponent.WHEN_IN_FOCUSED_WINDOW;
static ImageIcon pDefault = new ImageIcon("Assets\\playerBlock.png");
int row, col;
// static Image pIcon;
static Image pIcon = pDefault.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
Map map;
public Player(Map map, int[] coords) {
// pIcon = pDefault.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
this.map = map;
this.row = coords[0];
this.col = coords[1];
}
/**
* uses the internal coordinates {@value this.row} and {@value this.col}
* and previously logged pointer data from {@code this.canMoveObj(direct)}
* to reverse through the scanned data in {@code this.canMoveObj(direct)}
* and overwrite the data from the last action to the current action,
* starting at the logged pointer.
*/
public void move() {
for (int r = Map.r, c = Map.c; r > -1 && r < map.map.length && c > -1 &&
c < map.map[1].length; r -= Map.dr[Map.direct], c -= Map.dc[Map.direct]) {
if (map.map[r][c] == 'p') {
map.map[r][c] = ' ';
col = c + Map.dc[Map.direct];
row = r + Map.dr[Map.direct];
break;
}
map.map[r][c] = map.map[r - Map.dr[Map.direct]][c - Map.dc[Map.direct]];
if (r < 1 || r > map.map.length - 2 || c < 1 || c > map.map[1].length - 2) {
map.map[r][c] = 'd';
Map.blockCount--;
}
map.map_move[r][c] = true;
}
}
/**
* this method returns {@code true} if player is within the bounds of
* {@code map.map}.
*
* @param direct takes directional data
* @param r takes row coordinate 'y coord'
* @param c takes column coordinate 'x coord'
* @return boolean returns if a valid move is available
*/
public boolean canMove(int r, int c) { // checks all sides of map
return r + Map.dr[Map.direct] > 0 && r + Map.dr[Map.direct] < map.map.length - 1 &&
c + Map.dc[Map.direct] > 0 && c + Map.dc[Map.direct] < map.map[1].length - 1;
}
/**
* given a direction {@value direct}, the program loops through from the player's
* position {@value row, col} to the boarder or next empty space {@value ' '}.
* it then calls the nescessary methods to validate their movement conditions individually.
* Through the ternary operator, the method accumulates these method call outputs in a
* logical "and" operation. Furthermore, on termination of the {@code for loop} the
* method logs the pointer data in {@value Map.r} and {@value Map.c} to use in the
* {@code this.move()} method.
*
* @param direct provides directional data
* @return boolean outputs whether a valid move was made or not
*/
public boolean canMoveObj() {
boolean isValid = true;
if (Map.direct == 4)
return false;
for(int r = row, c = col; r > -1 && r < map.map.length &&
c > -1 && c < map.map[1].length; r += Map.dr[Map.direct], c += Map.dc[Map.direct]) {
if (map.map[r][c] == ' ') {
Map.r = r;
Map.c = c;
break;
}
else if (map.map[r][c] == 'p') {
isValid = (canMove(r, c)) ? isValid : false;
}
else {
isValid = (Block.canMoveObj(r, c)) ? isValid : false;
}
Map.r = r;
Map.c = c;
}
return isValid;
}
public void putInputMap(KeyStroke key, String actionString) {
this.getInputMap(ifFocused).put(key, actionString);
}
public void putActionMap(String actionString, Action action) {
this.getActionMap().put(actionString, action);
}
static public Image getImage() {return pIcon;}
}