Skip to content

Commit b6946a2

Browse files
Scripting minor update
1 parent d6891d7 commit b6946a2

File tree

3 files changed

+188
-10
lines changed

3 files changed

+188
-10
lines changed

Scripting/LuaModule.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,33 @@ public class LuaModule
1111
{
1212
public Lua Runtime;
1313
public Boolean IsUp = true;
14-
public Int32 EventID;
15-
public readonly Int32 EventAmount;
14+
public readonly LuaTable EventAmounts;
15+
public Int32[] EventIDs;
1616

17-
public LuaModule(String initFile, Int32 eventAmount)
17+
public LuaModule(String initFile, LuaTable eventAmounts)
1818
{
1919
Runtime = new Lua();
2020
Runtime.LoadCLRPackage();
2121
Runtime.DoString(initFile);
22-
EventAmount = eventAmount;
22+
EventAmounts = eventAmounts;
2323
}
2424
public LuaModule(String initFile)
2525
{
2626
Runtime = new Lua();
2727
Runtime.LoadCLRPackage();
2828
Runtime.DoString(initFile);
29-
EventAmount = (Int32) Runtime["event_count"];
29+
EventAmounts = (LuaTable) Runtime["event_counts"];
30+
EventIDs = new int[EventAmounts.Values.Count];
3031
}
3132

32-
public void Update(Single elapsedTime, Single totalTime)
33+
public void Update(Int32 ObjectID, Single elapsedTime, Single totalTime)
3334
{
3435
LuaFunction loopFunction = Runtime["loop"] as LuaFunction;
35-
loopFunction.Call(EventID, elapsedTime, totalTime);
36-
EventID++;
37-
if (EventID >= EventAmount)
36+
loopFunction.Call(ObjectID, EventIDs[ObjectID], elapsedTime, totalTime);
37+
EventIDs[ObjectID]++;
38+
if (EventIDs[ObjectID] >= (Int64)EventAmounts[ObjectID+1])
3839
{
39-
EventID = 0;
40+
EventIDs[ObjectID] = 0;
4041
}
4142
}
4243
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ('DotRPG.Scripting', 'DotRPG.Objects')
2+
3+
event_counts = {7}
4+
5+
function loop(objID, eventID, frameTime, totalTime)
6+
if objID == 0 then
7+
if eventID == 0 then
8+
dialog.Text = "You ran across white rectangular object.\nSo what?"
9+
elseif eventID == 1 then
10+
dialog.Text = "You should better go elsewhere."
11+
scene.ExitDialog = true
12+
elseif eventID == 2 then
13+
dialog.Text = "A white rectangular object."
14+
scene.ExitDialog = true
15+
elseif eventID == 3 then
16+
dialog.Text = "You've never seen object that is more white\nand rectangular than this one."
17+
scene.ExitDialog = true
18+
elseif eventID == 4 then
19+
dialog.Text = "Don't you have better things to do?"
20+
scene.ExitDialog = true
21+
elseif eventID == 5 then
22+
dialog.Text = "You ran across white rect"
23+
scene.AutoScroll = true
24+
elseif eventID == 6 then
25+
dialog.Text = "COME ON, QUIT IT ALREADY!"
26+
scene.ExitDialog = true
27+
end
28+
end
29+
end

_Example/ScriptTest.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.Xna.Framework;
5+
using DotRPG.Objects;
6+
using DotRPG.Objects.Dynamics;
7+
using DotRPG.Scripting;
8+
using Microsoft.Xna.Framework.Graphics;
9+
10+
namespace DotRPG._Example
11+
{
12+
class ScriptTest : Frame
13+
{
14+
LuaModule DialogTest1;
15+
TextObject DialogForm;
16+
SceneSwitchSet SceneSwitches = new SceneSwitchSet();
17+
PlayerObject Player;
18+
DynamicRectObject dro;
19+
Boolean[] lastInputCollection = new bool[8];
20+
Boolean ShowingText;
21+
22+
public override int FrameID
23+
{
24+
get
25+
{
26+
return 2;
27+
}
28+
}
29+
30+
public ScriptTest(Game owner, ResourceHeap globalGameResources, HashSet<TimedEvent> globalEventSet) : base(owner, globalGameResources, globalEventSet)
31+
{
32+
33+
}
34+
35+
public override void Initialize()
36+
{
37+
38+
}
39+
40+
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, Rectangle drawZone)
41+
{
42+
Player.Draw(spriteBatch, gameTime, 540, new Point(0, 0), new Point(drawZone.Width, drawZone.Height));
43+
dro.Draw(spriteBatch, gameTime, 540, new Point(0, 0), new Point(drawZone.Width, drawZone.Height));
44+
if (ShowingText)
45+
{
46+
DialogForm.Draw(spriteBatch, Owner.Window);
47+
}
48+
#if DEBUG
49+
spriteBatch.DrawString(FrameResources.Global.Fonts["vcr"], String.Format("Sight: {0}, Z key: {1}", Player.SightArea, lastInputCollection[4]), new Vector2(0, 12), Color.White);
50+
#endif
51+
}
52+
53+
public override void Update(GameTime gameTime, bool[] controls)
54+
{
55+
Single loco_x = 0.0f; Single loco_y = 0.0f;
56+
if (controls[0]) { loco_y -= 1.0f; }
57+
if (controls[1]) { loco_y += 1.0f; }
58+
if (controls[2]) { loco_x -= 1.0f; }
59+
if (controls[3]) { loco_x += 1.0f; }
60+
Vector2 Locomotion = new Vector2(loco_x, loco_y);
61+
if (controls[0] && !(controls[1] || controls[2] || controls[3]))
62+
{
63+
Player.SightDirection = Direction.Up;
64+
}
65+
if (controls[1] && !(controls[0] || controls[2] || controls[3]))
66+
{
67+
Player.SightDirection = Direction.Down;
68+
}
69+
if (controls[2] && !(controls[1] || controls[0] || controls[3]))
70+
{
71+
Player.SightDirection = Direction.Left;
72+
}
73+
if (controls[3] && !(controls[1] || controls[0] || controls[2]))
74+
{
75+
Player.SightDirection = Direction.Right;
76+
}
77+
Locomotion /= (Locomotion.Length() != 0 ? Locomotion.Length() : 1.0f);
78+
Locomotion *= 0.1f;
79+
if (ShowingText)
80+
{
81+
Locomotion = Vector2.Zero;
82+
}
83+
Player.Velocity = Locomotion;
84+
Player.TryCollideWith(dro);
85+
if (controls[4] && !lastInputCollection[4] || (ShowingText && DialogForm.ReachedEnd && SceneSwitches.AutoScroll))
86+
{
87+
if (Player.SightArea.Intersects(dro.Collider) && !ShowingText)
88+
{
89+
SceneSwitches.AutoScroll = false;
90+
SceneSwitches.ExitDialog = false;
91+
ShowingText = true;
92+
DialogTest1.Update(0, (float)gameTime.ElapsedGameTime.TotalMilliseconds, (float)gameTime.TotalGameTime.TotalMilliseconds);
93+
DialogForm.ResetToStart();
94+
}
95+
else if (DialogForm.ReachedEnd)
96+
{
97+
if (SceneSwitches.ExitDialog)
98+
{
99+
ShowingText = false;
100+
}
101+
else
102+
{
103+
SceneSwitches.AutoScroll = false;
104+
SceneSwitches.ExitDialog = false;
105+
DialogTest1.Update(0, (float)gameTime.ElapsedGameTime.TotalMilliseconds, (float)gameTime.TotalGameTime.TotalMilliseconds);
106+
DialogForm.ResetToStart();
107+
}
108+
}
109+
110+
}
111+
if (ShowingText)
112+
{
113+
DialogForm.Update(gameTime);
114+
}
115+
Player.Update(gameTime);
116+
base.Update(gameTime, controls);
117+
for (int i = 0; i < Math.Min(lastInputCollection.Length, controls.Length); i++)
118+
{
119+
lastInputCollection[i] = controls[i];
120+
}
121+
}
122+
123+
public override void SetPlayerPosition(object sender, EventArgs e, GameTime gameTime)
124+
{
125+
throw new NotImplementedException();
126+
}
127+
128+
public override void LoadContent()
129+
{
130+
DialogForm = new TextObject(FrameResources.Global.Fonts["vcr_large"], "...", 0.01f, 0.80f, Color.White, AlignMode.TopLeft, 1080, scrollPerTick: 1, scrollDelay: 0.04f);
131+
DialogTest1 = new LuaModule(System.IO.File.ReadAllText(System.IO.Path.Join(Owner.Content.RootDirectory, "Scripts/dialog.lua")));
132+
DialogTest1.Runtime["dialog"] = DialogForm;
133+
DialogTest1.Runtime["scene"] = SceneSwitches;
134+
Player = new PlayerObject(new Point(32, 32), new Point(32, 32), 20.0f, new Point(32, 8));
135+
dro = new DynamicRectObject(new Point(32, 128), new Point(32, 32), 30.0f, true);
136+
FrameResources.Textures.Add("cube-p", Owner.Content.Load<Texture2D>("Texture2D/cube-p"));
137+
Player.Sprite = new SpriteController(1000 / 60.0f, FrameResources.Textures["cube-p"]);
138+
FrameResources.Textures.Add("cube-o", Owner.Content.Load<Texture2D>("Texture2D/cube-o"));
139+
dro.Sprite = new SpriteController(1000 / 60.0f, FrameResources.Textures["cube-o"]);
140+
}
141+
142+
public override void UnloadContent()
143+
{
144+
DialogForm = null;
145+
FrameResources.Dispose();
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)