/// <see cref="Translator.Translate"/>
			public override void Translate( ScriptCompiler compiler, AbstractNode node )
			{
				var obj = (ObjectAbstractNode)node;

				// Find the name
				if ( obj != null )
				{
					if ( string.IsNullOrEmpty( obj.Name ) )
					{
						compiler.AddError( CompileErrorCode.ObjectNameExpected, obj.File, obj.Line );
						return;
					}
				}
				else
				{
					compiler.AddError( CompileErrorCode.ObjectNameExpected, obj.File, obj.Line );
					return;
				}

				// Allocate the particle system
				object sysObject;
				ScriptCompilerEvent evt = new CreateParticleSystemScriptCompilerEvent( obj.File, obj.Name, compiler.ResourceGroup );
				var processed = compiler._fireEvent( ref evt, out sysObject );

				if ( !processed )
				{
					this._System = ParticleSystemManager.Instance.CreateTemplate( obj.Name, compiler.ResourceGroup );
				}
				else
				{
					this._System = (ParticleSystem)sysObject;
				}

				if ( this._System == null )
				{
					compiler.AddError( CompileErrorCode.ObjectAllocationError, obj.File, obj.Line );
					return;
				}

				this._System.Origin = obj.File;
				this._System.RemoveAllEmitters();
				this._System.RemoveAllAffectors();

				obj.Context = this._System;

				foreach ( var i in obj.Children )
				{
					if ( i is PropertyAbstractNode )
					{
						var prop = (PropertyAbstractNode)i;
						switch ( (Keywords)prop.Id )
						{
							case Keywords.ID_MATERIAL:
								if ( prop.Values.Count == 0 )
								{
									compiler.AddError( CompileErrorCode.StringExpected, prop.File, prop.Line );
									return;
								}
								else
								{
									if ( prop.Values[ 0 ] is AtomAbstractNode )
									{
										var name = ( (AtomAbstractNode)prop.Values[ 0 ] ).Value;

										ScriptCompilerEvent locEvt =
											new ProcessResourceNameScriptCompilerEvent( ProcessResourceNameScriptCompilerEvent.ResourceType.Material,
											                                            name );

										compiler._fireEvent( ref locEvt );
										var locEvtName = ( (ProcessResourceNameScriptCompilerEvent)locEvt ).Name;

										if ( !this._System.SetParameter( "material", locEvtName ) )
										{
											if ( this._System.Renderer != null )
											{
												if ( !this._System.Renderer.SetParameter( "material", locEvtName ) )
												{
													compiler.AddError( CompileErrorCode.InvalidParameters, prop.File, prop.Line,
													                   "material property could not be set with material \"" + locEvtName + "\"" );
												}
											}
										}
									}
								}
								break;

							default:
								if ( prop.Values.Count == 0 )
								{
									compiler.AddError( CompileErrorCode.StringExpected, prop.File, prop.Line );
									return;
								}
								else
								{
									string name = prop.Name, value = string.Empty;

									// Glob the values together
									foreach ( var it in prop.Values )
									{
										if ( it is AtomAbstractNode )
										{
											if ( string.IsNullOrEmpty( value ) )
											{
												value = ( (AtomAbstractNode)it ).Value;
											}
											else
											{
												value = value + " " + ( (AtomAbstractNode)it ).Value;
											}
										}
										else
										{
											compiler.AddError( CompileErrorCode.InvalidParameters, prop.File, prop.Line );
											return;
										}
									}

									if ( !this._System.SetParameter( name, value ) )
									{
										if ( this._System.Renderer != null )
										{
											if ( !this._System.Renderer.SetParameter( name, value ) )
											{
												compiler.AddError( CompileErrorCode.InvalidParameters, prop.File, prop.Line );
											}
										}
									}
								}
								break;
						}
					}
					else
					{
						processNode( compiler, i );
					}
				}
			}
Пример #2
0
            /// <see cref="Translator.Translate"/>
            public override void Translate(ScriptCompiler compiler, AbstractNode node)
            {
                var obj = (ObjectAbstractNode)node;

                // Find the name
                if (obj != null)
                {
                    if (string.IsNullOrEmpty(obj.Name))
                    {
                        compiler.AddError(CompileErrorCode.ObjectNameExpected, obj.File, obj.Line);
                        return;
                    }
                }
                else
                {
                    compiler.AddError(CompileErrorCode.ObjectNameExpected, obj.File, obj.Line);
                    return;
                }

                // Allocate the particle system
                object sysObject;
                ScriptCompilerEvent evt = new CreateParticleSystemScriptCompilerEvent(obj.File, obj.Name, compiler.ResourceGroup);
                var processed           = compiler._fireEvent(ref evt, out sysObject);

                if (!processed)
                {
                    this._System = ParticleSystemManager.Instance.CreateTemplate(obj.Name, compiler.ResourceGroup);
                }
                else
                {
                    this._System = (ParticleSystem)sysObject;
                }

                if (this._System == null)
                {
                    compiler.AddError(CompileErrorCode.ObjectAllocationError, obj.File, obj.Line);
                    return;
                }

                this._System.Origin = obj.File;
                this._System.RemoveAllEmitters();
                this._System.RemoveAllAffectors();

                obj.Context = this._System;

                foreach (var i in obj.Children)
                {
                    if (i is PropertyAbstractNode)
                    {
                        var prop = (PropertyAbstractNode)i;
                        switch ((Keywords)prop.Id)
                        {
                        case Keywords.ID_MATERIAL:
                            if (prop.Values.Count == 0)
                            {
                                compiler.AddError(CompileErrorCode.StringExpected, prop.File, prop.Line);
                                return;
                            }
                            else
                            {
                                if (prop.Values[0] is AtomAbstractNode)
                                {
                                    var name = ((AtomAbstractNode)prop.Values[0]).Value;

                                    ScriptCompilerEvent locEvt =
                                        new ProcessResourceNameScriptCompilerEvent(ProcessResourceNameScriptCompilerEvent.ResourceType.Material,
                                                                                   name);

                                    compiler._fireEvent(ref locEvt);
                                    var locEvtName = ((ProcessResourceNameScriptCompilerEvent)locEvt).Name;

                                    if (!this._System.SetParameter("material", locEvtName))
                                    {
                                        if (this._System.Renderer != null)
                                        {
                                            if (!this._System.Renderer.SetParameter("material", locEvtName))
                                            {
                                                compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line,
                                                                  "material property could not be set with material \"" + locEvtName + "\"");
                                            }
                                        }
                                    }
                                }
                            }
                            break;

                        default:
                            if (prop.Values.Count == 0)
                            {
                                compiler.AddError(CompileErrorCode.StringExpected, prop.File, prop.Line);
                                return;
                            }
                            else
                            {
                                string name = prop.Name, value = string.Empty;

                                // Glob the values together
                                foreach (var it in prop.Values)
                                {
                                    if (it is AtomAbstractNode)
                                    {
                                        if (string.IsNullOrEmpty(value))
                                        {
                                            value = ((AtomAbstractNode)it).Value;
                                        }
                                        else
                                        {
                                            value = value + " " + ((AtomAbstractNode)it).Value;
                                        }
                                    }
                                    else
                                    {
                                        compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line);
                                        return;
                                    }
                                }

                                if (!this._System.SetParameter(name, value))
                                {
                                    if (this._System.Renderer != null)
                                    {
                                        if (!this._System.Renderer.SetParameter(name, value))
                                        {
                                            compiler.AddError(CompileErrorCode.InvalidParameters, prop.File, prop.Line);
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                    else
                    {
                        processNode(compiler, i);
                    }
                }
            }