コード例 #1
0
ファイル: OverlayMap.cs プロジェクト: walru5/ProjectPorcupine
    /// <summary>
    /// Grabs references, sets a dummy size and evaluation function.
    /// </summary>
    private void Start()
    {
        // Grab references.
        meshRenderer = GetComponent <MeshRenderer>();
        meshFilter   = GetComponent <MeshFilter>();

        // Read xml prototypes.
        overlays = OverlayDescriptor.ReadPrototypes(xmlFileName);

        // Read LUA.
        UserData.RegisterAssembly();
        string scriptFile = System.IO.Path.Combine(
            UnityEngine.Application.streamingAssetsPath,
            System.IO.Path.Combine("Overlay", LUAFileName));
        string scriptTxt = System.IO.File.ReadAllText(scriptFile);

        script = new Script();
        script.DoString(scriptTxt);

        // Build GUI.
        CreateGUI();

        // TODO: remove this dummy set size.
        SetSize(100, 100);
        SetOverlay("None");
    }
コード例 #2
0
    /// <summary>
    /// Creates an OverlayDescriptor form a xml subtree with node \<Overlay></Overlay>\
    /// </summary>
    /// <param name="xmlReader">The subtree pointing to Overlay</param>
    /// <returns></returns>
    static OverlayDescriptor ReadFromXml(XmlReader xmlReader)
    {
        xmlReader.Read();
        Debug.Assert(xmlReader.Name == "Overlay");
        OverlayDescriptor ret = new OverlayDescriptor();

        ret.id = xmlReader.GetAttribute("id");
        if (xmlReader.GetAttribute("min") != null)
        {
            ret.min = XmlConvert.ToInt32(xmlReader.GetAttribute("min"));
        }
        if (xmlReader.GetAttribute("max") != null)
        {
            ret.max = XmlConvert.ToInt32(xmlReader.GetAttribute("max"));
        }
        if (xmlReader.GetAttribute("color_map") != null)
        {
            try
            {
                ret.colorMap = (ColorMap)System.Enum.Parse(typeof(ColorMap), xmlReader.GetAttribute("color_map"));
            }
            catch (ArgumentException e) {
                Debug.LogWarning("Invalid color map!");
            }
        }
        xmlReader.Read();
        ret.luaFunctionName = xmlReader.ReadContentAsString();
        //Debug.Log(string.Format("Reading overlay prototype with id {0} and LUA function {1}", ret.id, ret.luaFunctionName));
        return(ret);
    }
コード例 #3
0
    /// <summary>
    /// Creates an OverlayDescriptor form a xml subtree with node \<Overlay></Overlay>\.
    /// </summary>
    /// <param name="xmlReader">The subtree pointing to Overlay.</param>
    /// <returns></returns>
    private static OverlayDescriptor ReadFromXml(XmlReader xmlReader)
    {
        xmlReader.Read();
        Debug.Assert(xmlReader.Name == "Overlay", "xmlReader.Name == 'Overlay'");
        OverlayDescriptor ret = new OverlayDescriptor();

        ret.id = xmlReader.GetAttribute("id");
        if (xmlReader.GetAttribute("min") != null)
        {
            ret.min = XmlConvert.ToInt32(xmlReader.GetAttribute("min"));
        }

        if (xmlReader.GetAttribute("max") != null)
        {
            ret.max = XmlConvert.ToInt32(xmlReader.GetAttribute("max"));
        }

        if (xmlReader.GetAttribute("color_map") != null)
        {
            try
            {
                ret.colorMap = (ColorMap)System.Enum.Parse(typeof(ColorMap), xmlReader.GetAttribute("color_map"));
            }
            catch (ArgumentException e)
            {
                Debug.ULogErrorChannel("OverlayMap", "Invalid color map!", e);
            }
        }

        xmlReader.Read();
        ret.luaFunctionName = xmlReader.ReadContentAsString();
        return(ret);
    }
コード例 #4
0
    /// <summary>
    /// Set overlay to display perototype with name "name".
    /// </summary>
    /// <param name="name">Name of overlay prototype.</param>
    public void SetOverlay(string name)
    {
        if (name == "None")
        {
            meshRenderer.enabled = false;
            currentOverlay       = name;
            HideGUITooltip();
            return;
        }
        else if (PrototypeManager.Overlay.Has(name))
        {
            meshRenderer.enabled = true;
            currentOverlay       = name;
            OverlayDescriptor descr = PrototypeManager.Overlay.Get(name);

            if (FunctionsManager.Overlay.HasFunction(descr.LuaFunctionName) == false)
            {
                UnityDebugger.Debugger.LogError("OverlayMap", string.Format("Couldn't find a function named '{0}' in Overlay functions", descr.LuaFunctionName));
                return;
            }

            bool loggedOnce = false;
            valueAt = (x, y, z) =>
            {
                if (WorldController.Instance == null)
                {
                    return(0);
                }

                Tile tile = WorldController.Instance.GetTileAtWorldCoord(new Vector3(x, y, z));

                DynValue result = FunctionsManager.Overlay.Call(descr.LuaFunctionName, new object[] { tile, World.Current });
                double?  value  = result.CastToNumber();
                if (value == null)
                {
                    if (loggedOnce == false)
                    {
                        UnityDebugger.Debugger.LogError("OverlayMap", string.Format("The return value from the function named '{0}' was null for tile at ({1}, {2}, {3})", descr.LuaFunctionName, x, y, z));
                        loggedOnce = true;
                    }

                    return(0);
                }

                return((int)value);
            };

            ColorMapSG = descr.ColorMap;
            Bake();
            ShowGUITooltip();
        }
        else
        {
            UnityDebugger.Debugger.LogWarning("OverlayMap", string.Format("Overlay with name {0} not found in prototypes", name));
        }
    }
コード例 #5
0
    /// <summary>
    /// Set overlay to display perototype with name "name".
    /// </summary>
    /// <param name="name">Name of overlay prototype.</param>
    public void SetOverlay(string name)
    {
        if (name == "None")
        {
            meshRenderer.enabled = false;
            currentOverlay       = name;
            HideGUITooltip();
            return;
        }
        else if (overlays.ContainsKey(name))
        {
            meshRenderer.enabled = true;
            currentOverlay       = name;
            OverlayDescriptor descr  = overlays[name];
            object            handle = script.Globals[descr.luaFunctionName];
            if (handle == null)
            {
                Debug.ULogErrorChannel("OverlayMap", string.Format("Couldn't find a function named '{0}' in '{1}'", descr.luaFunctionName));
                return;
            }

            valueAt = (x, y) =>
            {
                if (WorldController.Instance == null)
                {
                    return(0);
                }

                Tile tile = WorldController.Instance.GetTileAtWorldCoord(new Vector3(x, y, 0));
                return((int)script.Call(handle, new object[] { tile, World.Current }).ToScalar().CastToNumber());
            };

            ColorMapSG = descr.colorMap;
            Bake();
            ShowGUITooltip();
        }
        else
        {
            Debug.ULogWarningChannel("OverlayMap", string.Format("Overlay with name {0} not found in prototypes", name));
        }
    }