-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.java
216 lines (201 loc) · 3.99 KB
/
Model.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
/**
* Model: Contains all the state and logic
* Does not contain anything about images or graphics, must ask view for that
*
* has methods to
* detect collision with boundaries
* decide next direction
* provide direction
* provide location
**/
public class Model {
final static int Incr = 10;
final static int noIncr = 0;
final static int xIncr = 8;
final static int yIncr = 2;
int picNum;
int picDir;
static int right;
static int down;
int xloc;
int yloc;
int frameWidth;
int frameHeight;
int imgWidth;
int imgHeight;
int buffer = -50;
static Direction dir = Direction.SOUTHEAST;
Model(int frameWidth,int frameHeight,int imgWidth,int imgHeight) {
right = xIncr;
down = yIncr;
picNum = 0;
picDir = 0;
xloc = 0;
yloc = 0;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.imgWidth = imgWidth;
this.imgHeight = imgHeight;
}
public int getX() {
return xloc;
}
public int getY() {
return yloc;
}
public static Direction getDirect() {
return dir;
}
public static void setDirect(Direction d) {
dir = d;
if (dir == Direction.SOUTHWEST) {
right = -xIncr;
down = yIncr;
}
else if (dir == Direction.SOUTHEAST) {
right = xIncr;
down = yIncr;
}
else if (dir == Direction.NORTHEAST) {
right = xIncr;
down = -yIncr;
}
else if (dir == Direction.NORTHWEST) {
right = -xIncr;
down = -yIncr;
}
else if (dir == Direction.SOUTH) {
right = noIncr;
down = Incr;
}
else if (dir == Direction.NORTH) {
right = noIncr;
down = -Incr;
}
else if (dir == Direction.EAST) {
right = Incr;
down = noIncr;
}
else if (dir == Direction.WEST) {
right = -Incr;
down = noIncr;
}
else if (dir == Direction.JUMPNORTH) {
right = noIncr;
down = -Incr;
}
else if (dir == Direction.JUMPSOUTH) {
right = noIncr;
down = Incr;
}
else if (dir == Direction.JUMPEAST) {
right = Incr;
down = noIncr;
}
else if (dir == Direction.JUMPWEST) {
right = -Incr;
down = noIncr;
}
else if (dir == Direction.FIRENORTH) {
right = noIncr;
down = -Incr;
}
else if (dir == Direction.FIRESOUTH) {
right = noIncr;
down = Incr;
}
else if (dir == Direction.FIREEAST) {
right = Incr;
down = noIncr;
}
else if (dir == Direction.FIREWEST) {
right = -Incr;
down = noIncr;
}
else if (dir == Direction.IDLEEWNS) {
right = noIncr;
down = noIncr;
}
}
public int detectWall() {
// 1-right side, 2-top side, 3-left side, 4-bottom side, 0-not on a side
if (xloc >= frameWidth-imgWidth) {
if (down > 0) {
dir = Direction.SOUTHWEST;
} else if (down == noIncr) {
dir = Direction.WEST;
} else {
dir = Direction.NORTHWEST;
}
return 1;
}
else if (xloc <= buffer) {
if (down > 0) {
dir = Direction.SOUTHEAST;
} else if (down == noIncr) {
dir = Direction.EAST;
} else {
dir = Direction.NORTHEAST;
}
return 3;
}
else if (yloc >= frameHeight-imgHeight) {
if (right > 0) {
dir = Direction.NORTHEAST;
} else if (right == noIncr) {
dir = Direction.NORTH;
} else {
dir = Direction.NORTHWEST;
}
return 4;
}
else if (yloc <= buffer) {
if (right > 0) {
dir = Direction.SOUTHEAST;
} else if (right == noIncr) {
dir = Direction.SOUTH;
} else {
dir = Direction.SOUTHWEST;
}
return 2;
}
else { return 0;} // No wall detected
}
public static void ModelReDir(int wall) {
switch (wall) {
case 0: break;
case 1: if (down == noIncr) {
right = -Incr;
} else {
right = -xIncr;
}
break;
case 2: if (right == noIncr) {
down = Incr;
} else {
down = yIncr;
}
break;
case 3: if (down == noIncr) {
right = Incr;
} else {
right = xIncr;
}
break;
case 4: if (right == noIncr) {
down = -Incr;
} else {
down = -yIncr;
}
break;
}
}
public void UpdateLocation() {
xloc+=right;
yloc+=down;
}
public void updateLocationAndDirection() {
ModelReDir(detectWall());
UpdateLocation();
}
}