public List <WRoomTransform> GetRoomTransformTable()
        {
            List <WRoomTransform> roomTransforms = new List <WRoomTransform>();

            int multIndex = m_chunkList.FindIndex(x => x.FourCC == FourCC.MULT);

            if (multIndex >= 0)
            {
                ChunkHeader rtbl = m_chunkList[multIndex];
                m_reader.BaseStream.Position = rtbl.ChunkOffset;

                for (int i = 0; i < rtbl.ElementCount; i++)
                {
                    WRoomTransform roomTransform = new WRoomTransform(new Vector2(m_reader.ReadSingle(), m_reader.ReadSingle()), WMath.RotationShortToFloat(m_reader.ReadInt16()), m_reader.ReadByte(), m_reader.ReadByte());
                    roomTransforms.Add(roomTransform);
                }
            }

            return(roomTransforms);
        }
Example #2
0
        private WActorNode LoadActorFromChunk(string fourCC, MapActorDescriptor template)
        {
            var newActor = new WActorNode(fourCC, m_world);
            List <IPropertyValue> actorProperties = new List <IPropertyValue>();

            foreach (var field in template.Fields)
            {
                IPropertyValue propValue = null;

                switch (field.FieldType)
                {
                case PropertyValueType.Byte:
                    propValue = new TBytePropertyValue(m_reader.ReadByte(), field.FieldName);
                    break;

                case PropertyValueType.Bool:
                    propValue = new TBoolPropertyValue(m_reader.ReadBoolean(), field.FieldName);
                    break;

                case PropertyValueType.Short:
                    propValue = new TShortPropertyValue(m_reader.ReadInt16(), field.FieldName);
                    break;

                case PropertyValueType.Int:
                    propValue = new TIntPropertyValue(m_reader.ReadInt32(), field.FieldName);
                    break;

                case PropertyValueType.Float:
                    propValue = new TFloatPropertyValue(m_reader.ReadSingle(), field.FieldName);
                    break;

                case PropertyValueType.FixedLengthString:
                case PropertyValueType.String:
                    string stringVal = (field.Length == 0) ? m_reader.ReadStringUntil('\0') : m_reader.ReadString(field.Length);
                    stringVal = stringVal.Trim(new[] { '\0' });
                    propValue = new TStringPropertyValue(stringVal, field.FieldName);
                    break;

                case PropertyValueType.Vector2:
                    propValue = new TVector2PropertyValue(new OpenTK.Vector2(m_reader.ReadSingle(), m_reader.ReadSingle()), field.FieldName);
                    break;

                case PropertyValueType.Vector3:
                    propValue = new TVector3PropertyValue(new OpenTK.Vector3(m_reader.ReadSingle(), m_reader.ReadSingle(), m_reader.ReadSingle()), field.FieldName);
                    break;

                case PropertyValueType.XRotation:
                case PropertyValueType.YRotation:
                case PropertyValueType.ZRotation:
                    propValue = new TShortPropertyValue(m_reader.ReadInt16(), field.FieldName);
                    break;

                case PropertyValueType.Color24:
                    propValue = new TLinearColorPropertyValue(new WLinearColor(m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f), field.FieldName);
                    break;

                case PropertyValueType.Color32:
                    propValue = new TLinearColorPropertyValue(new WLinearColor(m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f), field.FieldName);
                    break;

                default:
                    Console.WriteLine("Unsupported PropertyValueType: {0}", field.FieldType);
                    break;
                }

                propValue.SetUndoStack(m_world.UndoStack);
                actorProperties.Add(propValue);
            }

            // Now that we have loaded all properties out of it, we need to post-process them.
            IPropertyValue positionProperty = actorProperties.Find(x => x.Name == "Position");
            IPropertyValue xRotProperty     = actorProperties.Find(x => x.Name == "X Rotation");
            IPropertyValue yRotProperty     = actorProperties.Find(x => x.Name == "Y Rotation");
            IPropertyValue zRotProperty     = actorProperties.Find(x => x.Name == "Z Rotation");
            IPropertyValue xScaleProperty   = actorProperties.Find(x => x.Name == "X Scale");
            IPropertyValue yScaleProperty   = actorProperties.Find(x => x.Name == "Y Scale");
            IPropertyValue zScaleProperty   = actorProperties.Find(x => x.Name == "Z Scale");

            // Remove these properties from the actor so they don't get added to the UI.
            actorProperties.Remove(positionProperty);
            actorProperties.Remove(xRotProperty);
            actorProperties.Remove(yRotProperty);
            actorProperties.Remove(zRotProperty);
            actorProperties.Remove(xScaleProperty);
            actorProperties.Remove(yScaleProperty);
            actorProperties.Remove(zScaleProperty);

            if (positionProperty != null)
            {
                newActor.Transform.Position = (Vector3)positionProperty.GetValue();
            }

            float xRot = 0, yRot = 0, zRot = 0;

            if (xRotProperty != null)
            {
                xRot = WMath.RotationShortToFloat((short)xRotProperty.GetValue());
            }
            if (yRotProperty != null)
            {
                yRot = WMath.RotationShortToFloat((short)yRotProperty.GetValue());
            }
            if (zRotProperty != null)
            {
                zRot = WMath.RotationShortToFloat((short)zRotProperty.GetValue());
            }

            // Build rotation with ZYX order.
            Quaternion xRotQ = Quaternion.FromAxisAngle(new Vector3(1, 0, 0), WMath.DegreesToRadians(xRot));
            Quaternion yRotQ = Quaternion.FromAxisAngle(new Vector3(0, 1, 0), WMath.DegreesToRadians(yRot));
            Quaternion zRotQ = Quaternion.FromAxisAngle(new Vector3(0, 0, 1), WMath.DegreesToRadians(zRot));

            newActor.Transform.Rotation = zRotQ * yRotQ * xRotQ;

            float xScale = 1, yScale = 1, zScale = 1;

            if (xScaleProperty != null)
            {
                xScale = ((byte)xScaleProperty.GetValue()) / 10f;
            }
            if (yScaleProperty != null)
            {
                yScale = ((byte)yScaleProperty.GetValue()) / 10f;
            }
            if (zScaleProperty != null)
            {
                zScale = ((byte)zScaleProperty.GetValue()) / 10f;
            }

            newActor.Transform.LocalScale = new Vector3(xScale, yScale, zScale);

            newActor.Properties.AddRange(actorProperties);
            newActor.PostFinishedLoad();
            return(newActor);
        }