-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap001.js
154 lines (133 loc) · 5.17 KB
/
map001.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
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
/**
* This file is part of "MPS Setagaya Pacman."
*
* MPS Setagaya Pacman is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MPS Setagaya Pacman is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
* (c) Junya Kaneko <[email protected]>
*/
/**
* Created by Junya Kaneko on 10/23/15.
* Authors: Junya Kaneko
*/
/**
* マップのコンストラクタ
* 0: 壁
* 1: 通路
*/
var Map = function (startX, startY, tileWidth, tileHeight, wallFillStyle) {
// キャンパス上での描画開始位置
this.startX = startX;
this.startY = startY;
// タイルの大きさ
this.tileSize = {'w': tileWidth, 'h': tileHeight};
this.wallFillStyle= wallFillStyle; // タイルの塗りつぶし方法
// マップ
this.map = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,1,1,0,0,0,0,0,1,1,1,0,0,0],
[0,1,0,1,1,1,1,1,1,1,0,1,1,1,0],
[0,1,1,1,0,1,0,1,0,1,0,1,0,1,0],
[0,1,0,1,0,1,0,1,1,1,1,1,0,1,0],
[0,1,1,1,1,1,1,1,0,0,1,0,1,1,0],
[0,1,0,0,1,0,0,1,0,0,1,0,1,0,0],
[0,1,1,1,1,0,0,1,1,1,1,1,1,1,0],
[0,1,0,0,1,0,0,1,0,1,0,1,0,1,0],
[0,1,1,1,1,1,1,1,0,1,0,1,0,1,0],
[0,0,1,0,0,1,0,1,0,1,0,1,0,1,0],
[0,0,1,0,0,1,0,1,0,1,0,1,0,1,0],
[0,0,1,1,1,1,1,1,1,1,0,1,0,1,0],
[0,0,0,1,0,0,0,0,0,1,1,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];
};
Map.prototype = {
// タイルの行数
getRowLength: function() {
return this.map.length;
},
// タイルの列数
getColLength: function() {
return this.map[0].length;
},
// タイルの幅
getTileWidth : function() {
return this.tileSize.w;
},
// タイルの高さ
getTileHeight : function() {
return this.tileSize.h;
},
// ピクセルで表される座標の点(x, y)が含まれるタイルを返す
getTile : function(x, y) {
var col = Math.floor((x - this.startX) / this.getTileWidth());
var row = Math.floor((y - this.startY) / this.getTileHeight());
return {'row': row, 'col': col, 'kind': this.map[row][col]};
},
// row行col列目のタイルのピクセルで表される座標の左上の点(x, y)を返す
getTileLeftTop : function(row, col) {
return {'left': this.startX + col * this.getTileWidth(), 'top': this.startY + row * this.getTileHeight()};
},
// ピクセル座標 (x, y) が属しているタイルの中心座標を返す
getTileCenter : function(x, y) {
var tile = this.getTile(x, y);
var leftTop = this.getTileLeftTop(tile.row, tile.col);
return {
'x': Math.floor(leftTop.left + this.getTileWidth() / 2),
'y': Math.floor(leftTop.top + this.getTileHeight() / 2)
};
},
// ピクセルで表される座標の点(x, y)が属するタイルの左のタイルが壁であれば true を返す。
isLeftBlockWall : function(x, y) {
var tile = this.getTile(x, y);
return tile.col == 0 || this.map[tile.row][tile.col - 1] == 0;
},
// ピクセルで表される座標の点(x, y)が属するタイルの上のタイルが壁であれば true を返す。
isAboveBlockWall : function(x, y) {
var tile = this.getTile(x, y);
return tile.row == 0 || this.map[tile.row - 1][tile.col] == 0;
},
// ピクセルで表される座標の点(x, y)が属するタイルの右のタイルが壁であれば true を返す。
isRightBlockWall : function(x, y) {
var tile = this.getTile(x, y);
return tile.col + 1 == this.getColLength() || this.map[tile.row][tile.col + 1] == 0;
},
// ピクセルで表される座標の点(x, y)が属するタイルの下のタイルが壁であれば true を返す。
isBelowBlockWall : function(x, y) {
var tile = this.getTile(x, y);
return tile.row + 1 == this.getRowLength || this.map[tile.row + 1][tile.col] == 0;
},
// 壁の描画
drawWall : function(ctx, row, col) {
ctx.fillStyle = this.wallFillStyle;
ctx.rect(this.startX + col * this.getTileWidth(), this.startY + row * this.getTileHeight(),
this.getTileWidth(), this.getTileHeight());
ctx.fill();
},
// クッキーの描画
drawCookie : function(ctx, i, j) {
},
// マップを描画
draw : function(ctx) {
ctx.beginPath();
for(var j = 0; j < this.getRowLength(); j++) {
for( var i = 0; i < this.getColLength(); i++) {
if(this.map[j][i] == 0) {
this.drawWall(ctx, j, i);
} else if (this.map[j][i] == 2 ){
this.drawCookie(ctx, j, i);
}
}
}
}
};