Example #1
0
        // Processing pressed keys (Player actions):
        private void KeyDown(object sender, KeyEventArgs e)
        {
            if (Level.Player.IsAlive == true && End == false)
            {
                int Position_x_new = Level.Player.Position_x;
                int Position_y_new = Level.Player.Position_y;
                inspect = false;
                move    = false;
                attack  = false;
                open    = false;

                if (inventory == true)
                {
                    int key = 999;
                    if (e.KeyCode == Keys.D0)
                    {
                        key = 0;
                    }
                    if (e.KeyCode == Keys.D1)
                    {
                        key = 1;
                    }
                    if (e.KeyCode == Keys.D2)
                    {
                        key = 2;
                    }
                    if (e.KeyCode == Keys.D3)
                    {
                        key = 3;
                    }
                    if (e.KeyCode == Keys.D4)
                    {
                        key = 4;
                    }
                    if (e.KeyCode == Keys.D5)
                    {
                        key = 5;
                    }
                    if (e.KeyCode == Keys.D6)
                    {
                        key = 6;
                    }
                    if (e.KeyCode == Keys.D7)
                    {
                        key = 5;
                    }
                    if (e.KeyCode == Keys.D8)
                    {
                        key = 8;
                    }
                    if (e.KeyCode == Keys.D9)
                    {
                        key = 9;
                    }

                    if (Level.Player.Inventory.Items.Count >= key)
                    {
                        if (Level.Player.Inventory.Weapons.Any(w => w.Name == Level.Player.Inventory.Items[key]))
                        {
                            if ((Level.Player.Inventory.Weapons.Find(w => w.Name == Level.Player.Inventory.Items[key])).Weight > Level.Player.Endurance)
                            {
                                c.Viewports[3].Buffer.Write(">It is too heavy for you\n");
                            }
                            else
                            {
                                Level.Player.Active_Weapon = Level.Player.Inventory.Weapons.Find(w => w.Name == Level.Player.Inventory.Items[key]);
                                c.Viewports[3].Buffer.Write(String.Format(">You have equipped {0}\n", Level.Player.Active_Weapon.Name));
                            }
                        }
                        if (Level.Player.Inventory.Armors.Any(w => w.Name == Level.Player.Inventory.Items[key]))
                        {
                            if ((Level.Player.Inventory.Armors.Find(w => w.Name == Level.Player.Inventory.Items[key])).Weight > Level.Player.Endurance)
                            {
                                c.Viewports[3].Buffer.Write(">It is too heavy for you\n");
                            }
                            else
                            {
                                Level.Player.Active_Armor = Level.Player.Inventory.Armors.Find(w => w.Name == Level.Player.Inventory.Items[key]);
                                c.Viewports[3].Buffer.Write(String.Format(">You have equipped {0}\n", Level.Player.Active_Armor.Name));
                            }
                        }
                        inventory = false;
                    }
                }

                // Controls Help:
                if (e.KeyCode == Keys.F1)
                {
                    inspect = false;
                    move    = false;
                    attack  = false;
                    c.Viewports[3].Buffer.Write("\n");
                    c.Viewports[3].Buffer.Write("[Controls]\n");
                    c.Viewports[3].Buffer.Write("[O] - Open Inventory\n");
                    c.Viewports[3].Buffer.Write("[P] - Character Info\n");
                    c.Viewports[3].Buffer.Write("[K], then point the direction - Attack\n");
                    c.Viewports[3].Buffer.Write("[L], then point the direction - Inspect\n");
                    c.Viewports[3].Buffer.Write("[N], then point the direction - Open (a chest)\n");
                    c.Viewports[3].Buffer.Write("[M] - Drink Health Poison\n");
                    c.Viewports[3].Buffer.Write("\n");
                }

                // Charachter Info:
                if (e.KeyCode == Keys.P)
                {
                    inspect = false;
                    move    = false;
                    attack  = false;
                    open    = false;
                    List <string> info = new List <string>();
                    info.Add(String.Format("\n{0}:\n", Level.Player.Name));
                    info.Add(String.Format("Level: {0}\n", Level.Player.Lvl));
                    info.Add(String.Format("Exp: {0}\n", Level.Player.Exp));
                    info.Add(String.Format("Strength: {0}\n", Level.Player.Strength));
                    info.Add(String.Format("Intelligence: {0}\n", Level.Player.Intelligence));
                    info.Add(String.Format("Dexterity: {0}\n", Level.Player.Dexterity));
                    info.Add(String.Format("Endurace: {0}\n\n", Level.Player.Endurance));

                    foreach (string str in info)
                    {
                        c.Viewports[3].Buffer.Write(str);
                    }
                }

                // Show Inventory:
                if (e.KeyCode == Keys.O)
                {
                    inspect   = false;
                    move      = false;
                    attack    = false;
                    open      = false;
                    inventory = true;
                    List <string> inv = new List <string>();
                    int           k   = 0;
                    inv.Add("\n>You have:\n");
                    inv.Add(">Weapons:\n");
                    foreach (Weapon weapon in Level.Player.Inventory.Weapons)
                    {
                        if (!inv.Contains(weapon.Name))
                        {
                            string isEquipped = "";
                            if (Level.Player.Active_Weapon == weapon)
                            {
                                isEquipped = "(E)";
                            }
                            inv.Add(String.Format(">{0} [{1}] {2}\n", weapon.Name, k, isEquipped));
                            k++;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    inv.Add("\n>Armors:\n");
                    foreach (Armor armor in Level.Player.Inventory.Armors)
                    {
                        if (!inv.Contains(armor.Name))
                        {
                            string isEquipped = "";
                            if (Level.Player.Active_Armor == armor)
                            {
                                isEquipped = "(E)";
                            }
                            inv.Add(String.Format(">{0} [{1}] {2}\n", armor.Name, k, isEquipped));
                            k++;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    inv.Add(String.Format("\n>Ale: {0}\n\n", Level.Player.Inventory.Poisons.Count));

                    foreach (string str in inv)
                    {
                        c.Viewports[3].Buffer.Write(str);
                    }
                }

                // Wait:
                if ((e.KeyCode == Keys.S || e.KeyCode == Keys.NumPad5) && lvlup == false)
                {
                    inspect = false;
                    move    = false;
                    attack  = false;
                    open    = false;
                    Next_Turn();
                }

                // Movement:
                if (e.KeyCode == Keys.Q || e.KeyCode == Keys.NumPad7)
                {
                    Position_x_new = Level.Player.Position_x - 1;
                    Position_y_new = Level.Player.Position_y - 1;
                    move           = true;
                }
                if (e.KeyCode == Keys.W || e.KeyCode == Keys.NumPad8)
                {
                    Position_x_new = Level.Player.Position_x;
                    Position_y_new = Level.Player.Position_y - 1;
                    move           = true;
                }
                if (e.KeyCode == Keys.E || e.KeyCode == Keys.NumPad9)
                {
                    Position_x_new = Level.Player.Position_x + 1;
                    Position_y_new = Level.Player.Position_y - 1;
                    move           = true;
                }
                if (e.KeyCode == Keys.D || e.KeyCode == Keys.NumPad6)
                {
                    Position_x_new = Level.Player.Position_x + 1;
                    Position_y_new = Level.Player.Position_y;
                    move           = true;
                }
                if (e.KeyCode == Keys.C || e.KeyCode == Keys.NumPad3)
                {
                    Position_x_new = Level.Player.Position_x + 1;
                    Position_y_new = Level.Player.Position_y + 1;
                    move           = true;
                }
                if (e.KeyCode == Keys.X || e.KeyCode == Keys.NumPad2)
                {
                    Position_x_new = Level.Player.Position_x;
                    Position_y_new = Level.Player.Position_y + 1;
                    move           = true;
                }
                if (e.KeyCode == Keys.Z || e.KeyCode == Keys.NumPad1)
                {
                    Position_x_new = Level.Player.Position_x - 1;
                    Position_y_new = Level.Player.Position_y + 1;
                    move           = true;
                }
                if (e.KeyCode == Keys.A || e.KeyCode == Keys.NumPad4)
                {
                    Position_x_new = Level.Player.Position_x - 1;
                    Position_y_new = Level.Player.Position_y;
                    move           = true;
                }


                // Inspect:
                // ( + inspect arg ex):
                if (pressedKey == Keys.L)
                {
                    if (e.KeyCode == Keys.L)
                    {
                        inspect = false;
                        move    = false;
                    }
                    else
                    {
                        inspect = true;
                        move    = false;
                    }
                }

                // Open:
                if (pressedKey == Keys.N)
                {
                    if (e.KeyCode == Keys.N)
                    {
                        open = false;
                        move = false;
                    }
                    else
                    {
                        open = true;
                        move = false;
                    }
                }

                // Attack:
                if (pressedKey == Keys.K)
                {
                    if (e.KeyCode == Keys.K)
                    {
                        attack = false;
                        move   = false;
                    }
                    else
                    {
                        attack = true;
                        move   = false;
                    }
                }

                // Action:

                if (e.KeyCode == Keys.M && lvlup == false && inspect == false && attack == false)
                {
                    nextTurnStrings.Add(Level.Player.Restore_HP());
                    Next_Turn();
                }

                if (open == true && lvlup == false)
                {
                    if (Level.Player.NearObjects.Any(obj => obj.Position_x == Position_x_new && obj.Position_y == Position_y_new && obj.GetType() == (new ItemContainer()).GetType()))
                    {
                        nextTurnStrings.AddRange(Level.Player.Take(Level.ItemContainers.Find(c => c.Position_x == Position_x_new && c.Position_y == Position_y_new)));
                        Level.ItemContainers.RemoveAll(c => c.Position_x == Position_x_new && c.Position_y == Position_y_new);
                        Level.Objects.Remove(Level.Objects.FindLast(c => c.Position_x == Position_x_new && c.Position_y == Position_y_new));
                        Next_Turn();
                    }
                    else
                    {
                        nextTurnStrings.Add("\n>You open an imaginary box and take imaginary nothing from it\n");
                        Next_Turn();
                    }
                }

                if (inspect == true && lvlup == false)
                {
                    c.Viewports[3].Buffer.Write(Inspect(Level.Player, Position_x_new, Position_y_new));
                    c.Viewports[3].Buffer.Write("\n\n");
                }

                if (attack == true && lvlup == false)
                {
                    if (Position_x_new == Level.Player.Position_x && Position_y_new == Level.Player.Position_y)
                    {
                        c.Viewports[3].Buffer.Write(">You attempt to injure yourself and make a small cut on a hand.\nIt is as far as you can go with self-harming\n\n");
                    }
                    else
                    {
                        if (!Level.Creatures.Any(cr => cr.Position_x == Position_x_new && cr.Position_y == Position_y_new))
                        {
                            c.Viewports[3].Buffer.Write(">You start slashing air in front of you, but nothing happens\n\n");
                        }
                        else
                        {
                            nextTurnStrings = Level.Player.Attack(Level.Creatures.Find(cr => cr.Position_x == Position_x_new && cr.Position_y == Position_y_new), rnd);
                            Next_Turn();
                        }
                    }
                }

                if (inspect == false && move == true && lvlup == false)
                {
                    if (Level.Player.MovementCheck(Position_x_new, Position_y_new))
                    {
                        Level.Player.Move(Position_x_new, Position_y_new);
                    }
                }

                if (lvlup == true)
                {
                    inspect = false;
                    move    = false;
                    attack  = false;
                    if (e.KeyCode == Keys.S)
                    {
                        Level.Player.LvlUp("S");
                        c.Viewports[3].Buffer.Write("Strength+\n");
                        lvlup = false;
                    }
                    if (e.KeyCode == Keys.I)
                    {
                        Level.Player.LvlUp("I");
                        c.Viewports[3].Buffer.Write("Intelligence+\n");
                        lvlup = false;
                    }
                    if (e.KeyCode == Keys.D)
                    {
                        Level.Player.LvlUp("D");
                        c.Viewports[3].Buffer.Write("Dexterity+\n");
                        lvlup = false;
                    }
                    if (e.KeyCode == Keys.E)
                    {
                        Level.Player.LvlUp("E");
                        c.Viewports[3].Buffer.Write("Endurance+\n");
                        lvlup = false;
                    }
                }

                pressedKey = e.KeyCode;
            }

            else
            {
                c.Close();
            }
        }
Example #2
0
        static void Main()
        {
            using (c = Console.NewConsole(45, 30))
            {
                c.Text = "9ROGUE";
                c.Show();


                Font currentFont = c.TexManager.DefaultFont;
                Font italic      = new Font(currentFont, FontStyle.Italic);
                Font bold        = new Font(currentFont, FontStyle.Bold);
                c.TexManager.AddPreset(italic, Color.Transparent, Color.White);
                c.TexManager.AddPreset(bold, Color.Transparent, Color.White);
                c.TexManager.AddPreset(currentFont, Color.Transparent, Color.Green);
                c.TexManager.AddPreset(currentFont, Color.White, Color.Red);

                string        playerName = "";
                List <string> toName     = new List <string>();

                c.Viewports[0].Buffer.Editing = true;

                c.Write("Enter your name:\n");

                c.KeyPress += (sender, e) =>
                {
                    if (!char.IsControl(e.KeyChar) && c.Viewports[0].Buffer.Editing == true)
                    {
                        if (toName.Count < 15)
                        {
                            c.Viewports[0].Buffer.Write(e.KeyChar.ToString());
                            toName.Add(e.KeyChar.ToString().Replace(' ', '_'));
                        }
                    }
                    if ((e.KeyChar == 'Y' || e.KeyChar == 'y') && c.Viewports[0].Buffer.Editing == false)
                    {
                        Player = new Player(playerName);
                        c.Close();
                        Run();
                    }
                };

                c.KeyDown += (sender, e) =>
                {
                    if (e.KeyCode == Keys.Enter && c.Viewports[0].Buffer.Editing == true && toName.Any(x => x != ""))
                    {
                        c.Viewports[0].Buffer.Write("\n");

                        char[] name = new char[toName.Count];
                        for (int i = 0; i < toName.Count; i++)
                        {
                            name[i] = Convert.ToChar(toName[i]);
                        }

                        playerName = new string (name);

                        c.Write("\n");
                        c.Write("Hello, ");
                        c.Write(playerName);
                        c.Write("!\n");
                        c.Write("Welcome to ");
                        c.WriteFormat("9ROGUE\n\n", 2);
                        c.WriteLine("Movement controls:\n");
                        c.WriteLine("Q W E              7 8 9");
                        c.WriteLine("A   D  or  numpad: 4   6");
                        c.WriteLine("Z X C              1 2 3\n");
                        c.WriteLine("[F1] (in-game) for additional controls");
                        c.WriteLine("\n");
                        c.WriteLine("Ready to start your journey? (Y)");
                        c.WriteLine("\n");
                        c.WriteLine("(Loading may take a while!)");

                        c.Viewports[0].Buffer.Editing = false;
                    }


                    if (e.KeyCode == Keys.Back && c.Viewports[0].Buffer.Editing == true)
                    {
                        if (toName.Count > 0)
                        {
                            c.Viewports[0].Buffer.Backspace();
                            toName.RemoveAt(toName.Count - 1);
                        }
                    }
                };

                while (ConsoleCreated)
                {
                    c.Render();
                    Application.DoEvents();
                }
            }
        }