Browse Source

add

new-deps
npmrun 9 months ago
parent
commit
1f0a7280f3
  1. BIN
      bun.lockb
  2. 4
      index.html
  3. 14
      package.json
  4. BIN
      public/controller_prompt_bg.png
  5. 29
      public/manifest.json
  6. 45
      src/Game/Assets.ts
  7. 55
      src/Game/Game.ts
  8. 8
      src/Game/Position.ts
  9. 34
      src/Game/Stage.ts
  10. 116
      src/Game/index.ts
  11. 21
      src/Game/type.ts
  12. 199
      src/components/Button.ts
  13. 43
      src/main.ts
  14. 38
      src/stages/_global/page_0global.ts
  15. 161
      src/stages/page_init.ts
  16. 9
      src/stages/welcome/circle.ts
  17. 31
      src/stages/welcome/page_welcome.ts
  18. 29
      src/stages/welcome2/page_welcome2.ts

BIN
bun.lockb

Binary file not shown.

4
index.html

@ -5,8 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Document</title>
<script src="//cdn.bootcdn.net/ajax/libs/eruda/2.3.3/eruda.js"></script> <!-- <script src="//cdn.bootcdn.net/ajax/libs/eruda/2.3.3/eruda.js"></script>
<script>eruda.init();</script> <script>eruda.init();</script> -->
<style> <style>
html,body{ html,body{
padding: 0; padding: 0;

14
package.json

@ -12,13 +12,15 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@pixi/sound": "^4.3.0", "@pixi/sound": "^6.0.1",
"@tweenjs/tween.js": "^18.6.4", "@pixi/ui": "^2.1.2",
"pixi.js": "^6.5.3", "@tweenjs/tween.js": "^25.0.0",
"vite": "^2.9.15", "pixi.js": "^8.2.5",
"vite-tsconfig-paths": "^3.5.0" "vite": "^5.3.5",
"vite-tsconfig-paths": "^4.3.2"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^4.8.3" "@pixi/devtools": "^2.0.1",
"typescript": "^5.5.4"
} }
} }

BIN
public/controller_prompt_bg.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 KiB

29
public/manifest.json

@ -0,0 +1,29 @@
{
"bundles":[
{
"name":"load-screen",
"assets":[
{
"alias":"cbg",
"src":"/controller_prompt_bg.png"
},
{
"alias":"bg",
"src":"/bg.png"
},
{
"alias":"dnf",
"src":"https://www.kkkk1000.com/images/learnPixiJS-AnimatedSprite/dnf.png"
},
{
"alias":"btn-bg",
"src":"/assets/images/button_square_depth_gloss.png"
},
{
"alias":"btn-bg-press",
"src":"/assets/images/button_square_depth_gradient.png"
}
]
}
]
}

45
src/Game/Assets.ts

@ -0,0 +1,45 @@
import { Assets } from "pixi.js";
export async function initAssets() {
await Assets.init({ manifest: "/manifest.json" })
}
const catchMap = new Map()
export async function loadAsset(name: string, cb?: (progress: number) => void) {
const check = () => {
if (catchMap.has(name)) {
const rr = catchMap.get(name)
rr.count++
console.log(2211);
catchMap.set(name, rr)
cb?.(1)
return rr.data
}
}
check()
const res = await Assets.loadBundle(name, (progress: number) => {
cb?.(progress)
})
let r = check()
if (r) return r
catchMap.set(name, {
data: res,
count: 1
})
return res
}
/**
*
*/
export async function unLoadAsset(name: string) {
if (catchMap.has(name)) {
const rr = catchMap.get(name)
rr.count--
if (rr.count == 0) {
await Assets.unloadBundle(name)
catchMap.delete(name)
}
}
}

55
src/Game/Game.ts

@ -1,18 +1,17 @@
import { import {
autoDetectRenderer, autoDetectRenderer,
Container, Container,
AbstractRenderer, Renderer,
Ticker, Ticker,
Loader, Loader,
utils, Graphics,
} from "pixi.js"; } from "pixi.js";
import detectOrient from "./detectOrient"; import detectOrient from "./detectOrient";
import { EDirection } from "@/enmu"; import { EDirection } from "@/enmu";
import TWEEN from "@tweenjs/tween.js"; import { initDevtools } from '@pixi/devtools';
export default class Game { export default class Game {
constructor() { constructor() {
this.init() // this.init()
} }
private static instance: Game = new Game(); private static instance: Game = new Game();
@ -24,34 +23,23 @@ export default class Game {
} }
private _stage: Container; private _stage: Container;
private _tween: typeof TWEEN; private _renderer: Renderer;
private _renderer: AbstractRenderer;
private _ticker: Ticker; private _ticker: Ticker;
private _loader: Loader; private _loader: Loader;
private direction: EDirection = EDirection.Portrait; private direction: EDirection = EDirection.Portrait;
public get renderer(): AbstractRenderer { public get renderer(): Renderer {
return this._renderer; return this._renderer;
} }
public get stage(): Container { public get stage(): Container {
return this._stage; return this._stage;
} }
public get tween(): typeof TWEEN {
if(!this._tween) {
this._tween = TWEEN
}
return this._tween;
}
public get ticker(): Ticker { public get ticker(): Ticker {
return this._ticker; return this._ticker;
} }
public get loader(): Loader { public get loader(): Loader {
return this._loader; return this._loader;
} }
public get utils() {
return utils;
}
public info = { public info = {
width: 0, width: 0,
@ -61,14 +49,26 @@ export default class Game {
private screenInfo() { private screenInfo() {
const curDirection = detectOrient(); const curDirection = detectOrient();
if (curDirection === EDirection.Portrait) { if (curDirection === EDirection.Portrait) {
const width = document.documentElement.clientWidth * 1136 / 640 // const width = document.documentElement.clientWidth * 1136 / 640
const height = document.documentElement.clientWidth // const height = document.documentElement.clientWidth
// return {
// widght: height,
// height: width,
// }
const width = document.documentElement.clientWidth
const height = document.documentElement.clientHeight
return { return {
widght: height, widght: width,
height: width, height: height,
} }
} }
const width = document.documentElement.clientHeight * 1136 / 640 // const width = document.documentElement.clientHeight * 1136 / 640
// const height = document.documentElement.clientHeight
// return {
// widght: width,
// height: height,
// }
const width = document.documentElement.clientWidth
const height = document.documentElement.clientHeight const height = document.documentElement.clientHeight
return { return {
widght: width, widght: width,
@ -140,13 +140,13 @@ export default class Game {
} }
} }
init() { async init() {
const screenInfo = this.screenInfo() const screenInfo = this.screenInfo()
let screenWidght = screenInfo.widght; let screenWidght = screenInfo.widght;
let screenHeight = screenInfo.height; let screenHeight = screenInfo.height;
const stage = new Container(); const stage = new Container();
stage.name = "root"; stage.label = "root";
let renderer = autoDetectRenderer({ let renderer = await autoDetectRenderer({
autoDensity: true, autoDensity: true,
width: screenWidght, width: screenWidght,
height: screenHeight, height: screenHeight,
@ -154,13 +154,12 @@ export default class Game {
backgroundAlpha: 255, backgroundAlpha: 255,
resolution: 2, resolution: 2,
backgroundColor: 0x1d9ce0, backgroundColor: 0x1d9ce0,
forceCanvas: true,
}); });
this._stage = stage; this._stage = stage;
this._renderer = renderer; this._renderer = renderer;
// https://www.cnblogs.com/haqiao/p/12515775.html // https://www.cnblogs.com/haqiao/p/12515775.html
this.renderer.resize(screenWidght, screenHeight) this.renderer.resize(screenWidght, screenHeight)
document.body.appendChild(renderer.view); document.body.appendChild(renderer.canvas);
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
this.updateView(); this.updateView();
@ -173,6 +172,8 @@ export default class Game {
const loader = new Loader(); const loader = new Loader();
this._loader = loader; this._loader = loader;
initDevtools({ stage, renderer });
} }
render() { render() {

8
src/Game/Position.ts

@ -1,5 +1,5 @@
import { gameManager } from "@/Game"; import { gameManager } from "@/Game";
import { IPointData } from "pixi.js"; import { PointData } from "pixi.js";
class Position { class Position {
static instance: Position = null static instance: Position = null
@ -11,13 +11,13 @@ class Position {
return Position.instance return Position.instance
} }
get(xType: "center", yType: "center", offset: Partial<IPointData>): IPointData { get(xType: "center", yType: "center", offset?: Partial<PointData>): PointData {
let x, y let x, y
if (xType === 'center') { if (xType === 'center') {
x = gameManager.getInfo().width / 2 + (offset.x ?? 0) x = gameManager.getInfo().width / 2 + (offset?.x ?? 0)
} }
if (yType === 'center') { if (yType === 'center') {
y = gameManager.getInfo().height / 2 + (offset.y ?? 0) y = gameManager.getInfo().height / 2 + (offset?.y ?? 0)
} }
return { x, y } return { x, y }
} }

34
src/Game/Stage.ts

@ -1,6 +1,7 @@
import { EP } from "@/enmu"; import { EP } from "@/enmu";
import { Container } from "pixi.js"; import { Container } from "pixi.js";
import Game from "./Game"; import Game from "./Game";
import { windows } from ".";
type TStage = { type TStage = {
type: EP, type: EP,
@ -28,6 +29,12 @@ export default class Stage {
watchStageChange(cb: (stage: TStage, lastStage?:TStage)=>void){ watchStageChange(cb: (stage: TStage, lastStage?:TStage)=>void){
this._wscb.push(cb) this._wscb.push(cb)
} }
async runStageChange(stage: TStage, lastStage?: TStage){
for (let i = 0; i < this._wscb.length; i++) {
const cb = this._wscb[i];
await cb(stage, lastStage)
}
}
initStage(name: string){ initStage(name: string){
if(!this._stages[name]){ if(!this._stages[name]){
@ -44,7 +51,7 @@ export default class Stage {
}else if(curStage&&curStage.type === EP.Resident){ }else if(curStage&&curStage.type === EP.Resident){
curStage.stage.visible = true curStage.stage.visible = true
} }
this._wscb.forEach(v=>v(curStage)) this.runStageChange(curStage)
} }
changeStage(name: string, opts?: {isHolderLast?: boolean}){ changeStage(name: string, opts?: {isHolderLast?: boolean}){
@ -61,7 +68,12 @@ export default class Stage {
lastStage.stage.visible = false lastStage.stage.visible = false
// @ts-ignore // @ts-ignore
lastStage.stage.$isHolderLast = opts.isHolderLast lastStage.stage.$isHolderLast = opts.isHolderLast
} else this.gameManager.stage.removeChild(lastStage.stage) } else {
lastStage.stage.destroy({
children: true
})
this.gameManager.stage.removeChild(lastStage.stage)
}
}else if(lastStage&&lastStage.type === EP.Resident){ }else if(lastStage&&lastStage.type === EP.Resident){
lastStage.stage.visible = false lastStage.stage.visible = false
} }
@ -82,7 +94,7 @@ export default class Stage {
}else if(curStage&&curStage.type === EP.Resident){ }else if(curStage&&curStage.type === EP.Resident){
curStage.stage.visible = true curStage.stage.visible = true
} }
this._wscb.forEach(v=>v(curStage, lastStage)) this.runStageChange(curStage, lastStage)
if(lastStage&&lastStage.type === EP.Normal && !opts?.isHolderLast){ if(lastStage&&lastStage.type === EP.Normal && !opts?.isHolderLast){
lastStage.stage.destroy() lastStage.stage.destroy()
@ -90,7 +102,17 @@ export default class Stage {
} }
} }
getStage(name: string, type: EP = EP.Normal): Container{ getStage(name: string): Container{
return this._stages[name].stage
}
checkStageMustExist(name: string, type: EP = EP.Normal){
if(!this._stages[name]){
this.create(name, type)
}
}
getInitStage(name: string, type: EP = EP.Normal): Container{
if(name === "root"){ if(name === "root"){
throw new Error("最好换过一个名字") throw new Error("最好换过一个名字")
} }
@ -105,14 +127,14 @@ export default class Stage {
private create(name: string, type: EP = EP.Normal): Container{ private create(name: string, type: EP = EP.Normal): Container{
const stage = new Container(); const stage = new Container();
stage.name = name stage.label = name
this._stages[name] = { this._stages[name] = {
type: type, type: type,
stage: stage, stage: stage,
name: name name: name
} }
if(type === EP.Resident){ if(type === EP.Resident){
stage.visible = false stage.visible = true
this.gameManager.stage.addChild(stage) this.gameManager.stage.addChild(stage)
} }
return stage return stage

116
src/Game/index.ts

@ -1,6 +1,6 @@
import Game from "./Game"; import Game from "./Game";
import Stage from "./Stage"; import Stage from "./Stage";
import { Container, LoaderResource, Loader } from "pixi.js"; import { Container, Ticker } from "pixi.js";
import * as PIXI from "pixi.js"; import * as PIXI from "pixi.js";
import { EP } from "@/enmu"; import { EP } from "@/enmu";
@ -10,10 +10,6 @@ const stageManager = Stage.getInstance();
const windows: Record<string, IWindow> = {} const windows: Record<string, IWindow> = {}
const residentList: IWindow[] = [] const residentList: IWindow[] = []
function defineLoadResource(resources: { name: string, url: string, callback?: LoaderResource.OnCompleteSignal }[]) {
return resources
}
function defineWindow(name: string, abstractClass: Constructor<IWindow>, type?: EP): any function defineWindow(name: string, abstractClass: Constructor<IWindow>, type?: EP): any
function defineWindow(abstractClass: Constructor<IWindow>, type?: EP): any function defineWindow(abstractClass: Constructor<IWindow>, type?: EP): any
function defineWindow(name: string | Constructor<IWindow>, abstractClass?: Constructor<IWindow> | EP, type?: EP) { function defineWindow(name: string | Constructor<IWindow>, abstractClass?: Constructor<IWindow> | EP, type?: EP) {
@ -23,71 +19,132 @@ function defineWindow(name: string | Constructor<IWindow>, abstractClass?: Const
return { abstractClass: name, type: abstractClass } return { abstractClass: name, type: abstractClass }
} }
const t = import.meta.globEager("../stages/**/page_*.ts"); function initStage() {
const t = import.meta.glob("../stages/**/page_*.ts", { eager: true });
for (const tKey in t) { for (const tKey in t) {
const mod = t[tKey].default || t[tKey] const mod = (t[tKey] as any).default || t[tKey]
const [_, tKeyName] = tKey.match(/page_(.*?)\.ts$/) const [_, tKeyName] = tKey.match(/page_(.*?)\.ts$/)
const { name, abstractClass, type } = mod as ReturnType<typeof defineWindow> const { name, abstractClass, type } = mod as ReturnType<typeof defineWindow>
const stage = stageManager.getStage(name || tKeyName, type); // stageManager.getInitStage(name || tKeyName, type);
const stageClass = new abstractClass(stage) const stageClass = new abstractClass()
stageClass.bindStage(stage) if ((name || tKeyName) == "init") {
setTimeout(() => {
// 延迟,等其他逻辑完成之后再执行
stageManager.checkStageMustExist(name || tKeyName, type)
stageManager.initStage(name || tKeyName)
}, 0);
}
stageClass.changeStage = (name: string, opts?: { isHolderLast?: boolean }) => {
stageManager.checkStageMustExist(name, type)
stageManager.changeStage(name, opts)
}
stageClass._type = type
stageClass._initLayout = false stageClass._initLayout = false
stageClass.$name = name || tKeyName stageClass.$name = name || tKeyName
if (EP.Resident === type) { if (EP.Resident === type) {
stageClass.stage = stageManager.getInitStage(name || tKeyName, type)
residentList.push(stageClass) residentList.push(stageClass)
} else windows[name || tKeyName] = stageClass } else windows[name || tKeyName] = stageClass
} }
// @ts-ignore // @ts-ignore
residentList.sort((a, b) => a.$name.localeCompare(b.$name, 'en')) residentList.sort((a, b) => a.$name.localeCompare(b.$name, 'en'))
}
type Constructor<T> = new (...args: any[]) => T; type Constructor<T> = new (...args: any[]) => T;
interface IWindow { export interface IWindow {
_type?: EP;
_initLayout?: Boolean; _initLayout?: Boolean;
_initAssets?: Boolean;
stage: Container; stage: Container;
loader?: Loader; components: Record<string, any>;
bindStage?(stage: Container): void; changeStage?(name: string, opts?: { isHolderLast?: boolean }): void;
loadBundle?(): void;
unLoadBundle?(): void;
layout?(): void; layout?(): void;
onLoad?(): void; onLoad?(): void;
onUnLoad?(): void; onUnLoad?(): void;
lateUpdate?(dt: number, name: string): void; lateUpdate?(dt: number, name: string, ticker: Ticker): void;
update?(dt: number, name: string): void; update?(dt: number, name: string, ticker: Ticker): void;
}
export function GG(name: string){
console.log(windows);
console.log(residentList);
const win = windows[name]
return win
} }
function initGame() { function initGame() {
initStage()
// 场景切换监听 // 场景切换监听
stageManager.watchStageChange((stage, lastStage) => { stageManager.watchStageChange(async (stage, lastStage) => {
if (stage && stage.type === EP.Normal) {
let curWindow = windows[stage.name];
if (lastStage && lastStage.type === EP.Normal) { if (lastStage && lastStage.type === EP.Normal) {
let lastWindow = windows[lastStage.name]; let lastWindow = windows[lastStage.name];
lastWindow.onUnLoad && lastWindow.onUnLoad(); // @ts-ignore
if (lastWindow._initAssets && lastWindow.stage && !lastWindow.stage.$isHolderLast) {
await lastWindow.unLoadBundle?.()
lastWindow._initAssets = false
}
lastWindow.onUnLoad && await lastWindow.onUnLoad();
lastWindow.stage = stageManager.getStage(lastStage.name)
} }
if (stage && stage.type === EP.Normal) {
let curWindow = windows[stage.name];
const isSameStage = curWindow.stage === stage.stage const isSameStage = curWindow.stage === stage.stage
!isSameStage && curWindow.bindStage(stage.stage) !isSameStage && (curWindow.stage = stage.stage)
if (!curWindow._initAssets) {
await curWindow.loadBundle?.()
curWindow._initAssets = true
}
if (!curWindow._initLayout || !isSameStage) { if (!curWindow._initLayout || !isSameStage) {
// console.log("不同的舞台"); // console.log("不同的舞台");
curWindow.layout?.() await curWindow.layout?.()
curWindow._initLayout = true curWindow._initLayout = true
} }
curWindow.onLoad && curWindow.onLoad(); curWindow.onLoad && await curWindow.onLoad();
console.log(curWindow);
console.log(windows);
console.log(stageManager);
} }
}); });
residentList.forEach((v) => v.onLoad && v.onLoad()); (async () => {
gameManager.ticker.add((dt: number) => { for (let i = 0; i < residentList.length; i++) {
const v = residentList[i];
if (!v._initAssets) {
await v.loadBundle?.()
v._initAssets = true
}
if (!v._initLayout) {
await v.layout?.()
v._initLayout = true
}
v.onLoad && await v.onLoad()
}
})()
gameManager.ticker.add(async (ticker: Ticker) => {
const dt = ticker.deltaTime
if (!stageManager.curStage) return; if (!stageManager.curStage) return;
let curWindow = windows[stageManager.curStage.name]; let curWindow = windows[stageManager.curStage.name];
gameManager.tween.update(); if (!curWindow.stage) return;
residentList.forEach((v) => v.update && v.update(dt, v.stage.name)); for (let i = 0; i < residentList.length; i++) {
const v = residentList[i];
v.update && await v.update(dt, v.stage.label, ticker)
}
stageManager.curStage && stageManager.curStage &&
curWindow && curWindow &&
curWindow.update && curWindow.update &&
curWindow.update(dt, curWindow.stage.name); await curWindow.update(dt, curWindow.stage.label, ticker);
gameManager.render(); gameManager.render();
stageManager.curStage && stageManager.curStage &&
curWindow && curWindow &&
curWindow.lateUpdate && curWindow.lateUpdate &&
curWindow.lateUpdate(dt, curWindow.stage.name); await curWindow.lateUpdate(dt, curWindow.stage.label, ticker);
residentList.forEach((v) => v.lateUpdate && v.lateUpdate(dt, v.stage.name)); for (let i = 0; i < residentList.length; i++) {
const v = residentList[i];
v.update && await v.lateUpdate && v.lateUpdate(dt, v.stage.label, ticker)
}
}); });
} }
@ -95,7 +152,6 @@ export {
initGame, initGame,
gameManager, gameManager,
stageManager, stageManager,
defineLoadResource,
defineWindow, defineWindow,
windows, windows,
residentList, residentList,

21
src/Game/type.ts

@ -0,0 +1,21 @@
import { EP } from "@/enmu";
import { IWindow } from ".";
import { Container, Ticker } from "pixi.js";
export abstract class PWindow implements IWindow {
components: Record<string, any>;
_type?: EP;
_initLayout?: Boolean;
_initAssets?: Boolean;
_layoutChild?: void | any[];
stage: Container;
loader?: Loader;
changeStage?(name: string, opts?: {isHolderLast?: boolean}): void;
loadBundle?(): void;
unLoadBundle?(): void;
layout?(): void;
onLoad?(): void;
onUnLoad?(): void;
lateUpdate?(dt: number, name: string, ticker: Ticker): void;
update?(dt: number, name: string, ticker: Ticker): void;
}

199
src/components/Button.ts

@ -1,18 +1,20 @@
import { Text, Graphics, Container, TextStyle, NineSlicePlane, Texture } from "pixi.js"; import { Text, Graphics, Container, TextStyle, NineSlicePlane, Texture, NineSliceSprite, Ticker } from "pixi.js";
interface IOpts { interface IOpts {
text: string; text: string;
bg: string; autoUpdate: Boolean;
pressBg: string; bg: Texture;
pressBg: Texture;
position: null | any; position: null | any;
click(): void; click(): void;
} }
const defaultOpts: IOpts = { const defaultOpts: IOpts = {
autoUpdate: true,
text: "", text: "",
bg: "", bg: null,
position: null, position: null,
pressBg: "", pressBg: null,
click() { }, click() { },
}; };
@ -20,85 +22,138 @@ function getConfig() {
return Object.assign({}, defaultOpts) return Object.assign({}, defaultOpts)
} }
export default function Button(opts: Partial<IOpts>) { export class Button {
let curOpts: IOpts = getConfig(); config: IOpts
_comp: Container
textObj: Text
bgRect: Graphics
bgNine: NineSliceSprite
padding = { x: 60, y: 40 }
getComp(){
return this._comp
}
constructor(opts: Partial<IOpts>) {
this.config = getConfig();
; (Object.keys(defaultOpts) as (keyof IOpts)[]).forEach((key) => { ; (Object.keys(defaultOpts) as (keyof IOpts)[]).forEach((key) => {
if (opts[key] != undefined) { if (opts[key] != undefined) {
curOpts[key] = opts[key] as any; this.config[key] = opts[key] as any;
} }
}); });
let textStyle = new TextStyle({ this._comp = new Container();
fontFamily: "Arial", this._comp.cursor = 'pointer';
fontSize: 30, this._comp.interactive = true;
fill: 0xffffff,
align: "center",
});
let textStr = new Text(curOpts.text, textStyle);
let xPadding = 60;
let yPadding = 40;
let button = new Container();
let width = textStr.width + xPadding;
let height = textStr.height + yPadding;
// https://www.cnblogs.com/ningmin/p/11412170.html
// https://blog.csdn.net/weixin_52919504/article/details/131854745
// https://blog.csdn.net/weixin_52919504/article/details/131854745
button.pivot.set(width / 2, height / 2)
textStr.x = xPadding / 2; if (!this.config.bg) {
textStr.y = yPadding / 2 - 2; this.createRect()
this._comp.addChild(this.bgRect)
if (!curOpts.bg) {
const rect = new Graphics();
rect.name = "rect";
rect.beginFill(0xff0000);
rect.drawRect(0, 0, width, height);
rect.endFill();
rect.interactive = true;
rect.buttonMode = true;
button.addChild(rect);
} else { } else {
const plane9 = new NineSlicePlane(Texture.from(curOpts.bg), 10, 10, 10, 10); this.createNine()
plane9.name = "nine-pic" this._comp.addChild(this.bgNine)
plane9.width = width }
plane9.height = height this.createText()
plane9.interactive = true; this._comp.addChild(this.textObj)
plane9.buttonMode = true; if (this.config.autoUpdate) {
button.addChild(plane9); const update = () => {
} this.updateView()
button.addChild(textStr); }
button.interactive = true; Ticker.shared.add(update)
if(curOpts.position) { this._comp.on("destroyed", ()=>{
button.position = curOpts.position console.log("销毁了");
} Ticker.shared.remove(update, this)
button.on("touchstart", () => { })
if (curOpts.pressBg) { }
button.getChildByName<NineSlicePlane>("nine-pic").texture = Texture.from(curOpts.pressBg) const downFN = () => {
if (this.config.pressBg && this.bgNine) {
this.bgNine.texture = this.config.pressBg
} else { } else {
button.alpha = 0.8; this._comp.alpha = 0.8;
button.scale = { x: .8, y: .8 } this._comp.scale = { x: .8, y: .8 }
} }
}); }
button.on("touchend", () => { this._comp.on("mousedown", downFN);
// 点击成功 this._comp.on("touchstart", downFN);
console.log("touchend success"); const upFN = () => {
if (curOpts.pressBg) { if (this.config.pressBg && this.bgNine) {
button.getChildByName<NineSlicePlane>("nine-pic").texture = Texture.from(curOpts.bg) this.bgNine.texture = this.config.bg
} else { } else {
button.alpha = 1; this._comp.alpha = 1;
button.scale = { x: 1, y: 1 } this._comp.scale = { x: 1, y: 1 }
} }
curOpts.click(); this.config.click();
}); }
button.on("touchendoutside", () => { this._comp.on('mouseup', upFN);
console.log("touchendoutside success"); this._comp.on('touchend', upFN);
if (curOpts.pressBg) { const upOutsideFN = () => {
button.getChildByName<NineSlicePlane>("nine-pic").texture = Texture.from(curOpts.bg) if (this.config.pressBg) {
this.bgNine.texture = this.config.bg
} else { } else {
button.alpha = 1; this._comp.alpha = 1;
button.scale = { x: 1, y: 1 } this._comp.scale = { x: 1, y: 1 }
}
}
this._comp.on("mouseupoutside", upOutsideFN);
this._comp.on("touchendoutside", upOutsideFN);
} }
updateView() {
if(this._comp.destroyed) return
let width = this.textObj.width + this.padding.x;
let height = this.textObj.height + this.padding.y;
this._comp.pivot.set(width / 2, height / 2)
this.textObj.x = this.padding.x / 2;
this.textObj.y = this.padding.y / 2 - 2;
if (this.bgRect) {
this.bgRect.rect(0, 0, width, height);
this.bgRect.fill(0xff0000);
}
if (this.bgNine) {
this.bgNine.width = width
this.bgNine.height = height
}
if (this.config.position) {
this._comp.position = this.config.position()
}
}
createNine() {
this.bgNine = new NineSliceSprite({
texture: this.config.bg,
leftWidth: 10,
topHeight: 10,
rightWidth: 10,
bottomHeight: 10,
}); });
return button; this.bgNine.cursor = 'pointer';
this.bgNine.label = "nine-pic"
this.bgNine.interactive = true;
// plane9.buttonMode = true;
}
createRect() {
this.bgRect = new Graphics();
this.bgRect.label = "rect";
this.bgRect.interactive = true;
this.bgRect.cursor = 'pointer';
// rect.buttonMode = true;
} }
createText() {
let textStyle = new TextStyle({
fontFamily: "Arial",
fontSize: 30,
fill: 0xffffff,
align: "center",
});
this.textObj = new Text({
text: this.config.text,
style: textStyle
});
this.textObj.cursor = 'pointer';
this.textObj.label = "text"
}
}
export default Button

43
src/main.ts

@ -1,21 +1,32 @@
import { Assets } from "pixi.js";
import { EDirection } from "./enmu"; import { EDirection } from "./enmu";
import { gameManager, initGame, stageManager, windows } from "./Game"; import { gameManager, initGame, stageManager } from "./Game";
import { addSound } from "./Game/Sound"; import Game from "./Game/Game";
import { initAssets } from "./Game/Assets";
gameManager.loader // gameManager.loader
// .add("my-sound", "/bg.mp3", (e) => addSound(e)) // 背景音乐 // // .add("my-sound", "/bg.mp3", (e) => addSound(e)) // 背景音乐
.add("bg", "/bg.png") // 背景图片 // .add("bg", "/bg.png") // 背景图片
.add("dnf", "https://www.kkkk1000.com/images/learnPixiJS-AnimatedSprite/dnf.png") // 背景图片 // .add("dnf", "https://www.kkkk1000.com/images/learnPixiJS-AnimatedSprite/dnf.png") // 背景图片
.add("btn-bg", "/assets/images/button_square_depth_gloss.png") // 背景图片 // .add("btn-bg", "/assets/images/button_square_depth_gloss.png") // 背景图片
.add("btn-bg-press", "/assets/images/button_square_depth_gradient.png") // 背景图片 // .add("btn-bg-press", "/assets/images/button_square_depth_gradient.png") // 背景图片
.load(); // .load();
gameManager.loader.onProgress.add((loader) => { // gameManager.loader.onProgress.add((loader) => {
console.log(loader.progress); // console.log(loader.progress);
}); // });
gameManager.loader.onComplete.once((loader) => { // gameManager.loader.onComplete.once((loader) => {
console.log(loader.resources); // console.log(loader.resources);
// initGame();
// gameManager.setDirection(EDirection.Landscape);
// stageManager.initStage("init");
// });
setTimeout(() => {
;(async ()=>{
await initAssets();
await Game.getInstance().init()
initGame(); initGame();
gameManager.setDirection(EDirection.Landscape); gameManager.setDirection(EDirection.Landscape);
stageManager.initStage("init"); })()
}); }, 200);

38
src/stages/_global/page_0global.ts

@ -1,22 +1,42 @@
import { Button } from "@/components/Button";
import { EP } from "@/enmu"; import { EP } from "@/enmu";
import { defineWindow } from "@/Game"; import { defineWindow } from "@/Game";
import { Container } from "pixi.js"; import { loadAsset } from "@/Game/Assets";
import Position from "@/Game/Position";
import { PWindow } from "@/Game/type";
import { Assets, Container } from "pixi.js";
export default defineWindow( export default defineWindow(
class { class extends PWindow {
stage: Container = null; btn: Button;
async loadBundle() {
console.log(this.stage);
console.log("加载资源");
Assets.add({ alias: 'btn-bgaa', src: '/assets/images/button_square_depth_gloss.png' })
Assets.add({ alias: 'btn-bg-pressaa', src: '/assets/images/button_square_depth_gradient.png' })
const aa = await Assets.load('btn-bgaa')
console.log(aa);
constructor(stage: Container) {
this.stage = stage;
} }
bindStage(stage: Container){ async layout() {
this.stage = stage; this.stage.zIndex = 9999
this.btn = new Button({
text: "全局",
bg: await Assets.load('btn-bgaa'),
pressBg: await Assets.load("btn-bg-pressaa"),
position: () => Position.get("center", "center", { y: 100, x: 0 })
})
this.stage.addChild(this.btn._comp)
} }
onLoad() { onLoad() {
console.log("global page loaded"); console.log("global page loaded");
} }
hide() {
// this.btn._comp.visible = false
}
show() {
// this.btn._comp.visible = true
}
onUnLoad() { onUnLoad() {
} }
}, },

161
src/stages/page_init.ts

@ -1,76 +1,120 @@
import Button from "@/components/Button"; import { Button } from "@/components/Button";
import { gameManager, defineWindow, stageManager } from "@/Game"; import { gameManager, defineWindow, stageManager } from "@/Game";
import Position from "@/Game/Position"; import Position from "@/Game/Position";
import Sound from "@/Game/Sound"; import Sound from "@/Game/Sound";
import { Container, Sprite, Text, NineSlicePlane, Texture, AnimatedSprite, Rectangle, utils } from "pixi.js"; import { Container, Sprite, Text, NineSlicePlane, Texture, AnimatedSprite, Rectangle, Assets, AssetsBundle, NineSliceSprite, Ticker } from "pixi.js";
import { PWindow } from "@/Game/type";
// defineLoadResource([ import { loadAsset, unLoadAsset } from "@/Game/Assets";
// { name: "my-sound", url: "/bg.mp3", callback: (e) => addSound(e) }, // import { Button } from '@pixi/ui';
// { name: "bg", url: "/bg.png" },
// ])
export default defineWindow( export default defineWindow(
class { class extends PWindow {
stage: Container = null; stage: Container = null;
title: Text; // 标题 title: Text; // 标题
startBtn: Button; // 标题
pixie: AnimatedSprite; // 标题 pixie: AnimatedSprite; // 标题
bindStage(stage: Container) { assets: any; // 标题
this.stage = stage; async loadBundle() {
const btn = new Button({
text: "加载中",
position: () => Position.get("center", "center"),
});
btn._comp.zIndex = 111111
this.stage.addChild(btn._comp);
this.assets = await loadAsset("load-screen", (progress: number) => {
console.log(progress);
btn.textObj.text = "进度:" + progress
if (progress == 1) {
this.stage.removeChild(btn._comp)
}
})
}
async unLoadBundle() {
// await unLoadAsset("load-screen");
// this.assets = undefined
} }
layout() { layout() {
const bg = new Sprite(Texture.from("bg")); const allChild = []
bg.width = gameManager.getInfo().width // const bg = new Sprite(this.assets["cbg"]);
bg.height = gameManager.getInfo().height // bg.width = gameManager.getInfo().width
this.stage.addChild(bg) // bg.height = gameManager.getInfo().height
this.title = new Text("时间魔法", { // allChild.push(bg)
fontSize: 80, // this.title = new Text("时间魔法", {
fill: "red", // fontSize: 80,
}); // fill: "red",
this.title.x = gameManager.getInfo().width / 2; // });
this.title.y = gameManager.getInfo().height / 2 - 200; // this.title.x = gameManager.getInfo().width / 2;
this.title.position = Position.get("center", "center", { y: -200, x: 0 }) // this.title.y = gameManager.getInfo().height / 2 - 200;
this.title.anchor.set(0.5, 0.5); // this.title.position = Position.get("center", "center", { y: -200, x: 0 })
this.stage.addChild(this.title); // this.title.anchor.set(0.5, 0.5);
// this.stage.addChild(this.title);
const plane9 = new NineSlicePlane(Texture.from("btn-bg"), 10, 10, 10, 10); const plane9 = new NineSliceSprite({
texture: this.assets["btn-bg"],
leftWidth: 10,
topHeight: 10,
rightWidth: 10,
bottomHeight: 10,
});
plane9.width = 100 plane9.width = 100
plane9.height = 50 plane9.height = 50
this.stage.addChild(plane9); allChild.push(plane9)
const btn = Button({ this.startBtn = new Button({
text: "开始游戏", text: "aaaa",
position: Position.get("center", "center", { y: 200, x: 0 }), bg: this.assets["btn-bg"],
bg: "btn-bg", pressBg: this.assets["btn-bg-press"],
pressBg: "btn-bg-press", position: () => Position.get("center", "center", { y: 200, x: 0 }),
click: () => { click: () => {
Sound.play("my-sound") this.changeStage("welcome", { isHolderLast: false })
// new gameManager.tween.Tween(btn.position).interpolation(gameManager.tween.Interpolation.Bezier) // this.pixie.gotoAndPlay(0);
// .to({ alpha: .2, x: btn.width / 2, y: btn.height / 2 }, 1300) // this.startBtn.textObj.text = "1111111111111111111111111111111111111"
// .onComplete(() => { }
// stageManager.changeStage("welcome", { isHolderLast: true }) })
//播放动画精灵 allChild.push(this.startBtn._comp)
this.pixie.gotoAndPlay(0); // console.log(button);
// })
// .start()
},
});
this.stage.addChild(btn);
console.log(this.stage);
let TextureCache = utils.TextureCache; // this.startBtn = Button({
let base = TextureCache["dnf"]; // text: "开始游戏",
// bg: this.assets["btn-bg"],
// pressBg: this.assets["btn-bg-press"],
// click: () => {
// // Sound.play("my-sound")
// // new gameManager.tween.Tween(btn.position).interpolation(gameManager.tween.Interpolation.Bezier)
// // .to({ alpha: .2, x: btn.width / 2, y: btn.height / 2 }, 1300)
// // .onComplete(() => {
// (this.startBtn.getChildByLabel("text") as Text).text = "1111111111111111111111111111111111111111111111111"
// // this.changeStage("welcome", { isHolderLast: false })
// //播放动画精灵
// // this.pixie.gotoAndPlay(0);
// // })
// // .start()
// },
// });
// this.stage.addChild(this.startBtn);
// console.log(this.stage);
let base = this.assets["dnf"];
//第一个纹理 //第一个纹理
let texture0 = new Texture(base as any) let texture0 = new Texture({
texture0.frame = new Rectangle(0, 0, 80, 143); source: base as any,
frame: new Rectangle(0, 0, 80, 143)
})
//第二个纹理 //第二个纹理
let texture1 = new Texture(base as any) let texture1 = new Texture({
texture1.frame = new Rectangle(80, 0, 80, 143); source: base as any,
frame: new Rectangle(80, 0, 80, 143)
})
//第三个纹理 //第三个纹理
let texture2 = new Texture(base as any) let texture2 = new Texture({
texture2.frame = new Rectangle(160, 0, 80, 143); source: base as any,
frame: new Rectangle(160, 0, 80, 143)
})
//第四个纹理 //第四个纹理
let texture3 = new Texture(base as any) let texture3 = new Texture({
texture3.frame = new Rectangle(240, 0, 80, 143); source: base as any,
frame: new Rectangle(240, 0, 80, 143)
})
//创建纹理数组 //创建纹理数组
let textures = [texture0, texture1, texture2, texture3]; let textures = [texture0, texture1, texture2, texture3];
@ -80,8 +124,9 @@ export default defineWindow(
//设置动画精灵的速度 //设置动画精灵的速度
this.pixie.animationSpeed = 0.1; this.pixie.animationSpeed = 0.1;
this.pixie.loop = true; this.pixie.loop = true;
this.stage.addChild(this.pixie)
this.pixie.y = 200 this.pixie.y = 200
allChild.push(this.pixie)
this.stage.addChild(...allChild)
} }
onLoad() { onLoad() {
console.log("onLoad init"); console.log("onLoad init");
@ -92,9 +137,11 @@ export default defineWindow(
console.log("onUnLoad loading"); console.log("onUnLoad loading");
} }
update(dt: number) { update(dt: number, name: string, ticker: Ticker) {
this.pixie.update(dt) if (this.pixie) {
this.title.rotation -= 0.01 * dt; this.pixie.update(ticker)
}
// this.title.rotation -= 0.01 * dt;
} }
}, },

9
src/stages/welcome/circle.ts

@ -4,14 +4,13 @@ import { Graphics} from "pixi.js"
export default new (class Circle{ export default new (class Circle{
render(){ render(){
const circle = new Graphics(); const circle = new Graphics();
circle.name = "circle"; circle.label = "circle";
circle.beginFill(0xfb6a8f); circle.circle(0, 0, 32);
circle.drawCircle(0, 0, 32); circle.fill(0xfb6a8f);
circle.endFill();
circle.x = 130; circle.x = 130;
circle.y = 130; circle.y = 130;
circle.interactive = true; circle.interactive = true;
circle.buttonMode = true; // circle.buttonMode = true;
return circle return circle
} }
}) })

31
src/stages/welcome/page_welcome.ts

@ -4,30 +4,31 @@ import Sound from "@/Game/Sound";
import { Container, Graphics, Text } from "pixi.js"; import { Container, Graphics, Text } from "pixi.js";
import Circle from "./circle" import Circle from "./circle"
import { PWindow } from "@/Game/type";
import Position from "@/Game/Position";
export default defineWindow("welcome", class{ export default defineWindow("welcome", class extends PWindow {
stage: Container = null stage: Container = null
elements: Record<string, Graphics> = {}
text: any text: any
bindStage(stage: Container){
this.stage = stage;
}
layout() { layout() {
const circle = Circle.render() const circle = Circle.render()
this.stage.addChild(circle) this.stage.addChild(circle)
circle.on("touchend", () => { circle.on("touchend", () => {
stageManager.changeStage("welcome2") this.changeStage("welcome2")
})
circle.on("mousedown", () => {
this.changeStage("welcome2")
}) })
this.text = new Text("修仙人生日常") // this.text = new Text({text: "修仙人生日常"})
this.stage.addChild(this.text) // this.stage.addChild(this.text)
const btn = Button({text: "修仙人生日常"}) const btn = new Button({
btn.on("touchend", ()=>{ text: "修仙人生日常", position: () => Position.get("center", "center"), click: () => {
// Sound.play('my-sound'); this.changeStage("init")
stageManager.changeStage("init") }
}) })
this.stage.addChild(btn) this.stage.addChild(btn._comp)
} }
onLoad() { onLoad() {
console.log("onLoad 1"); console.log("onLoad 1");
@ -37,8 +38,8 @@ export default defineWindow("welcome", class{
console.log("onUnLoad 1"); console.log("onUnLoad 1");
} }
update() { update() {
this.text.x = gameManager.getInfo().width-this.text.width // this.text.x = gameManager.getInfo().width-this.text.width
this.text.y = gameManager.getInfo().height-this.text.height // this.text.y = gameManager.getInfo().height-this.text.height
// console.log(22); // console.log(22);
} }
}) })

29
src/stages/welcome2/page_welcome2.ts

@ -1,27 +1,28 @@
import { EP } from "@/enmu"; import { EP } from "@/enmu";
import { stageManager, defineWindow } from "@/Game"; import { stageManager, defineWindow, GG } from "@/Game";
import { Graphics, Container } from "pixi.js"; import { Graphics, Container } from "pixi.js";
import { PWindow } from "@/Game/type";
export default defineWindow("welcome2", class extends PWindow {
layout() {
const circle = new Graphics(); const circle = new Graphics();
circle.name = "circle"; circle.label = "circle";
circle.beginFill(0xfb6a8f); circle.circle(0, 0, 32);
circle.drawCircle(0, 0, 32); circle.fill(0xfb6a8f);
circle.endFill();
circle.x = 130; circle.x = 130;
circle.y = 300; circle.y = 300;
circle.interactive = true; circle.interactive = true;
circle.buttonMode = true; // circle.buttonMode = true;
circle.on("touchend", () => { circle.on("touchend", () => {
circle.x += 10; circle.x += 10;
stageManager.changeStage("welcome") this.changeStage("welcome")
})
circle.on("mousedown", () => {
circle.x += 10;
// GG('0global')
// this.changeStage("welcome")
}) })
export default defineWindow("welcome2", class {
stage: Container = null
bindStage(stage: Container) {
this.stage = stage;
}
layout() {
this.stage.addChild(circle) this.stage.addChild(circle)
} }
onLoad() { onLoad() {

Loading…
Cancel
Save