Skip to content

Commit

Permalink
chore: refactor Entity#CanMove()'s return types (#1826)
Browse files Browse the repository at this point in the history
* rename to CanMoveInDirection

* Change MapSlideAttribute.Direction from byte to Direction

* chore: out-parameter version of CanMoveInDirection()

* undo the rename, easier to replace references to the existing implementation (which has the same parameter signature to the function I'm about to add)

* add bool CanMoveInDirection(Direction)

* remove all external direct references to CanMove and make it private

* move simple special NPC logic from CanMove() to NPC CanMoveInDirection() override

* minor CanMove cleanup

* EventPage -> EventPageInstance correction

* automatic switch statement refactor

* simple inversion refactors

* refactor out IsBlockedByEvent for players

* refactor out CanPassPlayer()

* simple LINQ conversion

* if -> switch conversion

* switch simplification

* inlining variables

* refactor offset

* refactor CanSlideInDirection()

* CanMoveOntoSlide is a more accurate name for what it is doing

* IgnoresNpcAvoid

* refactor out IsBlockedByMapAttribute

* finish removing parts of CanMove

* add missing cases
  • Loading branch information
lodicolo authored Aug 16, 2023
1 parent 732fc14 commit 003a365
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 304 deletions.
25 changes: 25 additions & 0 deletions Intersect (Core)/Enums/Direction.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Intersect.Enums
{
public enum Direction
Expand All @@ -20,4 +22,27 @@ public enum Direction

DownLeft
}

public static class DirectionExtensions
{
public static Direction GetOpposite(this Direction direction)
{
switch (direction)
{
case Direction.None: return Direction.None;
case Direction.Up: return Direction.Down;
case Direction.Down: return Direction.Up;
case Direction.Left: return Direction.Right;
case Direction.Right: return Direction.Left;
case Direction.UpLeft: return Direction.DownRight;
case Direction.UpRight: return Direction.DownLeft;
case Direction.DownRight: return Direction.UpLeft;
case Direction.DownLeft: return Direction.UpRight;
default: throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
}

public static bool IsOppositeOf(this Direction direction, Direction otherDirection) =>
otherDirection == direction.GetOpposite();
}
}
2 changes: 1 addition & 1 deletion Intersect (Core)/GameObjects/Maps/MapAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public partial class MapSlideAttribute : MapAttribute

[EditorLabel("Attributes", "Direction")]
[EditorDictionary(nameof(Direction), "WarpDirections")]
public byte Direction { get; set; }
public Direction Direction { get; set; }

public override MapAttribute Clone()
{
Expand Down
2 changes: 1 addition & 1 deletion Intersect.Editor/Forms/DockingElements/frmMapLayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public GameObjects.Maps.MapAttribute CreateAttribute()

case MapAttribute.Slide:
var slideAttribute = attribute as MapSlideAttribute;
slideAttribute.Direction = (byte)cmbSlideDir.SelectedIndex;
slideAttribute.Direction = (Direction)cmbSlideDir.SelectedIndex;
break;

case MapAttribute.Critter:
Expand Down
102 changes: 70 additions & 32 deletions Intersect.Server/Entities/Combat/Dash.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Intersect.Enums;
using System;
using Intersect.Enums;
using Intersect.Logging;
using Intersect.Server.Networking;
using Intersect.Utilities;

Expand Down Expand Up @@ -47,43 +49,79 @@ public void CalculateRange(
bool blockPass = false,
bool activeResourcePass = false,
bool deadResourcePass = false,
bool zdimensionPass = false
bool zDimensionPass = false
)
{
var n = 0;
en.MoveTimer = 0;
Range = 0;
for (var i = 1; i <= range; i++)
if (en == default)
{
n = en.CanMove(Direction);
if (n == -5) //Check for out of bounds
{
return;
} //Check for blocks

if (n == -2 && blockPass == false)
{
return;
} //Check for ZDimensionTiles

if (n == -3 && zdimensionPass == false)
{
return;
} //Check for active resources

if (n == (int) EntityType.Resource && activeResourcePass == false)
{
return;
} //Check for dead resources

if (n == (int) EntityType.Resource && deadResourcePass == false)
{
return;
} //Check for players and solid events
Log.Error(
new ArgumentNullException(
nameof(en),
"Entity was null when calling CalcuateRange(), this isn't supported."
)
);
return;
}

if (n == (int) EntityType.Player || n == (int) EntityType.Event)
en.MoveTimer = 0;
for (var i = 1; i <= range; i++)
{
if (!en.CanMoveInDirection(Direction, out var blockerType, out var blockingEntityType))
{
return;
switch (blockerType)
{
case MovementBlockerType.OutOfBounds:
return;

case MovementBlockerType.MapAttribute:
if (!blockPass)
{
return;
}

break;

case MovementBlockerType.ZDimension:
if (!zDimensionPass)
{
return;
}

break;

case MovementBlockerType.Entity:
switch (blockingEntityType)
{
case EntityType.Resource:
if (activeResourcePass || deadResourcePass)
{
break;
}

return;

case EntityType.Event:
case EntityType.Player:
return;

case EntityType.GlobalEntity:
case EntityType.Projectile:
break;

default:
throw new NotImplementedException($"{blockingEntityType} not implemented.");
}

break;

case MovementBlockerType.NotBlocked:
case MovementBlockerType.Slide:
break;

default:
throw new NotImplementedException($"{blockerType} not implemented.");
}
}

en.Move(Direction, null, true);
Expand Down
Loading

0 comments on commit 003a365

Please sign in to comment.