Commit c2e93616 authored by Master Q's avatar Master Q

添加配置项 + NewEventDispatcher

parent 27d4b17d
This diff is collapsed.
This diff is collapsed.
resource/skybox/nx1.jpg

147 KB | W: | H:

resource/skybox/nx1.jpg

44.3 KB | W: | H:

resource/skybox/nx1.jpg
resource/skybox/nx1.jpg
resource/skybox/nx1.jpg
resource/skybox/nx1.jpg
  • 2-up
  • Swipe
  • Onion skin
resource/skybox/ny1.jpg

233 KB | W: | H:

resource/skybox/ny1.jpg

80.3 KB | W: | H:

resource/skybox/ny1.jpg
resource/skybox/ny1.jpg
resource/skybox/ny1.jpg
resource/skybox/ny1.jpg
  • 2-up
  • Swipe
  • Onion skin
resource/skybox/nz1.jpg

126 KB | W: | H:

resource/skybox/nz1.jpg

37.6 KB | W: | H:

resource/skybox/nz1.jpg
resource/skybox/nz1.jpg
resource/skybox/nz1.jpg
resource/skybox/nz1.jpg
  • 2-up
  • Swipe
  • Onion skin
resource/skybox/px1.jpg

141 KB | W: | H:

resource/skybox/px1.jpg

44.5 KB | W: | H:

resource/skybox/px1.jpg
resource/skybox/px1.jpg
resource/skybox/px1.jpg
resource/skybox/px1.jpg
  • 2-up
  • Swipe
  • Onion skin
resource/skybox/py1.jpg

75.8 KB | W: | H:

resource/skybox/py1.jpg

22.1 KB | W: | H:

resource/skybox/py1.jpg
resource/skybox/py1.jpg
resource/skybox/py1.jpg
resource/skybox/py1.jpg
  • 2-up
  • Swipe
  • Onion skin
resource/skybox/pz1.jpg

144 KB | W: | H:

resource/skybox/pz1.jpg

44.5 KB | W: | H:

resource/skybox/pz1.jpg
resource/skybox/pz1.jpg
resource/skybox/pz1.jpg
resource/skybox/pz1.jpg
  • 2-up
  • Swipe
  • Onion skin
