private void parseAction(string s) { string[] pairs = s.Split('$'); if (pairs.Length > 1) { string sig = pairs[1].Substring(0, 4); if (sig.Equals("unit")) { string id = pairs[1].Substring(5, pairs[1].Length - 5); for (int i = 2; i < pairs.Length; i++) { switch (pairs[i].Substring(0, 4)) { case "move": try { string[] coords = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); Unit u = ((Unit)(GameData.LevelObjects.Find(id))); u.TargetPosition = new Vector2(float.Parse(coords[0]), float.Parse(coords[1])); u.TargetUnit = null; } catch (NullReferenceException e) { string[] coords = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); Unit u = ((Unit)(GameData.LevelObjects.Find(id))); if (u != null) { u.TargetPosition = new Vector2(float.Parse(coords[0]), float.Parse(coords[1])); //u.TargetUnit = null; } Console.WriteLine("null"); } break; case "targ": try { string targID = pairs[i].Substring(5, pairs[i].Length - 5); Unit theUnit = ((Unit)(GameData.LevelObjects.Find(id))); Unit targetU = (Unit)GameData.LevelObjects.Find(targID); theUnit.SetTargetUnit(targID); } catch (NullReferenceException e) { } break; case "tgbd": string bdtgID = pairs[i].Substring(5, pairs[i].Length - 5); ((Unit)(GameData.LevelObjects.Find(id))).targetBuilding = (Building)GameData.Buildings.Find(bdtgID); break; case "buil": //build break; case "damg": try { string[] parameters = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); string attackerID = parameters[1]; Unit attacker = (Unit)(GameData.LevelObjects.Find(attackerID)); ((Unit)(GameData.LevelObjects.Find(id))).DealDamage(int.Parse(parameters[0]), attacker); } catch (NullReferenceException e) { Console.WriteLine(e.ToString()); } break; case "dead": try { GameData.Units.Remove(((Unit)(GameData.LevelObjects.Find(id)))); } catch (NullReferenceException e) { Console.WriteLine(e.ToString()); } break; } } } else if (sig.Equals("bdng")) { Building b = null; bool polytile = false; string id = pairs[1].Substring(5, pairs[1].Length - 5); for (int i = 2; i < pairs.Length; i++) { switch (pairs[i].Substring(0, 4)) { case "type": string type = pairs[i].Substring(5, pairs[i].Length - 5); switch (type) { case "NatureBarracks": b = new NatureBarracks(); break; case "HumanityBarrack": b = new HumanityBarrack(); polytile = true; break; case "SunlightTree": b = new SunlightTree(); break; case "NatureBase": b = new NatureBase(); polytile = true; break; case "HumanityBase": b = new HumanityBase(); polytile = true; break; case "Mine": b = new Mine(); break; case "WaterTree": b = new WaterTree(); break; } break; case "posi": string[] coords = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); b.gridPosition = new Point(int.Parse(coords[0]), int.Parse(coords[1])); break; case "fnsh": GameData.LevelGrid.replaceTile((Tile)GameData.LevelGrid.Objects[b.gridPosition.X, b.gridPosition.Y], b, false); if (polytile) { ((PolyTileBuilding)b).AddQuadCoTiles(); } GameData.Buildings.Add(b); break; case "damg": try { string[] parameters = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); string attackerID = parameters[1]; Unit attacker = (Unit)(GameData.LevelObjects.Find(attackerID)); ((Building)(GameData.Buildings.Find(id))).DealDamage(int.Parse(parameters[0]), attacker); } catch (NullReferenceException e) { Console.WriteLine(e.ToString()); } break; case "dead": try { GameData.Buildings.Remove(((Building)(GameData.LevelObjects.Find(id)))); } catch (NullReferenceException e) { Console.WriteLine(e.ToString()); } break; } } } else if (sig.Equals("spel")) { Spell spell = null; string id = pairs[1].Substring(5, pairs[1].Length - 5); for (int i = 2; i < pairs.Length; i++) { switch (pairs[i].Substring(0, 4)) { case "type": switch (pairs[i].Substring(5, pairs[i].Length - 5)) { case "MeteorStorm": spell = new MeteorStorm(); break; case "SnowStorm": spell = new SnowStorm(); break; case "Spell": spell = new Spell(); break; } break; case "posi": string[] coords = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); spell.Position = new Vector2(float.Parse(coords[0]), float.Parse(coords[1])); break; } } spell.ID = id; GameData.LevelObjects.Add(spell); } else if (sig.Equals("addu")) { Unit u;//$addu:10$type:HumanityWorker$posi:1080,420 u = null; string id = pairs[1].Substring(5, pairs[1].Length - 5); for (int i = 2; i < pairs.Length; i++) { switch (pairs[i].Substring(0, 4)) { case "type": switch (pairs[i].Substring(5, pairs[i].Length - 5)) { case "HumanityWorker": u = new HumanityWorker(); break; case "NatureWorker": u = new NatureWorker(); break; case "Melee1": string asset = ""; asset = "natureWolf"; if (GameData.player.OppositeFaction == Player.Faction.humanity) { asset = "chainsaw"; } u = new Melee1(GameData.player.OppositeFaction, asset, id); break; case "Ranged": asset = "natureWolf"; if (GameData.player.OppositeFaction == Player.Faction.humanity) { asset = "flamethrower"; } u = new Ranged(GameData.player.OppositeFaction, asset, id); break; case "Melee2": asset = "treeUnit"; if (GameData.player.OppositeFaction == Player.Faction.humanity) { asset = "flamethrower"; } u = new Melee2(GameData.player.OppositeFaction, asset, id); break; case "FlameThrower": u = new FlameThrower(); break; case "Unicorn": asset = "unicorn"; if (GameData.player.OppositeFaction == Player.Faction.humanity) { asset = "quad"; } u = new Unicorn(GameData.player.OppositeFaction, asset, id); break; case "WoodCutter": u = new WoodCutter(); break; case "Unit": u = new Unit(); break; } break; case "posi": string[] coords = pairs[i].Substring(5, pairs[i].Length - 5).Split(','); u.Position = new Vector2(int.Parse(coords[0]), int.Parse(coords[1])); break; } } u.ID = id; GameData.Units.Add(u); GameData.unitIdIndex++; } } }
public override void HandleInput(InputHelper inputHelper) { base.HandleInput(inputHelper); Unit unit = null; if (button1 != null && button1.Pressed && (tile as Building).level < (tile as Building).maxLevel) { if (GameData.player.MainResource - 200 >= 0 && GameData.player.SecondaryResource - 150 >= 0) { button1.Sprite.SheetIndex = 1; (tile as Building).level += 1; GameData.Cursor.HasClickedTile = false; GameData.LevelObjects.Remove(this); } else { Notification n = new Notification("Not enough resources, it costs:", " 200 and 150", "", 3); n.CreateNotification(); } } //Create melee1 unit if (button2 != null && button2.Pressed) { button2.Sprite.SheetIndex = 1; GameData.Cursor.HasClickedTile = false; if (tile is NatureBarracks) { unit = new Melee1(Player.Faction.nature, "natureWolf", "natureWolf"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Sunlight and " + unit.ResourceCosts.Y.ToString() + " Water", "", 3); n.CreateNotification(); unit = null; } } else { unit = new Melee1(Player.Faction.humanity, "chainsaw", "chainsaw"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Coal and " + unit.ResourceCosts.Y.ToString() + " Wood", "", 3); n.CreateNotification(); unit = null; } } } //Create ranged unit else if (button3 != null && button3.Pressed) { button3.Sprite.SheetIndex = 1; GameData.Cursor.HasClickedTile = false; if (tile is NatureBarracks) { unit = new Ranged(Player.Faction.nature, "pelletGun", "rangedNature"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Sunlight and " + unit.ResourceCosts.Y.ToString() + " Water", "", 3); n.CreateNotification(); unit = null; } } else { unit = new Ranged(Player.Faction.humanity, "hunter", "rangedHumanity"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Coal and " + unit.ResourceCosts.Y.ToString() + " Wood", "", 3); n.CreateNotification(); unit = null; } } } //Create melee2 unit else if (button4 != null && button4.Pressed) { button4.Sprite.SheetIndex = 1; GameData.Cursor.HasClickedTile = false; if (tile is NatureBarracks) { unit = new Melee2(Player.Faction.nature, "treeUnit", "melee2Nature"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Sunlight and " + unit.ResourceCosts.Y.ToString() + " Water", "", 3); n.CreateNotification(); unit = null; } } else { unit = new Melee2(Player.Faction.humanity, "flamethrower", "melee2Human"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Coal and " + unit.ResourceCosts.Y.ToString() + " Wood", "", 3); n.CreateNotification(); unit = null; } } } //Create fast_melee unit (renamed to Unicorn) else if (button5 != null && button5.Pressed) { button5.Sprite.SheetIndex = 1; GameData.Cursor.HasClickedTile = false; if (tile is NatureBarracks) { unit = new Unicorn(Player.Faction.nature, "unicorn", "unicorn"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Sunlight and " + unit.ResourceCosts.Y.ToString() + " Water", "", 3); n.CreateNotification(); unit = null; } } else { unit = new Unicorn(Player.Faction.humanity, "quad", "fastmeleeHuman"); if (player.MainResource - unit.ResourceCosts.X >= 0 && player.SecondaryResource - unit.ResourceCosts.Y >= 0) { player.MainResource -= unit.ResourceCosts.X; player.SecondaryResource -= unit.ResourceCosts.Y; } else { Notification n = new Notification("Not enough resources, it costs:", unit.ResourceCosts.X.ToString() + " Coal and " + unit.ResourceCosts.Y.ToString() + " Wood", "", 3); n.CreateNotification(); unit = null; } } } if (unit != null) { unit.Position = new Vector2(tile.Position.X + new Tile().Sprite.Width / 2 - unit.Sprite.Width / 2, tile.Position.Y + new Tile().Sprite.Height / 2); GameData.AddUnit(unit); this.actionString = "$addu:" + unit.ID + "$type:" + unit.GetType() + "$posi:" + unit.Position.X + "," + unit.Position.Y;//; } }