@@ -2,6 +2,7 @@ import Phaser from 'phaser';
22
33import { key } from '../constants' ;
44import { Inventory } from './Player' ;
5+ import { EventBus } from '../EventBus' ;
56
67enum Animation {
78 Left = 'player_left' ,
@@ -31,6 +32,7 @@ export class Agent extends Phaser.Physics.Arcade.Sprite {
3132 private instruction : string = "" ;
3233 private bias : string = "" ;
3334 private isBiased : boolean = false ;
35+ private mssgSprite : Phaser . GameObjects . Image | null = null ;
3436
3537 private wasDragged : boolean = false ; // if user drag the agent now
3638
@@ -40,13 +42,129 @@ export class Agent extends Phaser.Physics.Arcade.Sprite {
4042
4143 public static currentBiasedAgent : Agent | null = null ;
4244
45+
46+ private agentInformation :string = "aaaaaaa" ;
47+
48+ public addMssgSprite ( scene : Phaser . Scene , texture : string , frame ?: string | number ) {
49+ if ( this . mssgSprite ) {
50+ console . log ( "Updating message sprite for agent:" , this . name ) ;
51+ this . mssgSprite . setTexture ( texture , frame ) ;
52+
53+ this . mssgSprite . removeAllListeners ( ) ;
54+ this . mssgSprite . disableInteractive ( ) ;
55+
56+ if ( texture === "agent_mssg" ) {
57+ this . mssgSprite . setInteractive ( { useHandCursor : true } ) ;
58+ this . mssgSprite . on ( 'pointerdown' , ( ) => {
59+ console . log ( `Message sprite of ${ this . name } clicked!` ) ;
60+ this . changeNameTagColor ( '#00ff00' ) ;
61+ EventBus . emit ( "open-agent-information" , {
62+ agent : this . name
63+ } ) ;
64+ } ) ;
65+ }
66+ return ;
67+ }
68+
69+ console . log ( "Adding message sprite to agent:" , this . name ) ;
70+ this . mssgSprite = scene . add . image ( this . x , this . y , texture , frame )
71+ . setOrigin ( 0.5 , 1 )
72+ . setDepth ( 10 ) ;
73+
74+ if ( texture === "agent_mssg" ) {
75+ this . mssgSprite . setInteractive ( { useHandCursor : true } ) ;
76+ this . mssgSprite . on ( 'pointerdown' , ( ) => {
77+ console . log ( `Message sprite of ${ this . name } clicked!` ) ;
78+ this . changeNameTagColor ( '#00ff00' ) ;
79+ EventBus . emit ( "open-agent-information" , {
80+ agent : this . name
81+ } ) ;
82+ } ) ;
83+ }
84+ }
85+
86+
87+
4388
4489 // Add reset method of the calculation of the biased agents
4590 public static resetBiasedAgentsCount ( ) {
4691 Agent . biasedAgentsCount = 0 ;
4792 Agent . currentBiasedAgent = null ;
4893 }
4994
95+ public getAgentInformation ( ) {
96+ return this . agentInformation ;
97+ }
98+
99+ public setAgentInformation ( info : string ) {
100+ this . agentInformation = info ;
101+ EventBus . emit ( "agent-information" , {
102+ agent : this . name ,
103+ mssg : this . getAgentInformation ( )
104+ } ) ;
105+ }
106+
107+ public playDialogue (
108+ scene : Phaser . Scene ,
109+ text : string ,
110+ speed : number = 50 ,
111+ sentencePause : number = 1500
112+ ) {
113+ // 1. 按标点切分成句子数组(支持中英文)
114+ const sentences = text . match ( / [ ^ . ! ? 。 ! ? ] + [ . ! ? 。 ! ? ] / g) || [ text ] ;
115+
116+ // 2. 在 Agent 右上角添加文本
117+ let textObj = scene . add . text ( this . x + 40 , this . y - 40 , "" , {
118+ fontSize : "16px" ,
119+ color : "#ffffff" ,
120+ backgroundColor : "rgba(0,0,0,0.5)" ,
121+ wordWrap : { width : 200 }
122+ } ) . setDepth ( 20 ) ;
123+
124+ let currentSentenceIndex = 0 ;
125+ let charIndex = 0 ;
126+ let isPaused = false ;
127+
128+ scene . time . addEvent ( {
129+ delay : speed ,
130+ loop : true ,
131+ callback : ( ) => {
132+ if ( isPaused ) return ;
133+
134+ const sentence = sentences [ currentSentenceIndex ] ;
135+
136+ if ( charIndex < sentence . length ) {
137+ // 逐字输出
138+ textObj . text += sentence [ charIndex ] ;
139+ charIndex ++ ;
140+ } else {
141+ // 一句话播完 → 停顿 → 清空 → 下一句
142+ isPaused = true ;
143+ scene . time . delayedCall ( sentencePause , ( ) => {
144+ currentSentenceIndex ++ ;
145+ charIndex = 0 ;
146+
147+ if ( currentSentenceIndex < sentences . length ) {
148+ textObj . text = "" ; // ✅ 清空上句
149+ isPaused = false ; // 开始下一句
150+ } else {
151+ console . log ( `✅ Agent ${ this . name } 所有句子播放完毕` ) ;
152+ textObj . destroy ( ) ; // 最后一条播完移除
153+ }
154+ } ) ;
155+ }
156+ }
157+ } ) ;
158+
159+ // 让气泡位置始终跟随 Agent
160+ scene . events . on ( "update" , ( ) => {
161+ if ( textObj && textObj . active ) {
162+ textObj . setPosition ( this . x + 40 , this . y - 40 ) ;
163+ }
164+ } ) ;
165+ }
166+
167+
50168
51169 public assignToWorkplace : boolean = false ;
52170 private activationFunction : ( state : any ) => any = ( state : any ) => {
@@ -139,8 +257,8 @@ export class Agent extends Phaser.Physics.Arcade.Sprite {
139257
140258 }
141259
142- update ( ) {
143- // this.nameTag .setPosition(this.x, this.y - 25 );
260+ update ( ) {
261+ this . mssgSprite ? .setPosition ( this . x - 15 , this . y ) ;
144262 }
145263
146264 public getName ( ) {
@@ -234,6 +352,9 @@ export class Agent extends Phaser.Physics.Arcade.Sprite {
234352 if ( gameObject === this ) {
235353 console . log ( `Agent ${ this . name } clicked!` ) ;
236354
355+ const agentInfo = this . getAgentInformation ( ) ;
356+ console . log ( `Agent Information: ${ agentInfo } ` ) ;
357+
237358 // If there is already another biased agent, restore it first.
238359 if ( Agent . currentBiasedAgent && Agent . currentBiasedAgent !== this ) {
239360 Agent . currentBiasedAgent . setToUnbiased ( ) ;
@@ -284,6 +405,8 @@ export class Agent extends Phaser.Physics.Arcade.Sprite {
284405 }
285406
286407
408+
409+
287410 private createWorkAnimations ( atlasKey : string ) {
288411
289412 // console.log("✅ texture keys:", this.scene.textures.getTextureKeys());
0 commit comments