Skip to content

Commit 1ef8d72

Browse files
author
arnaud.gregoire
committed
unown trigger server side
1 parent 21a3940 commit 1ef8d72

File tree

5 files changed

+135
-113
lines changed

5 files changed

+135
-113
lines changed

app/public/src/game/components/unown-manager.ts

+104-105
Original file line numberDiff line numberDiff line change
@@ -12,118 +12,117 @@ import Pokemon from "./pokemon"
1212
const SHARDS_PER_ENCOUNTER = 50
1313

1414
export default class UnownManager {
15+
uid: string
16+
scene: GameScene
17+
animationManager: AnimationManager
18+
19+
constructor(
20+
scene: GameScene,
21+
animationManager: AnimationManager,
1522
uid: string
16-
scene: GameScene
17-
animationManager: AnimationManager
18-
19-
constructor(
20-
scene: GameScene,
21-
animationManager: AnimationManager,
22-
uid: string
23-
) {
24-
this.uid = uid
25-
this.scene = scene
26-
this.animationManager = animationManager
27-
}
23+
) {
24+
this.uid = uid
25+
this.scene = scene
26+
this.animationManager = animationManager
27+
}
2828

29-
onNewPickPhase(){
30-
/* with 28 characters to unlock, expected number of encounters to
31-
complete the collection are 28*sum(i=1..28, 1/i) = 110
32-
We aim for 120 games to complete the collection, with an average duration
33-
of 25 stages per game, hence a prob of random unown encounter of
34-
110 / (120*25) ~= 0,037 */
35-
if(Math.random() < 0.037){
36-
setTimeout(() => this.addWanderingUnown(), Math.round((5+15*Math.random())*1000))
37-
}
38-
}
29+
addWanderingUnown() {
30+
const unowns = (Object.keys(PkmFamily) as Pkm[]).filter(
31+
(pkm) => PkmFamily[pkm] === Pkm.UNOWN_A
32+
)
33+
const [startX, endX] = coinflip()
34+
? [-100, +window.innerWidth + 100]
35+
: [+window.innerWidth + 100, -100]
36+
const [startY, endY] = [
37+
100 + Math.round(Math.random() * 400),
38+
100 + Math.round(Math.random() * 400)
39+
]
3940

40-
addWanderingUnown(){
41-
const unowns = (Object.keys(PkmFamily) as Pkm[]).filter(pkm => PkmFamily[pkm] === Pkm.UNOWN_A)
42-
const [startX, endX] = coinflip() ? [-100,+window.innerWidth+100] : [+window.innerWidth+100,-100]
43-
const [startY, endY] = [100+Math.round(Math.random()*400), 100+Math.round(Math.random()*400)]
41+
const unown = new Pokemon(
42+
this.scene,
43+
startX,
44+
startY,
45+
PokemonFactory.createPokemonFromName(pickRandomIn(unowns)),
46+
"unown",
47+
false
48+
)
49+
this.animationManager.animatePokemon(unown, PokemonActionState.IDLE)
4450

45-
const unown = new Pokemon(
46-
this.scene,
47-
startX,
48-
startY,
49-
PokemonFactory.createPokemonFromName(pickRandomIn(unowns)),
50-
"unown",
51-
false
52-
)
53-
this.animationManager.animatePokemon(unown, PokemonActionState.IDLE)
54-
55-
const tween = this.scene.tweens.add({
56-
targets: unown,
57-
x: endX,
58-
y: endY,
59-
ease: "Linear",
60-
duration: 5000,
61-
onComplete: () => {
62-
if (unown) {
63-
unown.destroy()
64-
}
51+
const tween = this.scene.tweens.add({
52+
targets: unown,
53+
x: endX,
54+
y: endY,
55+
ease: "Linear",
56+
duration: 5000,
57+
onComplete: () => {
58+
if (unown) {
59+
unown.destroy()
6560
}
66-
})
61+
}
62+
})
6763

68-
unown.isDisabled = true
69-
unown.sprite.setInteractive()
70-
unown.sprite.on('pointerdown', (pointer) => {
71-
getGameContainer().room.send(Transfer.UNOWN_ENCOUNTER, unown.index)
72-
this.displayShardGain([pointer.x, pointer.y], unown.index)
73-
tween.stop()
74-
unown.destroy()
75-
})
64+
unown.isDisabled = true
65+
unown.sprite.setInteractive()
66+
unown.sprite.on("pointerdown", (pointer) => {
67+
getGameContainer().room.send(Transfer.UNOWN_ENCOUNTER, unown.index)
68+
this.displayShardGain([pointer.x, pointer.y], unown.index)
69+
tween.stop()
70+
unown.destroy()
71+
})
72+
}
73+
74+
displayShardGain(coordinates: number[], index: string) {
75+
const textStyle = {
76+
fontSize: "25px",
77+
fontFamily: "Verdana",
78+
color: "#fff",
79+
align: "center",
80+
strokeThickness: 2,
81+
stroke: "#000"
7682
}
7783

78-
displayShardGain(
79-
coordinates: number[],
80-
index: string
81-
) {
82-
const textStyle = {
83-
fontSize: "25px",
84-
fontFamily: "Verdana",
85-
color: "#fff",
86-
align: "center",
87-
strokeThickness: 2,
88-
stroke: "#000"
89-
}
90-
91-
const image = this.scene.add.existing(
92-
new GameObjects.Image(this.scene, 0, 0, `portrait-${index}`)
93-
.setScale(0.5, 0.5)
94-
.setOrigin(0, 0)
95-
)
96-
const text = this.scene.add.existing(
97-
new GameObjects.Text(this.scene, 25, 0, SHARDS_PER_ENCOUNTER.toString(), textStyle)
84+
const image = this.scene.add.existing(
85+
new GameObjects.Image(this.scene, 0, 0, `portrait-${index}`)
86+
.setScale(0.5, 0.5)
87+
.setOrigin(0, 0)
88+
)
89+
const text = this.scene.add.existing(
90+
new GameObjects.Text(
91+
this.scene,
92+
25,
93+
0,
94+
SHARDS_PER_ENCOUNTER.toString(),
95+
textStyle
9896
)
99-
image.setDepth(9)
100-
text.setDepth(10)
101-
102-
const container = this.scene.add.existing(
103-
new GameObjects.Container(
104-
this.scene,
105-
coordinates[0],
106-
coordinates[1] - 50,
107-
[text, image]
108-
)
97+
)
98+
image.setDepth(9)
99+
text.setDepth(10)
100+
101+
const container = this.scene.add.existing(
102+
new GameObjects.Container(
103+
this.scene,
104+
coordinates[0],
105+
coordinates[1] - 50,
106+
[text, image]
109107
)
110-
111-
this.scene.add.tween({
112-
targets: [container],
113-
ease: "linear",
114-
duration: 1500,
115-
delay: 0,
116-
alpha: {
117-
getStart: () => 1,
118-
getEnd: () => 0
119-
},
120-
y: {
121-
getStart: () => coordinates[1] - 50,
122-
getEnd: () => coordinates[1] - 110
123-
},
124-
onComplete: () => {
125-
container.destroy(true)
126-
}
127-
})
128-
}
129-
}
108+
)
109+
110+
this.scene.add.tween({
111+
targets: [container],
112+
ease: "linear",
113+
duration: 1500,
114+
delay: 0,
115+
alpha: {
116+
getStart: () => 1,
117+
getEnd: () => 0
118+
},
119+
y: {
120+
getStart: () => coordinates[1] - 50,
121+
getEnd: () => coordinates[1] - 110
122+
},
123+
onComplete: () => {
124+
container.destroy(true)
125+
}
126+
})
127+
}
128+
}

app/public/src/game/scenes/game-scene.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ export default class GameScene extends Scene {
160160
"/assets/ui/snowflakes.json",
161161
"/assets/ui/"
162162
)
163-
163+
164164
loadStatusMultiAtlas(this)
165-
165+
166166
this.load.multiatlas("item", "/assets/item/item.json", "/assets/item/")
167167
this.load.multiatlas("lock", "/assets/lock/lock.json", "/assets/lock/")
168168
this.load.multiatlas(
@@ -451,7 +451,11 @@ export default class GameScene extends Scene {
451451
this.animationManager
452452
)
453453
this.weatherManager = new WeatherManager(this)
454-
this.unownManager = new UnownManager(this, this.animationManager, this.uid)
454+
this.unownManager = new UnownManager(
455+
this,
456+
this.animationManager,
457+
this.uid
458+
)
455459
this.music = this.sound.add("sound", {
456460
loop: true
457461
}) as Phaser.Sound.WebAudioSound
@@ -510,7 +514,6 @@ export default class GameScene extends Scene {
510514
this.board?.battleMode()
511515
} else {
512516
this.board?.pickMode()
513-
this.unownManager?.onNewPickPhase()
514517
}
515518
}
516519

@@ -779,8 +782,7 @@ export default class GameScene extends Scene {
779782
// });
780783
// }
781784

782-
783-
export function loadStatusMultiAtlas(scene: Scene){
785+
export function loadStatusMultiAtlas(scene: Scene) {
784786
scene.load.multiatlas(
785787
"status",
786788
"/assets/status/status.json",
@@ -818,4 +820,4 @@ export function loadStatusMultiAtlas(scene: Scene){
818820
"/assets/status/PSYCHIC_SURGE.json",
819821
"/assets/status"
820822
)
821-
}
823+
}

app/public/src/pages/game.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ export default function Game() {
261261
gameContainer.handleDisplayHeal(message)
262262
})
263263

264+
room.onMessage(Transfer.UNOWN_WANDERING, () => {
265+
if (gameContainer.game) {
266+
const g: any = gameContainer.game.scene.getScene("gameScene")
267+
if (g && g.unownManager) {
268+
g.unownManager.addWanderingUnown()
269+
}
270+
}
271+
})
272+
264273
room.state.onChange = (changes) => {
265274
changes.forEach((change) => {
266275
if (change.field == "roundTime") {

app/rooms/commands/game-commands.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,17 @@ export class OnUpdatePhaseCommand extends Command<GameRoom, any> {
10601060
9,
10611061
Math.round(this.state.stageLevel / 2)
10621062
)
1063+
} else {
1064+
if (Math.random() < 0.037) {
1065+
const client = this.room.clients.find(
1066+
(cli) => cli.auth.uid === player.id
1067+
)
1068+
if (client) {
1069+
setTimeout(() => {
1070+
client.send(Transfer.UNOWN_WANDERING)
1071+
}, Math.round((5 + 15 * Math.random()) * 1000))
1072+
}
1073+
}
10631074
}
10641075
if (isPVE && player.getLastBattleResult() == BattleResult.WIN) {
10651076
const items = ItemFactory.createRandomItems()

app/types/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export enum Transfer {
144144
BAN = "BAN",
145145
POKEMON_DAMAGE = "POKEMON_DAMAGE",
146146
POKEMON_HEAL = "POKEMON_HEAL",
147-
UNOWN_ENCOUNTER = "UNOWN_ENCOUNTER"
147+
UNOWN_ENCOUNTER = "UNOWN_ENCOUNTER",
148+
UNOWN_WANDERING = "UNOWN_WANDERING"
148149
}
149150

150151
export enum AttackSprite {

0 commit comments

Comments
 (0)