| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Xml; |
| 4 |
using Oni.Metadata; |
| 5 |
|
| 6 |
namespace Oni.Objects |
| 7 |
{ |
| 8 |
internal class PatrolPath : ObjectBase |
| 9 |
{ |
| 10 |
private string name; |
| 11 |
private PatrolPathPoint[] points; |
| 12 |
private int patrolId; |
| 13 |
private int returnToNearest; |
| 14 |
|
| 15 |
public PatrolPath() |
| 16 |
{ |
| 17 |
TypeId = ObjectType.PatrolPath; |
| 18 |
} |
| 19 |
|
| 20 |
protected override void WriteOsd(BinaryWriter writer) |
| 21 |
{ |
| 22 |
writer.Write(name, 32); |
| 23 |
writer.Write(points.Length); |
| 24 |
writer.WriteUInt16(patrolId); |
| 25 |
writer.WriteUInt16(returnToNearest); |
| 26 |
|
| 27 |
foreach (var point in points) |
| 28 |
{ |
| 29 |
writer.Write((int)point.Type); |
| 30 |
|
| 31 |
switch (point.Type) |
| 32 |
{ |
| 33 |
case PatrolPathPointType.Loop: |
| 34 |
case PatrolPathPointType.Stop: |
| 35 |
case PatrolPathPointType.StopLooking: |
| 36 |
case PatrolPathPointType.StopScanning: |
| 37 |
case PatrolPathPointType.FreeFacing: |
| 38 |
break; |
| 39 |
|
| 40 |
case PatrolPathPointType.LoopFrom: |
| 41 |
writer.Write((int)point.Attributes["From"]); |
| 42 |
break; |
| 43 |
|
| 44 |
case PatrolPathPointType.IgnorePlayer: |
| 45 |
writer.WriteByte((byte)point.Attributes["Value"]); |
| 46 |
break; |
| 47 |
|
| 48 |
case PatrolPathPointType.ForkScript: |
| 49 |
case PatrolPathPointType.CallScript: |
| 50 |
writer.Write((short)point.Attributes["ScriptId"]); |
| 51 |
break; |
| 52 |
|
| 53 |
case PatrolPathPointType.MoveToFlag: |
| 54 |
case PatrolPathPointType.LookAtFlag: |
| 55 |
case PatrolPathPointType.MoveAndFaceFlag: |
| 56 |
writer.Write((short)point.Attributes["FlagId"]); |
| 57 |
break; |
| 58 |
|
| 59 |
case PatrolPathPointType.MovementMode: |
| 60 |
writer.Write((int)(PatrolPathMovementMode)point.Attributes["Mode"]); |
| 61 |
break; |
| 62 |
|
| 63 |
case PatrolPathPointType.LockFacing: |
| 64 |
writer.Write((int)(PatrolPathFacing)point.Attributes["Facing"]); |
| 65 |
break; |
| 66 |
|
| 67 |
case PatrolPathPointType.Pause: |
| 68 |
writer.Write((int)point.Attributes["Frames"]); |
| 69 |
break; |
| 70 |
|
| 71 |
case PatrolPathPointType.GlanceAtFlagFor: |
| 72 |
writer.Write((short)point.Attributes["FlagId"]); |
| 73 |
writer.Write((int)point.Attributes["Frames"]); |
| 74 |
break; |
| 75 |
|
| 76 |
case PatrolPathPointType.Scan: |
| 77 |
writer.Write((short)point.Attributes["Frames"]); |
| 78 |
writer.Write((float)point.Attributes["Rotation"]); |
| 79 |
break; |
| 80 |
|
| 81 |
case PatrolPathPointType.MoveThroughFlag: |
| 82 |
case PatrolPathPointType.MoveNearFlag: |
| 83 |
writer.Write((short)point.Attributes["FlagId"]); |
| 84 |
writer.Write((float)point.Attributes["Distance"]); |
| 85 |
break; |
| 86 |
|
| 87 |
case PatrolPathPointType.MoveToFlagLookAndWait: |
| 88 |
writer.Write((short)point.Attributes["Frames"]); |
| 89 |
writer.Write((short)point.Attributes["FlagId"]); |
| 90 |
writer.Write((float)point.Attributes["Rotation"]); |
| 91 |
break; |
| 92 |
|
| 93 |
case PatrolPathPointType.FaceToFlagAndFire: |
| 94 |
writer.Write((short)point.Attributes["FlagId"]); |
| 95 |
writer.Write((short)point.Attributes["Frames"]); |
| 96 |
writer.Write((float)point.Attributes["Spread"]); |
| 97 |
break; |
| 98 |
|
| 99 |
case PatrolPathPointType.LookAtPoint: |
| 100 |
case PatrolPathPointType.MoveToPoint: |
| 101 |
writer.Write((Vector3)point.Attributes["Point"]); |
| 102 |
break; |
| 103 |
|
| 104 |
case PatrolPathPointType.MoveThroughPoint: |
| 105 |
writer.Write((Vector3)point.Attributes["Point"]); |
| 106 |
writer.Write((float)point.Attributes["Distance"]); |
| 107 |
break; |
| 108 |
|
| 109 |
default: |
| 110 |
throw new NotSupportedException(string.Format("Unsupported path point type {0}", point.Type)); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
protected override void ReadOsd(BinaryReader reader) |
| 116 |
{ |
| 117 |
throw new NotImplementedException(); |
| 118 |
} |
| 119 |
|
| 120 |
protected override void WriteOsd(XmlWriter xml) |
| 121 |
{ |
| 122 |
throw new NotImplementedException(); |
| 123 |
} |
| 124 |
|
| 125 |
protected override void ReadOsd(XmlReader xml, ObjectLoadContext context) |
| 126 |
{ |
| 127 |
name = xml.ReadElementContentAsString("Name", ""); |
| 128 |
patrolId = xml.ReadElementContentAsInt("PatrolId", ""); |
| 129 |
returnToNearest = xml.ReadElementContentAsInt("ReturnToNearest", ""); |
| 130 |
bool isEmpty = xml.IsEmptyElement; |
| 131 |
xml.ReadStartElement("Points"); |
| 132 |
|
| 133 |
if (isEmpty) |
| 134 |
{ |
| 135 |
points = new PatrolPathPoint[0]; |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
var pointList = new List<PatrolPathPoint>(); |
| 140 |
int loopStart = -1; |
| 141 |
bool inLoop = false; |
| 142 |
|
| 143 |
for (int index = 0; xml.IsStartElement() || inLoop; index++) |
| 144 |
{ |
| 145 |
if (!xml.IsStartElement()) |
| 146 |
{ |
| 147 |
xml.ReadEndElement(); |
| 148 |
inLoop = false; |
| 149 |
continue; |
| 150 |
} |
| 151 |
|
| 152 |
if (xml.LocalName == "Loop") |
| 153 |
{ |
| 154 |
if (xml.SkipEmpty()) |
| 155 |
continue; |
| 156 |
|
| 157 |
inLoop = true; |
| 158 |
loopStart = index; |
| 159 |
xml.ReadStartElement(); |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
var point = new PatrolPathPoint(MetaEnum.Parse<PatrolPathPointType>(xml.LocalName)); |
| 164 |
|
| 165 |
switch (point.Type) |
| 166 |
{ |
| 167 |
case PatrolPathPointType.Stop: |
| 168 |
case PatrolPathPointType.StopLooking: |
| 169 |
case PatrolPathPointType.StopScanning: |
| 170 |
case PatrolPathPointType.FreeFacing: |
| 171 |
break; |
| 172 |
|
| 173 |
case PatrolPathPointType.IgnorePlayer: |
| 174 |
point.Attributes["Value"] = (byte)(xml.GetAttribute("Value") == "Yes" ? 1 : 0); |
| 175 |
break; |
| 176 |
|
| 177 |
case PatrolPathPointType.ForkScript: |
| 178 |
case PatrolPathPointType.CallScript: |
| 179 |
point.Attributes["ScriptId"] = XmlConvert.ToInt16(xml.GetAttribute("ScriptId")); |
| 180 |
break; |
| 181 |
|
| 182 |
case PatrolPathPointType.MoveToFlag: |
| 183 |
case PatrolPathPointType.LookAtFlag: |
| 184 |
case PatrolPathPointType.MoveAndFaceFlag: |
| 185 |
point.Attributes["FlagId"] = XmlConvert.ToInt16(xml.GetAttribute("FlagId")); |
| 186 |
break; |
| 187 |
|
| 188 |
case PatrolPathPointType.MovementMode: |
| 189 |
point.Attributes["Mode"] = Convert.ToInt32(MetaEnum.Parse<PatrolPathMovementMode>(xml.GetAttribute("Mode"))); |
| 190 |
break; |
| 191 |
|
| 192 |
case PatrolPathPointType.LockFacing: |
| 193 |
point.Attributes["Facing"] = Convert.ToInt32(MetaEnum.Parse<PatrolPathFacing>(xml.GetAttribute("Facing"))); |
| 194 |
break; |
| 195 |
|
| 196 |
case PatrolPathPointType.Pause: |
| 197 |
point.Attributes["Frames"] = XmlConvert.ToInt32(xml.GetAttribute("Frames")); |
| 198 |
break; |
| 199 |
|
| 200 |
case PatrolPathPointType.GlanceAtFlagFor: |
| 201 |
point.Attributes["FlagId"] = XmlConvert.ToInt16(xml.GetAttribute("FlagId")); |
| 202 |
point.Attributes["Frames"] = XmlConvert.ToInt32(xml.GetAttribute("Frames")); |
| 203 |
break; |
| 204 |
|
| 205 |
case PatrolPathPointType.Scan: |
| 206 |
point.Attributes["Frames"] = XmlConvert.ToInt16(xml.GetAttribute("Frames")); |
| 207 |
point.Attributes["Rotation"] = XmlConvert.ToSingle(xml.GetAttribute("Rotation")); |
| 208 |
break; |
| 209 |
|
| 210 |
case PatrolPathPointType.MoveThroughFlag: |
| 211 |
case PatrolPathPointType.MoveNearFlag: |
| 212 |
point.Attributes["FlagId"] = XmlConvert.ToInt16(xml.GetAttribute("FlagId")); |
| 213 |
point.Attributes["Distance"] = XmlConvert.ToSingle(xml.GetAttribute("Distance")); |
| 214 |
break; |
| 215 |
|
| 216 |
case PatrolPathPointType.MoveToFlagLookAndWait: |
| 217 |
point.Attributes["Frames"] = XmlConvert.ToInt16(xml.GetAttribute("Frames")); |
| 218 |
point.Attributes["FlagId"] = XmlConvert.ToInt16(xml.GetAttribute("FlagId")); |
| 219 |
point.Attributes["Rotation"] = XmlConvert.ToSingle(xml.GetAttribute("Rotation")); |
| 220 |
break; |
| 221 |
|
| 222 |
case PatrolPathPointType.FaceToFlagAndFire: |
| 223 |
point.Attributes["FlagId"] = XmlConvert.ToInt16(xml.GetAttribute("FlagId")); |
| 224 |
point.Attributes["Frames"] = XmlConvert.ToInt16(xml.GetAttribute("Frames")); |
| 225 |
point.Attributes["Spread"] = XmlConvert.ToSingle(xml.GetAttribute("Spread")); |
| 226 |
break; |
| 227 |
|
| 228 |
case PatrolPathPointType.LookAtPoint: |
| 229 |
case PatrolPathPointType.MoveToPoint: |
| 230 |
point.Attributes["Point"] = new Vector3( |
| 231 |
XmlConvert.ToSingle(xml.GetAttribute("X")), |
| 232 |
XmlConvert.ToSingle(xml.GetAttribute("Y")), |
| 233 |
XmlConvert.ToSingle(xml.GetAttribute("Z"))); |
| 234 |
break; |
| 235 |
|
| 236 |
case PatrolPathPointType.MoveThroughPoint: |
| 237 |
point.Attributes["Point"] = new Vector3( |
| 238 |
XmlConvert.ToSingle(xml.GetAttribute("X")), |
| 239 |
XmlConvert.ToSingle(xml.GetAttribute("Y")), |
| 240 |
XmlConvert.ToSingle(xml.GetAttribute("Z"))); |
| 241 |
point.Attributes["Distance"] = XmlConvert.ToSingle(xml.GetAttribute("Distance")); |
| 242 |
break; |
| 243 |
|
| 244 |
default: |
| 245 |
throw new NotSupportedException(string.Format("Unsupported path point type {0}", point.Type)); |
| 246 |
} |
| 247 |
|
| 248 |
xml.Skip(); |
| 249 |
pointList.Add(point); |
| 250 |
} |
| 251 |
|
| 252 |
xml.ReadEndElement(); |
| 253 |
|
| 254 |
if (loopStart == 0) |
| 255 |
{ |
| 256 |
pointList.Add(new PatrolPathPoint(PatrolPathPointType.Loop)); |
| 257 |
} |
| 258 |
else if (loopStart > 0) |
| 259 |
{ |
| 260 |
var point = new PatrolPathPoint(PatrolPathPointType.LoopFrom); |
| 261 |
point.Attributes["From"] = loopStart; |
| 262 |
pointList.Add(point); |
| 263 |
} |
| 264 |
|
| 265 |
points = pointList.ToArray(); |
| 266 |
} |
| 267 |
} |
| 268 |
} |