11using Intersect . Client . Framework . Entities ;
22using Intersect . Client . Maps ;
3+ using Intersect . Core ;
34using Intersect . Enums ;
45using Intersect . Utilities ;
6+ using Microsoft . Extensions . Logging ;
57
68namespace Intersect . Client . Entities ;
79
810public partial class Dash : IDash
911{
1012 private readonly Direction mChangeDirection = Direction . None ;
1113
12- private readonly int mDashTime ;
14+ private readonly long _serverDashEndMilliseconds ;
15+
16+ private readonly long _dashLengthMilliseconds ;
17+
18+ private long _dashEndMilliseconds ;
1319
1420 private readonly Guid mEndMapId ;
1521
@@ -31,75 +37,99 @@ public partial class Dash : IDash
3137
3238 public float OffsetY => GetYOffset ( ) ;
3339
34- public Dash ( Guid endMapId , byte endX , byte endY , int dashTime , Direction changeDirection = Direction . None )
40+ public Dash (
41+ Guid endMapId ,
42+ byte endX ,
43+ byte endY ,
44+ long dashEndMilliseconds ,
45+ long dashLengthMilliseconds ,
46+ Direction changeDirection = Direction . None
47+ )
3548 {
3649 mChangeDirection = changeDirection ;
3750 mEndMapId = endMapId ;
3851 mEndX = endX ;
3952 mEndY = endY ;
40- mDashTime = dashTime ;
53+ _serverDashEndMilliseconds = dashEndMilliseconds ;
54+ _dashLengthMilliseconds = dashLengthMilliseconds ;
4155 }
4256
43- public void Start ( Entity en )
57+ public void Start ( Entity entity )
4458 {
45- if ( MapInstance . Get ( en . MapId ) == null ||
46- MapInstance . Get ( mEndMapId ) == null ||
47- mEndMapId == en . MapId && mEndX == en . X && mEndY == en . Y )
59+ var now = Timing . Global . Milliseconds ;
60+
61+ if ( _serverDashEndMilliseconds < now )
62+ {
63+ ApplicationContext . CurrentContext . Logger . LogDebug ( "Skipping already-expired dash" ) ;
64+ entity . Dashing = null ;
65+ return ;
66+ }
67+
68+ if ( mEndMapId == entity . MapId && mEndX == entity . X && mEndY == entity . Y )
69+ {
70+ entity . Dashing = null ;
71+ return ;
72+ }
73+
74+ if ( ! MapInstance . TryGet ( entity . MapId , out var currentMap ) || ! MapInstance . TryGet ( mEndMapId , out var targetMap ) )
4875 {
49- en . Dashing = null ;
76+ entity . Dashing = null ;
77+ return ;
5078 }
51- else
79+
80+ mStartTime = now ;
81+ _dashEndMilliseconds = Math . Min ( _serverDashEndMilliseconds , mStartTime + _dashLengthMilliseconds ) ;
82+
83+ ApplicationContext . CurrentContext . Logger . LogDebug (
84+ "Starting dash that is {DashLength}ms long, and there are {RemainingTime}ms before it is completed on the server" ,
85+ _dashLengthMilliseconds ,
86+ _serverDashEndMilliseconds - now
87+ ) ;
88+
89+ mStartXCoord = entity . X ;
90+ mStartYCoord = entity . Y ;
91+ mEndXCoord = targetMap . X + mEndX * Options . Instance . Map . TileWidth - ( currentMap . X + entity . X * Options . Instance . Map . TileWidth ) ;
92+ mEndYCoord = targetMap . Y + mEndY * Options . Instance . Map . TileHeight - ( currentMap . Y + entity . Y * Options . Instance . Map . TileHeight ) ;
93+ if ( mChangeDirection > Direction . None )
5294 {
53- var startMap = MapInstance . Get ( en . MapId ) ;
54- var endMap = MapInstance . Get ( mEndMapId ) ;
55- mStartTime = Timing . Global . Milliseconds ;
56- mStartXCoord = en . OffsetX ;
57- mStartYCoord = en . OffsetY ;
58- mEndXCoord = endMap . X + mEndX * Options . Instance . Map . TileWidth - ( startMap . X + en . X * Options . Instance . Map . TileWidth ) ;
59- mEndYCoord = endMap . Y + mEndY * Options . Instance . Map . TileHeight - ( startMap . Y + en . Y * Options . Instance . Map . TileHeight ) ;
60- if ( mChangeDirection > Direction . None )
61- {
62- en . Dir = mChangeDirection ;
63- }
95+ entity . Dir = mChangeDirection ;
6496 }
6597 }
6698
6799 public float GetXOffset ( )
68100 {
69- if ( Timing . Global . Milliseconds > mStartTime + mDashTime )
101+ if ( Timing . Global . Milliseconds > mStartTime + _dashLengthMilliseconds )
70102 {
71103 return mEndXCoord ;
72104 }
73- else
74- {
75- return ( mEndXCoord - mStartXCoord ) * ( ( Timing . Global . Milliseconds - mStartTime ) / ( float ) mDashTime ) ;
76- }
105+
106+ return ( mEndXCoord - mStartXCoord ) * ( ( Timing . Global . Milliseconds - mStartTime ) / ( float ) _dashLengthMilliseconds ) ;
77107 }
78108
79109 public float GetYOffset ( )
80110 {
81- if ( Timing . Global . Milliseconds > mStartTime + mDashTime )
111+ if ( Timing . Global . Milliseconds > mStartTime + _dashLengthMilliseconds )
82112 {
83113 return mEndYCoord ;
84114 }
85- else
86- {
87- return ( mEndYCoord - mStartYCoord ) * ( ( Timing . Global . Milliseconds - mStartTime ) / ( float ) mDashTime ) ;
88- }
115+
116+ return ( mEndYCoord - mStartYCoord ) * ( ( Timing . Global . Milliseconds - mStartTime ) / ( float ) _dashLengthMilliseconds ) ;
89117 }
90118
91- public bool Update ( Entity en )
119+ public bool Update ( Entity entity )
92120 {
93- if ( Timing . Global . Milliseconds > mStartTime + mDashTime )
121+ if ( Timing . Global . Milliseconds <= _dashEndMilliseconds )
94122 {
95- en . Dashing = null ;
96- en . OffsetX = 0 ;
97- en . OffsetY = 0 ;
98- en . MapId = mEndMapId ;
99- en . X = mEndX ;
100- en . Y = mEndY ;
123+ return entity . Dashing != null ;
101124 }
102125
103- return en . Dashing != null ;
126+ ApplicationContext . CurrentContext . Logger . LogDebug ( "Dash finished" ) ;
127+ entity . Dashing = null ;
128+ entity . OffsetX = 0 ;
129+ entity . OffsetY = 0 ;
130+ entity . MapId = mEndMapId ;
131+ entity . X = mEndX ;
132+ entity . Y = mEndY ;
133+ return false ;
104134 }
105135}
0 commit comments