-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cs
205 lines (155 loc) · 4.66 KB
/
Entity.cs
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
using LiteNetLib;
using System;
using LiteNetLib.Utils;
using System.Xml.Serialization;
namespace MmorpgServer
{
[Serializable]
[XmlInclude(typeof(Wall))]
[XmlInclude(typeof(Pillar))]
public class Entity
{
[XmlIgnore]
public Int32 Id;
[XmlIgnore]
public Shape CollisionShape;
[XmlIgnore]
public Scene World;
private BoundingBox bounds;
private bool solid;
[XmlIgnore]
private bool visible;
[XmlIgnore]
public ref readonly BoundingBox BoundingBox
{
get
{
return ref bounds;
}
}
public bool CanSee(Entity other)
{
var direction = other.CollisionShape.Position - Position;
direction.Normalize();
var dir = direction * Vector2.Deg90;
var p1 = CollisionShape.GetPointOnDirection(dir);
var p2 = other.CollisionShape.GetPointOnDirection(dir);
Entity raycastResult = World.Raycast(p1, p2, entity => entity != this);
if (raycastResult == null || raycastResult == other)
{
return true;
}
dir = direction * Vector2.Deg270;
p1 = CollisionShape.GetPointOnDirection(dir);
p2 = other.CollisionShape.GetPointOnDirection(dir);
raycastResult = World.Raycast(p1, p2, entity => entity != this);
if (raycastResult == null || raycastResult == other)
{
return true;
}
return false;
}
public virtual bool HideOnFog
{
get { return false; }
}
public Entity()
{
Visible = true;
}
[XmlIgnore]
public bool Solid
{
get { return solid; }
set
{
if (value != solid)
{
var from = solid;
solid = value;
SolidChanged?.Invoke(this, from, value);
}
}
}
[XmlIgnore]
public bool Visible
{
get { return visible; }
set
{
if (value != visible)
{
var from = visible;
visible = value;
VisibleChanged?.Invoke(this, from, value);
}
}
}
protected void UpdateBoundingBox()
{
BoundingBox value = CollisionShape.GetClusters();
if (value != bounds)
{
BoundingBox from = bounds;
bounds = value;
BoundingBoxChanged?.Invoke(this, from, value);
}
}
public delegate void PositionChangedHandler(Entity entity, in Vector2 from, in Vector2 to);
public delegate void BoundingBoxChangedHandler(Entity entity, in BoundingBox from, in BoundingBox to);
public event PropertyChangedHandler<Entity, bool> VisibleChanged;
public event PropertyChangedHandler<Entity, bool> SolidChanged;
public event PositionChangedHandler PositionChanged;
public event BoundingBoxChangedHandler BoundingBoxChanged;
protected void OnPositionChanged(in Vector2 from, in Vector2 to)
{
PositionChanged?.Invoke(this, from, to);
UpdateBoundingBox();
}
public virtual void Initialize()
{
bounds = CollisionShape.GetClusters();
}
[XmlIgnore]
public bool IsDestroyed
{
get { return Id == 0; }
}
public virtual void Destroy()
{
}
public virtual void SendEnterPacket(NetPeer peer)
{
}
public void SendLeavePacket(NetPeer peer)
{
NetDataWriter writer = new NetDataWriter();
writer.Put((byte)4);
writer.Put((Int32)Id);
peer.Send(writer, DeliveryMethod.ReliableOrdered);
}
public virtual void AddEvents(PlayerController pc)
{
}
public virtual void RemoveEvents(PlayerController pc)
{
}
[XmlElement("Position")]
public Vector2 Position
{
get
{
return CollisionShape.Position;
}
set
{
if (CollisionShape.Position != value)
{
Vector2 from = CollisionShape.Position;
CollisionShape.Position = value;
OnPositionChanged(from, value);
}
}
}
}
}