/** * @brief Implementation of the new_root_entity_cmd_ and new_entity_cmd_ commands * @param[in] obj (WorldObject) The entity to add to the hierarchy. * @param[in] parent (sulphur.editor.WorldObject) The parameter passed to the * command. This parameter will become the parent for the newly created * entity. * @remarks Passing null for the parent will parent the entity to the root. * @remarks This function is mainly used to construct an entity before adding it to the hierarchy. this way all components can be added to the entity up front. */ public void AddEntity(WorldObject obj, WorldObject parent) { WorldObject new_object = hierarchy.Add(obj, parent); OnNotification handler = notify_subscribers_; NotificationEventArgs args = new NotificationEventArgs( new_object, (uint)Notifications.kEntityAdded, id <WorldHierarchy> .type_id_); handler?.Invoke(this, args); // Notify the engine native.messages.EntityCreatedMessage msg = new native.messages.EntityCreatedMessage(); msg.entity_index = (UInt64)new_object.GetIndex(); msg.sibling_index = (UInt64)new_object.GetSiblingIndex(); msg.parent_index = parent != null ? (UInt64)parent.GetIndex() : UInt64.MaxValue; msg.position = new float[3] { 0.0f, 0.0f, 0.0f }; msg.rotation = new float[4] { 0.0f, 0.0f, 0.0f, 0.0f }; msg.scale = new float[3] { 1.0f, 1.0f, 1.0f }; byte[] data = Utils.StructToBytes(msg); native.Networking.SNetSendData( (uint)native.NetworkMessages.kEntityCreated, data, (uint)data.Length); }
/** * @brief Loads a '*.swo' file from disk and fils the hierarchy with its contents. * @param[in] path (string) The path to the *.swo file. */ public void LoadFromDisk(string path) { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new HierarchyConverter()); serializer.Formatting = Formatting.Indented; List <WorldObject> objects = null; using (StreamReader sw = new StreamReader(path)) using (JsonReader reader = new JsonTextReader(sw)) { objects = serializer.Deserialize <List <WorldObject> >(reader); } if (objects == null) { is_dirty = false; return; } foreach (WorldObject obj in objects) { obj.is_dirty = false; Add(obj, obj.parent); // temp code untill engine side serialization is done native.messages.EntityCreatedMessage msg = new native.messages.EntityCreatedMessage(); msg.entity_index = (UInt64)obj.GetIndex(); msg.sibling_index = (UInt64)obj.GetSiblingIndex(); msg.parent_index = obj.parent != null ? (UInt64)obj.parent.GetIndex() : UInt64.MaxValue; msg.position = new float[3]; msg.rotation = new float[4]; msg.scale = new float[3]; msg.position[0] = obj.transform_.position_.X; msg.position[1] = obj.transform_.position_.Y; msg.position[2] = obj.transform_.position_.Z; msg.rotation[0] = obj.transform_.rotation_.X; msg.rotation[1] = obj.transform_.rotation_.Y; msg.rotation[2] = obj.transform_.rotation_.Z; msg.rotation[3] = obj.transform_.rotation_.W; msg.scale[0] = obj.transform_.scale_.X; msg.scale[1] = obj.transform_.scale_.Y; msg.scale[2] = obj.transform_.scale_.Z; byte[] data = Utils.StructToBytes(msg); native.Networking.SNetSendData( (uint)native.NetworkMessages.kEntityCreated, data, (uint)data.Length); // end temp code } is_dirty = false; }