예제 #1
0
        public void CreateEntity(string fourCC)
        {
            if (m_world.Map == null || m_world.Map.FocusedScene == null)
            {
                return;
            }

            var actorDescriptors = new List <MapActorDescriptor>();

            foreach (var file in Directory.GetFiles("resources/templates/"))
            {
                MapActorDescriptor descriptor = JsonConvert.DeserializeObject <MapActorDescriptor>(File.ReadAllText(file));
                actorDescriptors.Add(descriptor);
            }

            MapActorDescriptor entityDescriptor = actorDescriptors.Find(x => string.Compare(x.FourCC, fourCC, true) == 0);

            if (entityDescriptor == null)
            {
                Console.WriteLine("Attempted to spawn unsupported FourCC: {0}", fourCC);
                return;
            }

            List <IPropertyValue> actorProperties = new List <IPropertyValue>();

            foreach (var field in entityDescriptor.Fields)
            {
                switch (field.FieldName)
                {
                case "Position":
                case "X Rotation":
                case "Y Rotation":
                case "Z Rotation":
                case "X Scale":
                case "Y Scale":
                case "Z Scale":
                    continue;
                }

                IPropertyValue propValue = null;
                switch (field.FieldType)
                {
                case PropertyValueType.Byte:
                    propValue = new TBytePropertyValue(0, field.FieldName);
                    break;

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

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

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

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

                case PropertyValueType.FixedLengthString:
                case PropertyValueType.String:
                    propValue = new TStringPropertyValue("", field.FieldName);
                    break;

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

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

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

                case PropertyValueType.Color24:
                    propValue = new TLinearColorPropertyValue(new WLinearColor(1f, 1f, 1f), field.FieldName);
                    break;

                case PropertyValueType.Color32:
                    propValue = new TLinearColorPropertyValue(new WLinearColor(1f, 1f, 1f, 1f), field.FieldName);
                    break;

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

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

            var newActor = new WActorNode(fourCC, m_world);

            newActor.Transform.Position = new OpenTK.Vector3(0, 200, 0);

            newActor.SetParent(m_world.Map.FocusedScene);
            newActor.Properties.AddRange(actorProperties);
            newActor.PostFinishedLoad();

            ModifySelection(SelectionType.Add, newActor, true);
        }
예제 #2
0
        private void WriteActorToChunk(WActorNode actor, MapActorDescriptor template, EndianBinaryWriter writer)
        {
            // Just convert their rotation to Euler Angles now instead of doing it in parts later.
            Vector3 eulerRot = actor.Transform.Rotation.ToEulerAngles();

            foreach (var field in template.Fields)
            {
                IPropertyValue propValue = actor.Properties.Find(x => x.Name == field.FieldName);
                if (field.FieldName == "Position")
                {
                    propValue = new TVector3PropertyValue(actor.Transform.Position, "Position");
                }
                else if (field.FieldName == "X Rotation")
                {
                    short xRotShort = WMath.RotationFloatToShort(eulerRot.X);
                    propValue = new TShortPropertyValue(xRotShort, "X Rotation");
                }
                else if (field.FieldName == "Y Rotation")
                {
                    short yRotShort = WMath.RotationFloatToShort(eulerRot.Y);
                    propValue = new TShortPropertyValue(yRotShort, "Y Rotation");
                }
                else if (field.FieldName == "Z Rotation")
                {
                    short zRotShort = WMath.RotationFloatToShort(eulerRot.Z);
                    propValue = new TShortPropertyValue(zRotShort, "Z Rotation");
                }
                else if (field.FieldName == "X Scale")
                {
                    float xScale = actor.Transform.LocalScale.X;
                    propValue = new TBytePropertyValue((byte)(xScale * 10), "X Scale");
                }
                else if (field.FieldName == "Y Scale")
                {
                    float yScale = actor.Transform.LocalScale.Y;
                    propValue = new TBytePropertyValue((byte)(yScale * 10), "Y Scale");
                }
                else if (field.FieldName == "Z Scale")
                {
                    float zScale = actor.Transform.LocalScale.Z;
                    propValue = new TBytePropertyValue((byte)(zScale * 10), "Z Scale");
                }

                switch (field.FieldType)
                {
                case PropertyValueType.Byte:
                    writer.Write((byte)propValue.GetValue()); break;

                case PropertyValueType.Bool:
                    writer.Write((bool)propValue.GetValue()); break;

                case PropertyValueType.Short:
                    writer.Write((short)propValue.GetValue()); break;

                case PropertyValueType.Int:
                    writer.Write((int)propValue.GetValue()); break;

                case PropertyValueType.Float:
                    writer.Write((float)propValue.GetValue()); break;

                case PropertyValueType.String:
                    writer.Write(System.Text.Encoding.ASCII.GetBytes((string)propValue.GetValue())); break;

                case PropertyValueType.FixedLengthString:
                    string fixedLenStr = (string)propValue.GetValue();
                    for (int i = 0; i < field.Length; i++)
                    {
                        writer.Write(i < fixedLenStr.Length ? (byte)fixedLenStr[i] : (byte)0);
                    }
                    //writer.WriteFixedString((string)propValue.GetValue(), (int)field.Length); break;
                    break;

                case PropertyValueType.Vector2:
                    Vector2 vec2Val = (Vector2)propValue.GetValue();
                    writer.Write(vec2Val.X);
                    writer.Write(vec2Val.Y);
                    break;

                case PropertyValueType.Vector3:
                    Vector3 vec3Val = (Vector3)propValue.GetValue();
                    writer.Write(vec3Val.X);
                    writer.Write(vec3Val.Y);
                    writer.Write(vec3Val.Z);
                    break;

                case PropertyValueType.XRotation:
                case PropertyValueType.YRotation:
                case PropertyValueType.ZRotation:
                    writer.Write((short)propValue.GetValue()); break;

                case PropertyValueType.Color24:
                    WLinearColor color24 = (WLinearColor)propValue.GetValue();
                    writer.Write((byte)color24.R);
                    writer.Write((byte)color24.G);
                    writer.Write((byte)color24.B);
                    break;

                case PropertyValueType.Color32:
                    WLinearColor color32 = (WLinearColor)propValue.GetValue();
                    writer.Write((byte)color32.R);
                    writer.Write((byte)color32.G);
                    writer.Write((byte)color32.B);
                    writer.Write((byte)color32.A);
                    break;

                default:
                    Console.WriteLine("Unsupported PropertyValueType: {0}", field.FieldType);
                    break;
                }
            }
        }
예제 #3
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);
        }