Skip to content

Commit 86b748e

Browse files
committed
add pet walk
1 parent e0c5548 commit 86b748e

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

TaleKit/Game/Entities/Character.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public interface IActionBridge
1111
Session Session { get; set; }
1212

1313
void Walk(Position position, int speed);
14+
void WalkNosmate(Position position, int speed);
1415
void Attack(LivingEntity entity);
1516
void Attack(LivingEntity entity, Skill skill);
1617
void PickUp(Drop drop);
@@ -53,6 +54,11 @@ public Character(IActionBridge bridge, INetwork network)
5354
public override EntityType EntityType => EntityType.Player;
5455

5556
public void Walk(Position destination)
57+
{
58+
_ = WalkAsync(destination);
59+
}
60+
61+
public Task WalkAsync(Position destination)
5662
{
5763
if (currentTaskCts is not null)
5864
{
@@ -63,10 +69,10 @@ public void Walk(Position destination)
6369

6470
if (!Map.IsWalkable(destination))
6571
{
66-
return;
72+
return Task.CompletedTask;
6773
}
6874

69-
_ = Walk(destination, currentTaskCts.Token);
75+
return Walk(destination, currentTaskCts.Token);
7076
}
7177

7278
public void Summon(Nosmate nosmate)

TaleKit/Game/Entities/Nosmate.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,36 @@ public class SummonedNosmate
6666

6767
public Character Owner { get; }
6868
public LivingEntity Entity => Owner.Map.GetEntity<Npc>(EntityType.Npc, Nosmate.Id);
69+
70+
private CancellationTokenSource currentTaskCts;
6971

7072
public SummonedNosmate(Character owner)
7173
{
7274
Owner = owner;
7375
}
7476

77+
public void Walk(Position destination)
78+
{
79+
_ = WalkAsync(destination);
80+
}
81+
82+
public Task WalkAsync(Position destination)
83+
{
84+
if (currentTaskCts is not null)
85+
{
86+
currentTaskCts.Cancel();
87+
}
88+
89+
currentTaskCts = new CancellationTokenSource();
90+
91+
if (!Entity.Map.IsWalkable(destination))
92+
{
93+
return Task.CompletedTask;
94+
}
95+
96+
return Walk(destination, currentTaskCts.Token);
97+
}
98+
7599
public void Attack(LivingEntity target, NosmateSkill skill)
76100
{
77101
if (!Skills.Contains(skill))
@@ -86,4 +110,50 @@ public void AttackSelf(NosmateSkill skill)
86110
{
87111
Attack(Entity, skill);
88112
}
113+
114+
private async Task Walk(Position destination, CancellationToken cancellationToken)
115+
{
116+
var positiveX = destination.X > Entity.Position.X;
117+
var positiveY = destination.Y > Entity.Position.Y;
118+
119+
while(!Entity.Position.Equals(destination) && !cancellationToken.IsCancellationRequested)
120+
{
121+
var distance = Entity.Position.Distance(destination);
122+
123+
var stepX = distance.X >= 5 ? 5 : distance.X;
124+
var stepY = distance.Y >= 5 ? 5 : distance.Y;
125+
126+
var stepTotal = stepX + stepY;
127+
if (stepTotal > 5)
128+
{
129+
var difference = stepTotal - 5;
130+
var split = difference / 2 + (difference % 2);
131+
132+
stepX -= split;
133+
stepY -= split;
134+
}
135+
136+
var x = (positiveX ? 1 : -1) * stepX + Entity.Position.X;
137+
var y = (positiveY ? 1 : -1) * stepY + Entity.Position.Y;
138+
139+
var target = new Position(x, y);
140+
141+
if (!Entity.Map.IsWalkable(target))
142+
{
143+
break;
144+
}
145+
146+
Owner.GetBridge().WalkNosmate(target, Entity.Speed);
147+
148+
try
149+
{
150+
await Task.Delay((stepX + stepY) * (2000 / Entity.Speed), cancellationToken);
151+
}
152+
catch (TaskCanceledException) { }
153+
finally
154+
{
155+
Entity.Position = target;
156+
}
157+
}
158+
}
89159
}

0 commit comments

Comments
 (0)