Skip to content

Commit 9aef0c3

Browse files
fix: homing projectiles not transversing maps (AscensionGameDev#2274)
1 parent 07f981b commit 9aef0c3

File tree

2 files changed

+203
-33
lines changed

2 files changed

+203
-33
lines changed

Intersect.Client/Entities/Projectiles/Projectile.cs

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Intersect.Server.Core/Entities/Projectile.cs

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Intersect.GameObjects;
33
using Intersect.GameObjects.Maps;
44
using Intersect.Network.Packets.Server;
5+
using Intersect.Server.Database;
56
using Intersect.Server.Entities.Combat;
67
using Intersect.Server.Maps;
78
using Intersect.Utilities;
@@ -42,6 +43,8 @@ public partial class Projectile : Entity
4243

4344
private int mLastTargetY = -1;
4445

46+
private Guid mLastTargetMapId = Guid.Empty;
47+
4548
public Projectile(
4649
Entity owner,
4750
SpellBase parentSpell,
@@ -360,18 +363,92 @@ public bool CheckForCollision(ProjectileSpawn spawn)
360363
return killSpawn;
361364
}
362365

363-
private float GetProjectileX(ProjectileSpawn spawn, int targetX, float directionY)
366+
private float GetProjectileX(ProjectileSpawn spawn)
364367
{
365-
var directionX = targetX - spawn.X;
366-
var length = Math.Sqrt(directionX * directionX + directionY * directionY);
367-
return directionX / (float)length;
368+
if (mLastTargetMapId != Guid.Empty && mLastTargetMapId != spawn.MapId)
369+
{
370+
var map = MapController.Get(spawn.MapId);
371+
var grid = DbInterface.GetGrid(map.MapGrid);
372+
373+
//loop through surrounding maps
374+
for (var y = map.MapGridY - 1; y <= map.MapGridY + 1; y++)
375+
{
376+
if (y == -1 || y >= grid.Height)
377+
{
378+
continue;
379+
}
380+
381+
for (var x = map.MapGridY - 1; x <= map.MapGridX + 1; x++)
382+
{
383+
if (x == -1 || x >= grid.Width)
384+
{
385+
continue;
386+
}
387+
388+
if (grid.MapIdGrid[x, y] != Guid.Empty && grid.MapIdGrid[x, y] == mLastTargetMapId)
389+
{
390+
var leftSide = x == map.MapGridX - 1;
391+
var rightSide = x == map.MapGridX + 1;
392+
393+
if (leftSide)
394+
{
395+
return mLastTargetX - Options.MapWidth - spawn.X;
396+
}
397+
398+
if (rightSide)
399+
{
400+
return mLastTargetX + Options.MapWidth - spawn.X;
401+
}
402+
}
403+
}
404+
}
405+
}
406+
407+
return mLastTargetX - spawn.X;
368408
}
369409

370-
private float GetProjectileY(ProjectileSpawn spawn, int targetY, float directionX)
410+
private float GetProjectileY(ProjectileSpawn spawn)
371411
{
372-
var directionY = targetY - spawn.Y;
373-
var length = Math.Sqrt(directionX * directionX + directionY * directionY);
374-
return directionY / (float)length;
412+
if (mLastTargetMapId != Guid.Empty && mLastTargetMapId != spawn.MapId)
413+
{
414+
var map = MapController.Get(spawn.MapId);
415+
var grid = DbInterface.GetGrid(map.MapGrid);
416+
417+
//loop through surrounding maps
418+
for (var y = map.MapGridY - 1; y <= map.MapGridY + 1; y++)
419+
{
420+
if (y == -1 || y >= grid.Height)
421+
{
422+
continue;
423+
}
424+
425+
for (var x = map.MapGridY - 1; x <= map.MapGridX + 1; x++)
426+
{
427+
if (x == -1 || x >= grid.Width)
428+
{
429+
continue;
430+
}
431+
432+
if (grid.MapIdGrid[x, y] != Guid.Empty && grid.MapIdGrid[x, y] == mLastTargetMapId)
433+
{
434+
var topSide = y == map.MapGridY - 1;
435+
var bottomSide = y == map.MapGridY + 1;
436+
437+
if (topSide)
438+
{
439+
return mLastTargetY - Options.MapHeight - spawn.Y;
440+
}
441+
442+
if (bottomSide)
443+
{
444+
return mLastTargetY + Options.MapHeight - spawn.Y;
445+
}
446+
}
447+
}
448+
}
449+
}
450+
451+
return mLastTargetY - spawn.Y;
375452
}
376453

377454
public bool MoveFragment(ProjectileSpawn spawn, bool move = true)
@@ -390,8 +467,13 @@ public bool MoveFragment(ProjectileSpawn spawn, bool move = true)
390467
//homing logic
391468
mLastTargetX = Target.X;
392469
mLastTargetY = Target.Y;
393-
newx += GetProjectileX(spawn, Target.X, Target.Y - spawn.Y);
394-
newy += GetProjectileY(spawn, Target.Y, Target.X - spawn.X);
470+
mLastTargetMapId = Target.MapId;
471+
var directionX = GetProjectileX(spawn);
472+
var directionY = GetProjectileY(spawn);
473+
var length = Math.Sqrt(directionX * directionX + directionY * directionY);
474+
475+
newx += (float)(directionX / length);
476+
newy += (float)(directionY / length);
395477

396478
if (Base.DirectShotBehavior)
397479
{
@@ -401,8 +483,12 @@ public bool MoveFragment(ProjectileSpawn spawn, bool move = true)
401483
else if (mLastTargetX != -1 && mLastTargetY != -1)
402484
{
403485
//last target location logic
404-
newx += GetProjectileX(spawn, mLastTargetX, mLastTargetY - spawn.Y);
405-
newy += GetProjectileY(spawn, mLastTargetY, mLastTargetX - spawn.X);
486+
var directionX = GetProjectileX(spawn);
487+
var directionY = GetProjectileY(spawn);
488+
var length = Math.Sqrt(directionX * directionX + directionY * directionY);
489+
490+
newx += (float)(directionX / length);
491+
newy += (float)(directionY / length);
406492
}
407493
else
408494
{

0 commit comments

Comments
 (0)