...@@ -11,5 +11,6 @@ fs.writeFileSync(endPath + "output.js", ...@@ -11,5 +11,6 @@ fs.writeFileSync(endPath + "output.js",
// 'import * as FYGE from "fyge-tbmini";\n' + // 'import * as FYGE from "fyge-tbmini";\n' +
// 'import * as FYGE from "fyge";\n' +//以后改成这个 // 'import * as FYGE from "fyge";\n' +//以后改成这个
// 'import * as SvgaParser from "svga-parser";\n' + // 'import * as SvgaParser from "svga-parser";\n' +
'/* eslint-disable */\n' +
fs.readFileSync("./output.js")); fs.readFileSync("./output.js"));
console.log("js生成") console.log("js生成")
...@@ -6,6 +6,7 @@ export const GameConfig = { ...@@ -6,6 +6,7 @@ export const GameConfig = {
helperDebuger: false, helperDebuger: false,
collisionDebuger: false, collisionDebuger: false,
initVehiclePos: new THREE.Vector3(0, 3, 0), initVehiclePos: new THREE.Vector3(0, 3, 0),
maxSpeed: 80,
CameraPosConfig: (function() { CameraPosConfig: (function() {
let posList = [ let posList = [
0, 8, -20, 0, 8, -20,
......
...@@ -13,7 +13,7 @@ import { RES } from "../module/RES"; ...@@ -13,7 +13,7 @@ import { RES } from "../module/RES";
import { GameConfig } from "./GameConfig"; import { GameConfig } from "./GameConfig";
import { Dom } from "./dom"; import { Dom } from "./dom";
import { ResJson } from "../ResJson"; import { ResJson } from "../ResJson";
import { GDispather } from ".."; import { GDispather } from "../message";
function gradTexture(color: [number[], string[]]) { function gradTexture(color: [number[], string[]]) {
var c = document.createElement("canvas"); var c = document.createElement("canvas");
...@@ -324,10 +324,13 @@ export class CarScene extends PerspectiveScene { ...@@ -324,10 +324,13 @@ export class CarScene extends PerspectiveScene {
}, },
styles: { styles: {
backgroundImage: `url(${ResJson.path}UI/left-icon.png)`, backgroundImage: `url(${ResJson.path}UI/left-icon.png)`,
backgroundSize: '100% 100%', backgroundSize: 'contain',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
width: `${70 * remScale}px`, width: `${70 * remScale}px`,
height: `${126 * remScale}px`, height: `${126 * remScale}px`,
marginLeft: '40px' marginLeft: '60px',
padding: '0 20px'
}, },
events: { events: {
touchstart: { touchstart: {
...@@ -349,10 +352,13 @@ export class CarScene extends PerspectiveScene { ...@@ -349,10 +352,13 @@ export class CarScene extends PerspectiveScene {
}, },
styles: { styles: {
backgroundImage: `url(${ResJson.path}UI/right-icon.png)`, backgroundImage: `url(${ResJson.path}UI/right-icon.png)`,
backgroundSize: '100% 100%', backgroundSize: 'contain',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
width: `${70 * remScale}px`, width: `${70 * remScale}px`,
height: `${126 * remScale}px`, height: `${126 * remScale}px`,
marginLeft: '40px' marginLeft: '40px',
padding: '0 20px'
}, },
events: { events: {
touchstart: { touchstart: {
...@@ -386,7 +392,9 @@ export class CarScene extends PerspectiveScene { ...@@ -386,7 +392,9 @@ export class CarScene extends PerspectiveScene {
}, },
styles: { styles: {
backgroundImage: `url(${ResJson.path}UI/brake-icon.png)`, backgroundImage: `url(${ResJson.path}UI/brake-icon.png)`,
backgroundSize: '100% 100%', backgroundSize: 'contain',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
width: `${102 * remScale}px`, width: `${102 * remScale}px`,
height: `${150 * remScale}px`, height: `${150 * remScale}px`,
}, },
...@@ -820,6 +828,9 @@ export class CarScene extends PerspectiveScene { ...@@ -820,6 +828,9 @@ export class CarScene extends PerspectiveScene {
set currDistance(v: number) { set currDistance(v: number) {
this._currDistance = v this._currDistance = v
GDispather.dispatchEvent('distanceUpdateSync', {
distance: v
})
} }
onEnterFrame(e: any) { onEnterFrame(e: any) {
...@@ -872,6 +883,9 @@ export class CarScene extends PerspectiveScene { ...@@ -872,6 +883,9 @@ export class CarScene extends PerspectiveScene {
onGameSubmit(n: number) { onGameSubmit(n: number) {
console.log('当前分数:', Math.abs(n)) console.log('当前分数:', Math.abs(n))
GDispather.dispatchEvent('distanceUpdate', {
distance: Math.abs(n)
})
if (this.UICtn.querySelector('.tips')) { if (this.UICtn.querySelector('.tips')) {
this.UICtn.querySelector('.tips').textContent = '当前提交分数:' + Math.abs(n).toFixed(2) this.UICtn.querySelector('.tips').textContent = '当前提交分数:' + Math.abs(n).toFixed(2)
} }
......
...@@ -4,6 +4,7 @@ import { EventDispatcher } from "../module/EventDispatcher"; ...@@ -4,6 +4,7 @@ import { EventDispatcher } from "../module/EventDispatcher";
import { GLTFLoader } from '../module/loaders/GLTFLoader'; import { GLTFLoader } from '../module/loaders/GLTFLoader';
import { PreloadGroup } from '../module/UI/PreloadGroup/PreloadGroup'; import { PreloadGroup } from '../module/UI/PreloadGroup/PreloadGroup';
import { RES } from '../module/RES'; import { RES } from '../module/RES';
import { GameConfig } from './GameConfig';
const materialDynamic = new THREE.MeshPhongMaterial( { color:0xfca400 } ); const materialDynamic = new THREE.MeshPhongMaterial( { color:0xfca400 } );
const materialStatic = new THREE.MeshPhongMaterial( { color:0x999999 } ); const materialStatic = new THREE.MeshPhongMaterial( { color:0x999999 } );
...@@ -287,7 +288,7 @@ export class Vehicle extends PreloadGroup{ ...@@ -287,7 +288,7 @@ export class Vehicle extends PreloadGroup{
// } // }
// 最大速度 // 最大速度
if (speed < 80) { if (speed < GameConfig.maxSpeed) {
if (isAccelerating) { if (isAccelerating) {
stopFlag = false stopFlag = false
if (speed < -1) { if (speed < -1) {
......
...@@ -2,10 +2,15 @@ import { CarScene } from "./CarScene" ...@@ -2,10 +2,15 @@ import { CarScene } from "./CarScene"
import * as THREE from 'three' import * as THREE from 'three'
import { RES } from "./module/RES" import { RES } from "./module/RES"
import { ResJson } from "./ResJson" import { ResJson } from "./ResJson"
import { EventDispatcher } from "./module/EventDispatcher"
window['THREE'] = THREE window['THREE'] = THREE
export const loadAllGroup = function() {
RES.loadConfig(ResJson)
return RES.loadAllGroup()
}
export * from './message'
export class StageScene { export class StageScene {
renderDom: HTMLCanvasElement renderDom: HTMLCanvasElement
stage: CarScene stage: CarScene
...@@ -33,12 +38,4 @@ export class StageScene { ...@@ -33,12 +38,4 @@ export class StageScene {
destroy() { destroy() {
this.stage.destroy() this.stage.destroy()
} }
} }
\ No newline at end of file
type GDType = {
'orientationChange': {
isPortrait: boolean // 是否是竖屏
}
}
export const GDispather = new EventDispatcher<keyof GDType>()
\ No newline at end of file
import { NewEventDispatcher } from "./module/NewEventDispatcher"
type GDType = {
'orientationChange': {
isPortrait: boolean // 是否是竖屏
},
'distanceUpdate': {
distance: number
},
'resetVehicle': any,
'changeCameraPos': any,
'distanceUpdateSync': {
distance: number
}
}
export const GDispather = new NewEventDispatcher<GDType>()
\ No newline at end of file
// type BaseEventMap = { type BaseEventMap = {
// [eventName: string]: any [eventName: string]: any
// } }
type EventCall<T extends any, S extends any = any, U extends any = any> = (this: S, e?: {
type: T,
data: U
}) => any
// type BaseEvent = { // type BaseEvent = {
// type: string // type: string
...@@ -8,14 +13,99 @@ ...@@ -8,14 +13,99 @@
// type AddRecordArgs2<T extends Record<string, any>, E extends > // type AddRecordArgs2<T extends Record<string, any>, E extends >
// /** /**
// * Event object. * Event object.
// */ */
// export interface Event extends BaseEvent { // export interface Event extends BaseEvent {
// target?: any; // target?: any;
// [attachment: string]: any; // [attachment: string]: any;
// } // }
// export class NewEventDispatcher<E extends BaseEventMap> { export class NewEventDispatcher<E extends BaseEventMap> {
private listeners: {
[key in keyof E]?: {
listener: EventCall<key>,
context: any
}[]
}
constructor() {
this.listeners = Object.create(null)
}
addEventListener<T extends keyof E, S extends any>(type: T, listener: EventCall<T, S, E[T]>, context?: S) {
if (!this.listeners[type]) {
this.listeners[type] = []
}
this.listeners[type]!.push({
listener,
context
})
}
removeEventListener<T extends keyof E, S extends any>(type: T, listener: EventCall<T, S, E[T]>, context?: S) {
if (!this.listeners[type]) {
return
}
const cbs = this.listeners[type]!
let i = cbs.length - 1
while (i >= 0) {
const cb = cbs[i]
if (cb.listener === listener && cb.context === context) {
cbs.splice(i, 1)
}
i --
}
}
removeAllEventListener() {
this.listeners = Object.create(null)
}
onceEventListener<T extends keyof E, S extends any>(type: T, listener: EventCall<T, S, E[T]>, context?: S) {
const once = (e: any) => {
listener.call(context, e)
this.removeEventListener(type, once)
}
this.addEventListener(type, once, this)
}
dispatchEvent<T extends keyof E>(type: T, args: E[T]) {
if (!this.listeners[type]) {
return
}
const array = this.listeners[type].slice(0)
array.forEach(item => {
const {
listener,
context
} = item
listener.call(context, {
type,
data: args
})
})
}
}
// type t1 = {
// 'test1': {
// t: boolean
// },
// 'test2': boolean
// }
// type EnumType = keyof t1
// const tt = new NewEventDispatcher<t1>()
// tt.onceEventListener('test1', function (e) {
// this.a = 2
// e.data = 1
// }, {
// a: 2
// })
// } // tt.dispatchEvent('test1', {
\ No newline at end of file // t: false
// })
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment