Skip to content

Commit

Permalink
靜音功能 scene抽出來
Browse files Browse the repository at this point in the history
  • Loading branch information
Parsons committed Jun 15, 2024
1 parent 0241873 commit ca5b62f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 37 deletions.
76 changes: 50 additions & 26 deletions components/PhaserGame.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,40 @@ import chuKoNuAudio from '~/assets/chuKoNu.mp3'
import eightDiagramFormation from '~/assets/eightDiagramFormation.mp3'
import redHare from '~/assets/redHare.mp3'
import shadowrunner from '~/assets/shadowrunner.mp3'
import { Card, Game } from '~/src/classes'
import { Card, Game, BattleScene } from '~/src/classes'
import threeKingdomsCards from '~/assets/cards.json'
import { atkLine } from '~/src/utils/drawing'
import { Client } from '@stomp/stompjs'
// import api from '~/src/utils/api'
import generalCards from '~/assets/generalCards.json'
// import BattleScene from '~/src/classes'
const api = useApi()
const runtimeConfig = useRuntimeConfig()
console.log(runtimeConfig, 'runtimeConfig')
class BattleTable extends Phaser.Scene {
constructor() {
super('BattleTable')
}
preload() {
// 預加載
this.load.audio('peach', peachAudio)
this.load.audio('kill', killAudio)
this.load.audio('dodge', dodgeAudio)
this.load.audio('dismantle', dismantleAudio)
this.load.audio('barbarianInvasion', barbarianInvasionAudio)
this.load.audio('somethingForNothing', somethingForNothingAudio)
this.load.audio('arrowBarrage', arrowBarrageAudio)
this.load.audio('qilinBow', qilinBowAudio)
this.load.audio('chuKoNu', chuKoNuAudio)
this.load.audio('eightDiagramFormation', eightDiagramFormation)
this.load.audio('shadowrunner', shadowrunner)
this.load.audio('redHare', redHare)
}
create() {
myScene.value = this
// myGame.value = new Game(gameData.value, this)
}
}
// class BattleTable extends Phaser.Scene {
// constructor() {
// super('BattleTable')
// }
// preload() {
// // 預加載
// this.load.audio('peach', peachAudio)
// this.load.audio('kill', killAudio)
// this.load.audio('dodge', dodgeAudio)
// this.load.audio('dismantle', dismantleAudio)
// this.load.audio('barbarianInvasion', barbarianInvasionAudio)
// this.load.audio('somethingForNothing', somethingForNothingAudio)
// this.load.audio('arrowBarrage', arrowBarrageAudio)
// this.load.audio('qilinBow', qilinBowAudio)
// this.load.audio('chuKoNu', chuKoNuAudio)
// this.load.audio('eightDiagramFormation', eightDiagramFormation)
// this.load.audio('shadowrunner', shadowrunner)
// this.load.audio('redHare', redHare)
// }
// create() {
// myScene.value = this
// // myGame.value = new Game(gameData.value, this)
// }
// }
const roleText = {
MONARCH: '主公',
REBEL: '反賊',
Expand Down Expand Up @@ -159,6 +160,20 @@ const initDemo = () => {
}
onMounted(() => {
if (process.client) {
const audios = {
peach: peachAudio,
kill: killAudio,
dodge: dodgeAudio,
dismantle: dismantleAudio,
barbarianInvasion: barbarianInvasionAudio,
somethingForNothing: somethingForNothingAudio,
arrowBarrage: arrowBarrageAudio,
qilinBow: qilinBowAudio,
chuKoNu: chuKoNuAudio,
eightDiagramFormation: eightDiagramFormation,
shadowrunner: shadowrunner,
redHare: redHare,
}
const config = {
type: Phaser.AUTO,
parent: 'phaser-game',
Expand All @@ -167,9 +182,18 @@ onMounted(() => {
},
width: 800,
height: 600,
scene: BattleTable,
// scene: BattleScene,
scene: [],
}
game.value = new Phaser.Game(config)
game.value.scene.add('battleScene', BattleScene)
game.value.scene.start('battleScene', { audios })
game.value.events.on('ready', () => {
// console.log('ready')
// console.log(game.value, 'game')
console.log(game.value.scene.scenes[0], 'myScene')
myScene.value = game.value.scene.scenes[0]
})
socketClient = new Client({
// brokerURL: 'ws://localhost:8080/legendsOfTheThreeKingdoms',
brokerURL:
Expand Down
26 changes: 26 additions & 0 deletions src/classes/BattleScene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Audios } from '~/src/types'
export default class BattleScene extends Phaser.Scene {
audioMute: boolean
audios?: Audios
constructor() {
super({ key: 'battleScene' })
// 添加自己的變數
this.audioMute = true
// this.audios = audios
}
init(data: { audios: Audios }) {
// 初始化
this.audios = data.audios
}
preload() {
// 預加載
if (!this.audios) return
// 預加載音樂
Object.entries(this.audios).forEach(([key, value]) => {
this.load.audio(key, value)
})
}
create() {
console.log('BattleScene created', this)
}
}
9 changes: 5 additions & 4 deletions src/classes/Card.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import threeKingdomsCards from '~/assets/cards.json'
import { BattleScene } from './index'
import { suits } from '~/src/utils/domain'
export default class Card {
id: keyof typeof threeKingdomsCards
name: string = ''
instance!: Phaser.GameObjects.Container
scene!: Phaser.Scene
scene!: BattleScene
audio: Phaser.Sound.BaseSound | null = null
x: number = 0
y: number = 0
Expand All @@ -22,7 +23,7 @@ export default class Card {
cardId: keyof typeof threeKingdomsCards
x: number
y: number
scene: Phaser.Scene
scene: BattleScene
playCardHandler?: any
}) {
this.id = cardId
Expand All @@ -40,7 +41,7 @@ export default class Card {
}: {
baseX: number
baseY: number
scene: Phaser.Scene
scene: BattleScene
}) {
// 創建一個白色的矩形
const card = threeKingdomsCards[this.id]
Expand Down Expand Up @@ -106,8 +107,8 @@ export default class Card {
}
playCard({ x = 400, y = 300 }: { x?: number; y?: number } = { x: 400, y: 300 }) {
const instance = this.instance
if (this.audio) this.audio.play()
if (instance === null || this.scene === null) return
if (this.audio && !this.scene.audioMute) this.audio.play()
this.scene.tweens.add({
targets: instance,
x: x,
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Player, MainPlayer, Card } from './index'
import { Player, MainPlayer, Card, BattleScene } from './index'
import { atkLine } from '../utils/drawing'
import type { ThreeKingdomsCardIds, GameData, PlayType, EquipmentPlayType } from '~/src/types'
// import api from '~/src/utils/api'
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class Game {
handCards: [],
}
me!: MainPlayer
scene!: Phaser.Scene
scene!: BattleScene
gameId: string = 'my-id'
gameData: GameData = {
gamePhase: 'Initial',
Expand All @@ -52,7 +52,7 @@ export default class Game {
}
hintInstance!: Phaser.GameObjects.Container
api: any
constructor(gameData: any, scene: Phaser.Scene, api: any) {
constructor(gameData: any, scene: BattleScene, api: any) {
this.scene = scene
this.api = api
this.seats = gameData.seats.map((player: any, index: number) => {
Expand Down
4 changes: 2 additions & 2 deletions src/classes/MainPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, Player } from './index'
import { Card, Player, BattleScene } from './index'
import threeKingdomsCards from '~/assets/cards.json'
import { atkLine } from '../utils/drawing'
import { roleMap } from '~/src/utils/domain'
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class MainPlayer extends Player {
discardCardsAction: ([]) => void
x: number
y: number
scene: Phaser.Scene
scene: BattleScene
seats: any
game: any
}) {
Expand Down
5 changes: 3 additions & 2 deletions src/classes/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import generalCards from '~/assets/generalCards.json'
import threeKingdomsCards from '~/assets/cards.json'
import { roleMap, suits } from '~/src/utils/domain'
import type { ThreeKingdomsCardIds, ThreeKingdomsGeneralIds } from '~/src/types'
import { BattleScene } from './index'
export default class Player {
id: string
generral: string
Expand All @@ -16,7 +17,7 @@ export default class Player {
equipments: ThreeKingdomsCardIds[]
delayScrolls: string[]
instance!: Phaser.GameObjects.Container
scene!: Phaser.Scene
scene!: BattleScene
handleClickPlayer!: (player: Player) => void
x: number = 0
y: number = 0
Expand Down Expand Up @@ -54,7 +55,7 @@ export default class Player {
handleClickPlayer: any
x: number
y: number
scene: Phaser.Scene
scene: BattleScene
}) {
this.id = id
this.generral = generral
Expand Down
1 change: 1 addition & 0 deletions src/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as Game } from './Game'
export { default as Player } from './Player'
export { default as MainPlayer } from './MainPlayer'
export { default as Card } from './Card'
export { default as BattleScene } from './BattleScene'
15 changes: 15 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ export interface GameData {
}
gamePhase: GamePhase
}

export interface Audios {
peach: string
kill: string
dodge: string
dismantle: string
barbarianInvasion: string
somethingForNothing: string
arrowBarrage: string
qilinBow: string
chuKoNu: string
eightDiagramFormation: string
shadowrunner: string
redHare: string
}

0 comments on commit ca5b62f

Please sign in to comment.