-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
230 lines (205 loc) · 5.24 KB
/
Model.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
*/
import java.util.ArrayList;
import java.util.Iterator;
public class Model {
//Member variables
Link link;
ArrayList<Sprite> sprites;
//Constructor
Model(){
link = new Link();
sprites = new ArrayList<Sprite>();
sprites.add(link);
}
/*
* Our update funciton is going to take care of any updates that are needed within our world or our sprites
*/
public void update(){
for (int i = 0; i < sprites.size(); i++){
//Collison
for (int j = 0; j < sprites.size(); j++){
if ( i != j){
boolean collide = 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)
else if (sprites.get(i).isBoomerang() && !sprites.get(j).isLink()){
sprites.get(i).Collided();
}
//Collision for pots with anything (besides link)
else if (sprites.get(i).isPot() && !sprites.get(j).isLink()){
Pot n = (Pot)sprites.get(i);
n.Collided();
}
//Collision for Link hitting pot
if (sprites.get(i).isLink() && sprites.get(j).isPot()){
if (link.getDirection() == 1){
Pot n = (Pot)sprites.get(j);
n.moveUp = true;
n.moveLeft = false;
n.moveDown = false;
n.moveRight = false;
}
else if (link.getDirection() == 2){
//Need to move the pot right
Pot n = (Pot)sprites.get(j);
n.moveRight = true;
n.moveLeft = false;
n.moveDown = false;
n.moveUp = false;
}
else if (link.getDirection() == 3){
//need to move the pot down
Pot n = (Pot)sprites.get(j);
n.moveDown = true;
n.moveLeft = false;
n.moveRight = false;
n.moveUp = false;
}
else if (link.getDirection() == 4){
//Need to move the pot left
Pot n = (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
public void addBoomerang(){
Boomerang boom = new Boomerang();
if (link.getDirection() == 1){
boom.xdirection = 0;
boom.ydirection = -1;
boom.x = link.x + (link.w * 1/2);
boom.y = link.y;
}
else if (link.getDirection() == 2){
boom.xdirection = 1;
boom.ydirection = 0;
boom.x = link.x + link.w;
boom.y = link.y + (link.h * 1/2);
}
else if (link.getDirection() == 3){
boom.xdirection = 0;
boom.ydirection = 1;
boom.x = link.x + (link.w * 1/2);
boom.y = link.y + link.h;
}
else if (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
void addPotToScreen(int X, int Y) {
int x = X - X % 50;
int y = Y - Y % 50;
Pot p = new Pot(x, y);
sprites.add(p);
}
//Brick stuff
void addBrickToScreen(int X, int Y) {
int x = X - X % 50;
int y = Y - Y % 50;
Brick n = new Brick(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
*/
boolean collideBrick(int locationx, int locationy) {
if(sprites.size() == 0) {
return false;
}
Iterator<Sprite> it = sprites.iterator();
while (it.hasNext()){
Sprite testing = 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();
return true;
}
}
}
return false;
}
//Marshalling methods
Json Marshal(){
Json ob1 = Json.newObject();
Json tmpList = Json.newList();
ob1.add("brick", tmpList);
ob1.add("pot", tmpList);
for (int i = 0; i < sprites.size(); i++){
tmpList.add(sprites.get(i).Marshal());
}
return ob1;
}
void Unmarshal(Json ob){
sprites = new ArrayList<Sprite>(); //Making a new list for our sprites
sprites.add(link); //Adding link to that list
Json tmplistBricks = ob.get("brick"); //Getting bricks from our file
//Json tmpListPots = ob.get("pot");
for (int i = 0; i < tmplistBricks.size(); i++){
try {
sprites.add(new Brick(tmplistBricks.get(i)));
} catch (Exception e) {
//Do nothing
}
try {
sprites.add(new Pot(tmplistBricks.get(i)));
} catch (Exception e) {
//Do nothing
}
}
}
//Loading the map
public void loadFile(){
Json loadObject = Json.load("brickLocation.json");
Unmarshal(loadObject);
}
//Checking collisions between sprites
static boolean isThereACollision(Sprite l, Sprite b){
if (l.x + l.w < b.x){
return false;
}
if (l.x > b.x + b.w){
return false;
}
if(l.y + l.h < b.y){
return false;
}
if (l.y > b.y + b.h){
return false;
}
return true;
}
}