Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/ts/Resource.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 170 - (show annotations) (download) (as text)
Mon May 4 03:43:41 2015 UTC (9 years ago) by tsugehara
File MIME type: text/texmacs
File size: 4878 byte(s)


1 ///<reference path="all.ts"/>
2 module jg {
3 /**
4 * 画像やサウンドなどのリソースを管理するクラス。
5 * このクラスはjgame.jsでは例外的な単一インスタンスのクラスであるため、複数ゲームを1ページに読み込む場合は注意が必要
6 */
7 export class Resource {
8 /** 読み込み済みの画像 */
9 images: {[key:string]: HTMLImageElement; };
10 /** 読み込み済みのスクリプト */
11 scripts: {[key:string]: HTMLScriptElement; };
12 /** 読み込み済みのサウンド */
13 sounds: {[key:string]: any; };
14 /** 読み込み予定の識別子リスト */
15 requests: string[];
16 /**
17 * 読み込みが完了するたびに発火されるイベント。
18 * パラメータで残りリクエスト数を受け取るため、全読み込み完了はloaded.handle((e) => {if (e == 0) console.log("completed");});などで判定可能 */
19 loaded: Trigger;
20 /**
21 * リソースの読み込みリクエストが追加される度に発火されるイベント。
22 * パラメータとして、{name: リソース識別子, url: リソースのURL, loader: ResourceLoaderクラス}を持つ
23 */
24 added: Trigger;
25 /**
26 * このクラスが管理しているResourceLoader。拡張子ごとのResourceLoaderのマップになっている。
27 * デフォルトでは以下の通り。"default"は登録されていないすべての拡張子が該当する。
28 * "js": ScriptResourceLoader
29 * "default": ImageResourceLoader
30 * "mp3": SoundResourceLoader
31 * "ogg": SoundResourceLoader
32 * "wav": SoundResourceLoader
33 * "mid": SoundResourceLoader
34 */
35 loaders: {[key:string]: ResourceLoader; };
36 /** このリソースの現在の構造 */
37 structure: ResourceStructure;
38
39 /** 単一のインスタンス */
40 static instance: Resource;
41
42 /**
43 * 唯一のインスタンスを取得する。
44 */
45 static getInstance():Resource {
46 if (! Resource.instance)
47 Resource.instance = new Resource();
48 return Resource.instance;
49 }
50
51 /**
52 * 新しいResourceクラスを生成するが、この方法でResourceクラスを生成するべきではなく、jgame.jsではgetInstanceによる単一インスタンスでの利用を推奨している
53 */
54 constructor() {
55 this.requests = [];
56 this.loaded = new Trigger();
57 this.added = new Trigger();
58 this.clear();
59 this.loaders = {};
60 this.loaders["js"] = new ScriptResourceLoader(this);
61 this.loaders["default"] = new ImageResourceLoader(this);
62 this.loaders["mp3"] = new SoundResourceLoader(this);
63 this.loaders["ogg"] = this.loaders["mp3"];
64 this.loaders["wav"] = this.loaders["mp3"];
65 this.loaders["mid"] = this.loaders["mp3"];
66 this.structure = ResourceStructure.Default;
67 }
68
69 /**
70 * すべてのリソースを解放する
71 */
72 clear() {
73 this.images = {};
74 this.scripts = {};
75 this.sounds = {};
76 }
77
78 /**
79 * 画像リソースを取得する
80 */
81 get(name:string) {
82 return this.images[name];
83 }
84
85 /**
86 * サウンドリソースを取得する
87 */
88 sound(name:string) {
89 return this.sounds[name];
90 }
91
92 /**
93 * リクエストの完了を通知する
94 * @param name リクエストが完了したリソースの識別子
95 */
96 requestCompleted(name:string) {
97 for (var i=0; i<this.requests.length; i++) {
98 if (this.requests[i] == name) {
99 this.requests.splice(i, 1);
100 break;
101 }
102 }
103 this.loaded.fire(this.requests.length);
104 }
105
106 /**
107 * リソースを読み込む
108 * @param name リソースの識別子。url省略時は、urlと同じ値になる
109 * @param url リソースのURL
110 */
111 load(name:string, url?:string) {
112 if (! url)
113 url = name;
114
115 this.requests.push(name);
116
117 var dot = url.split(/\./g);
118 var ext;
119 if (dot.length == 0)
120 ext = "";
121 else
122 ext = dot[dot.length - 1];
123
124 ext = ext.toLowerCase();
125 var loader = this.loaders[ext] ? this.loaders[ext] : this.loaders["default"];
126 loader.load(url, name);
127
128 this.added.fire({
129 name: name,
130 url: url,
131 loader: loader
132 });
133 }
134
135 /**
136 * 手動で読み込むを行う。
137 * このメソッド内でResourceクラスは何も処理をせず、ただリクエストを登録する。
138 * ロードが完了したリソースは、別途completeManualを通じてResourceクラスに読み込み完了を手動通知する必要がある。
139 * 主に、外部ライブラリでの読み込み処理での利用を想定している。
140 * @param name リソース識別子
141 */
142 loadManual(name:string) {
143 this.requests.push(name);
144 this.added.fire({
145 name: name,
146 url: null,
147 loader: null
148 });
149 }
150
151 /**
152 * 手動で読み込み完了を行う。通常はloadManualとセットで利用する
153 * @param name リソース識別子
154 */
155 completeManual(name:string) {
156 this.requestCompleted(name);
157 }
158 }
159 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26