Merge branch 'lua'
Conflicts: DepthsBelow/DepthsBelow/Core.cs DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj
This commit is contained in:
Executable
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup useLegacyV2RuntimeActivationPolicy="true">
|
||||
<supportedRuntime version="v4.0"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using DepthsBelow.GUI;
|
||||
using Microsoft.Xna.Framework;
|
||||
@@ -21,6 +22,8 @@ namespace DepthsBelow
|
||||
public static GraphicsDeviceManager GraphicsDeviceManager;
|
||||
SpriteBatch spriteBatch;
|
||||
|
||||
public Lua Lua;
|
||||
|
||||
public Camera Camera;
|
||||
|
||||
public bool PlayerTurn = true;
|
||||
@@ -47,6 +50,9 @@ namespace DepthsBelow
|
||||
GraphicsDeviceManager.ApplyChanges();
|
||||
|
||||
this.IsMouseVisible = true;
|
||||
|
||||
GameServices.AddService<GraphicsDevice>(GraphicsDevice);
|
||||
GameServices.AddService<ContentManager>(Content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -65,6 +71,10 @@ namespace DepthsBelow
|
||||
|
||||
TestMonster = new SmallEnemy(this, ref Swarm);
|
||||
|
||||
// Run scripts after everything is initialized
|
||||
Lua = new Lua();
|
||||
Lua.LoadScripts();
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
@@ -197,5 +207,7 @@ namespace DepthsBelow
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
static class CustomExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Load all content within a certain folder. The function
|
||||
/// returns a dictionary where the file name, without type
|
||||
/// extension, is the key and the texture object is the value.
|
||||
///
|
||||
/// The contentFolder parameter has to be relative to the
|
||||
/// game.Content.RootDirectory folder.
|
||||
///
|
||||
/// http://danielsaidi.wordpress.com/2010/01/26/xna-load-all-content-files-in-a-folder/
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The content type.</typeparam>
|
||||
/// <param name="contentManager">The content manager for which content is to be loaded.</param>
|
||||
/// <param name="contentFolder">The game project root folder relative folder path.</param>
|
||||
/// <returns>A list of loaded content objects.</returns>
|
||||
public static Dictionary<String, T> LoadContent<T>(this ContentManager contentManager, string contentFolder)
|
||||
{
|
||||
//Load directory info, abort if none
|
||||
DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "\\" + contentFolder);
|
||||
if (!dir.Exists)
|
||||
throw new DirectoryNotFoundException();
|
||||
//Init the resulting list
|
||||
Dictionary<String, T> result = new Dictionary<String, T>();
|
||||
|
||||
//Load all files that matches the file filter
|
||||
FileInfo[] files = dir.GetFiles("*.*");
|
||||
foreach (FileInfo file in files)
|
||||
{
|
||||
string key = Path.GetFileNameWithoutExtension(file.Name);
|
||||
|
||||
result[key] = contentManager.Load<T>(contentFolder + "/" + key);
|
||||
}
|
||||
//Return the result
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
<RootNamespace>DepthsBelow</RootNamespace>
|
||||
<AssemblyName>DepthsBelow</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||
<XnaFrameworkVersion>v4.0</XnaFrameworkVersion>
|
||||
<XnaPlatform>Windows</XnaPlatform>
|
||||
<XnaProfile>HiDef</XnaProfile>
|
||||
@@ -73,6 +73,10 @@
|
||||
<SignManifests>true</SignManifests>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="LuaInterface, Version=2.0.0.16708, Culture=neutral, processorArchitecture=x86">
|
||||
<HintPath>..\..\LuaInterface\LuaInterface.dll</HintPath>
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
@@ -107,22 +111,19 @@
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Core">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Net">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Camera.cs" />
|
||||
<Compile Include="CustomExtensions.cs" />
|
||||
<Compile Include="GameServices.cs" />
|
||||
<Compile Include="Interface.cs" />
|
||||
<Compile Include="Lua.cs" />
|
||||
<Compile Include="Shot.cs" />
|
||||
<Compile Include="Component\Shooting.cs" />
|
||||
<Compile Include="GUIManager.cs" />
|
||||
@@ -191,6 +192,7 @@
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="DepthsBelow_TemporaryKey.pfx" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace DepthsBelow.GUI
|
||||
@@ -86,6 +87,11 @@ namespace DepthsBelow.GUI
|
||||
GUIManager.Remove(this);
|
||||
}
|
||||
|
||||
public void SetTexture(string fileName)
|
||||
{
|
||||
Texture = GameServices.GetService<ContentManager>().Load<Texture2D>(fileName);
|
||||
}
|
||||
|
||||
public virtual void Update(GameTime gameTime)
|
||||
{
|
||||
foreach (var child in Children)
|
||||
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
/*
|
||||
* http://roy-t.nl/index.php/2010/08/25/xna-accessing-contentmanager-and-graphicsdevice-anywhere-anytime-the-gameservicecontainer/
|
||||
*/
|
||||
public static class GameServices
|
||||
{
|
||||
private static GameServiceContainer container;
|
||||
public static GameServiceContainer Instance
|
||||
{
|
||||
get { return container ?? (container = new GameServiceContainer()); }
|
||||
}
|
||||
|
||||
public static T GetService<T>()
|
||||
{
|
||||
return (T)Instance.GetService(typeof(T));
|
||||
}
|
||||
|
||||
public static void AddService<T>(T service)
|
||||
{
|
||||
Instance.AddService(typeof(T), service);
|
||||
}
|
||||
|
||||
public static void RemoveService<T>()
|
||||
{
|
||||
Instance.RemoveService(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+111
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DepthsBelow.GUI;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
// TODO: The frames created here must be disposed correctly when the Lua context is disposed
|
||||
static class LuaGUI
|
||||
{
|
||||
public static GUI.Frame CreateFrame()
|
||||
{
|
||||
return new Frame();
|
||||
}
|
||||
public static GUI.Frame CreateFrame(GUI.Frame parent)
|
||||
{
|
||||
return new GUI.Frame(parent);
|
||||
}
|
||||
|
||||
public static GUI.Button CreateButton()
|
||||
{
|
||||
return new Button();
|
||||
}
|
||||
public static GUI.Button CreateButton(GUI.Frame parent)
|
||||
{
|
||||
return new GUI.Button(parent);
|
||||
}
|
||||
}
|
||||
|
||||
public class Lua
|
||||
{
|
||||
private LuaInterface.Lua lua;
|
||||
|
||||
public Lua()
|
||||
{
|
||||
ResetContext();
|
||||
ExposeLibraries();
|
||||
|
||||
/*lua.DoString(@"
|
||||
local frame = CreateFrame('Frame');
|
||||
frame.X = 100;
|
||||
local button = CreateFrame('Button', frame);
|
||||
button:SetTexture('images/Enter');
|
||||
button.Width = 100;
|
||||
button.Height = 50;
|
||||
button.OnClick = function(clickPos)
|
||||
Console.WriteLine(clickPos.X .. ' ' .. clickPos.Y);
|
||||
end
|
||||
");*/
|
||||
}
|
||||
|
||||
public void ExposeLibraries()
|
||||
{
|
||||
lua.NewTable("Console");
|
||||
lua.RegisterFunction("Console.Write", null, typeof(System.Console).GetMethod("Write", new Type[] { typeof(string) }));
|
||||
lua.RegisterFunction("Console.WriteLine", null, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
|
||||
|
||||
// Register GUI creation functions
|
||||
lua.RegisterFunction("_CreateFrame", null, typeof(LuaGUI).GetMethod("CreateFrame", new Type[] { }));
|
||||
lua.RegisterFunction("_CreateFrameAsChild", null, typeof(LuaGUI).GetMethod("CreateFrame", new Type[] { typeof(GUI.Frame) }));
|
||||
lua.RegisterFunction("_CreateButton", null, typeof(LuaGUI).GetMethod("CreateButton", new Type[] { }));
|
||||
lua.RegisterFunction("_CreateButtonAsChild", null, typeof(LuaGUI).GetMethod("CreateButton", new Type[] { typeof(GUI.Frame) }));
|
||||
lua.NewTable("GUI");
|
||||
/*
|
||||
* Because Lua can't dynamically call overloads of registered functions, a generic Lua function needs to do the decision making.
|
||||
* This way we can also dynamically create different frame classes with the same function.
|
||||
*/
|
||||
lua.DoString(@"
|
||||
CreateFrame = function(frameType, parent)
|
||||
frameType = string.lower(frameType):gsub('^%l', string.upper)
|
||||
if (parent == nil) then
|
||||
return _G['_Create' .. frameType]()
|
||||
else
|
||||
return _G['_Create' .. frameType .. 'AsChild'](parent)
|
||||
end
|
||||
end
|
||||
");
|
||||
}
|
||||
|
||||
public void ResetContext()
|
||||
{
|
||||
if (lua != null)
|
||||
lua.Dispose();
|
||||
|
||||
lua = new LuaInterface.Lua();
|
||||
}
|
||||
|
||||
public void LoadScripts()
|
||||
{
|
||||
// Load scripts from script folder
|
||||
// HACK: This needs to load files on the fly instead of loading precompiled files by the content manager.
|
||||
// http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_2 maybe?
|
||||
var scripts = GameServices.GetService<ContentManager>().LoadContent<string>("scripts");
|
||||
foreach (var script in scripts)
|
||||
{
|
||||
try
|
||||
{
|
||||
lua.DoString(script.Value);
|
||||
}
|
||||
catch (LuaInterface.LuaException e)
|
||||
{
|
||||
Console.WriteLine("Lua error: " + e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Regular → Executable
+11
@@ -118,6 +118,7 @@
|
||||
<Name>Arial</Name>
|
||||
<Importer>FontDescriptionImporter</Importer>
|
||||
<Processor>FontDescriptionProcessor</Processor>
|
||||
<SubType>Designer</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -134,6 +135,16 @@
|
||||
<Processor>TextureProcessor</Processor>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="scripts\gui.lua">
|
||||
<Name>gui</Name>
|
||||
<Importer>LuaImporter</Importer>
|
||||
</Compile>
|
||||
<Compile Include="scripts\test.lua">
|
||||
<Name>test</Name>
|
||||
<Importer>LuaImporter</Importer>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
local frame = CreateFrame('Frame');
|
||||
frame.X = 100;
|
||||
|
||||
local button = CreateFrame('Button', frame);
|
||||
button:SetTexture('images/Enter');
|
||||
button.Width = 100;
|
||||
button.Height = 50;
|
||||
button.OnClick = function(clickPos)
|
||||
Console.WriteLine(clickPos.X .. ' ' .. clickPos.Y);
|
||||
end
|
||||
+1
@@ -0,0 +1 @@
|
||||
Console.WriteLine("Test.lua loaded");
|
||||
@@ -27,6 +27,7 @@
|
||||
<Private>False</Private>
|
||||
<SpecificVersion>True</SpecificVersion>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=MSIL" />
|
||||
<Reference Include="Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
<SpecificVersion>True</SpecificVersion>
|
||||
@@ -49,6 +50,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="LuaImporter.cs" />
|
||||
<Compile Include="MapProcessor.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Content.Pipeline;
|
||||
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
|
||||
|
||||
// TODO: replace this with the type you want to import.
|
||||
using TImport = System.String;
|
||||
|
||||
namespace DepthsBelowContentPipeline
|
||||
{
|
||||
/// <summary>
|
||||
/// This class will be instantiated by the XNA Framework Content Pipeline
|
||||
/// to import a file from disk into the specified type, TImport.
|
||||
///
|
||||
/// This should be part of a Content Pipeline Extension Library project.
|
||||
///
|
||||
/// TODO: change the ContentImporter attribute to specify the correct file
|
||||
/// extension, display name, and default processor for this importer.
|
||||
/// </summary>
|
||||
[ContentImporter(".lua", DisplayName = "LUA Importer", DefaultProcessor = "LuaProcessor")]
|
||||
public class LuaImporter : ContentImporter<TImport>
|
||||
{
|
||||
public override TImport Import(string filename, ContentImporterContext context)
|
||||
{
|
||||
return System.IO.File.ReadAllText(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user