Exemplo n.º 1
0
    public override void HandleInput(InputHelper inputHelper)
    {
        base.HandleInput(inputHelper);

        Building b = null;

        //Mine or SunlightTree
        if (button1 != null && button1.Pressed)
        {
            button1.Sprite.SheetIndex      = 1;
            GameData.Cursor.HasClickedTile = false;
            GameData.LevelObjects.Remove(this);
            if (GameData.player.GetFaction == Player.Faction.humanity)
            {
                b = new Mine();
                if (GameData.player.MainResource - b.ResourceCosts.X >= 0 && GameData.player.SecondaryResource - b.ResourceCosts.Y >= 0)
                {
                    GameData.player.MainResource      -= b.ResourceCosts.X;
                    GameData.player.SecondaryResource -= b.ResourceCosts.Y;
                    tile.AddTimer(new Timer(sunlightTreeCooldown), new Mine());
                }
                else
                {
                    Notification n = new Notification("Not enough resources, it costs:", b.ResourceCosts.X.ToString() + " Coal and " + b.ResourceCosts.Y.ToString() + " Wood", "", 3);
                    n.CreateNotification();
                }
            }
            else
            {
                b = new SunlightTree();
                if (GameData.player.MainResource - b.ResourceCosts.X >= 0 && GameData.player.SecondaryResource - b.ResourceCosts.Y >= 0)
                {
                    GameData.player.MainResource      -= b.ResourceCosts.X;
                    GameData.player.SecondaryResource -= b.ResourceCosts.Y;
                    tile.AddTimer(new Timer(sunlightTreeCooldown), new SunlightTree());
                }
                else
                {
                    Notification n = new Notification("Not enough resources, it costs:", b.ResourceCosts.X.ToString() + " Sunlight and " + b.ResourceCosts.Y.ToString() + " Water", "", 3);
                    n.CreateNotification();
                }
            }
        }
        //Barracks, barracks
        else if (button2 != null && button2.Pressed)
        {
            button2.Sprite.SheetIndex      = 1;
            GameData.Cursor.HasClickedTile = false;
            GameData.LevelObjects.Remove(this);
            if (GameData.player.GetFaction == Player.Faction.humanity)
            {
                b = new HumanityBarrack();
                if (GameData.player.MainResource - b.ResourceCosts.X >= 0 && GameData.player.SecondaryResource - b.ResourceCosts.Y >= 0)
                {
                    GameData.player.MainResource      -= b.ResourceCosts.X;
                    GameData.player.SecondaryResource -= b.ResourceCosts.Y;
                    tile.AddTimer(new Timer(sunlightTreeCooldown), new HumanityBarrack());
                }
                else
                {
                    Notification n = new Notification("Not enough resources, it costs:", b.ResourceCosts.X.ToString() + " Coal and " + b.ResourceCosts.Y.ToString() + " Wood", "", 3);
                    n.CreateNotification();
                }
            }
            else
            {
                b = new NatureBarracks();
                if (GameData.player.MainResource - b.ResourceCosts.X >= 0 && GameData.player.SecondaryResource - b.ResourceCosts.Y >= 0)
                {
                    GameData.player.MainResource      -= b.ResourceCosts.X;
                    GameData.player.SecondaryResource -= b.ResourceCosts.Y;
                    tile.AddTimer(new Timer(sunlightTreeCooldown), new NatureBarracks());
                }
                else
                {
                    Notification n = new Notification("Not enough resources, it costs:", b.ResourceCosts.X.ToString() + " Sunlight and " + b.ResourceCosts.Y.ToString() + " Water", "", 3);
                    n.CreateNotification();
                }
            }
        }
        else if (button3 != null && button3.Pressed)
        {
            button3.Sprite.SheetIndex      = 1;
            GameData.Cursor.HasClickedTile = false;
            GameData.LevelObjects.Remove(this);
            if (GameData.player.GetFaction == Player.Faction.humanity)
            {
                b = new Mine();
                if (GameData.player.MainResource - b.ResourceCosts.X >= 0 && GameData.player.SecondaryResource - b.ResourceCosts.Y >= 0)
                {
                    GameData.player.MainResource      -= b.ResourceCosts.X;
                    GameData.player.SecondaryResource -= b.ResourceCosts.Y;
                    tile.AddTimer(new Timer(sunlightTreeCooldown), new Mine());
                }
                else
                {
                    Notification n = new Notification("Not enough resources, it costs:", b.ResourceCosts.X.ToString() + " Coal and " + b.ResourceCosts.Y.ToString() + " Wood", "", 3);
                    n.CreateNotification();
                }
            }
            else
            {
                b = new WaterTree();
                if (GameData.player.MainResource - b.ResourceCosts.X >= 0 && GameData.player.SecondaryResource - b.ResourceCosts.Y >= 0)
                {
                    GameData.player.MainResource      -= b.ResourceCosts.X;
                    GameData.player.SecondaryResource -= b.ResourceCosts.Y;
                    tile.AddTimer(new Timer(sunlightTreeCooldown), new WaterTree());
                }
                else
                {
                    Notification n = new Notification("Not enough resources, it costs:", b.ResourceCosts.X.ToString() + " Sunlight and " + b.ResourceCosts.Y.ToString() + " Water", "", 3);
                    n.CreateNotification();
                }
            }
        }
    }
Exemplo n.º 2
0
    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++;
            }
        }
    }