00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using System;
00018 using System.Collections.Generic;
00019 using System.Text;
00020
00021 namespace DXGfxLib
00022 {
00027 public class ExecutableBlock
00028 {
00032 public SceneNode parent = null;
00033
00038 public Scene scene = null;
00039
00043 public void Dispose()
00044 {
00045 }
00046
00055 public virtual void Evaluate(Scene scene, SceneNode parent, double appTime, float elapsedTime)
00056 {
00057 }
00058
00067 public virtual void Click(Scene scene, SceneNode parent, double appTime, float elapsedTime)
00068 {
00069 }
00070
00080 public virtual void Collision(Scene scene, SceneNode parent, double appTime, float elapsedTime, SceneObject collidingObject)
00081 {
00082 }
00083 }
00084
00088 public class Script
00089 {
00093 private SceneObject parent = null;
00094
00098 private Scene scene = null;
00099
00103 private string skeleton;
00104
00109 public string fileName = "";
00110
00114 public string name = "";
00115
00120 private string varSrc = "// If you want to add variables that will retain their values until the script is reseted please define them below:\n";
00121
00122 public string VarSrc
00123 {
00124 get { return GetVarSrc(); }
00125 set { UpdateVarSrc(value); }
00126 }
00127
00133 private string codeSrc = "// Please write your script code below. You can reference the variables you have defined and also the 'parent' (the SceneNode object to which this script is attached) and the 'scene' (the Scene object to which the parent is attached)";
00134
00135 public string CodeSrc
00136 {
00137 get { return GetCodeSrc(); }
00138 set { UpdateCodeSrc(value); }
00139 }
00140
00144 private string clickCodeSrc = "// Write code to react to click event below!";
00145
00146 public string ClickCodeSrc
00147 {
00148 get { return GetClickCodeSrc(); }
00149 set { UpdateClickCodeSrc(value); }
00150 }
00151
00155 private string collisionCodeSrc = "// Write code to react to collision below!";
00156
00157 public string CollisionCodeSrc
00158 {
00159 get { return GetCollisionCodeSrc(); }
00160 set { UpdateCollisionCodeSrc(value); }
00161 }
00165 private ExecutableBlock currentExecutableBlockInstance = null;
00166
00170 private double scriptStartTime = 0;
00171
00175 private double scriptLastEvaluation = 0;
00176
00180 private double scriptLastClick = 0;
00181
00185 private double scriptLastCollision = 0;
00186
00191 public Script()
00192 {
00193 StringBuilder strBuilder = new StringBuilder("");
00194 strBuilder.AppendLine("using System;");
00195 strBuilder.AppendLine("using System.Windows.Forms;");
00196 strBuilder.AppendLine("using System.Collections.Generic;");
00197 strBuilder.AppendLine("using System.Text;");
00198 strBuilder.AppendLine("using System.Diagnostics;");
00199 strBuilder.AppendLine("using Microsoft.DirectX;");
00200 strBuilder.AppendLine("using Microsoft.DirectX.Direct3D;");
00201 strBuilder.AppendLine("using Microsoft.Samples.DirectX.UtilityToolkit;");
00202 strBuilder.AppendLine("namespace DXGfxLib {");
00203 strBuilder.AppendLine("public class ReplaceByName : ExecutableBlock {");
00204 strBuilder.AppendLine("ReplaceByMemberVariableSrc");
00205 strBuilder.AppendLine("public override void Evaluate(Scene scene, SceneNode parent, double appTime, float elapsedTime) {");
00206 strBuilder.AppendLine("ReplaceByMethodCodeSrc");
00207 strBuilder.AppendLine("}");
00208 strBuilder.AppendLine("public override void Click(Scene scene, SceneNode parent, double appTime, float elapsedTime) {");
00209 strBuilder.AppendLine("ReplaceByMethodClickCodeSrc");
00210 strBuilder.AppendLine("}");
00211 strBuilder.AppendLine("public override void Collision(Scene scene, SceneNode parent, double appTime, float elapsedTime, SceneObject collidingObject) {");
00212 strBuilder.AppendLine("ReplaceByMethodCollisionCodeSrc");
00213 strBuilder.AppendLine("}}}");
00214 skeleton = strBuilder.ToString();
00215 }
00216
00221 public void SetParent(SceneObject parent)
00222 {
00225 if (parent == null)
00226 {
00227
00228
00229 fileName = DXGfxManager.GetGlobalInstance().currentSceneName;
00230 }
00231 else
00232 {
00233 this.parent = parent;
00234 fileName = parent.fileName;
00235 }
00236 fileName += ".script.cs";
00237 name = StringUtil.GetName(fileName);
00238 int firstDotIndex = name.IndexOf('.');
00239 name = name.Substring(0, firstDotIndex);
00240 }
00241
00246 public void SetScene(Scene scene)
00247 {
00248 this.scene = scene;
00249 }
00250
00255 public string GetCodeSrc()
00256 {
00257 return codeSrc;
00258 }
00259
00264 public string GetClickCodeSrc()
00265 {
00266 return clickCodeSrc;
00267 }
00268
00273 public string GetCollisionCodeSrc()
00274 {
00275 return collisionCodeSrc;
00276 }
00277
00282 public string GetVarSrc()
00283 {
00284 return varSrc;
00285 }
00286
00291 public string GetSrcForCompilation()
00292 {
00293 string output = skeleton;
00294
00295 output = output.Replace("ReplaceByName", name);
00296 output = output.Replace("ReplaceByMemberVariableSrc", varSrc);
00297 output = output.Replace("ReplaceByMethodCodeSrc", codeSrc);
00298 output = output.Replace("ReplaceByMethodClickCodeSrc", clickCodeSrc);
00299 output = output.Replace("ReplaceByMethodCollisionCodeSrc", collisionCodeSrc);
00300
00301 return output;
00302 }
00303
00308 public void UpdateVarSrc(string src)
00309 {
00310 this.varSrc = src;
00311
00312 ScriptManager.GetGlobalInstance().ScriptUpdated(this);
00313 }
00314
00319 public void UpdateCodeSrc(string src)
00320 {
00321 this.codeSrc = src;
00322
00323 ScriptManager.GetGlobalInstance().ScriptUpdated(this);
00324 }
00325
00330 public void UpdateClickCodeSrc(string src)
00331 {
00332 this.clickCodeSrc = src;
00333
00334 ScriptManager.GetGlobalInstance().ScriptUpdated(this);
00335 }
00336
00341 public void UpdateCollisionCodeSrc(string src)
00342 {
00343 this.collisionCodeSrc = src;
00344
00345 ScriptManager.GetGlobalInstance().ScriptUpdated(this);
00346 }
00347
00352 public bool Reset()
00353 {
00354 scriptStartTime = 0;
00355 scriptLastEvaluation = 0;
00356 scriptLastClick = 0;
00357 scriptLastCollision = 0;
00358
00359
00360
00361 return true;
00362 }
00363
00367 public virtual void Evaluate(double appTime, float elapsedTime)
00368 {
00369 if (scene == null)
00370 {
00371 throw new Exception("No scene associated with script. Script can not be run!");
00372 }
00373
00374
00375
00376 if (!ScriptManager.GetGlobalInstance().scriptsAreReady)
00377 {
00378 ScriptManager.GetGlobalInstance().GetReady();
00379 }
00380
00381 if ((scriptStartTime == 0)||(currentExecutableBlockInstance == null))
00382 {
00383
00384
00385
00386 scriptStartTime = appTime;
00387
00388 currentExecutableBlockInstance = ScriptManager.GetGlobalInstance().GetNewExecutableBlock(name);
00389
00390 currentExecutableBlockInstance.parent = parent;
00391 currentExecutableBlockInstance.scene = scene;
00392 }
00393
00394
00395 currentExecutableBlockInstance.Evaluate(scene, parent, appTime, elapsedTime);
00396
00397 scriptLastEvaluation = appTime;
00398 }
00399
00404 public virtual void Clicked(double appTime, float elapsedTime)
00405 {
00406 if (parent == null)
00407 {
00408 throw new Exception("No parent associated with script. Script can not be run!");
00409 }
00410 if (scene == null)
00411 {
00412 throw new Exception("No scene associated with script. Script can not be run!");
00413 }
00414
00415
00416
00417 if (!ScriptManager.GetGlobalInstance().scriptsAreReady)
00418 {
00419 ScriptManager.GetGlobalInstance().GetReady();
00420 }
00421
00422 if ((scriptStartTime == 0)||(currentExecutableBlockInstance == null))
00423 {
00424
00425
00426
00427 scriptStartTime = appTime;
00428
00429 currentExecutableBlockInstance = ScriptManager.GetGlobalInstance().GetNewExecutableBlock(name);
00430
00431 currentExecutableBlockInstance.parent = parent;
00432 currentExecutableBlockInstance.scene = scene;
00433 }
00434
00435
00436 currentExecutableBlockInstance.Click(scene, parent, appTime, elapsedTime);
00437
00438 scriptLastClick = appTime;
00439 parent.HasBeenClicked = false;
00440 }
00441
00449 public virtual void Collision(double appTime, float elapsedTime, SceneObject collidingObject)
00450 {
00451 if (parent == null)
00452 {
00453 throw new Exception("No parent associated with script. Script can not be run!");
00454 }
00455 if (scene == null)
00456 {
00457 throw new Exception("No scene associated with script. Script can not be run!");
00458 }
00459
00460
00461
00462 if (!ScriptManager.GetGlobalInstance().scriptsAreReady)
00463 {
00464 ScriptManager.GetGlobalInstance().GetReady();
00465 }
00466
00467 if ((scriptStartTime == 0)||(currentExecutableBlockInstance == null))
00468 {
00469
00470
00471
00472 scriptStartTime = appTime;
00473
00474 currentExecutableBlockInstance = ScriptManager.GetGlobalInstance().GetNewExecutableBlock(name);
00475
00476 currentExecutableBlockInstance.parent = parent;
00477 currentExecutableBlockInstance.scene = scene;
00478 }
00479
00480
00481 currentExecutableBlockInstance.Collision(scene, parent, appTime, elapsedTime, collidingObject);
00482
00483 scriptLastCollision = appTime;
00484 }
00485 }
00486 }