-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.js
121 lines (100 loc) · 3.38 KB
/
level.js
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
import Utilities from './utilities.js';
import Tile from './tile.js';
import { level_1, level_2 } from './level_1.js';
// 40x40
// 1280 = 32
// 720 = 18
// 576 total
class Level {
constructor(engine) {
this.utilities = new Utilities();
this.engine = engine;
this.x = 0;
this.y = 0;
this.width = 40; // this is of the tiles, we should more this to the tile object
this.height = 40;
this.loading = false;
this.level = 1; // our default (should probably take this from props)
this.load();
}
/**
* Load the map into an array of tiles
*/
load() {
this.loading = true;
console.log('Loading level...' + this.engine.stageProps.level);
this.level = this.engine.stageProps.level; // set our level
if(this.level == 1) {
this.map = level_1;
} else {
this.map = level_2;
}
// create an array of tile objects based on the map
var count = 1;
this.tiles = [];
for (var i = 0; i < this.map.length; i++) {
var tile = new Tile(this.map[i], this.x, this.y, this.width, this.height);
this.tiles.push(tile); // add tile to tiles array
this.x += this.width;
count++;
if(count == 33) { // move to a new row
this.x = 0;
this.y += this.height;
count = 1;
}
}
console.log(this.tiles);
console.log('Loading Complete!');
this.loading = false;
}
/**
* Draw the map to screen
*/
draw(context) {
// if the map changes, load the new map
if(this.engine.stageProps.level != this.level) {
this.load();
}
this.tiles.forEach((tile) => {
context.fillStyle = tile.color;
context.beginPath();
context.rect(tile.x, tile.y, tile.width, tile.height);
context.fill();
context.closePath();
});
}
collide(player) {
//player.grounded = false;
this.tiles.forEach((tile) => {
if(tile.collision) {
var collisionInfo = this.utilities.collide(player, tile);
if (collisionInfo) {
switch(collisionInfo.direction) {
case 't':
player.y += collisionInfo.overlap.oY;
break;
case 'b':
player.y -= collisionInfo.overlap.oY;
break;
case 'l':
player.x += collisionInfo.overlap.oX;
break;
case 'r':
player.x -= collisionInfo.overlap.oX;
break;
}
if (collisionInfo.direction == 'l' || collisionInfo.direction == 'r') {
player.velX = 0;
//player.jumping = false;
} else if (collisionInfo.direction == 'b') {
//player.grounded = true;
//player.jumping = false;
} else if (collisionInfo.direction == 't') {
player.velY = 0;
}
}
}
});
}
}
export default Level;