using System; using System.Collections.Generic; using System.Linq; using System.Text; using VBXSE; namespace TalkingIsAFreeAction { public static class Main { public enum Mode { Free, Text, Fading } public enum FadeReason { TimeUp, SwitchLevel } public static Sprite exclamationSprite = null; public static GText timerText = null; public static Sprite blackness = null; public static float exclamationTimer = 0; public static float lifeTimer = 10f; public static float fadeTimer = 0f; public static bool finishedLoading = false; public static bool climbing = false; public static Player player; public static Area currentArea = null; public static Mode currentMode = Mode.Free; public static Dictionary flags = new Dictionary(); public static Object carryingObject = null; public static FadeReason fadeReason = FadeReason.TimeUp; public static string nextLevel = "tutorial1"; public static void Initialize() { Textbox.Initialize(); blackness = SpriteEngine.CreateSprite("black", 0, 0, 1f); blackness.depth = -290; exclamationSprite = SpriteEngine.CreateSprite("exclamation"); exclamationSprite.depth = -12; exclamationSprite.enabled = false; timerText = SpriteEngine.CreateGText("10.000", 153, 4); timerText.color = Microsoft.Xna.Framework.Color.Red; timerText.depth = -300; blackness.color = new Microsoft.Xna.Framework.Color(1f, 1f, 1f, 0f); blackness.positionLocked = true; LoadLevel("menu"); player = new Player(); player.CreatePlayer(100, 80); Physics.activePhysicsBodies.Add(player); SpawnPlayer(); } public static void LoadLevel(string levelname) { currentArea = new Area(Util.FileToStringArray("levels/" + levelname + ".lvl")); if (!currentArea.timerOn) timerText.enabled = false; else timerText.enabled = true; Audio.PlayMusic(currentArea.music, true); } public static void FinishText() { if (EventHandler.handling) EventHandler.ContinueHandling(); if (!EventHandler.handling) { Textbox.Close(); currentMode = Mode.Free; } } public static void Interact() { //Check for action events for (int i = 0; i < currentArea.objects.Count; i++) { Object current = currentArea.objects[i]; if (current.onAction != "") { if (Util.CheckCollision(player.x, player.y, player.width, player.height, current.x, current.y, current.width, current.height)) { HandleEvent(current.onAction, current); } } } } public static void HandleEvent(string eventName, Object source) { //Activate action event! switch (eventName) { case "exclamation": exclamationTimer = 0.1f; break; case "finish": Audio.PlaySound("door"); SwitchLevel(source.parameter); break; case "quit": Environment.Exit(0); break; default: //Custom event if (currentArea.areaEvents.ContainsKey(eventName)) { EventHandler.StartHandling(currentArea.areaEvents[eventName], source); } break; } } public static void SwitchLevel(string whatlevel) { currentMode = Mode.Fading; nextLevel = whatlevel; fadeReason = FadeReason.SwitchLevel; fadeTimer = 2f; finishedLoading = false; lifeTimer = 10; } public static void Carry() { //Drop an object if (carryingObject != null) { currentArea.objects.Add(carryingObject); carryingObject.x = player.x; carryingObject.y = player.y; carryingObject.sprite.depth = Object.OBJECT_DEPTH; carryingObject.sprite.SetPosition(carryingObject.x, carryingObject.y); carryingObject = null; Audio.PlaySound("drop"); } else //Pick up an object { bool pickedSomethingUp = false; for (int i = 0; i < currentArea.objects.Count; i++) { Object obj = currentArea.objects[i]; if (Util.CheckCollision(player.x, player.y, player.width, player.height, obj.x, obj.y, obj.width, obj.height)) { if (obj.carryid != "") //Only if it can be picked up { if (!pickedSomethingUp) { carryingObject = obj; currentArea.objects.Remove(obj); obj.sprite.depth = -1; pickedSomethingUp = true; Audio.PlaySound("pickup"); } } } } } } public static void SpawnPlayer() { for (int i = 0; i < currentArea.objects.Count; i++) { if (currentArea.objects[i].special == "portal") { player.x = currentArea.objects[i].x; player.y = currentArea.objects[i].y; } } lifeTimer = 10; timerText.text = "10.000"; player.sprite.enabled = true; } public static void Update(double milliseconds) { Input.Update(milliseconds); float seconds = (float)milliseconds / 1000f; Physics.ApplyPhysics(seconds); //Check for touch events if (currentMode == Mode.Free) { for (int i = 0; i < currentArea.objects.Count; i++) { Object current = currentArea.objects[i]; if (current.onTouch != "") { if (Util.CheckCollision(player.x, player.y, player.width, player.height, current.x, current.y, current.width, current.height)) { HandleEvent(current.onTouch, current); } } } } Textbox.Update(seconds); //Center camera on player - 160 and 80 are half the screen width and height SpriteEngine.cameraOffsetX = player.x + (player.width / 2) - 160; SpriteEngine.cameraOffsetY = player.y + (player.height / 2) - 80 - 32; //Extra offset //Exclamation if (exclamationTimer > 0) { exclamationSprite.enabled = true; exclamationSprite.SetPosition(player.x - 1, player.y - exclamationSprite.GetImage().Height - 1); exclamationTimer -= seconds; } else exclamationSprite.enabled = false; //Carrying object if (carryingObject != null) { carryingObject.sprite.SetPosition( player.x + player.width / 2 - carryingObject.width / 2, player.y + player.height / 2 - carryingObject.height / 2); } //Update timer if (currentMode == Mode.Free && currentArea.timerOn) { lifeTimer -= seconds; if (lifeTimer < 0) { lifeTimer = 0; finishedLoading = false; fadeTimer = 2f; fadeReason = FadeReason.TimeUp; } string toPrint = Convert.ToString(Math.Round(lifeTimer, 3)); if (toPrint.Length == 1) toPrint += "."; while (toPrint.Length < 5) toPrint += "0"; timerText.text = toPrint; } //Fading if (fadeTimer > 0) { currentMode = Mode.Fading; fadeTimer -= seconds; if (fadeTimer > 1f) { float transparency = 1f - (fadeTimer - 1f); blackness.color = new Microsoft.Xna.Framework.Color(1f, 1f, 1f, transparency); } else { if (!finishedLoading) { switch (fadeReason) { case FadeReason.TimeUp: //Refresh area currentArea.Refresh(); //Clear flags flags.Clear(); //Respawn player at portal SpawnPlayer(); finishedLoading = true; break; case FadeReason.SwitchLevel: currentArea.DestroyArea(); flags.Clear(); LoadLevel(nextLevel); SpawnPlayer(); finishedLoading = true; break; } } blackness.color = new Microsoft.Xna.Framework.Color(1f, 1f, 1f, fadeTimer); } } else if (currentMode == Mode.Fading) { blackness.color = new Microsoft.Xna.Framework.Color(1f, 1f, 1f, fadeTimer); currentMode = Mode.Free; } //if (player != null) SpriteEngine.Log("X: " + player.x + "Y: " + player.y + " xvel: " + player.velocityX); //SpriteEngine.logEnabled = true; } } }