目次
0 。 仕事の準備[
]
1 はじめに
2 。 リソースの読み込み
3 。 ゲーム世界の創造
4 。 ( wip )グループ
5 。 ( wip )物理学の世界
6 。 ( wip )管理
7 。 ( wip )目標の追加
8 。 ( wip )最終タッチ
このシリーズの記事では、 Phaserゲーミングフレームワークの基本と「良いトーン」について説明します。 このコースでは、フレームワークの主なアイデアと可能性を説明し、 TypeScriptおよびWebpackと組み合わせて適切に使用する方法を示します。
トレーニングの主なコースは公式マニュアルから取られていますが、これは文字通りの翻訳ではなく、 ES5からTypeScriptへの書き換えられた例とプロジェクト構造の変更を伴うマニュアルの適合です。 また、いくつかのトピックをより詳細に明らかにしました。
この記事の執筆時点で、 Phaser v2.6.2およびTypeScript v2.2.1を使用していることを予約する価値があると思います。
このリポジトリには、すべてのレッスンのソースコードがあります 。 タグは、特定の記事の最後にあるプロジェクトの開発の段階を示すことに注意してください。例:
-
part-0
現在の記事の時点でのプロジェクトのステータス -
part-1
記事1の時点で
などなど。
Phaserの概要
Phaserは、 WebGLとCanvasを使用してブラウザゲームを作成するためのオープンソース( MIT )のクロスブラウザHTML5フレームワークです。 他のフレームワークとは異なり、 Phaserは主にモバイルプラットフォームを対象とし、モバイルプラットフォーム向けに最適化されています。
ツール
まず最初に、プロジェクトを使用してリポジトリを自分で複製する必要があります。
git clone https://github.com/SuperPaintman/phaser-typescript-tutorial.git
Node.jsをインストールして、コレクターおよびその他のNPMスクリプトを実行します。
プロジェクト構造
フレームワーク自体に直接移動する前に、将来のアプリケーションの構造を検討してください。
私たちのプロジェクトの基礎として、私はコレクターとしてWebpackを使用するこのPhaser TypeScriptテンプレートを取りました。
その主なファイルを見てみましょう(コメントに注意してください):
webpack.config.js
Webpackコレクターの構成。 環境変数に応じて、 NODE_ENV
は開発用のビルド、または本番用の最適化されたビルドをビルドします。
'use strict'; /** Requires */ const path = require('path'); const webpack = require('webpack'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin'); const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin; const ImageminPlugin = require('imagemin-webpack-plugin').default; const p = require('./package.json'); /** Constants */ const IS_PRODUCTION = process.env.NODE_ENV === 'production'; const assetsPath = path.join(__dirname, 'assets/'); // const stylesPath = path.join(__dirname, 'styles/'); // css // phaser. - , phaser // -, ( ) // . const phaserRoot = path.join(__dirname, 'node_modules/phaser/build/custom/'); // phaser' const phaserPath = path.join(phaserRoot, 'phaser-split.js'); const pixiPath = path.join(phaserRoot, 'pixi.js'); const p2Path = path.join(phaserRoot, 'p2.js'); // , const outputPath = path.join(__dirname, 'dist'); // `index.html` const templatePath = path.join(__dirname, 'templates/index.ejs'); /** Helpers */ /** * , * @param {T[]} array * @param {T} searchElement * * @return {boolean} */ function includes(array, searchElement) { return !!~array.indexOf(searchElement); } /** * `expose-loader`, * window * @param {string} modulePath * @param {string} name] * * @return {Object} */ function exposeRules(modulePath, name) { return { test: (path) => modulePath === path, loader: 'expose-loader', options: name }; } /** * `null` * @param {T[]} array * * @return {T[]} */ function filterNull(array) { return array.filter((item) => item !== null); } /** * `fn`, `isIt` `true`, * `fail`. * * @param {boolean} isIt * @param {function} fn * @param {function} fail * * @return {any} */ function only(isIt, fn, fail) { if (!isIt) { return fail !== undefined ? fail() : null; } return fn(); } /** * `only`. , * `NODE_ENV` === 'production', .. . * @param {function} fn * @param {function} fail * * @return {any} */ const onlyProd = (fn, fail) => only(IS_PRODUCTION, fn, fail); /** * `only`. , * `NODE_ENV` !== 'production', .. . * @param {function} fn * @param {function} fail * * @return {any} */ const onlyDev = (fn, fail) => only(!IS_PRODUCTION, fn, fail); module.exports = { entry: { main: path.join(__dirname, 'src/index.ts') }, output: { path: outputPath, // , // filename: `js/[name]${onlyProd(() => '.[chunkhash]', () => '')}.js`, chunkFilename: `js/[name]${onlyProd(() => '.[chunkhash]', () => '')}.chunk.js`, sourceMapFilename: '[file].map', publicPath: '/' }, devtool: onlyDev(() => 'source-map', () => ''), // sourcemap' . resolve: { extensions: ['.ts', '.js'], alias: { pixi: pixiPath, // 'pixi' NPM phaser: phaserPath, // 'phaser' NPM p2: p2Path, // 'p2' NPM assets: assetsPath, // `assets/` styles: stylesPath // `styles/` } }, plugins: filterNull([ /** DefinePlugin */ // , - // , . new webpack.DefinePlugin({ IS_PRODUCTION: JSON.stringify(IS_PRODUCTION), VERSION: JSON.stringify(p.version) }), /** JavaScript */ // JS onlyProd(() => new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, comments: false })), /** Clean */ // `dist` new CleanWebpackPlugin([outputPath]), /** TypeScript */ new CheckerPlugin(), /** Images */ // svg' onlyProd(() => new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/ })), /** Template */ // `index.html` // `templatePath`, // new HtmlWebpackPlugin({ title: 'Phaser TypeScript boilerplate project', template: templatePath }), /** CSS */ // CSS import' `.css` (- Webpack // CSS JS ). new ExtractTextPlugin({ filename: `css/[name]${onlyProd(() => '.[chunkhash]', () => '')}.css` }), /** Chunks */ // (.. // phaser' , // . // , , // ): // * new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', minChunks: (module) => /node_modules/.test(module.resource) }), // * phaser (p2, PIXI, phaser) new webpack.optimize.CommonsChunkPlugin({ name: 'phaser', minChunks: (module) => includes([p2Path, pixiPath, phaserPath], module.resource) }), // * webpack' new webpack.optimize.CommonsChunkPlugin({ name: 'commons' }) ]), devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 8080, inline: true, watchOptions: { aggregateTimeout: 300, poll: true, ignored: /node_modules/ } }, module: { rules: [ /** Assets */ // asset' { test: (path) => path.indexOf(assetsPath) === 0, loader: 'file-loader', options: { name: `[path][name]${onlyProd(() => '.[sha256:hash]', () => '')}.[ext]` } }, /** CSS */ { test: /\.styl$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: [ 'css-loader', 'stylus-loader' ] }) }, /** JavaScript */ exposeRules(pixiPath, 'PIXI'), // `PIXI` `window` exposeRules(p2Path, 'p2'), // `p2` `window` exposeRules(phaserPath, 'Phaser'), // `Phaser` `window` { test: /\.ts$/, exclude: /node_modules/, loader: 'awesome-typescript-loader' } ] } };
assets/
このディレクトリに、アプリケーションのすべてのリソース(画像、音楽、JSONなど)を追加します。
styles/style.styl
ページのCSSスタイル( スタイラスプリプロセッサを使用)が含まれます。 このシリーズでは、これで十分です。
body margin: 0px
templates/index.ejs
ゲームページのEJSテンプレート( Webpack自体がすべてのスクリプトとスタイルのロードを追加します):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> </body> </html>
tsconfig.json
TypeScriptの構成:
{ "compilerOptions": { "target": "es5", // "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "removeComments": false, "noImplicitAny": false, "pretty": true }, "files": [ // Phaser' , .. // , // . "./node_modules/phaser/typescript/box2d.d.ts", "./node_modules/phaser/typescript/p2.d.ts", "./node_modules/phaser/typescript/phaser.comments.d.ts", "./node_modules/phaser/typescript/pixi.comments.d.ts" ], "include": [ // - glob' "./src/**/*.ts", "./node_modules/@types/**/*.ts" ] }
src/typings.d.ts
このファイルでは、 webpack.DefinePlugin
作成したすべてのグローバル変数を宣言する必要があります。
declare const IS_PRODUCTION: boolean; declare const VERSION: string;
src/index.ts
これはアプリケーションのメインファイルであり、正確な入力になります。
'use strict';
これに基づいて、プラットフォーマーを作成します。
Githubレポ : https : //github.com/SuperPaintman/phaser-typescript-tutorial