using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TalkingIsAFreeAction { public static class EventHandler { public static string[] currentEvent; public static int currentLine = 0; public static bool handling = false; public static Dictionary labels; public static Object sourceObject = null; public static void StartHandling(string[] theEvent, Object source) { sourceObject = source; currentEvent = theEvent; currentLine = 0; handling = true; labels = new Dictionary(); for (int i = 0; i < theEvent.Length; i++) { if (theEvent[i].StartsWith("==")) { labels.Add(theEvent[i].Substring(2), i); } } ContinueHandling(); } public static void ContinueHandling() { if (currentLine < currentEvent.Length) { bool keepgoing = true; string line = currentEvent[currentLine]; while (line.StartsWith(" ")) line = line.Substring(1); //Remove leading spaces if (!(line.StartsWith("//") || line.StartsWith("==") || line == "")) //Ignore comments, labels, blank lines { string[] splitup = line.Split(new string[] { " " }, StringSplitOptions.None); string command = splitup[0]; switch (command) { case "text": Textbox.PrintText(line.Substring(command.Length + 1)); keepgoing = false; Main.currentMode = Main.Mode.Text; break; case "finaltext": Textbox.PrintText(line.Substring(command.Length + 1), true); keepgoing = false; Main.currentMode = Main.Mode.Text; break; case "stop": handling = false; keepgoing = false; break; case "changetile": int x = Convert.ToInt32(splitup[1]); int y = Convert.ToInt32(splitup[2]); Main.currentArea.RemoveTile(x, y); Main.currentArea.PlaceTile(splitup[3], x, y); break; case "flag": { if (Main.flags.ContainsKey(splitup[1])) Main.flags.Remove(splitup[1]); bool isTrue = false; if (splitup[2] == "on") isTrue = true; Main.flags.Add(splitup[1], isTrue); } break; case "goto": currentLine = labels[splitup[1]]; break; case "njump": { bool isTrue = false; if (Main.flags.ContainsKey(splitup[1])) { isTrue = Main.flags[splitup[1]]; } if (!isTrue) currentLine = labels[splitup[2]]; } break; case "jump": { bool isTrue = false; if (Main.flags.ContainsKey(splitup[1])) { isTrue = Main.flags[splitup[1]]; } if (isTrue) currentLine = labels[splitup[2]]; } break; case "objjump": { bool isTrue = false; if (Main.carryingObject != null) { isTrue = (Main.carryingObject.carryid == splitup[1]); } if (isTrue) currentLine = labels[splitup[2]]; } break; case "destroyitem": { if (Main.carryingObject != null) { VBXSE.SpriteEngine.EraseGObject(Main.carryingObject.sprite); Main.carryingObject = null; } } break; case "destroyobject": { if (sourceObject != null) { VBXSE.SpriteEngine.EraseGObject(sourceObject.sprite); if(Main.currentArea.objects.Contains(sourceObject)) Main.currentArea.objects.Remove(sourceObject); } } break; case "die": { Main.player.sprite.enabled = false; } break; case "respawn": { Main.lifeTimer = 0; } break; case "switchlevel": { Main.SwitchLevel(splitup[1]); } break; case "playmusic": { Audio.PlayMusic(splitup[1]); } break; case "playsound": { Audio.PlaySound(splitup[1]); } break; case "giveitem": { Area.TileDefinition def = Main.currentArea.tileDefs[splitup[1]]; Object toGive = new Object(def.symbol, def.foreground, Convert.ToInt32(Main.player.x), Convert.ToInt32(Main.player.y), def.onaction, def.ontouch, def.special, def.carryid, def.parameter); if (Main.carryingObject == null) { Main.carryingObject = toGive; toGive.sprite.depth = -1; } else { Main.currentArea.objects.Add(toGive); } } break; case "removeobject": { List toRemove = new List(); for (int i = 0; i < Main.currentArea.objects.Count; i++) { if (Main.currentArea.objects[i].symbol == splitup[1]) { toRemove.Add(Main.currentArea.objects[i]); } } for (int i = 0; i < toRemove.Count; i++) { Main.currentArea.objects.Remove(toRemove[i]); VBXSE.SpriteEngine.EraseGObject(toRemove[i].sprite); } } break; } } currentLine++; if (keepgoing) ContinueHandling(); } else { //Event's over, everybody go home handling = false; } } } }