- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
Latest commit
230 lines (205 loc) · 5.24 KB
/
Copy pathModel.java
File metadata and controls
230 lines (205 loc) · 5.24 KB
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
/*
* Levi Crider
* 2/9/22
* Legend of Zelda
*/
importjava.util.ArrayList;
importjava.util.Iterator;
publicclassModel {
//Member variables
Linklink;
ArrayList<Sprite> sprites;
//Constructor
Model(){
link = newLink();
sprites = newArrayList<Sprite>();
sprites.add(link);
}
/*
* Our update funciton is going to take care of any updates that are needed within our world or our sprites
*/
publicvoidupdate(){
for (inti = 0; i < sprites.size(); i++){
//Collison
for (intj = 0; j < sprites.size(); j++){
if ( i != j){
booleancollide = isThereACollision(sprites.get(i), sprites.get(j));
if (collide){
//Collision detection for Link
if (sprites.get(i).isLink()){
link.getOutOfSprite(sprites.get(j));
}
//Collision for boomerang hitting anything (besides link)
elseif (sprites.get(i).isBoomerang() && !sprites.get(j).isLink()){
sprites.get(i).Collided();
}
//Collision for pots with anything (besides link)
elseif (sprites.get(i).isPot() && !sprites.get(j).isLink()){
Potn = (Pot)sprites.get(i);
n.Collided();
}
//Collision for Link hitting pot
if (sprites.get(i).isLink() && sprites.get(j).isPot()){
if (link.getDirection() == 1){
Potn = (Pot)sprites.get(j);
n.moveUp = true;
n.moveLeft = false;
n.moveDown = false;
n.moveRight = false;
}
elseif (link.getDirection() == 2){
//Need to move the pot right
Potn = (Pot)sprites.get(j);
n.moveRight = true;
n.moveLeft = false;
n.moveDown = false;
n.moveUp = false;
}
elseif (link.getDirection() == 3){
//need to move the pot down
Potn = (Pot)sprites.get(j);
n.moveDown = true;
n.moveLeft = false;
n.moveRight = false;
n.moveUp = false;
}
elseif (link.getDirection() == 4){
//Need to move the pot left
Potn = (Pot)sprites.get(j);
n.moveLeft = true;
n.moveDown = false;
n.moveUp = false;
n.moveRight = false;
}
}
}
}
}
//Update all sprites
sprites.get(i).update();
if (sprites.get(i).update() == false){
sprites.remove(i);
}
}
}
//Boomerang Stuff
publicvoidaddBoomerang(){
Boomerangboom = newBoomerang();
if (link.getDirection() == 1){
boom.xdirection = 0;
boom.ydirection = -1;
boom.x = link.x + (link.w * 1/2);
boom.y = link.y;
}
elseif (link.getDirection() == 2){
boom.xdirection = 1;
boom.ydirection = 0;
boom.x = link.x + link.w;
boom.y = link.y + (link.h * 1/2);
}
elseif (link.getDirection() == 3){
boom.xdirection = 0;
boom.ydirection = 1;
boom.x = link.x + (link.w * 1/2);
boom.y = link.y + link.h;
}
elseif (link.getDirection() == 4){
boom.xdirection = -1;
boom.ydirection = 0;
boom.x = link.x;
boom.y = link.y + (link.h * 1/2);
}
sprites.add(boom);
}
//Pot Stuff
voidaddPotToScreen(intX, intY) {
intx = X - X % 50;
inty = Y - Y % 50;
Potp = newPot(x, y);
sprites.add(p);
}
//Brick stuff
voidaddBrickToScreen(intX, intY) {
intx = X - X % 50;
inty = Y - Y % 50;
Brickn = newBrick(x,y);
//Brick detection/deletion
if (!collideBrick(x,y)) {
sprites.add(n);
}
else {
n = null;
System.out.println("There is already a brick there!");
}
}
/*
Makes sure we aren't placing two bricks in the same location (USING ITERATOR HERE)
Location x and y are the snap to grid locations of the brick we are trying to place
*/
booleancollideBrick(intlocationx, intlocationy) {
if(sprites.size() == 0) {
returnfalse;
}
Iterator<Sprite> it = sprites.iterator();
while (it.hasNext()){
Spritetesting = it.next(); //Testing our new brick vs the existing bricks
if (testing.isBrick()){
if ((locationx >= testing.x && locationx < testing.x + testing.w && locationy >= testing.y && locationy < testing.y + testing.h)) {
it.remove();
returntrue;
}
}
}
returnfalse;
}
//Marshalling methods
JsonMarshal(){
Jsonob1 = Json.newObject();
JsontmpList = Json.newList();
ob1.add("brick", tmpList);
ob1.add("pot", tmpList);
for (inti = 0; i < sprites.size(); i++){
tmpList.add(sprites.get(i).Marshal());
}
returnob1;
}
voidUnmarshal(Jsonob){
sprites = newArrayList<Sprite>(); //Making a new list for our sprites
sprites.add(link); //Adding link to that list
JsontmplistBricks = ob.get("brick"); //Getting bricks from our file
//Json tmpListPots = ob.get("pot");
for (inti = 0; i < tmplistBricks.size(); i++){
try {
sprites.add(newBrick(tmplistBricks.get(i)));
} catch (Exceptione) {
//Do nothing
}
try {
sprites.add(newPot(tmplistBricks.get(i)));
} catch (Exceptione) {
//Do nothing
}
}
}
//Loading the map
publicvoidloadFile(){
JsonloadObject = Json.load("brickLocation.json");
Unmarshal(loadObject);
}
//Checking collisions between sprites
staticbooleanisThereACollision(Spritel, Spriteb){
if (l.x + l.w < b.x){
returnfalse;
}
if (l.x > b.x + b.w){
returnfalse;
}
if(l.y + l.h < b.y){
returnfalse;
}
if (l.y > b.y + b.h){
returnfalse;
}
returntrue;
}
}