@@ -44,6 +44,8 @@ public partial class Projectile : Entity
4444
4545 public int mLastTargetY = - 1 ;
4646
47+ public Guid mLastTargetMapId = Guid . Empty ;
48+
4749 /// <summary>
4850 /// The constructor for the inherated projectile class
4951 /// </summary>
@@ -314,10 +316,11 @@ public override bool Update()
314316 var target = Globals . Entities [ TargetId ] ;
315317 mLastTargetX = target . X ;
316318 mLastTargetY = target . Y ;
319+ mLastTargetMapId = target . MapId ;
317320
318- Spawns [ s ] . OffsetX = GetProjectileX ( Spawns [ s ] , target . X , target . Y - Spawns [ s ] . SpawnY ) ;
319- Spawns [ s ] . OffsetY = GetProjectileY ( Spawns [ s ] , target . Y , target . X - Spawns [ s ] . SpawnX ) ;
320- SetProjectileRotation ( Spawns [ s ] , target . X , target . Y ) ;
321+ Spawns [ s ] . OffsetX = GetProjectileLerping ( Spawns [ s ] , true ) ;
322+ Spawns [ s ] . OffsetY = GetProjectileLerping ( Spawns [ s ] , false ) ;
323+ SetProjectileRotation ( Spawns [ s ] ) ;
321324
322325 if ( mMyBase . DirectShotBehavior )
323326 {
@@ -326,9 +329,9 @@ public override bool Update()
326329 }
327330 else if ( mLastTargetX != - 1 && mLastTargetY != - 1 )
328331 {
329- Spawns [ s ] . OffsetX = GetProjectileX ( Spawns [ s ] , mLastTargetX , mLastTargetY - Spawns [ s ] . SpawnY ) ;
330- Spawns [ s ] . OffsetY = GetProjectileY ( Spawns [ s ] , mLastTargetY , mLastTargetX - Spawns [ s ] . SpawnX ) ;
331- SetProjectileRotation ( Spawns [ s ] , mLastTargetX , mLastTargetY ) ;
332+ Spawns [ s ] . OffsetX = GetProjectileLerping ( Spawns [ s ] , true ) ;
333+ Spawns [ s ] . OffsetY = GetProjectileLerping ( Spawns [ s ] , false ) ;
334+ SetProjectileRotation ( Spawns [ s ] ) ;
332335 }
333336 else
334337 {
@@ -362,32 +365,113 @@ public override bool Update()
362365 return true ;
363366 }
364367
365- private float GetProjectileX ( ProjectileSpawns spawn , int targetX , float directionY )
368+ private float GetProjectileX ( ProjectileSpawns spawn )
366369 {
367- float directionX = targetX - spawn . SpawnX ;
368- var length = ( float ) Math . Sqrt ( directionX * directionX + directionY * directionY ) ;
369- directionX /= length ;
370+ if ( mLastTargetMapId != Guid . Empty && mLastTargetMapId != spawn . SpawnMapId )
371+ {
372+ var map = Maps . MapInstance . Get ( spawn . SpawnMapId ) ;
373+ for ( var y = map . GridY - 1 ; y <= map . GridY + 1 ; y ++ )
374+ {
375+ if ( y < 0 || y >= Options . MapHeight )
376+ {
377+ continue ;
378+ }
370379
371- var desiredX = GetDisplacement ( spawn . SpawnTime ) * directionX ;
372- var lerpFactor = 0.1f ;
373- return spawn . OffsetX + ( desiredX - spawn . OffsetX ) * lerpFactor ;
380+ for ( var x = map . GridX - 1 ; x <= map . GridX + 1 ; x ++ )
381+ {
382+ if ( x < 0 || x >= Options . MapWidth )
383+ {
384+ continue ;
385+ }
386+
387+ if ( Globals . MapGrid [ x , y ] != Guid . Empty && Globals . MapGrid [ x , y ] == mLastTargetMapId )
388+ {
389+ var leftSide = x == map . GridX - 1 ;
390+ var rightSide = x == map . GridX + 1 ;
391+
392+ if ( leftSide )
393+ {
394+ return mLastTargetX - Options . MapWidth - spawn . SpawnX ;
395+ }
396+
397+ if ( rightSide )
398+ {
399+ return mLastTargetX + Options . MapWidth - spawn . SpawnX ;
400+ }
401+ }
402+ }
403+ }
404+ }
405+
406+ return mLastTargetX - spawn . SpawnX ;
407+ }
408+
409+ private float GetProjectileY ( ProjectileSpawns spawn )
410+ {
411+ if ( mLastTargetMapId != Guid . Empty && mLastTargetMapId != spawn . SpawnMapId )
412+ {
413+ var map = Maps . MapInstance . Get ( spawn . SpawnMapId ) ;
414+ for ( var y = map . GridY - 1 ; y <= map . GridY + 1 ; y ++ )
415+ {
416+ if ( y < 0 || y >= Options . MapHeight )
417+ {
418+ continue ;
419+ }
420+
421+ for ( var x = map . GridX - 1 ; x <= map . GridX + 1 ; x ++ )
422+ {
423+ if ( x < 0 || x >= Options . MapWidth )
424+ {
425+ continue ;
426+ }
427+
428+ if ( x >= Globals . MapGrid . GetLength ( 0 ) || y >= Globals . MapGrid . GetLength ( 1 ) )
429+ {
430+ continue ;
431+ }
432+
433+ if ( Globals . MapGrid [ x , y ] != Guid . Empty && Globals . MapGrid [ x , y ] == mLastTargetMapId )
434+ {
435+ var upSide = y == map . GridY + 1 ;
436+ var downSide = y == map . GridY - 1 ;
437+
438+ if ( upSide )
439+ {
440+ return mLastTargetY + Options . MapHeight - spawn . SpawnY ;
441+ }
442+
443+ if ( downSide )
444+ {
445+ return mLastTargetY - Options . MapHeight - spawn . SpawnY ;
446+ }
447+ }
448+ }
449+ }
450+ }
451+
452+ return mLastTargetY - spawn . SpawnY ;
374453 }
375454
376- private float GetProjectileY ( ProjectileSpawns spawn , int targetY , float directionX )
455+ private float GetProjectileLerping ( ProjectileSpawns spawn , bool isXAxis )
377456 {
378- float directionY = targetY - spawn . SpawnY ;
457+ var directionX = GetProjectileX ( spawn ) ;
458+ var directionY = GetProjectileY ( spawn ) ;
459+ var valueToLerp = isXAxis ? directionX : directionY ;
460+
379461 var length = ( float ) Math . Sqrt ( directionX * directionX + directionY * directionY ) ;
380- directionY /= length ;
462+ valueToLerp /= length ;
381463
382- var desiredY = GetDisplacement ( spawn . SpawnTime ) * directionY ;
383464 var lerpFactor = 0.1f ;
384- return spawn . OffsetY + ( desiredY - spawn . OffsetY ) * lerpFactor ;
465+ var offset = isXAxis ? spawn . OffsetX : spawn . OffsetY ;
466+ var desiredValue = GetDisplacement ( spawn . SpawnTime ) * valueToLerp ;
467+
468+ return offset + ( desiredValue - offset ) * lerpFactor ;
385469 }
386470
387- private void SetProjectileRotation ( ProjectileSpawns spawn , int targetX , int targetY )
471+ private void SetProjectileRotation ( ProjectileSpawns spawn )
388472 {
389- var directionX = targetX - spawn . SpawnX ;
390- var directionY = targetY - spawn . SpawnY ;
473+ var directionX = GetProjectileX ( spawn ) ;
474+ var directionY = GetProjectileY ( spawn ) ;
391475 var angle = ( float ) ( Math . Atan2 ( directionY , directionX ) * ( 180.0 / Math . PI ) + 90 ) ;
392476 spawn . Anim . SetRotation ( angle ) ;
393477 }
0 commit comments