Merge branch 'lua'

Conflicts:
	DepthsBelow/DepthsBelow/Core.cs
	DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj
This commit is contained in:
Simon Holmberg
2012-10-17 01:55:30 +02:00
14 changed files with 626 additions and 352 deletions
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
+12
View File
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using DepthsBelow.GUI; using DepthsBelow.GUI;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
@@ -21,6 +22,8 @@ namespace DepthsBelow
public static GraphicsDeviceManager GraphicsDeviceManager; public static GraphicsDeviceManager GraphicsDeviceManager;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
public Lua Lua;
public Camera Camera; public Camera Camera;
public bool PlayerTurn = true; public bool PlayerTurn = true;
@@ -47,6 +50,9 @@ namespace DepthsBelow
GraphicsDeviceManager.ApplyChanges(); GraphicsDeviceManager.ApplyChanges();
this.IsMouseVisible = true; this.IsMouseVisible = true;
GameServices.AddService<GraphicsDevice>(GraphicsDevice);
GameServices.AddService<ContentManager>(Content);
} }
/// <summary> /// <summary>
@@ -65,6 +71,10 @@ namespace DepthsBelow
TestMonster = new SmallEnemy(this, ref Swarm); TestMonster = new SmallEnemy(this, ref Swarm);
// Run scripts after everything is initialized
Lua = new Lua();
Lua.LoadScripts();
base.Initialize(); base.Initialize();
} }
@@ -197,5 +207,7 @@ namespace DepthsBelow
base.Draw(gameTime); base.Draw(gameTime);
} }
} }
} }
+47
View File
@@ -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;
}
}
}
+9 -7
View File
@@ -10,7 +10,7 @@
<RootNamespace>DepthsBelow</RootNamespace> <RootNamespace>DepthsBelow</RootNamespace>
<AssemblyName>DepthsBelow</AssemblyName> <AssemblyName>DepthsBelow</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile> <TargetFrameworkProfile>Client</TargetFrameworkProfile>
<XnaFrameworkVersion>v4.0</XnaFrameworkVersion> <XnaFrameworkVersion>v4.0</XnaFrameworkVersion>
<XnaPlatform>Windows</XnaPlatform> <XnaPlatform>Windows</XnaPlatform>
<XnaProfile>HiDef</XnaProfile> <XnaProfile>HiDef</XnaProfile>
@@ -73,6 +73,10 @@
<SignManifests>true</SignManifests> <SignManifests>true</SignManifests>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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"> <Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
@@ -107,22 +111,19 @@
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Xml">
<Private>False</Private>
</Reference>
<Reference Include="System.Core"> <Reference Include="System.Core">
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="System.Xml.Linq">
<Private>False</Private>
</Reference>
<Reference Include="System.Net"> <Reference Include="System.Net">
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Camera.cs" /> <Compile Include="Camera.cs" />
<Compile Include="CustomExtensions.cs" />
<Compile Include="GameServices.cs" />
<Compile Include="Interface.cs" /> <Compile Include="Interface.cs" />
<Compile Include="Lua.cs" />
<Compile Include="Shot.cs" /> <Compile Include="Shot.cs" />
<Compile Include="Component\Shooting.cs" /> <Compile Include="Component\Shooting.cs" />
<Compile Include="GUIManager.cs" /> <Compile Include="GUIManager.cs" />
@@ -191,6 +192,7 @@
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" />
<None Include="DepthsBelow_TemporaryKey.pfx" /> <None Include="DepthsBelow_TemporaryKey.pfx" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
+6
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow.GUI namespace DepthsBelow.GUI
@@ -86,6 +87,11 @@ namespace DepthsBelow.GUI
GUIManager.Remove(this); GUIManager.Remove(this);
} }
public void SetTexture(string fileName)
{
Texture = GameServices.GetService<ContentManager>().Load<Texture2D>(fileName);
}
public virtual void Update(GameTime gameTime) public virtual void Update(GameTime gameTime)
{ {
foreach (var child in Children) foreach (var child in Children)
+35
View File
@@ -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));
}
}
}
+111
View File
@@ -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);
}
}
}
}
}
+11
View File
@@ -118,6 +118,7 @@
<Name>Arial</Name> <Name>Arial</Name>
<Importer>FontDescriptionImporter</Importer> <Importer>FontDescriptionImporter</Importer>
<Processor>FontDescriptionProcessor</Processor> <Processor>FontDescriptionProcessor</Processor>
<SubType>Designer</SubType>
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -134,6 +135,16 @@
<Processor>TextureProcessor</Processor> <Processor>TextureProcessor</Processor>
</Compile> </Compile>
</ItemGroup> </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" /> <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. <!-- 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. Other similar extension points exist, see Microsoft.Common.targets.
+10
View File
@@ -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
View File
@@ -0,0 +1 @@
Console.WriteLine("Test.lua loaded");
@@ -27,6 +27,7 @@
<Private>False</Private> <Private>False</Private>
<SpecificVersion>True</SpecificVersion> <SpecificVersion>True</SpecificVersion>
</Reference> </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"> <Reference Include="Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
<Private>False</Private> <Private>False</Private>
<SpecificVersion>True</SpecificVersion> <SpecificVersion>True</SpecificVersion>
@@ -49,6 +50,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="LuaImporter.cs" />
<Compile Include="MapProcessor.cs" /> <Compile Include="MapProcessor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
+31
View File
@@ -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);
}
}
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.