-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainModel.js
More file actions
279 lines (253 loc) · 10.2 KB
/
Copy pathmainModel.js
File metadata and controls
279 lines (253 loc) · 10.2 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import BlockModel from './block/blockModel';
import CommandModel from './command/commandModel';
import Level from './level';
const numOfRows = 5;
const numOfColumns = 5;
const width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
const height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
//Stars acquire from the level
//The output area covers the 42% of the window's width and is devided in 25 squares(5*5),
//so the width and the height of each square are:
const sqw = width * 0.42 / numOfColumns; //square width
const sqh = height / numOfRows; //square height
export default class MainModel {
constructor(curentLevel = 1) {
this.levels = [];
this.curentLevel = curentLevel;
this.setLevels();
this.levelTitle = "";
this.levelIstructions = "";
this.levelBlocks = [];
this.levelCommands = [];
this.checkPoints = [];
this.stepsStars = [];
this.stars = 0;
this.startingPoint = [];
this.rocketPosition = {};
this.charactersList = [];
this.animCharacter = [];
this.windowVariables = { width: width, height: height, sqw: sqw, sqh: sqh }
this.observers = [];
this.setCurentLevel(1);//curentLevel);
}
setLevels() {
//set all the levels
//Level 1; the spaceship panda has to move to the planet, it needs only move blocks
let l1 = new Level(
1, //id
'Go to the supermarket planet',//title
'Our friend Panda works in a supermarket, lets help him by giving him instructions to move to his work.', //istructions
[ new BlockModel("Move Down", "move", []), //blocks
new BlockModel("Move Up", "move", []),
new BlockModel("Move Right", "move", []),
new BlockModel("Move Left", "move", [])//,
],
{ //rocketPosition
row: 5,
column: 3,
top: this.setTop(5),
bottom: this.setBottom(5),
left: this.rocketStartPositionLeft(3),
right: this.rocketStartPositionRight(3)
},
[[3,1,null]],//checkPoints,
[4,5], // 3 Stars = 4, 2 Stars = 5, 1 Star = anything else
[{//characterList
name: 'supermarket',
row: 1,
column: 3
}]
)
this.levels = [...this.levels, l1]; //add l1 in levels
//Level 2; the spaceship panda has to move to the planet, we integratea foor loop to introduce a quicker way to code.
let l2 = new Level(
2,
'Go faster to the supermarket planet',//title
'We have to be quicker for work, lets use a loop to repeat the same movement, but with less blocks.',
[ new BlockModel("Move Down", "move", []), //blocks
new BlockModel("Move Up", "move", []),
new BlockModel("Move Right", "move", []),
new BlockModel("Move Left", "move", []),
new BlockModel("Repeat", "loop", ['1', '2', '3', '4']),
new BlockModel("End Repeat", "loop", [])
],
{ //rocketPosition
row: 5,
column: 3,
top: this.setTop(5),
bottom: this.setBottom(5),
left: this.rocketStartPositionLeft(3),
right: this.rocketStartPositionRight(3)
},
[[3,1,null]],//checkPoints,
[3,4], // 3 Stars = 4, 2 Stars = 5, 1 Star = anything else
[{//characterList
name: 'bananaPlanet',
row: 1,
column: 3
}]
)
this.levels = [...this.levels, l2]; //add l2 in levels
//Level 3; the spaceship panda has to get bananas from the banana planet and then move to the position of the monkey, they use move and loop blocks
let l3 = new Level(
3,
'Delever bananas to the monkeys',//title
'Monky friend order some Bananas. The good Panda will deliver them to him, lets go and pick up the bananas at the Banana planet and get back to Monkeys house.',
[ new BlockModel("Move Down", "move", []), //blocks
new BlockModel("Move Up", "move", []),
new BlockModel("Move Right", "move", []),
new BlockModel("Move Left", "move", []),
new BlockModel("Repeat", "loop", ['1', '2', '3', '4']),
new BlockModel("End Repeat", "loop", [])
],
{ //rocketPosition
row: 5,
column: 3,
top: this.setTop(5),
bottom: this.setBottom(5),
left: this.rocketStartPositionLeft(3),
right: this.rocketStartPositionRight(3)
},
[[1,2,null],[3,1,null]],//checkPoints,
[7,8], // 3 Stars = 4, 2 Stars = 5, 1 Star = anything else
[{//characterList
name: 'bananaPlanet',
row: 2,
column: 1
},
{
name: 'monkey',
row: 1,
column: 3
},
]
)
this.levels = [...this.levels, l3]; //add l3 in levels
//Level 4; The panda waits for the animals to come by and using If, the panda will give them the right frut to each animal
//Turtle = Strawberry, Monkey = Banana, Rabbit = Carrot
let l4 = new Level(
4,
'title',//title
'instruction to level4',
[new BlockModel("Move Down", "move", []), //blocks
new BlockModel("Move Up", "move", []),
new BlockModel("Move Right", "move", []),
new BlockModel("Move Left", "move", []),
new BlockModel("If", "if", ['monkey', 'rabbit', 'turtle']),
new BlockModel("End If", "if", []),
new BlockModel("Give Strawberry", "move"),
new BlockModel("Give Carrot", "move"),
new BlockModel("Give Banana", "move")
],
{ //rocketPosition
row: 3,
column: 3,
top: this.setTop(5),
bottom: this.setBottom(5),
left: this.rocketStartPositionLeft(3),
right: this.rocketStartPositionRight(3)
},
[],
[10, 11], // 3 Stars = 10, 2 Stars = 11, 1 Star = anything else
[[3, 3, "banana"], [3, 3, "carrot"], [3, 3, "strawberry"]],//checkPoints,
['supermarket', 2, 3, 'Supermarket_Planet.png', 'horizontal'],//characterList
[["monkey", 6, 3, 3, 3], ["monkey", 3, 3, 0, 3], ["rabbit", 6, 3, 3, 3], ["rabbit", 3, 3, 0, 3], ["turtle", 6, 3, 3, 3], ["turtle", 3, 3, 0, 3]]
)
this.levels = [...this.levels, l4]; //add l4 in levels
//console.log(this.levels);
}
setCurentLevel(l) {
this.curentLevel = l;
const level = this.geLevelById(l);
this.levelBlocks = this.levelCommands = [];
this.notifyObservers();
if (this.curentLevel) {
const lb = level.getBlocks();
// console.log("lb: " + lb)
this.levelBlocks = [...lb];
let emptyArray = [];
this.levelCommands = [new CommandModel(0, 0, "When play", 'start', 150, emptyArray)]; //commands list is empty when the level is initialized
const cp = level.getCheckPoints();
this.checkPoints = [...cp];
const ss = level.getStepsStars();
this.stepsStars = [...ss];
const rp = level.getRocketPosition();
this.rocketPosition = rp;
// this.rocketPosition.bottom = 0;
// console.log("this.rocketPosition.bottom: " + this.rocketPosition.bottom);
const cl = level.getCharacterList();
this.charactersList = [...cl];
this.levelTitle = level.getTitle();
this.levelIstructions = level.getIstructions();
this.notifyObservers();
}
}
geLevelById(id) {
return this.levels.find(element => element.id === id);
}
setCommands(commands) {
this.levelCommands = [...commands]
this.notifyObservers();
}
//function to center the position of the rocket at any screen
rocketStartPositionRight(row) {
//the rocket height is equal the height of square(sqh)
//we know that the width = height/3, so the width of the rocket is:
let rw = sqh / 3;
//find how many squares to the right we have to place the item
let right = this.setRight(row);
//center the item by calculating the difference between its width and the width of the square
//we have to leave equal distance from right and left of the rocket
let distToCenter = (sqw - rw) / 2
right = right + distToCenter;
//console.log("right: " + right);
return right;
}
getStars(){ return this.stars;}
setStars(amount){
console.log("Stars : "+ amount);
this.stars = amount;
this.notifyObservers();
}
rocketStartPositionLeft(row) {
return width - this.rocketStartPositionRight(row);
this.notifyObservers();
}
setRight(row) {
return (numOfRows - row) * sqw;
this.notifyObservers();
}
setLeft(row) {
return width - this.setRight(row);
this.notifyObservers();
}
setBottom(column) {
return (numOfColumns - column) * sqh;
this.notifyObservers();
}
setTop(column) {
return height - this.setBottom(column);
this.notifyObservers();
}
addObserver(callback) {
this.observers = [...this.observers, callback];
}
removeObserver(callback) {
this.observers = this.observers.filter(cb => cb !== callback);
}
notifyObservers() {
if (this.observers != undefined) {
this.observers.forEach(cb => {
try {
cb();
} catch (error) {
console.log(error);
}
})
}
}
}