-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage.ts
147 lines (133 loc) · 9.45 KB
/
stage.ts
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
const {ccclass, property} = cc._decorator;
@ccclass
export default class Stage extends cc.Component { // クラス名をNewClassからStageにする
tiledMap: cc.TiledMap = null; // タイルマップ
tiledMapSize: cc.Size = null; // タイルマップのサイズ
start () {
this.tiledMap = this.getComponent(cc.TiledMap); // stageノードの『TiledMap』コンポーネントを取得
this.tiledMapSize = cc.size( // タイルマップのサイズを計算
this.tiledMap.getTileSize().width * this.tiledMap.getMapSize().width, // タイルの幅 × x方向のタイルの枚数
this.tiledMap.getTileSize().height * this.tiledMap.getMapSize().height); // タイルの高さ × y方向のタイルの枚数
this.addPhisicsNode(); // コライダーを設置する
this.addPrefab(); // プレハブを設置する
}
addPhisicsNode () { // 『Tiled』のオブジェクト情報を元にコライダーを配置
let objects: any[] = this.tiledMap.getObjectGroup("platform").getObjects(); // 『Tiled』のplatformレイヤーのオブジェクトを取得
let layerNode: cc.Node = this.tiledMap.node.getChildByName("platform"); // platformノードを取得
let physicsNode: cc.Node = new cc.Node(); // コライダーを配置するノードを作成
physicsNode.name = "platformCollider"; // ノード名を変更する
let rigidBody: cc.RigidBody = physicsNode.addComponent(cc.RigidBody); // ノードにRigidBody(剛体)コンポーネントを追加
rigidBody.type = cc.RigidBodyType.Static; // 剛体のタイプをスタティック(静的)にする
for (let i = 0; i < objects.length; i ++) { // オブジェクトの数だけループ
let curObject = objects[i]; // オブジェクトを取得
let pos = cc.v2( // オブジェクトの位置をCocos Creatorの座標(中心が原点)に変換
curObject.offset.x + curObject.width / 2 - this.tiledMapSize.width / 2, // マップの幅/2オフセット
this.tiledMapSize.height / 2 - curObject.offset.y - curObject.height / 2); // マップの高さ/2オフセットし、正負逆転
switch (curObject.type) { // タイプ別に処理をする
case 0: // ボックス, ポイント
let boxCollider: cc.PhysicsBoxCollider = physicsNode.addComponent(cc.PhysicsBoxCollider); // 物理ノードにボックスコライダーを追加して取得
boxCollider.name = curObject.name; // 名前を取得
boxCollider.density = 0; // 密度
boxCollider.friction = 0; // 摩擦係数
boxCollider.restitution = 0; // 反発係数
boxCollider.size = cc.size(curObject.width, curObject.height); // ボックスのサイズ
boxCollider.offset = pos; // ボックスの中心位置
break;
case 1: // サークル
let circleCollider: cc.PhysicsCircleCollider = physicsNode.addComponent(cc.PhysicsCircleCollider); // 物理ノードにサークルコライダーを追加して取得
circleCollider.name = curObject.name; // 名前を取得
circleCollider.density = 0; // 密度
circleCollider.friction = 0; // 摩擦係数
circleCollider.restitution = 0; // 反発係数
circleCollider.radius = curObject.width / 2; // 円の半径
circleCollider.offset = pos; // 円の中心位置
break;
case 2: // ポリゴン
let polygonCollider:cc.PhysicsPolygonCollider = physicsNode.addComponent(cc.PhysicsPolygonCollider); // 物理ノードにポリゴンコライダーを追加して取得
polygonCollider.name = curObject.name; // 名前を取得
polygonCollider.density = 0; // 密度
polygonCollider.friction = 0; // 摩擦係数
polygonCollider.restitution = 0; // 反発係数
for (let j = 0; j < curObject.points.length; j ++) { // ポリゴンの頂点の数だけループ
polygonCollider.points[j] = cc.v2(curObject.points[j]); // ポリゴンコライダーに頂点をコピー
}
if (curObject.points.length == 3) { // 三角形の場合
polygonCollider.points[curObject.points.length] = polygonCollider.points[0]; // 最後に始点を入れてポリゴンを閉じる
}
polygonCollider.offset = pos; // ポリゴンの始点
break;
}
}
layerNode.addChild(physicsNode); // 物理ノードをstaticノードの子ノードとして追加
}
@property(cc.Prefab) // Cocos Creatorのエディタに表示する
coinGoldPrefab : cc.Prefab = null; // coinGoldのプレハブを取得する
@property(cc.Prefab) // Cocos Creatorのエディタに表示する
slimeBluePrefab : cc.Prefab = null; // slimeBlueのプレハブを取得する
addPrefab () { // 『Tiled』のオブジェクト情報を元にプレハブを配置
let objects = this.tiledMap.getObjectGroup("prefab").getObjects(); // 『Tiled』のオブジェクトを取得
let layerNode = this.tiledMap.node.getChildByName("prefab"); // タイルマップの子ノードを取得
let prefabArray: { [key: string]: cc.Prefab; } = {}; // 名前をkeyにしてプレハブを連想配列に収める
prefabArray["coinGold"] = this.coinGoldPrefab; // コインのプレハブを配列に追加する
prefabArray["slimeBlue"] = this.slimeBluePrefab; // コインのプレハブを配列に追加する
for (let i = 0; i < objects.length; i ++) { // オブジェクトの数だけループ
let curObject = objects[i]; // オブジェクトを取得
switch (curObject.type) { // タイプ別に処理をする
case 0: // ボックス, ポイント
let prefabNode = cc.instantiate(prefabArray[curObject.name]); // Prefabからノードを作成
prefabNode.scale /= this.node.scale; // ステージのスケールを相殺する
prefabNode.position = cc.v3( // ノードの位置を設定
curObject.offset.x + curObject.width / 2 - this.tiledMapSize.width / 2, // マップの幅/2オフセット
this.tiledMapSize.height / 2 - curObject.offset.y - curObject.height / 2, // マップの高さ/2オフセットし、正負逆転
0); // z座標は0
layerNode.addChild(prefabNode); // 子ノードとして追加
break;
}
}
}
setStage (url: string) {
let self = this;
cc.loader.loadRes(url, (err, res) => {
self.tiledMap = self.node.addComponent(cc.TiledMap);
self.tiledMap.tmxAsset = res;
self.tiledMapSize = cc.size( // タイルマップのサイズを計算
self.tiledMap.getTileSize().width * self.tiledMap.getMapSize().width, // タイルの幅 × x方向のタイルの枚数
self.tiledMap.getTileSize().height * self.tiledMap.getMapSize().height); // タイルの高さ × y方向のタイルの枚数
self.addPhisicsNode();
self.addPrefab(); // プレハブを設置する
});
}
// @property(cc.Prefab)
// coinGoldPrefab : cc.Prefab = null;
// @property(cc.Prefab)
// doorPrefab : cc.Prefab = null;
// @property(cc.Prefab)
// slimeBluePrefab : cc.Prefab = null;
// @property(cc.Prefab)
// brickBrown : cc.Prefab = null;
// @property(cc.Prefab)
// upBridge : cc.Prefab = null;
// addPrefab (tiledMap: cc.TiledMap) {
// let objects = tiledMap.getObjectGroup("items").getObjects(); // 『Tiled』のオブジェクトを取得
// let layerNode = tiledMap.node.getChildByName("items"); // タイルマップの子ノードを取得
// let prefabArray: { [key: string]: cc.Prefab; } = {};
// prefabArray["coin"] = this.coinGoldPrefab;
// prefabArray["door"] = this.doorPrefab;
// prefabArray["slime"] = this.slimeBluePrefab;
// prefabArray["brickBrown"] = this.brickBrown;
// prefabArray["upBridge"] = this.upBridge;
// for (let j = 0; j < objects.length; j++) { // オブジェクトの数だけループ
// let curObject = objects[j]; // オブジェクトを取得
// switch (curObject.type) { // タイプ別に処理をする
// case 0: // ボックス, ポイント
// let prefabNode = cc.instantiate(prefabArray[curObject.name]); // Prefabからノードを作成
// prefabNode.position = cc.v3( // ノードの位置を設定
// curObject.offset.x + curObject.width / 2 - this.tiledMapSize.width / 2, // マップの幅/2オフセット
// this.tiledMapSize.height / 2 - curObject.offset.y - curObject.height / 2, // マップの高さ/2オフセットし、正負逆転
// 0);
// layerNode.addChild(prefabNode); // 子ノードとして追加
// break;
// }
// }
// }
}