From 8ccd2b3394a0f8bfe8597cfc2fcac221e7aa4c35 Mon Sep 17 00:00:00 2001 From: red-the-random-dev Date: Fri, 1 Oct 2021 16:38:05 +0300 Subject: [PATCH 1/4] Creating ScriptTest frame --- _Example/DotRPG.Example.csproj | 2 ++ _Example/ScriptTest.cs | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 _Example/ScriptTest.cs diff --git a/_Example/DotRPG.Example.csproj b/_Example/DotRPG.Example.csproj index c937658..52a386c 100644 --- a/_Example/DotRPG.Example.csproj +++ b/_Example/DotRPG.Example.csproj @@ -31,5 +31,7 @@ + + \ No newline at end of file diff --git a/_Example/ScriptTest.cs b/_Example/ScriptTest.cs new file mode 100644 index 0000000..ee7165d --- /dev/null +++ b/_Example/ScriptTest.cs @@ -0,0 +1,59 @@ +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; + Player P; + 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) + { + + } + + 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, 540, scrollPerTick: 1, scrollDelay: 0.16f); + } + + public override void UnloadContent() + { + DialogForm = null; + FrameResources.Dispose(); + } + } +} From c3897a108ce3863e62bd263eafb24c19ff8e969b Mon Sep 17 00:00:00 2001 From: red-the-random-dev Date: Fri, 1 Oct 2021 18:02:34 +0300 Subject: [PATCH 2/4] Scripting test (complete) --- .../Dynamics/{Player.cs => PlayerObject.cs} | 33 +++---- Scripting/LuaModule.cs | 4 +- Scripting/Pipeline/ScriptImporter.cs | 8 +- Scripting/Pipeline/ScriptProcessor.cs | 4 +- Scripting/SceneSwitchSet.cs | 12 +++ _Example/DotRPG.Example.csproj | 1 - _Example/Game1.cs | 4 +- _Example/GameData/DotRPG.Example_data.mgcb | 3 + _Example/GameData/Scripts/dialog.lua | 14 +++ _Example/ScriptTest.cs | 92 ++++++++++++++++++- 10 files changed, 145 insertions(+), 30 deletions(-) rename Objects/Dynamics/{Player.cs => PlayerObject.cs} (62%) create mode 100644 Scripting/SceneSwitchSet.cs create mode 100644 _Example/GameData/Scripts/dialog.lua 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 52a386c..ed002ac 100644 --- a/_Example/DotRPG.Example.csproj +++ b/_Example/DotRPG.Example.csproj @@ -32,6 +32,5 @@ - \ 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..5030c48 --- /dev/null +++ b/_Example/GameData/Scripts/dialog.lua @@ -0,0 +1,14 @@ +import ('DotRPG.Scripting', 'DotRPG.Objects') + +event_count = 2 + +function loop(eventID, frameTime, totalTime) + if eventID == 0 then + dialog.Text = "You ran across white rectangular object.\nSo what?" + scene.ExitDialog = true + else if eventID == 1 then + dialog.Text = "A white rectangular object." + scene.ExitDialog = true + end +end +end \ No newline at end of file diff --git a/_Example/ScriptTest.cs b/_Example/ScriptTest.cs index ee7165d..547a606 100644 --- a/_Example/ScriptTest.cs +++ b/_Example/ScriptTest.cs @@ -13,7 +13,9 @@ class ScriptTest : Frame { LuaModule DialogTest1; TextObject DialogForm; - Player P; + SceneSwitchSet SceneSwitches = new SceneSwitchSet(); + PlayerObject Player; + DynamicRectObject dro; Boolean[] lastInputCollection = new bool[8]; Boolean ShowingText; @@ -37,7 +39,82 @@ 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); + lastInputCollection = controls; } public override void SetPlayerPosition(object sender, EventArgs e, GameTime gameTime) @@ -47,7 +124,16 @@ public override void SetPlayerPosition(object sender, EventArgs e, GameTime game public override void LoadContent() { - DialogForm = new TextObject(FrameResources.Global.Fonts["vcr_large"], "...", 0.01f, 0.80f, Color.White, AlignMode.TopLeft, 540, scrollPerTick: 1, scrollDelay: 0.16f); + 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() From dd7c4b3446667f8d22832d274853fde593fcd38b Mon Sep 17 00:00:00 2001 From: red-the-random-dev Date: Fri, 1 Oct 2021 18:06:07 +0300 Subject: [PATCH 3/4] A small script update --- _Example/GameData/Scripts/dialog.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/_Example/GameData/Scripts/dialog.lua b/_Example/GameData/Scripts/dialog.lua index 5030c48..c0b8136 100644 --- a/_Example/GameData/Scripts/dialog.lua +++ b/_Example/GameData/Scripts/dialog.lua @@ -5,8 +5,10 @@ event_count = 2 function loop(eventID, frameTime, totalTime) if eventID == 0 then dialog.Text = "You ran across white rectangular object.\nSo what?" - scene.ExitDialog = true else if eventID == 1 then + dialog.Text = "You should better go elsewhere." + scene.ExitDialog = true + else if eventID == 2 then dialog.Text = "A white rectangular object." scene.ExitDialog = true end From 187ec15eb6635152444b7659f6f69f3680c853c5 Mon Sep 17 00:00:00 2001 From: red-the-random-dev Date: Fri, 1 Oct 2021 18:59:57 +0300 Subject: [PATCH 4/4] Script test improvements --- _Example/GameData/Scripts/dialog.lua | 19 +++++++++++++++---- _Example/ScriptTest.cs | 7 +++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/_Example/GameData/Scripts/dialog.lua b/_Example/GameData/Scripts/dialog.lua index c0b8136..7732ee6 100644 --- a/_Example/GameData/Scripts/dialog.lua +++ b/_Example/GameData/Scripts/dialog.lua @@ -1,16 +1,27 @@ import ('DotRPG.Scripting', 'DotRPG.Objects') -event_count = 2 +event_count = 7 function loop(eventID, frameTime, totalTime) if eventID == 0 then dialog.Text = "You ran across white rectangular object.\nSo what?" - else if eventID == 1 then + elseif eventID == 1 then dialog.Text = "You should better go elsewhere." scene.ExitDialog = true - else if eventID == 2 then + 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 end \ No newline at end of file diff --git a/_Example/ScriptTest.cs b/_Example/ScriptTest.cs index 547a606..ab10a61 100644 --- a/_Example/ScriptTest.cs +++ b/_Example/ScriptTest.cs @@ -82,7 +82,7 @@ public override void Update(GameTime gameTime, bool[] controls) } Player.Velocity = Locomotion; Player.TryCollideWith(dro); - if (controls[4] /*&& !lastInputCollection[4]*/ || (ShowingText && DialogForm.ReachedEnd && SceneSwitches.AutoScroll)) + if (controls[4] && !lastInputCollection[4] || (ShowingText && DialogForm.ReachedEnd && SceneSwitches.AutoScroll)) { if (Player.SightArea.Intersects(dro.Collider) && !ShowingText) { @@ -114,7 +114,10 @@ public override void Update(GameTime gameTime, bool[] controls) } Player.Update(gameTime); base.Update(gameTime, controls); - lastInputCollection = 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)