forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGrid.cs
More file actions
130 lines (114 loc) · 3.24 KB
/
Copy pathCartesianGrid.cs
File metadata and controls
130 lines (114 loc) · 3.24 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
using System;
using Unity.Entities;
using Unity.Mathematics;
/// <summary>
/// Available directions along grid axis
/// </summary>
[Flags]
public enum CartesianGridDirectionBit : byte
{
None = 0x00,
North = 0x01,
South = 0x02,
West = 0x04,
East = 0x08
}
/// <summary>
/// Direction along grid axis
/// 0 = N
/// 1 = S
/// 2 = W
/// 3 = E
/// 0xff = No movement
/// </summary>
public struct CartesianGridDirection : IComponentData
{
public byte Value; // 2 bits current direction
}
/// <summary>
/// Speed of movement in grid-space
/// - 6:10 fixed point instead of 32bit float (for size)
/// </summary>
public struct CartesianGridSpeed : IComponentData
{
public ushort Value;
}
/// <summary>
/// Coordinates quantized to the grid
/// </summary>
public struct CartesianGridCoordinates : IComponentData, IEquatable<CartesianGridCoordinates>
{
public short x;
public short y;
public CartesianGridCoordinates(float2 pos, int rowCount, int colCount)
{
// Quantized grid coordinates from position in grid space
// - Always positive when on the grid. (Up, Right)
// - Basically just float to int cast.
// - However, in the case of a cube, the translation may go off the grid when travelling around the corner.
// In this case, stretch the one-off grid element to encompass the whole area off the grid.
// e.g. if it's off to the left, grid x value is always -1.
x = (short)math.clamp(((int)(pos.x + 1.0f)) - 1, -1, colCount);
y = (short)math.clamp(((int)(pos.y + 1.0f)) - 1, -1, rowCount);
}
public bool Equals(CartesianGridCoordinates other)
{
return ((other.x == x) && (other.y == y));
}
public bool OnGrid(int rowCount, int colCount) =>
((x >= 0) &&
(x <= (colCount - 1)) &&
(y >= 0) &&
(y <= (rowCount - 1)));
public override int GetHashCode()
{
unchecked
{
return (x.GetHashCode() * 397) ^ y.GetHashCode();
}
}
}
/// <summary>
/// Is a target on the map, so will be tracked by CartesianGridFollowTarget
/// </summary>
public struct CartesianGridTarget : IComponentData
{
}
/// <summary>
/// Follow nearest CartesianGridTarget
/// </summary>
[WriteGroup(typeof(CartesianGridDirection))]
public struct CartesianGridFollowTarget : IComponentData
{
}
/// <summary>
/// Table of shortest paths to target for every grid cell on map.
/// </summary>
public struct CartesianGridTargetDirection : IBufferElementData
{
byte m_Value; // Never accessed directly.
}
/// <summary>
/// Table of shortest distance along shortest path to target for every grid cell on map.
/// </summary>
public struct CartesianGridTargetDistance : IBufferElementData
{
int m_Value; // Never accessed directly.
}
/// <summary>
/// Track last known grid coordinate of target
/// </summary>
public struct CartesianGridTargetCoordinates : IComponentData, IEquatable<CartesianGridCoordinates>
{
public short x;
public short y;
public CartesianGridTargetCoordinates(CartesianGridCoordinates other)
{
x = other.x;
y = other.y;
}
public bool Equals(CartesianGridCoordinates other)
{
return ((other.x == x) && (other.y == y));
}
}