Skip to content

Commit 48aaba4

Browse files
Merge pull request #7 from red-the-random-dev/intermediate-changes
Scripting implementation
2 parents d6891d7 + 187ec15 commit 48aaba4

File tree

10 files changed

+218
-26
lines changed

10 files changed

+218
-26
lines changed

Objects/Dynamics/Player.cs renamed to Objects/Dynamics/PlayerObject.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public enum Direction
1010
Up=0, Down=1, Left=2, Right=3
1111
};
1212

13-
public class Player : DynamicRectObject
13+
public class PlayerObject : DynamicRectObject
1414
{
1515
protected Point SightAreaSize;
1616
public Direction SightDirection;
@@ -19,14 +19,15 @@ public virtual Rectangle SightArea
1919
{
2020
get
2121
{
22+
Rectangle c = Collider;
2223
switch (SightDirection)
2324
{
2425
case Direction.Up:
2526
{
2627
return new Rectangle
2728
(
28-
Collider.X + Collider.Width / 2 - SightAreaSize.X,
29-
Collider.Y - SightAreaSize.Y,
29+
c.X + c.Width / 2 - SightAreaSize.X,
30+
c.Y - SightAreaSize.Y,
3031
SightAreaSize.X,
3132
SightAreaSize.Y
3233
);
@@ -35,8 +36,8 @@ public virtual Rectangle SightArea
3536
{
3637
return new Rectangle
3738
(
38-
Collider.X + Collider.Width / 2 - SightAreaSize.X,
39-
Collider.Y + Collider.Height + SightAreaSize.Y,
39+
c.X + c.Width / 2 - SightAreaSize.X,
40+
c.Y + c.Height + SightAreaSize.Y,
4041
SightAreaSize.X,
4142
SightAreaSize.Y
4243
);
@@ -45,28 +46,28 @@ public virtual Rectangle SightArea
4546
{
4647
return new Rectangle
4748
(
48-
Collider.X - SightArea.X,
49-
Collider.Y + Collider.Height / 2 - SightAreaSize.Y,
50-
SightAreaSize.X,
51-
SightAreaSize.Y
49+
c.X - SightAreaSize.X,
50+
c.Y + c.Height / 2 - SightAreaSize.Y,
51+
SightAreaSize.Y,
52+
SightAreaSize.X
5253
);
5354
}
5455
case Direction.Right:
5556
{
5657
return new Rectangle
5758
(
58-
Collider.X + Collider.Width + SightArea.X,
59-
Collider.Y + Collider.Height / 2 - SightAreaSize.Y,
60-
SightAreaSize.X,
61-
SightAreaSize.Y
59+
c.X + c.Width + SightAreaSize.X,
60+
c.Y + c.Height / 2 - SightAreaSize.Y,
61+
SightAreaSize.Y,
62+
SightAreaSize.X
6263
);
6364
}
6465
default:
6566
{
6667
return new Rectangle
6768
(
68-
Collider.X + Collider.Width / 2 - SightAreaSize.X / 2,
69-
Collider.Y + Collider.Height / 2 - SightAreaSize.Y / 2,
69+
c.X + c.Width / 2 - SightAreaSize.X / 2,
70+
c.Y + c.Height / 2 - SightAreaSize.Y / 2,
7071
SightAreaSize.X,
7172
SightAreaSize.Y
7273
);
@@ -75,7 +76,7 @@ public virtual Rectangle SightArea
7576
}
7677
}
7778

78-
public Player(Point StartLocation, Point colliderSize, Single mass, Point sightAreaSize) : base(StartLocation, colliderSize, mass, false)
79+
public PlayerObject(Point StartLocation, Point colliderSize, Single mass, Point sightAreaSize) : base(StartLocation, colliderSize, mass, false)
7980
{
8081
SightAreaSize = sightAreaSize;
8182
}

Scripting/LuaModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class LuaModule
1212
public Lua Runtime;
1313
public Boolean IsUp = true;
1414
public Int32 EventID;
15-
public readonly Int32 EventAmount;
15+
public readonly Double EventAmount;
1616

1717
public LuaModule(String initFile, Int32 eventAmount)
1818
{
@@ -26,7 +26,7 @@ public LuaModule(String initFile)
2626
Runtime = new Lua();
2727
Runtime.LoadCLRPackage();
2828
Runtime.DoString(initFile);
29-
EventAmount = (Int32) Runtime["event_count"];
29+
EventAmount = (Double) Runtime["event_count"];
3030
}
3131

3232
public void Update(Single elapsedTime, Single totalTime)

Scripting/Pipeline/ScriptImporter.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
using Scripting = DotRPG.Scripting;
33
using System.IO;
44

5-
using TImport = System.String;
6-
75
namespace DotRPG.Scripting.Pipeline
86
{
97
[ContentImporter(".lua", DisplayName = "DotRPG embeddable script handler", DefaultProcessor = "ScriptProcessor")]
10-
public class ScriptImporter : ContentImporter<TImport>
8+
public class ScriptImporter : ContentImporter<LuaModule>
119
{
12-
public override TImport Import(string filename, ContentImporterContext context)
10+
public override LuaModule Import(string filename, ContentImporterContext context)
1311
{
14-
return File.ReadAllText(filename);
12+
return new Scripting::LuaModule(File.ReadAllText(filename));
1513
}
1614
}
1715
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.Xna.Framework.Content.Pipeline;
22

3-
using TInput = System.String;
3+
using TInput = DotRPG.Scripting.LuaModule;
44
using TOutput = DotRPG.Scripting.LuaModule;
55

66
namespace ScriptImporter
@@ -10,7 +10,7 @@ class ScriptProcessor : ContentProcessor<TInput, TOutput>
1010
{
1111
public override TOutput Process(TInput input, ContentProcessorContext context)
1212
{
13-
return new TOutput(input);
13+
return input;
1414
}
1515
}
1616
}

Scripting/SceneSwitchSet.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DotRPG.Scripting
6+
{
7+
public class SceneSwitchSet
8+
{
9+
public Boolean AutoScroll = false;
10+
public Boolean ExitDialog = false;
11+
}
12+
}

_Example/DotRPG.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
<ItemGroup>
3232
<ProjectReference Include="..\Objects\DotRPG.Objects.csproj" />
3333
<ProjectReference Include="..\Objects\Dynamics\DotRPG.Objects.Dynamics.csproj" />
34+
<ProjectReference Include="..\Scripting\DotRPG.Scripting.csproj" />
3435
</ItemGroup>
3536
</Project>

_Example/Game1.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static void SetFrameNumber(Object sender, EventArgs e, GameTime gameTime)
8686

8787
private void StartScroll(Object sender, EventArgs e, GameTime gameTime)
8888
{
89-
ActiveFrame = Frames[1];
89+
ActiveFrame = Frames[2];
9090
ActiveFrame.LoadContent();
9191
}
9292

@@ -104,6 +104,8 @@ protected override void Initialize()
104104
Frames[0].Initialize();
105105
Frames.Add(new DynamicsTestFrame(this, ResourceHGlobal, LogicEventSet));
106106
Frames[1].Initialize();
107+
Frames.Add(new ScriptTest(this, ResourceHGlobal, LogicEventSet));
108+
Frames[2].Initialize();
107109
base.Initialize();
108110
}
109111

_Example/GameData/DotRPG.Example_data.mgcb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
/processorParam:TextureFormat=Compressed
2828
/build:Fonts/MainFont_Large.spritefont
2929

30+
#begin Scripts/dialog.lua
31+
/copy:Scripts/dialog.lua
32+
3033
#begin Sounds/clickText.wav
3134
/importer:WavImporter
3235
/processor:SoundEffectProcessor
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import ('DotRPG.Scripting', 'DotRPG.Objects')
2+
3+
event_count = 7
4+
5+
function loop(eventID, frameTime, totalTime)
6+
if eventID == 0 then
7+
dialog.Text = "You ran across white rectangular object.\nSo what?"
8+
elseif eventID == 1 then
9+
dialog.Text = "You should better go elsewhere."
10+
scene.ExitDialog = true
11+
elseif eventID == 2 then
12+
dialog.Text = "A white rectangular object."
13+
scene.ExitDialog = true
14+
elseif eventID == 3 then
15+
dialog.Text = "You've never seen object that is more white\nand rectangular than this one."
16+
scene.ExitDialog = true
17+
elseif eventID == 4 then
18+
dialog.Text = "Don't you have better things to do?"
19+
scene.ExitDialog = true
20+
elseif eventID == 5 then
21+
dialog.Text = "You ran across white rect"
22+
scene.AutoScroll = true
23+
elseif eventID == 6 then
24+
dialog.Text = "COME ON, QUIT IT ALREADY!"
25+
scene.ExitDialog = true
26+
end
27+
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((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((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)