/// <summary> /// Exports the level into an XML file /// </summary> private void SaveLevel() { LevelData level = new LevelData(); level.size = new Vector2(levelSize.Width, levelSize.Height); level.R = controls.R; level.G = controls.G; level.B = controls.B; level.tiles = new List<TileInformation>(); foreach (TileInformation t in tileInfo) TileInformation.AddTile(level.tiles, t.texture, new Vector2(t.X - levelSize.X, t.Y - levelSize.Y), t.Scale, t.Rotation, t.Layer, t.Effect); level.staticBodies = new List<StaticBodyInformation>(); foreach (StaticBodyInformation sb in sbInfo) { StaticBodyInformation body = new StaticBodyInformation(); foreach (Vector2 p in sb.Points) body.AddPoint(new Vector2(p.X - levelSize.X, p.Y - levelSize.Y)); level.staticBodies.Add(body); } level.objects = new List<ObjectInformation>(); foreach (ObjectInformation obj in objInfo) { ObjectInformation moved = new ObjectInformation(); moved.Index = obj.Index; moved.ParameterNames = obj.ParameterNames; moved.ParameterValues = obj.ParameterValues; moved.Position = obj.Position - new Vector2(levelSize.X, levelSize.Y); moved.Texture = obj.Texture; moved.Type = obj.Type; moved.Scripts = obj.Scripts; level.objects.Add(moved); } level.cameras = new List<CameraBoxInformation>(); foreach (CameraBoxInformation cam in camInfo) { CameraBoxInformation moved = new CameraBoxInformation( cam.Bounds.Left - (int)levelSize.X, cam.Bounds.Top - (int)levelSize.Y, cam.Bounds.Right - (int)levelSize.X, cam.Bounds.Bottom - (int)levelSize.Y, cam.Target, cam.Priority); level.cameras.Add(moved); } level.triggers = new List<TriggerInformation>(); foreach (TriggerInformation trigger in triggerInfo) { TriggerInformation moved = new TriggerInformation( trigger.Bounds.Left - (int)levelSize.X, trigger.Bounds.Top - (int)levelSize.Y, trigger.Bounds.Right - (int)levelSize.X, trigger.Bounds.Bottom - (int)levelSize.Y, trigger.Name, trigger.ObjID, trigger.WhenTrigger); level.triggers.Add(moved); } controls.Save(level); controls.SavePressed = false; }
/// <summary> /// Resets the buttons in the control window /// </summary> private void ResetControlButtons() { if (currentBody != null && currentBody.Count() > 2) { sbInfo.Add(currentBody); currentBody = null; } step = 1; currentBox = new Vector2[2]; controls.ColDonePressed = false; controls.CreatingMap = false; controls.CamDonePressed = false; controls.CreatingCam = false; controls.TriggerDonePressed = false; controls.CreatingTrigger = false; }
/// <summary> /// Proceeds to the next step of drawing collision polygons /// </summary> private void CreateMap() { if (currentBody == null) currentBody = new StaticBodyInformation(camera.CameraToGlobalPos(Mouse.GetState().X, Mouse.GetState().Y)); else { bool done = currentBody.AddPoint(camera.CameraToGlobalPos(Mouse.GetState().X, Mouse.GetState().Y)); if (done) { sbInfo.Add(currentBody); currentBody = null; controls.CreatingMap = false; } } }
/// <summary> /// Removes the items selected by the cursor /// </summary> private void RemoveThings() { Vector2 mouseCoords = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); if (controls.Tab == 0) { Vector2 camToGlob = camera.CameraToGlobalPos(mouseCoords); // Remove entire polygon if (Keyboard.GetState().IsKeyDown(Keys.LeftShift)) { StaticBodyInformation toRemove = null; foreach (StaticBodyInformation sb in sbInfo) { bool brk = false; for (int i = 0; i < sb.Points.Count; i++) if ((camToGlob - sb.Points[i]).Length() <= 5) { toRemove = sb; brk = true; break; } if (brk) break; } if (toRemove != null) sbInfo.Remove(toRemove); } // Remove a point from the polyon and continue drawing else { StaticBodyInformation toRemove = null; foreach (StaticBodyInformation sb in sbInfo) { bool brk = false; for (int i = 0; i < sb.Points.Count; i++) if ((camToGlob - sb.Points[i]).Length() <= 5) { sb.RemovePoint(sb.Points[i]); toRemove = sb; currentBody = sb; controls.CreatingMap = true; brk = true; break; } if (brk) break; } if (toRemove != null) sbInfo.Remove(toRemove); CameraBoxInformation toRemove2 = null; foreach (CameraBoxInformation cam in camInfo) { if ((camToGlob - cam.Target).Length() <= 5) { toRemove2 = cam; break; } } if (toRemove2 != null) camInfo.Remove(toRemove2); TriggerInformation toRemove3 = null; foreach (TriggerInformation trigger in triggerInfo) { Vector2 topleft = new Vector2(trigger.Bounds.Left, trigger.Bounds.Top); Vector2 topright = new Vector2(trigger.Bounds.Right, trigger.Bounds.Top); Vector2 bottomleft = new Vector2(trigger.Bounds.Left, trigger.Bounds.Bottom); Vector2 bottomright = new Vector2(trigger.Bounds.Right, trigger.Bounds.Bottom); if ((camToGlob - topleft).Length() <= 5 || (camToGlob - topright).Length() <= 5 || (camToGlob - bottomleft).Length() <= 5 || (camToGlob - bottomright).Length() <= 5) { toRemove3 = trigger; break; } } if (toRemove3 != null) triggerInfo.Remove(toRemove3); } } else if (controls.Tab == 1) { List<TileInformation> toRemove = new List<TileInformation>(); foreach (TileInformation obj in tileInfo) { Vector2 translated = mouseCoords - camera.GlobalToCameraPos(obj.X, obj.Y); float newAngle = (float)Math.Atan2(translated.Y, translated.X) - obj.Rotation; float m = translated.Length(); translated.X = m * (float)Math.Cos(newAngle) + camera.GlobalToCameraPos(obj.X, obj.Y).X; translated.Y = m * (float)Math.Sin(newAngle) + camera.GlobalToCameraPos(obj.X, obj.Y).Y; Rectangle boundingBox = new Rectangle((int)camera.GlobalToCameraPos((int)(obj.X - TileTextures[obj.texture].Width / 2 * selTexScale.X), (int)(obj.Y - TileTextures[obj.texture].Height / 2 * selTexScale.Y)).X, (int)camera.GlobalToCameraPos((int)(obj.X - TileTextures[obj.texture].Width / 2 * selTexScale.X), (int)(obj.Y - TileTextures[obj.texture].Height / 2 * selTexScale.Y)).Y, (int)(TileTextures[obj.texture].Width * selTexScale.X * camera.TotalScale), (int)(TileTextures[obj.texture].Width * selTexScale.Y * camera.TotalScale)); if (boundingBox.Contains((int)translated.X, (int)translated.Y)) toRemove.Add(obj); } foreach (TileInformation obj in toRemove) tileInfo.Remove(obj); } else if (controls.Tab == 2) { List<ObjectInformation> toRemove = new List<ObjectInformation>(); foreach (ObjectInformation obj in objInfo) { Rectangle boundingBox = new Rectangle((int)camera.GlobalToCameraPos((int)(obj.Position.X - ObjectTextures[obj.Texture].Width / 2 * selTexScale.X), (int)(obj.Position.Y - ObjectTextures[obj.Texture].Height / 2 * selTexScale.Y)).X, (int)camera.GlobalToCameraPos((int)(obj.Position.X - ObjectTextures[obj.Texture].Width / 2 * selTexScale.X), (int)(obj.Position.Y - ObjectTextures[obj.Texture].Height / 2 * selTexScale.Y)).Y, (int)(ObjectTextures[obj.Texture].Width * selTexScale.X * camera.TotalScale), (int)(ObjectTextures[obj.Texture].Width * selTexScale.Y * camera.TotalScale)); if (boundingBox.Contains(Mouse.GetState().X, Mouse.GetState().Y)) toRemove.Add(obj); } foreach (ObjectInformation obj in toRemove) { indices.Remove(obj.Index); objInfo.Remove(obj); } UpdateIndex(); } }
/// <summary> /// Updates the screen /// </summary> /// <param name="gameTime">The amount of time passed</param> protected override void Update(GameTime gameTime) { UpdateFocus(); if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { if (!escapePressed) { escapePressed = true; if (controls.CreatingCam) { controls.CreatingCam = false; step = 1; } else if (controls.CreatingMap) { controls.CreatingMap = false; currentBody = null; } else if (controls.CreatingTrigger) { controls.CreatingTrigger = false; step = 1; } else if (currentObject != null) { currentObject = null; movedObject = null; step = 1; } else if (changingBounds) changingBounds = false; else this.Exit(); } } else escapePressed = false; if (Keyboard.GetState().IsKeyDown(Keys.Tab) && Keyboard.GetState().IsKeyDown(Keys.LeftControl) && !windowOpen) { if (!tabPressed) { tabPressed = true; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift)) ((System.Windows.Forms.TabControl)controls.Controls["tabControl"]).SelectedIndex = (((System.Windows.Forms.TabControl)controls.Controls["tabControl"]).SelectedIndex == 0 ? 2 : ((System.Windows.Forms.TabControl)controls.Controls["tabControl"]).SelectedIndex - 1); else ((System.Windows.Forms.TabControl)controls.Controls["tabControl"]).SelectedIndex = (((System.Windows.Forms.TabControl)controls.Controls["tabControl"]).SelectedIndex == 2 ? 0 : ((System.Windows.Forms.TabControl)controls.Controls["tabControl"]).SelectedIndex + 1); } } else tabPressed = false; if (controls.Tab == 0) { if (controls.NewPressed) NewLevel(); else if (controls.SavePressed) SaveLevel(); else if (controls.LoadPressed) LoadLevel(); else if (controls.ColDonePressed || controls.CamDonePressed) ResetControlButtons(); } else if (controls.Tab == 1) { if (controls.SelectedTile != null) selectedTexture = TileTextures[controls.SelectedTile]; } else if (controls.Tab == 2) { if (controls.SelectedObject != null) selectedTexture = ObjectTextures[controls.SelectedObject.ToString()]; } CheckRightClickMenu(); UpdateMouseStuff(); base.Update(gameTime); }