-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.java
239 lines (207 loc) · 9.43 KB
/
Block.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import javax.swing.ImageIcon;
import legacy.Debug;
import java.awt.Image;
public class Block {
static ImageIcon universalBlock = new ImageIcon("Assets\\universalBlock.png");
static ImageIcon immovableBlock = new ImageIcon("Assets\\immovableBlock.png");
static ImageIcon destroyedBlock = new ImageIcon("Assets\\dead.png");
static ImageIcon pushLimitBlock = new ImageIcon("Assets\\pushMaximum2Block.png");
static ImageIcon chainedBlock = new ImageIcon("Assets\\chainBlock.png");
static ImageIcon directLRBlock = new ImageIcon("Assets\\directionalBlockLeftRight.png");
static ImageIcon directUDBlock = new ImageIcon("Assets\\directionalBlockUpDown.png");
static ImageIcon pushMinimumBlock = new ImageIcon("Assets\\pushMiniumum2Block.png");
static Image pushMinimumBlockImage = pushMinimumBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image universalBlockImage = universalBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image immovableBlockImage = immovableBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image destroyedBlockImage = destroyedBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image pushLimitBlockImage = pushLimitBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image directLRBlockImage = directLRBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image directUDBlockImage = directUDBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Image chainedBlockImage = chainedBlock.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static Map map;
Debug debug = new Debug();
public Block(Map loadedMap) { map = loadedMap; }
/**
* takes in {@value tpye} data to allocate the correct movement conditions onto
* the box that is being scanned in {@code player.canMoveObj(dircet)}. The method
* then returns the data that is returned from the repective move conditions back
* to the player instance.
*
* @param direct parses directional data to the movement condition validators
* @param type uses (char) type data to correctly determine block type
*
* @return boolean returns output from the movement condition validators
*/
public static boolean canMoveObj(int row, int col) {
boolean isValid = false;
char type = map.map[row][col];
switch(type) {
case 'u':
isValid = canMoveBasic();
break;
case 'x':
isValid = canMoveImmovable();
break;
case 'd':
isValid = canMoveDead();
break;
case '>':
isValid = canMoveDirectLR();
break;
case '^':
isValid = canMoveDirectUD();
break;
case 'm':
isValid = canMovePushMin2(row, col);
break;
case 'l':
isValid = canMovePushLim2(row, col);
break;
case 'C':
isValid = canMoveChain(row, col);
break;
}
return isValid;
}
/**
* directional movement validator for the "basic" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
public static boolean canMoveBasic() {
return true;
}
/**
* directional movement validator for the "immoveable" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMoveImmovable() {
return false;
}
/**
* directional movement validator for the "dead" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMoveDead() {
return true;
}
/**
* directional movement validator for the "left-right" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMoveDirectLR() {
return Map.direct == 1 || Map.direct == 3;
}
/**
* directional movement validator for the "up-down" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMoveDirectUD() {
return Map.direct == 0 || Map.direct == 2;
}
/**
* directional movement validator for the "push minimum 2" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMovePushMin2(int row, int col) {
if (row + Map.dr[Map.direct] > -1 && row + Map.dr[Map.direct] < map.map.length &&
col + Map.dc[Map.direct] > -1 && col + Map.dc[Map.direct] < map.map[1].length) {
return map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] != 'd' &&
map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] != ' ' ||
map.map[row - Map.dr[Map.direct]][col - Map.dc[Map.direct]] != 'p';
} else {
return false;
}
}
/**
* directional movement validator for the "push limit 2" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMovePushLim2(int row, int col) {
if (row + Map.dr[Map.direct] > -1 && row + Map.dr[Map.direct] < map.map.length &&
col + Map.dc[Map.direct] > -1 && col + Map.dc[Map.direct] < map.map[1].length) {
return (map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] == 'd' ||
map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] == ' ') &&
(map.map[row - Map.dr[Map.direct]][col - Map.dc[Map.direct]] != 'p' &&
map.map[row - 2 * Map.dr[Map.direct]][col - 2 * Map.dc[Map.direct]] == 'p' ||
map.map[row - Map.dr[Map.direct]][col - Map.dc[Map.direct]] == 'p') ||
(row + 2 * Map.dr[Map.direct] == 0 || row + 2 * Map.dr[Map.direct] == map.map.length - 1 ||
col + 2 * Map.dc[Map.direct] == 0 || col + 2 * Map.dc[Map.direct] == map.map[1].length - 1 ||
row + 2 * Map.dr[Map.direct] > -1 && row + 2 * Map.dr[Map.direct] < map.map.length &&
col + 2 * Map.dc[Map.direct] > -1 && col + 2 * Map.dc[Map.direct] < map.map[1].length &&
map.map[row + 2 * Map.dr[Map.direct]][col + 2 * Map.dc[Map.direct]] == ' ') &&
map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] != ' ' &&
map.map[row - Map.dr[Map.direct]][col - Map.dc[Map.direct]] == 'p';
} else {
return false;
}
}
/**
* directional movement validator for the "chain" block.
*
* @param direct uses directional data to return its movability state
*
* @return boolean returns movability of the block.
*/
private static boolean canMoveChain(int row, int col) {
if (row + Map.dr[Map.direct] > -1 && row + Map.dr[Map.direct] < map.map.length &&
col + Map.dc[Map.direct] > -1 && col + Map.dc[Map.direct] < map.map[1].length) {
return (map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] == 'C'||
map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] == 'd'||
map.map[row + Map.dr[Map.direct]][col + Map.dc[Map.direct]] == ' ') &&
(map.map[row - Map.dr[Map.direct]][col - Map.dc[Map.direct]] == 'C'||
map.map[row - Map.dr[Map.direct]][col - Map.dc[Map.direct]] == 'p');
} else {
return false;
}
}
/**
* uses type data to correctly assign assets to the correct blocks
*
* @param type uses (char) type data to correctly determine block type
*
* @return Image returns correct sprites for the block type.
*/
static public Image getImage(char type) {
switch(type) {
case 'u':
return universalBlockImage;
case 'x':
return immovableBlockImage;
case 'd':
return destroyedBlockImage;
case '>':
return directLRBlockImage;
case '^':
return directUDBlockImage;
case 'm':
return pushMinimumBlockImage;
case 'l':
return pushLimitBlockImage;
case 'C':
return chainedBlockImage;
default:
return null;
}
}
}