private void worldView_DragDrop(object sender, DragEventArgs e) { Vector2 worldPos = ScreenToWorld(e.X, e.Y); if (snapping) { worldPos = SnapToGrid(worldPos); } //get item ListViewItem item = new ListViewItem(); if (e.Data.GetData(typeof(ListViewItem)) != null) { item = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); } object selectedObject = null; switch ((string)item.Tag) { case "Light": if (level.Lights.Count < 8) { Light l = new Light(); l.Diffuse = Color4.White; l.Ambient = new Color4(0.1f, 0.1f, 0.1f, 1f); l.Position = new Vector4(worldPos.X, worldPos.Y, 36, 1); l.ConstantAttenuation = 1f; l.LinearAttenuation = 1f / 3000f; l.QuadraticAttenuation = 1f / 40000f; level.Lights.Add(l); for (int i = 0; i < 8; i++) { bool inUse = false; for (int j = 0; j < level.Lights.Count; j++) { if (level.Lights[j].Index == i) { inUse = true; } } if (!inUse) { l.Index = i; } } selectedObject = l; } else { MessageBox.Show("To many lights. Max light count is 8.", "Light Count", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; case "Ball": Ball ball = new Ball(worldPos.X, worldPos.Y); level.Entities.Add(ball); selectedObject = ball; break; case "Block": Block block = new Block(worldPos.X, worldPos.Y); level.Entities.Add(block); selectedObject = block; break; case "BreakableDoor": BreakableDoor bdoor = new BreakableDoor(worldPos.X, worldPos.Y); bdoor.MaxHits = 3; level.Entities.Add(bdoor); selectedObject = bdoor; break; case "Button": Entities.Button button = new Entities.Button(worldPos.X, worldPos.Y); level.Entities.Add(button); selectedObject = button; break; case "Cannon": Cannon cannon = new Cannon(worldPos.X, worldPos.Y); level.Entities.Add(cannon); selectedObject = cannon; break; case "Door": Door door = new Door(worldPos.X, worldPos.Y); level.Entities.Add(door); selectedObject = door; break; /*case "LaserShooter": * LaserShooter ls = new LaserShooter(worldPos.X, worldPos.Y); * level.Entities.Add(ls); * selectedObject = ls; * break;*/ case "Player": Player p = new Player(worldPos.X, worldPos.Y); level.Entities.Add(p); selectedObject = p; break; /*case "PuzzleBox": * PuzzleBox pb = new PuzzleBox(worldPos.X, worldPos.Y); * level.Entities.Add(pb); * selectedObject = pb; * break;*/ case "SpikeWall": SpikeWall sw = new SpikeWall(worldPos.X, worldPos.Y); level.Entities.Add(sw); selectedObject = sw; break; /*case "Teleporter": * Teleporter tp = new Teleporter(worldPos.X, worldPos.Y); * level.Entities.Add(tp); * selectedObject = tp; * break;*/ case "CauseAND": CauseAND cAnd = new CauseAND(); level.Causes.Add(cAnd); selectedObject = cAnd; break; case "CauseNOT": CauseNOT cNot = new CauseNOT(); level.Causes.Add(cNot); selectedObject = cNot; break; case "CauseOR": CauseOR cOr = new CauseOR(); level.Causes.Add(cOr); selectedObject = cOr; break; case "CauseXOR": CauseXOR cXor = new CauseXOR(); level.Causes.Add(cXor); selectedObject = cXor; break; case "CauseButton": CauseButton cBtn = new CauseButton(); level.Causes.Add(cBtn); selectedObject = cBtn; break; case "CauseEntityDestruction": CauseEntityDestruction cEntD = new CauseEntityDestruction(); level.Causes.Add(cEntD); selectedObject = cEntD; break; case "CauseLocation": CauseLocation cLoc = new CauseLocation(); level.Causes.Add(cLoc); selectedObject = cLoc; break; case "CauseTimePassed": CauseTimePassed cTime = new CauseTimePassed(); level.Causes.Add(cTime); selectedObject = cTime; break; case "EffectAND": EffectAND eAnd = new EffectAND(); level.Effects.Add(eAnd); selectedObject = eAnd; break; case "EffectList": EffectList eList = new EffectList(); level.Effects.Add(eList); selectedObject = eList; break; case "EffectDoor": EffectDoor eDoor = new EffectDoor(); level.Effects.Add(eDoor); selectedObject = eDoor; break; case "EffectEndGame": EffectEndGame eEnd = new EffectEndGame(); level.Effects.Add(eEnd); selectedObject = eEnd; break; case "EffectRaiseBridge": EffectRaiseBridge eBridge = new EffectRaiseBridge(); level.Effects.Add(eBridge); selectedObject = eBridge; break; case "EffectRemoveEntity": EffectRemoveEntity eRemove = new EffectRemoveEntity(); level.Effects.Add(eRemove); selectedObject = eRemove; break; case "EffectTriggerTimer": EffectTriggerTimer eTimer = new EffectTriggerTimer(); level.Effects.Add(eTimer); selectedObject = eTimer; break; case "Trigger": Trigger t = new Trigger(); level.Triggers.Add(t); selectedObject = t; break; } selectedItemProperties.SelectedObject = selectedObject; UpdateWorldTree(); }
private static bool LoadLevel(String filepath, out Tile[][] tileset, out List<Entity> entities, out List<Cause> causes, out List<Effect> effects, out List<Trigger> triggers, out List<Light> lights) { try { StreamReader xmlReader = new StreamReader(filepath); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Level)); Level level = (Level)xmlSerializer.Deserialize(xmlReader); entities = new List<Entity>(); causes = new List<Cause>(); effects = new List<Effect>(); triggers = new List<Trigger>(); lights = new List<Light>(); #region Load Tileset int x = Convert.ToInt32(level.Tileset.x); int y = Convert.ToInt32(level.Tileset.y); tileset = new Tile[x][]; for (int i = 0; i < x; i++) tileset[i] = new Tile[y]; for (int j = 0; j < y; j++) for (int i = 0; i < x; i++) tileset[i][j] = new Tile(new Point(i, j), x, y, (TileType)level.Tileset.Tile[j * x + i].state); #endregion #region Load Entities if (level.Entities.Button != null) { foreach (ButtonInfo bInfo in level.Entities.Button) { Button b = new Button(bInfo.xPos, bInfo.yPos); b = LoadEntity<ButtonInfo, Button>(bInfo, b); entities.Add(b); } } if (level.Entities.BreakableDoor != null) { foreach (BreakableDoorInfo bdi in level.Entities.BreakableDoor) { BreakableDoor breakableDoor = new BreakableDoor(bdi.xPos, bdi.yPos); breakableDoor.MaxHits = bdi.maxHits; breakableDoor = LoadEntity<BreakableDoorInfo, BreakableDoor>(bdi, breakableDoor); entities.Add(breakableDoor); } } if (level.Entities.Cannon != null) { foreach (CannonInfo cInfo in level.Entities.Cannon) { Cannon c = new Cannon(cInfo.xPos, cInfo.yPos); c.BallSpeed = cInfo.shotVelocity; c.Stupidity = cInfo.stupidity; c = LoadEntity<CannonInfo, Cannon>(cInfo, c); entities.Add(c); } } if (level.Entities.Door != null) { foreach (DoorInfo dInfo in level.Entities.Door) { Door d = new Door(dInfo.xPos, dInfo.yPos); d.Dir = (Direction)dInfo.dir; d = LoadEntity<DoorInfo, Door>(dInfo, d); entities.Add(d); } } if (level.Entities.LaserShooter != null) { foreach (LaserShooterInfo lsi in level.Entities.LaserShooter) { LaserShooter ls = new LaserShooter(lsi.xPos, lsi.yPos); ls.BeamWidth = lsi.beamWidth; ls.ShotsPerSecond = lsi.shotsPerSecond; ls.Stupidity = lsi.stupidity; ls = LoadEntity<LaserShooterInfo, LaserShooter>(lsi, ls); entities.Add(ls); } } if (level.Entities.Ball != null) { foreach (BallInfo ballInfo in level.Entities.Ball) { Ball ball = new Ball(ballInfo.xPos, ballInfo.yPos); ball.Friction = ballInfo.friction; ball = LoadEntity<BallInfo, Ball>(ballInfo, ball); entities.Add(ball); } } if (level.Entities.Block != null) { foreach (BlockInfo blockInfo in level.Entities.Block) { Block block = new Block(blockInfo.xPos, blockInfo.yPos); block.Friction = blockInfo.friction; block = LoadEntity<BlockInfo, Block>(blockInfo, block); entities.Add(block); } } if (level.Entities.Player != null) { PlayerInfo pi = level.Entities.Player; Player player = new Player(pi.xPos, pi.yPos); player = LoadEntity<PlayerInfo, Player>(pi, player); entities.Add(player); } if (level.Entities.PuzzleBox != null) { foreach (PuzzleBoxInfo pbi in level.Entities.PuzzleBox) { PuzzleBox pb = new PuzzleBox(pbi.xPos, pbi.yPos); pb = LoadEntity<PuzzleBoxInfo, PuzzleBox>(pbi, pb); entities.Add(pb); } } if (level.Entities.SpikeWall != null) { foreach (SpikeWallInfo swi in level.Entities.SpikeWall) { SpikeWall sw = new SpikeWall(swi.xPos, swi.yPos); sw.Dir = (Direction)swi.dir; sw = LoadEntity<SpikeWallInfo, SpikeWall>(swi, sw); entities.Add(sw); } } if (level.Entities.Teleporter != null) { foreach (TeleporterInfo tInfo in level.Entities.Teleporter) { Teleporter t = new Teleporter(tInfo.xPos, tInfo.yPos); t = LoadEntity<TeleporterInfo, Teleporter>(tInfo, t); entities.Add(t); } } #endregion if (level.Triggers != null) { #region Load Causes if (level.Triggers.Cause != null) { foreach (LevelTriggersCause curCause in level.Triggers.Cause) { switch (curCause.type) { case causeType.CauseAND: { CauseAND c = new CauseAND(); string[] data = curCause.Value.Split(','); c.Cause1 = causes.Find(cause => cause.ID == data[0]); c.Cause2 = causes.Find(cause => cause.ID == data[1]); c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseButton: { CauseButton c = new CauseButton(); c.Button = entities.Find(ent => ent.ID == curCause.Value) as Button; c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseEntityDestruction: { CauseEntityDestruction c = new CauseEntityDestruction(); c.Entity = entities.Find(ent => ent.ID == curCause.Value); c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseLocation: { CauseLocation c = new CauseLocation(); string[] data = curCause.Value.Split(','); c.Player = entities.Find(ent => ent.ID == data[0]) as Player; c.MinimumX = float.Parse(data[1]); c.MaximumX = float.Parse(data[2]); c.MinimumY = float.Parse(data[3]); c.MaximumY = float.Parse(data[4]); c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseNOT: { CauseNOT c = new CauseNOT(); c.Cause = causes.Find(cause => cause.ID == curCause.Value); c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseOR: { CauseOR c = new CauseOR(); string[] data = curCause.Value.Split(','); c.Cause1 = causes.Find(cause => cause.ID == data[0]); c.Cause2 = causes.Find(cause => cause.ID == data[1]); c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseTimePassed: { CauseTimePassed c = new CauseTimePassed(); c.TriggerTime = int.Parse(curCause.Value); c.ID = curCause.id; causes.Add(c); break; } case causeType.CauseXOR: { CauseXOR c = new CauseXOR(); string[] data = curCause.Value.Split(','); c.Cause1 = causes.Find(cause => cause.ID == data[0]); c.Cause2 = causes.Find(cause => cause.ID == data[1]); c.ID = curCause.id; causes.Add(c); break; } } } } #endregion #region Load Effects if (level.Triggers.Effect != null) { foreach (LevelTriggersEffect curEffect in level.Triggers.Effect) { switch (curEffect.type) { case effectType.EffectAND: { EffectAND e = new EffectAND(); string[] data = curEffect.Value.Split(','); e.Effect1 = effects.Find(effect => effect.ID == data[0]); e.Effect2 = effects.Find(effect => effect.ID == data[1]); e.ID = curEffect.id; effects.Add(e); break; } case effectType.EffectDoor: { EffectDoor e = new EffectDoor(); e.Door = entities.Find(ent => ent.ID == curEffect.Value) as Door; e.ID = curEffect.id; effects.Add(e); break; } case effectType.EffectEndGame: { EffectEndGame e = new EffectEndGame(); e.CharlieSheen = bool.Parse(curEffect.Value); e.ID = curEffect.id; effects.Add(e); break; } case effectType.EffectList: { EffectList e = new EffectList(); string[] data = curEffect.Value.Split(','); foreach (string s in data) { e.List.Add(effects.Find(effect => effect.ID == s)); } effects.Add(e); break; } case effectType.EffectRaiseBridge: { EffectRaiseBridge e = new EffectRaiseBridge(); string[] data = curEffect.Value.Split(','); e.TileX = int.Parse(data[0]); e.TileY = int.Parse(data[1]); e.ID = curEffect.id; effects.Add(e); break; } case effectType.EffectRemoveEntity: { EffectRemoveEntity e = new EffectRemoveEntity(); e.Entity = entities.Find(ent => ent.ID == curEffect.Value); e.ID = curEffect.id; effects.Add(e); break; } case effectType.EffectTriggerTimer: { EffectTriggerTimer e = new EffectTriggerTimer(); e.Cause = causes.Find(cause => cause.ID == curEffect.Value) as CauseTimePassed; e.ID = curEffect.id; effects.Add(e); break; } } } } #endregion #region Load Triggers if (level.Triggers.Trigger != null) { foreach (LevelTriggersTrigger triggerData in level.Triggers.Trigger) { Trigger t = new Trigger(); t.Cause = causes.Find(cause => cause.ID == triggerData.cause); t.Effect = effects.Find(effect => effect.ID == triggerData.effect); triggers.Add(t); } } #endregion } #region Load Lights if (level.Lights != null) { for (int i = 0; i < level.Lights.Length; i++) { LightInfo ll = level.Lights[i]; Light l = new Light(); l.Index = ll.index; string[] ambient = ll.ambient.Split(','); l.AmbientR = float.Parse(ambient[0]); l.AmbientG = float.Parse(ambient[1]); l.AmbientB = float.Parse(ambient[2]); l.AmbientA = float.Parse(ambient[3]); string[] diffuse = ll.diffuse.Split(','); l.DiffuseR = float.Parse(diffuse[0]); l.DiffuseG = float.Parse(diffuse[1]); l.DiffuseB = float.Parse(diffuse[2]); l.DiffuseA = float.Parse(diffuse[3]); string[] position = ll.position.Split(','); l.PositionX = float.Parse(position[0]); l.PositionY = float.Parse(position[1]); l.PositionZ = float.Parse(position[2]); l.PositionW = float.Parse(position[3]); l.ConstantAttenuation = ll.constAtten; l.LinearAttenuation = ll.linAtten; l.QuadraticAttenuation = ll.quadAtten; lights.Add(l); } } #endregion UIEntListEditor.entList = entities; UICauseListEditor.causeList = causes; UIEffectListEditor.effectList = effects; UIFormEffectListEditor.effectList = effects; return true; } catch (Exception e) { causes = null; effects = null; entities = null; tileset = null; triggers = null; lights = null; System.Windows.Forms.MessageBox.Show("Error. Unable to load level. Reason: " + e.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return false; } }