diff --git a/Objects/Dynamics/Player.cs b/Objects/Dynamics/PlayerObject.cs similarity index 62% rename from Objects/Dynamics/Player.cs rename to Objects/Dynamics/PlayerObject.cs index f042584..a04295d 100644 --- a/Objects/Dynamics/Player.cs +++ b/Objects/Dynamics/PlayerObject.cs @@ -10,7 +10,7 @@ public enum Direction Up=0, Down=1, Left=2, Right=3 }; - public class Player : DynamicRectObject + public class PlayerObject : DynamicRectObject { protected Point SightAreaSize; public Direction SightDirection; @@ -19,14 +19,15 @@ public virtual Rectangle SightArea { get { + Rectangle c = Collider; switch (SightDirection) { case Direction.Up: { return new Rectangle ( - Collider.X + Collider.Width / 2 - SightAreaSize.X, - Collider.Y - SightAreaSize.Y, + c.X + c.Width / 2 - SightAreaSize.X, + c.Y - SightAreaSize.Y, SightAreaSize.X, SightAreaSize.Y ); @@ -35,8 +36,8 @@ public virtual Rectangle SightArea { return new Rectangle ( - Collider.X + Collider.Width / 2 - SightAreaSize.X, - Collider.Y + Collider.Height + SightAreaSize.Y, + c.X + c.Width / 2 - SightAreaSize.X, + c.Y + c.Height + SightAreaSize.Y, SightAreaSize.X, SightAreaSize.Y ); @@ -45,28 +46,28 @@ public virtual Rectangle SightArea { return new Rectangle ( - Collider.X - SightArea.X, - Collider.Y + Collider.Height / 2 - SightAreaSize.Y, - SightAreaSize.X, - SightAreaSize.Y + c.X - SightAreaSize.X, + c.Y + c.Height / 2 - SightAreaSize.Y, + SightAreaSize.Y, + SightAreaSize.X ); } case Direction.Right: { return new Rectangle ( - Collider.X + Collider.Width + SightArea.X, - Collider.Y + Collider.Height / 2 - SightAreaSize.Y, - SightAreaSize.X, - SightAreaSize.Y + c.X + c.Width + SightAreaSize.X, + c.Y + c.Height / 2 - SightAreaSize.Y, + SightAreaSize.Y, + SightAreaSize.X ); } default: { return new Rectangle ( - Collider.X + Collider.Width / 2 - SightAreaSize.X / 2, - Collider.Y + Collider.Height / 2 - SightAreaSize.Y / 2, + c.X + c.Width / 2 - SightAreaSize.X / 2, + c.Y + c.Height / 2 - SightAreaSize.Y / 2, SightAreaSize.X, SightAreaSize.Y ); @@ -75,7 +76,7 @@ public virtual Rectangle SightArea } } - public Player(Point StartLocation, Point colliderSize, Single mass, Point sightAreaSize) : base(StartLocation, colliderSize, mass, false) + public PlayerObject(Point StartLocation, Point colliderSize, Single mass, Point sightAreaSize) : base(StartLocation, colliderSize, mass, false) { SightAreaSize = sightAreaSize; } diff --git a/Scripting/LuaModule.cs b/Scripting/LuaModule.cs index c076911..fb27d99 100644 --- a/Scripting/LuaModule.cs +++ b/Scripting/LuaModule.cs @@ -12,7 +12,7 @@ public class LuaModule public Lua Runtime; public Boolean IsUp = true; public Int32 EventID; - public readonly Int32 EventAmount; + public readonly Double EventAmount; public LuaModule(String initFile, Int32 eventAmount) { @@ -26,7 +26,7 @@ public LuaModule(String initFile) Runtime = new Lua(); Runtime.LoadCLRPackage(); Runtime.DoString(initFile); - EventAmount = (Int32) Runtime["event_count"]; + EventAmount = (Double) Runtime["event_count"]; } public void Update(Single elapsedTime, Single totalTime) diff --git a/Scripting/Pipeline/ScriptImporter.cs b/Scripting/Pipeline/ScriptImporter.cs index 867ae73..1070e46 100644 --- a/Scripting/Pipeline/ScriptImporter.cs +++ b/Scripting/Pipeline/ScriptImporter.cs @@ -2,16 +2,14 @@ using Scripting = DotRPG.Scripting; using System.IO; -using TImport = System.String; - namespace DotRPG.Scripting.Pipeline { [ContentImporter(".lua", DisplayName = "DotRPG embeddable script handler", DefaultProcessor = "ScriptProcessor")] - public class ScriptImporter : ContentImporter + public class ScriptImporter : ContentImporter { - public override TImport Import(string filename, ContentImporterContext context) + public override LuaModule Import(string filename, ContentImporterContext context) { - return File.ReadAllText(filename); + return new Scripting::LuaModule(File.ReadAllText(filename)); } } } diff --git a/Scripting/Pipeline/ScriptProcessor.cs b/Scripting/Pipeline/ScriptProcessor.cs index e375fba..9a549bb 100644 --- a/Scripting/Pipeline/ScriptProcessor.cs +++ b/Scripting/Pipeline/ScriptProcessor.cs @@ -1,6 +1,6 @@ using Microsoft.Xna.Framework.Content.Pipeline; -using TInput = System.String; +using TInput = DotRPG.Scripting.LuaModule; using TOutput = DotRPG.Scripting.LuaModule; namespace ScriptImporter @@ -10,7 +10,7 @@ class ScriptProcessor : ContentProcessor { public override TOutput Process(TInput input, ContentProcessorContext context) { - return new TOutput(input); + return input; } } } diff --git a/Scripting/SceneSwitchSet.cs b/Scripting/SceneSwitchSet.cs new file mode 100644 index 0000000..5b3beea --- /dev/null +++ b/Scripting/SceneSwitchSet.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotRPG.Scripting +{ + public class SceneSwitchSet + { + public Boolean AutoScroll = false; + public Boolean ExitDialog = false; + } +} diff --git a/_Example/DotRPG.Example.csproj b/_Example/DotRPG.Example.csproj index c937658..ed002ac 100644 --- a/_Example/DotRPG.Example.csproj +++ b/_Example/DotRPG.Example.csproj @@ -31,5 +31,6 @@ + \ No newline at end of file diff --git a/_Example/Game1.cs b/_Example/Game1.cs index c82404c..7c45527 100644 --- a/_Example/Game1.cs +++ b/_Example/Game1.cs @@ -86,7 +86,7 @@ public static void SetFrameNumber(Object sender, EventArgs e, GameTime gameTime) private void StartScroll(Object sender, EventArgs e, GameTime gameTime) { - ActiveFrame = Frames[1]; + ActiveFrame = Frames[2]; ActiveFrame.LoadContent(); } @@ -104,6 +104,8 @@ protected override void Initialize() Frames[0].Initialize(); Frames.Add(new DynamicsTestFrame(this, ResourceHGlobal, LogicEventSet)); Frames[1].Initialize(); + Frames.Add(new ScriptTest(this, ResourceHGlobal, LogicEventSet)); + Frames[2].Initialize(); base.Initialize(); } diff --git a/_Example/GameData/DotRPG.Example_data.mgcb b/_Example/GameData/DotRPG.Example_data.mgcb index 3e75ae6..239d975 100644 --- a/_Example/GameData/DotRPG.Example_data.mgcb +++ b/_Example/GameData/DotRPG.Example_data.mgcb @@ -27,6 +27,9 @@ /processorParam:TextureFormat=Compressed /build:Fonts/MainFont_Large.spritefont +#begin Scripts/dialog.lua +/copy:Scripts/dialog.lua + #begin Sounds/clickText.wav /importer:WavImporter /processor:SoundEffectProcessor diff --git a/_Example/GameData/Scripts/dialog.lua b/_Example/GameData/Scripts/dialog.lua new file mode 100644 index 0000000..7732ee6 --- /dev/null +++ b/_Example/GameData/Scripts/dialog.lua @@ -0,0 +1,27 @@ +import ('DotRPG.Scripting', 'DotRPG.Objects') + +event_count = 7 + +function loop(eventID, frameTime, totalTime) + if eventID == 0 then + dialog.Text = "You ran across white rectangular object.\nSo what?" + elseif eventID == 1 then + dialog.Text = "You should better go elsewhere." + scene.ExitDialog = true + elseif eventID == 2 then + dialog.Text = "A white rectangular object." + scene.ExitDialog = true + elseif eventID == 3 then + dialog.Text = "You've never seen object that is more white\nand rectangular than this one." + scene.ExitDialog = true + elseif eventID == 4 then + dialog.Text = "Don't you have better things to do?" + scene.ExitDialog = true + elseif eventID == 5 then + dialog.Text = "You ran across white rect" + scene.AutoScroll = true + elseif eventID == 6 then + dialog.Text = "COME ON, QUIT IT ALREADY!" + scene.ExitDialog = true + end +end \ No newline at end of file diff --git a/_Example/ScriptTest.cs b/_Example/ScriptTest.cs new file mode 100644 index 0000000..ab10a61 --- /dev/null +++ b/_Example/ScriptTest.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Xna.Framework; +using DotRPG.Objects; +using DotRPG.Objects.Dynamics; +using DotRPG.Scripting; +using Microsoft.Xna.Framework.Graphics; + +namespace DotRPG._Example +{ + class ScriptTest : Frame + { + LuaModule DialogTest1; + TextObject DialogForm; + SceneSwitchSet SceneSwitches = new SceneSwitchSet(); + PlayerObject Player; + DynamicRectObject dro; + Boolean[] lastInputCollection = new bool[8]; + Boolean ShowingText; + + public override int FrameID + { + get + { + return 2; + } + } + + public ScriptTest(Game owner, ResourceHeap globalGameResources, HashSet globalEventSet) : base(owner, globalGameResources, globalEventSet) + { + + } + + public override void Initialize() + { + + } + + public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, Rectangle drawZone) + { + Player.Draw(spriteBatch, gameTime, 540, new Point(0, 0), new Point(drawZone.Width, drawZone.Height)); + dro.Draw(spriteBatch, gameTime, 540, new Point(0, 0), new Point(drawZone.Width, drawZone.Height)); + if (ShowingText) + { + DialogForm.Draw(spriteBatch, Owner.Window); + } +#if DEBUG + spriteBatch.DrawString(FrameResources.Global.Fonts["vcr"], String.Format("Sight: {0}, Z key: {1}", Player.SightArea, lastInputCollection[4]), new Vector2(0, 12), Color.White); +#endif + } + + public override void Update(GameTime gameTime, bool[] controls) + { + Single loco_x = 0.0f; Single loco_y = 0.0f; + if (controls[0]) { loco_y -= 1.0f; } + if (controls[1]) { loco_y += 1.0f; } + if (controls[2]) { loco_x -= 1.0f; } + if (controls[3]) { loco_x += 1.0f; } + Vector2 Locomotion = new Vector2(loco_x, loco_y); + if (controls[0] && !(controls[1] || controls[2] || controls[3])) + { + Player.SightDirection = Direction.Up; + } + if (controls[1] && !(controls[0] || controls[2] || controls[3])) + { + Player.SightDirection = Direction.Down; + } + if (controls[2] && !(controls[1] || controls[0] || controls[3])) + { + Player.SightDirection = Direction.Left; + } + if (controls[3] && !(controls[1] || controls[0] || controls[2])) + { + Player.SightDirection = Direction.Right; + } + Locomotion /= (Locomotion.Length() != 0 ? Locomotion.Length() : 1.0f); + Locomotion *= 0.1f; + if (ShowingText) + { + Locomotion = Vector2.Zero; + } + Player.Velocity = Locomotion; + Player.TryCollideWith(dro); + if (controls[4] && !lastInputCollection[4] || (ShowingText && DialogForm.ReachedEnd && SceneSwitches.AutoScroll)) + { + if (Player.SightArea.Intersects(dro.Collider) && !ShowingText) + { + SceneSwitches.AutoScroll = false; + SceneSwitches.ExitDialog = false; + ShowingText = true; + DialogTest1.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds, (float)gameTime.TotalGameTime.TotalMilliseconds); + DialogForm.ResetToStart(); + } + else if (DialogForm.ReachedEnd) + { + if (SceneSwitches.ExitDialog) + { + ShowingText = false; + } + else + { + SceneSwitches.AutoScroll = false; + SceneSwitches.ExitDialog = false; + DialogTest1.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds, (float)gameTime.TotalGameTime.TotalMilliseconds); + DialogForm.ResetToStart(); + } + } + + } + if (ShowingText) + { + DialogForm.Update(gameTime); + } + Player.Update(gameTime); + base.Update(gameTime, controls); + for (int i = 0; i < Math.Min(lastInputCollection.Length, controls.Length); i++) + { + lastInputCollection[i] = controls[i]; + } + } + + public override void SetPlayerPosition(object sender, EventArgs e, GameTime gameTime) + { + throw new NotImplementedException(); + } + + public override void LoadContent() + { + DialogForm = new TextObject(FrameResources.Global.Fonts["vcr_large"], "...", 0.01f, 0.80f, Color.White, AlignMode.TopLeft, 1080, scrollPerTick: 1, scrollDelay: 0.04f); + DialogTest1 = new LuaModule(System.IO.File.ReadAllText(System.IO.Path.Join(Owner.Content.RootDirectory, "Scripts/dialog.lua"))); + DialogTest1.Runtime["dialog"] = DialogForm; + DialogTest1.Runtime["scene"] = SceneSwitches; + Player = new PlayerObject(new Point(32, 32), new Point(32, 32), 20.0f, new Point(32, 8)); + dro = new DynamicRectObject(new Point(32, 128), new Point(32, 32), 30.0f, true); + FrameResources.Textures.Add("cube-p", Owner.Content.Load("Texture2D/cube-p")); + Player.Sprite = new SpriteController(1000 / 60.0f, FrameResources.Textures["cube-p"]); + FrameResources.Textures.Add("cube-o", Owner.Content.Load("Texture2D/cube-o")); + dro.Sprite = new SpriteController(1000 / 60.0f, FrameResources.Textures["cube-o"]); + } + + public override void UnloadContent() + { + DialogForm = null; + FrameResources.Dispose(); + } + } +}