-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathCritter.cs
More file actions
276 lines (236 loc) · 8.28 KB
/
Copy pathCritter.cs
File metadata and controls
276 lines (236 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using Intersect.Client.Core;
using Intersect.Client.Framework.Entities;
using Intersect.Client.Framework.Maps;
using Intersect.Client.General;
using Intersect.Client.Maps;
using Intersect.Enums;
using Intersect.Framework.Core;
using Intersect.Framework.Core.GameObjects.Animations;
using Intersect.Framework.Core.GameObjects.Maps.Attributes;
using Intersect.GameObjects;
using Intersect.Utilities;
namespace Intersect.Client.Entities;
public partial class Critter : Entity
{
private readonly MapCritterAttribute mAttribute;
// Critter's Movement
private long _lastMove = -1;
private byte _randomMoveRange;
public Critter(MapInstance map, byte x, byte y, MapCritterAttribute att) : base(Guid.NewGuid(), null, EntityType.GlobalEntity)
{
mAttribute = att;
//setup Sprite & Animation
Sprite = att.Sprite;
if (AnimationDescriptor.TryGet(att.AnimationId, out var animationDescriptor))
{
TryAddAnimation(new Animation(animationDescriptor, true));
}
//Define Location
MapId = map?.Id ?? default;
X = x;
Y = y;
//Determine Direction
if (mAttribute.Direction == 0)
{
DirectionFacing = Randomization.NextDirection();
}
else
{
DirectionFacing = (Direction)(mAttribute.Direction - 1);
}
//Block Players?
Passable = !att.BlockPlayers;
}
public override bool Update()
{
if (!base.Update())
{
return false;
}
// Only skip if we are NOT in the middle of a range-walk AND the frequency timer is active
if (_randomMoveRange <= 0 && _lastMove >= Timing.Global.MillisecondsUtc)
{
return true;
}
switch (mAttribute.Movement)
{
case 0: // Move Randomly
MoveRandomly();
break;
case 1: // Turn Randomly
DirectionFacing = Randomization.NextDirection();
// Set pause after turning
_lastMove = Timing.Global.MillisecondsUtc + mAttribute.Frequency + Globals.Random.Next((int)(mAttribute.Frequency * .5f));
break;
}
return true;
}
private void MoveRandomly()
{
// Don't start a new step if currently moving between tiles
if (IsMoving || MoveTimer >= Timing.Global.MillisecondsUtc)
{
return;
}
// No range left: pick a new direction and range
if (_randomMoveRange <= 0)
{
DirectionFacing = Randomization.NextDirection();
_randomMoveRange = (byte)Randomization.Next(1, 5);
}
var deltaX = 0;
var deltaY = 0;
switch (DirectionFacing)
{
case Direction.Up:
deltaY = -1;
break;
case Direction.Down:
deltaY = 1;
break;
case Direction.Left:
deltaX = -1;
break;
case Direction.Right:
deltaX = 1;
break;
case Direction.UpLeft:
deltaX = -1;
deltaY = -1;
break;
case Direction.UpRight:
deltaX = 1;
deltaY = -1;
break;
case Direction.DownLeft:
deltaX = -1;
deltaY = 1;
break;
case Direction.DownRight:
deltaX = 1;
deltaY = 1;
break;
}
var newX = (sbyte)X + deltaX;
var newY = (sbyte)Y + deltaY;
IEntity? blockedBy = null;
// Boundary checks
var isBlocked = -1 == IsTileBlocked(new Point(newX, newY), Z, MapId, ref blockedBy, true, true, mAttribute.IgnoreNpcAvoids);
var playerOnTile = PlayerOnTile(MapId, newX, newY);
if (isBlocked && !playerOnTile &&
newX >= 0 && newX < Options.Instance.Map.MapWidth &&
newY >= 0 && newY < Options.Instance.Map.MapHeight)
{
X = (byte)newX;
Y = (byte)newY;
IsMoving = true;
OffsetX = deltaX == 0 ? 0 : (deltaX > 0 ? -Options.Instance.Map.TileWidth : Options.Instance.Map.TileWidth);
OffsetY = deltaY == 0 ? 0 : (deltaY > 0 ? -Options.Instance.Map.TileHeight : Options.Instance.Map.TileHeight);
MoveTimer = Timing.Global.MillisecondsUtc + (long)GetMovementTime();
_randomMoveRange--;
// Critter's last step: set an idle pause timer
if (_randomMoveRange <= 0)
{
_lastMove = Timing.Global.MillisecondsUtc + mAttribute.Frequency + Globals.Random.Next((int)(mAttribute.Frequency * .5f));
}
}
else
{
// Blocked by something: end range early and trigger pause
_randomMoveRange = 0;
_lastMove = Timing.Global.MillisecondsUtc + mAttribute.Frequency;
}
}
public bool PlayerOnTile(Guid mapId, int x, int y)
{
foreach (var en in Globals.Entities)
{
if (en.Value == null)
{
continue;
}
if (en.Value.MapId == mapId &&
en.Value.X == x &&
en.Value.Y == y)
{
if (en.Value is Player)
{
return true;
}
}
}
return false;
}
public override HashSet<Entity>? DetermineRenderOrder(HashSet<Entity>? renderList, IMapInstance? map)
{
if (mAttribute.Layer == 1)
{
return base.DetermineRenderOrder(renderList, map);
}
_ = (renderList?.Remove(this));
if (map == null || Globals.Me == null || Globals.Me.MapInstance == null || Globals.MapGrid == default)
{
return null;
}
var gridX = Globals.Me.MapInstance.GridX;
var gridY = Globals.Me.MapInstance.GridY;
for (var x = gridX - 1; x <= gridX + 1; x++)
{
for (var y = gridY - 1; y <= gridY + 1; y++)
{
if (x >= 0 &&
x < Globals.MapGridWidth &&
y >= 0 &&
y < Globals.MapGridHeight &&
Globals.MapGrid[x, y] != Guid.Empty)
{
if (Globals.MapGrid[x, y] == MapId)
{
if (mAttribute.Layer == 0)
{
y--;
}
if (mAttribute.Layer == 2)
{
y++;
}
var priority = mRenderPriority;
if (Z != 0)
{
priority += 3;
}
HashSet<Entity>? renderSet = null;
if (y == gridY - 2)
{
renderSet = Graphics.RenderingEntities[priority, Y];
}
else if (y == gridY - 1)
{
renderSet = Graphics.RenderingEntities[priority, Options.Instance.Map.MapHeight + Y];
}
else if (y == gridY)
{
renderSet = Graphics.RenderingEntities[priority, Options.Instance.Map.MapHeight * 2 + Y];
}
else if (y == gridY + 1)
{
renderSet = Graphics.RenderingEntities[priority, Options.Instance.Map.MapHeight * 3 + Y];
}
else if (y == gridY + 2)
{
renderSet = Graphics.RenderingEntities[priority, Options.Instance.Map.MapHeight * 4 + Y];
}
_ = (renderSet?.Add(this));
renderList = renderSet;
return renderList;
}
}
}
}
return renderList;
}
public override float GetMovementTime()
{
return mAttribute.Speed;
}
}