@@ -9,8 +9,6 @@ import { selectElement } from "./viewportManager";
99import { circleGraphicsContext , Colors , ZIndexLevels } from "../utils" ;
1010import { RightBar } from "../index" ;
1111
12- export const packetTicker = new Ticker ( ) ;
13-
1412const contextPerPacketType : Record < string , GraphicsContext > = {
1513 IP : circleGraphicsContext ( Colors . Green , 0 , 0 , 5 ) ,
1614 ICMP : circleGraphicsContext ( Colors . Red , 0 , 0 , 5 ) ,
@@ -29,6 +27,16 @@ export class Packet extends Graphics {
2927 sourceId : number ;
3028 destinationId : number ;
3129
30+ static animationPaused = false ;
31+
32+ static pauseAnimation ( ) {
33+ Packet . animationPaused = true ;
34+ }
35+
36+ static unpauseAnimation ( ) {
37+ Packet . animationPaused = false ;
38+ }
39+
3240 constructor (
3341 type : string ,
3442 speed : number ,
@@ -89,39 +97,45 @@ export class Packet extends Graphics {
8997 console . error (
9098 "No se puede animar un paquete a lo largo de un camino vacío" ,
9199 ) ;
100+ this . destroy ( ) ;
92101 return ;
93102 }
94103 console . log ( path ) ;
95104 this . currentPath = path ;
96105 this . currentEdge = this . currentPath . shift ( ) ;
97106 this . currentStart = start ;
98- // TODO: use global ticker, and add "shouldProgress" flag
99- packetTicker . add ( this . updateProgress , this ) ;
107+ // Add packet as a child of the current edge
108+ this . currentEdge . addChild ( this ) ;
109+ this . updatePosition ( ) ;
110+ Ticker . shared . add ( this . animationTick , this ) ;
100111 }
101112
102- updateProgress ( ticker : Ticker ) {
113+ animationTick ( ticker : Ticker ) {
103114 if ( this . progress >= 1 ) {
104115 this . progress = 0 ;
116+ this . removeFromParent ( ) ;
105117 if ( this . currentPath . length == 0 ) {
106- ticker . remove ( this . updateProgress , this ) ;
107- this . removeFromParent ( ) ;
118+ ticker . remove ( this . animationTick , this ) ;
119+ this . destroy ( ) ;
108120 return ;
109121 }
110122 this . currentStart = this . currentEdge . otherEnd ( this . currentStart ) ;
111123 this . currentEdge = this . currentPath . shift ( ) ;
124+ this . currentEdge . addChild ( this ) ;
125+ }
126+ if ( ! Packet . animationPaused ) {
127+ this . progress += ( ticker . deltaMS * this . speed ) / 100000 ;
112128 }
113- this . progress += ( ticker . deltaMS * this . speed ) / 100000 ;
114129
130+ this . updatePosition ( ) ;
131+ }
132+
133+ updatePosition ( ) {
115134 const current = this . currentEdge ;
116135 const start = this . currentStart ;
117136
118- console . log ( "current: " , current ) ;
119- console . log ( "start: " , start ) ;
120-
121137 const startPos = current . nodePosition ( start ) ;
122- console . log ( "startPos: " , startPos ) ;
123138 const endPos = current . nodePosition ( current . otherEnd ( start ) ) ;
124- console . log ( "endPos: " , endPos ) ;
125139 this . setPositionAlongEdge ( startPos , endPos , this . progress ) ;
126140 }
127141
0 commit comments