-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.java
200 lines (177 loc) · 3.88 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
/**
* 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 int X_SPEED = 6;
final int Y_SPEED = 6;
int frameWidth;
int frameHeight;
int imageWidth;
int imageHeight;
int x = 0;
int y = 0;
Direction direction = Direction.SOUTHWEST;
final int leftBound = 0;
int rightBound;
final int topBound = 0;
int bottomBound;
public Model(int width, int height, int imageWidth, int imageHeight) {
frameWidth = width;
frameHeight = height;
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;
findBounds();
}
public int getX() {
// TODO Auto-generated method stub
return x;
}
public int getY() {
// TODO Auto-generated method stub
return y;
}
public Direction getDirect() {
// TODO Auto-generated method stub
return direction;
}
public void setDirect(Direction d) {
direction = d;
}
public void updateLocationAndDirection() {
int newX = x + xIncriment() * X_SPEED;
int newY = y + yIncriment() * Y_SPEED;
if (newX < leftBound) {
newX = leftBound;
flipDirection(Direction.WEST);
}
if (newX > rightBound){
newX = rightBound;
flipDirection(Direction.EAST);
}
if (newY < topBound){
newY = topBound;
flipDirection(Direction.NORTH);
}
if (newY > bottomBound){
newY = bottomBound;
flipDirection(Direction.SOUTH);
}
x = newX;
y = newY;
}
private void findBounds(){
rightBound = frameWidth - imageWidth;
bottomBound = frameHeight - imageWidth;
}
private int yIncriment(){
switch (direction){
case NORTH:
return -1;
case NORTHEAST:
return -1;
case EAST:
return 0;
case SOUTHEAST:
return 1;
case SOUTH:
return 1;
case SOUTHWEST:
return 1;
case WEST:
return 0;
default: //NORTHWEST
return -1;
}
}
private int xIncriment(){
switch (direction){
case NORTH:
return 0;
case NORTHEAST:
return 1;
case EAST:
return 1;
case SOUTHEAST:
return 1;
case SOUTH:
return 0;
case SOUTHWEST:
return -1;
case WEST:
return -1;
default: //NORTHWEST
return -1;
}
}
// Flips the direction, in the direction of the flip argument provided,
// i.e. if direction == NORTHEAST, flipDirection(EAST) makes
// direction = NORTHWEST
private void flipDirection(Direction flip){
switch (direction){
case NORTH:
direction = Direction.SOUTH;
break;
case NORTHEAST:
if (flip == Direction.EAST) direction = Direction.NORTHWEST;
else direction = Direction.SOUTHEAST;
break;
case EAST:
direction = Direction.WEST;
break;
case SOUTHEAST:
if (flip == Direction.EAST) direction = Direction.SOUTHWEST;
else direction = Direction.NORTHEAST;
break;
case SOUTH:
direction = Direction.NORTH;
break;
case SOUTHWEST:
if (flip == Direction.WEST) direction = Direction.SOUTHEAST;
else direction = Direction.NORTHWEST;
break;
case WEST:
direction = Direction.EAST;
break;
default: //NORTHWEST
if (flip == Direction.WEST) direction = Direction.NORTHEAST;
else direction = Direction.SOUTHWEST;
break;
}
}
// Reverse Current Direction
public void reverseDir() {
switch (direction){
case NORTH:
direction = Direction.SOUTH;
break;
case NORTHEAST:
direction = Direction.SOUTHWEST;
break;
case EAST:
direction = Direction.WEST;
break;
case SOUTHEAST:
direction = Direction.NORTHWEST;
break;
case SOUTH:
direction = Direction.NORTH;
break;
case SOUTHWEST:
direction = Direction.NORTHEAST;
break;
case WEST:
direction = Direction.EAST;
break;
default: //NORTHWEST
direction = Direction.SOUTHEAST;
break;
}
}
}