-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameModel.java
232 lines (196 loc) · 7.43 KB
/
GameModel.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
import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
import javax.swing.undo.*;
import javax.vecmath.*;
import java.util.Random;
public class GameModel extends Observable {
Rectangle2D.Double worldBounds;
Polygon Terrain;
int Ylowerbound, Xupperbound;
int padLastX, padLastY, terrainLastY;
int padNewX, padNewY, terrainNewY;
int[] XP, YP;
UndoManager undoManager;
Rectangle LandingPad;
public Ship ship;
boolean enableUndo, enableRedo;
int shipMessage = 3;
public GameModel(int fps, int width, int height, int peaks) {
undoManager = new UndoManager();
ship = new Ship(60, width/2, 50);
worldBounds = new Rectangle2D.Double(0, 0 ,width, height);
LandingPad = new Rectangle(330,100,40,10);
// anonymous class to monitor ship updates
Ylowerbound = (int)worldBounds.getMaxY();
Xupperbound = (int)worldBounds.getMaxX();
XP = generateTerrainX(worldBounds);
YP = generateTerrainY(worldBounds);
Terrain = new Polygon(XP, YP, peaks+2);
ship.addObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
//shipHittest();
setChangedAndNotify();
}
});
}
// World
// - - - - - - - - - - -
public final Rectangle2D getWorldBounds() {
return worldBounds;
}
// Edit View
// - - - - - - - - - - -
public void setLastXY(int type, int x, int y, int index){
// Record the position of terrain or landingpad before dragged
if (type == 1){ // pad
padLastX = x;
padLastY = y;
} else { // terrain
terrainLastY = YP[index];
}
}
public void setNewXY(int type, int x, int y, int index){ // Record the final position of terrain or landingpad
if (type == 1){ // pad
padNewX = x;
padNewY = y;
} else { // terrain
terrainNewY = YP[index];
}
}
public void setLandingPadPos(int x, int y, boolean final_){
if(final_){
// final: the dragged LandingPad has reached its final position
System.out.println("Model: set X to " + x);
System.out.println("Model: set Y to " + y);
// create undoable edit
UndoableEdit undoableEdit = new AbstractUndoableEdit() {
// capture variables for closure
final int oldX = padLastX;
final int oldY = padLastY;
final int newX = padNewX;
final int newY = padNewY;
// Method that is called when we must redo the undone action
public void redo() throws CannotRedoException {
super.redo();
LandingPad.setLocation(newX, newY);
System.out.println("Model: redo X to " + x);
System.out.println("Model: redo Y to " + y);
setChangedAndNotify();
}
public void undo() throws CannotUndoException {
super.undo();
LandingPad.setLocation(oldX, oldY);
System.out.println("Model: undo X to " + x);
System.out.println("Model: undo Y to " + y);
setChangedAndNotify();
}
};
// Add this undoable edit to the undo manager
undoManager.addEdit(undoableEdit);
}
LandingPad.setLocation(x, y);
setChangedAndNotify();
}
public Rectangle getLandingPad(){return LandingPad;}
public boolean LandingPadHittest(double x, double y){return LandingPad.contains(x,y); }
public int getLandingPadX() {return (int)LandingPad.getX();}
public int getLandingPadY() {return (int)LandingPad.getY();}
public int getLandingPadW() {return (int)LandingPad.getWidth();}
public int getLandingPadH() {return (int)LandingPad.getHeight();}
public int[] getXP() {return XP;}
public int[] getYP() {return YP;}
public void setYP(int index, int value, boolean final_){
if (final_){ // final: the dragged peak has reached its final position
UndoableEdit undoableEdit = new AbstractUndoableEdit() {
// capture variables for closure
final int oldValue = terrainLastY;
final int newValue = terrainNewY;
// Method that is called when we must redo the undone action
public void redo() throws CannotRedoException {
super.redo();
YP[index] = newValue;
Terrain.reset();
Terrain = new Polygon(XP, YP, 22);
System.out.println("Model: redo value to " + value);
setChangedAndNotify();
}
public void undo() throws CannotUndoException {
super.undo();
YP[index] = oldValue;
Terrain.reset();
Terrain = new Polygon(XP, YP, 22);
System.out.println("Model: undo value to " + value);
setChangedAndNotify();
}
};
// Add this undoable edit to the undo manager
undoManager.addEdit(undoableEdit);
}
YP[index] = value;
Terrain.reset();
Terrain = new Polygon(XP, YP, 22);
setChangedAndNotify();
}
public Polygon getTerrain() {return Terrain;}
public int[] generateTerrainY(Rectangle2D bound){
Random r = new Random();
int[] Y_Points = new int[22];
int Low = Ylowerbound/2;
int High = Ylowerbound;
int Result;
Y_Points[0] = High;
Y_Points[21] = High;
for (int i = 1; i < 21; i++){
Result = r.nextInt(High-Low) + Low;
//System.out.print("Terrain: Y Point Generated at Y = "+Result +"\n");
Y_Points[i] = Result;
}
return Y_Points;
}
public int[] generateTerrainX(Rectangle2D bound){
int[] X_Points = new int[22];
int X_Interval = Xupperbound/19;
X_Points[21] = Xupperbound;
X_Points[20] = Xupperbound;
X_Points[0] = 0;
X_Points[1] = 0;
for (int i = 2; i < 20; i++){
X_Points[i] = (i-1) * X_Interval;
//System.out.print("Terrain: X Point Generated at X = "+X_Points[i] +"\n");
}
return X_Points;
}
// Ship
// - - - - - - - - - - -
public int getMessage(){return shipMessage;}
public void setMessage(int i){shipMessage = i;}
//public Ship ship;
// Observerable
// - - - - - - - - - - -
// helper function to do both
void setChangedAndNotify() {
setChanged();
notifyObservers();
}
// undo and redo methods
// - - - - - - - - - - - - - -
public void undo() {
if (canUndo())
undoManager.undo();
}
public void redo() {
if (canRedo())
undoManager.redo();
}
public boolean canUndo() {
return undoManager.canUndo();
}
public boolean canRedo() {
return undoManager.canRedo();
}
}