internal static void Give(string[] cmdParts) { if (cmdParts.Length < 3) { Utility.Print(ConsoleColor.Yellow, "Invalid command!"); return; } string monsterName = cmdParts[1].ToLower(); string itemName = string.Join(" ", cmdParts.Skip(2)).ToLower(); using (var context = new TextEngineContext()) { Room room = Room.ByID(context, Program.character.CurrentRoom.RoomID); Monster monster = room.Monsters.FirstOrDefault(m => m.MonsterType.Name.ToLower() == monsterName); if (monster == null) { Utility.Print(ConsoleColor.Yellow, "You don't see that monster here!"); return; } Item item = Character.GetInstance(context).Inventory.Items.FirstOrDefault(i => i.Name.ToLower() == itemName); if (item == null) { Utility.Print(ConsoleColor.Yellow, "You don't posses that item!"); return; } item.Inventory = monster.Inventory; context.SaveChanges(); Program.RefreshGameState(context); } }
private static void SetItem(string[] args) { using (var context = new TextEngineContext()) { Item item = Item.FromID(context, args[3]); if (item == null) { Utility.Print(ConsoleColor.Yellow, "Invalid item!"); return; } // todo: validate these var attribute = args[2]; var value = string.Join(" ", args.Skip(4)); switch (attribute) { case "name": item.Name = value; break; case "desc": item.Description = value; break; } context.SaveChanges(); Program.RefreshGameState(context); } }
public static string GetMapRow(int xMin, int xMax, int y, int z, int padLength) { using (var context = new TextEngineContext()) { var rooms = context.Rooms .Where(r => r.Z == z && r.Y == y && r.X >= xMin && r.X <= xMax) .OrderBy(o => o.X); string padString = new string(' ', padLength); string[] rows = Enumerable.Repeat(string.Empty, 5).ToArray(); for (int x = xMin; x <= xMax; ++x) { Room room = rooms.Where(r => r.X == x).FirstOrDefault(); int rowLength = 15; if (room == null) { for (int i = 0; i < rows.Length; i++) { rows[i] = new string(' ', padLength + rowLength++); } } else { // first string } } } return(""); }
public static void Set(string[] args) { if (args.Length < 3) { Utility.Print(ConsoleColor.Yellow, "Set command incorrect!\nExample: set title This is a title."); return; } string type = args[1].ToLower(); string value = string.Join(" ", args.Skip(2)); switch (type) { case "name": Program.character.CurrentRoom.Name = value; using (var context = new TextEngineContext()) { Room room = context.Rooms.Where(x => x.RoomID == Program.character.CurrentRoom.RoomID).First(); room.Name = value; context.SaveChanges(); } break; case "desc": Program.character.CurrentRoom.Description = value; using (var context = new TextEngineContext()) { Room room = context.Rooms.Where(x => x.RoomID == Program.character.CurrentRoom.RoomID).First(); room.Description = value; context.SaveChanges(); } break; case "n": case "s": case "e": case "w": case "nw": case "ne": case "sw": case "se": case "u": case "d": AdminCommands.SetDoorCondition(args); break; case "item": AdminCommands.SetItem(args); break; default: Utility.Print(ConsoleColor.Yellow, "Invalid command."); break; } }
public static void ListMonsterTypes() { using (var context = new TextEngineContext()) { Utility.Print(ConsoleColor.Blue, "Name\tTypeID\tHP\tHit%\tDmgMin\tDmgMax"); foreach (MonsterType mt in context.MonsterTypes) { Utility.Print(ConsoleColor.Blue, mt.Name + "\t" + mt.MonsterTypeID + "\t" + mt.HP + "\t" + mt.HitChance + "\t" + mt.DamageMinimum + "\t" + mt.DamageMaximum); } } }
public static void RefreshGameState(TextEngineContext context) { character = Character.GetInstance(context); if (character == null) { character = new Character(); character.SetInitialRoom(context); context.Add(character); context.SaveChanges(); } }
public static void DrawMap() { int x_min = Program.character.CurrentRoom.X - 3; int x_max = Program.character.CurrentRoom.X + 3; int y_min = Program.character.CurrentRoom.Y - 3; int y_max = Program.character.CurrentRoom.Y + 3; using (var context = new TextEngineContext()) { var rooms = context.Rooms.Where( r => r.X <= x_max && r.X >= x_min && r.Y <= y_max && r.Y >= y_min && r.Z == Program.character.CurrentRoom.Z); for (int y = y_max; y >= y_min; y--) { string line1 = string.Empty; string line2 = string.Empty; string line3 = string.Empty; for (int x = x_min; x <= x_max; x++) { var room = rooms .Where(r => r.X == x && r.Y == y) .Include(r => r.Doors) .FirstOrDefault(); if (room == null) { line1 += " "; line2 += " "; line3 += " "; } else { string[] temp = room.getMap(); line1 += temp[0]; line2 += temp[1]; line3 += temp[2]; } } Utility.Print(ConsoleColor.DarkGreen, line1); Utility.Print(ConsoleColor.DarkGreen, line2); Utility.Print(ConsoleColor.DarkGreen, line3); } } }
private static void CreateItem(string name) { using (var context = new TextEngineContext()) { var room = Room.ByID(context, Program.character.CurrentRoom.RoomID); //room.Inventory = Program.character.CurrentRoom.Inventory; var item = new Item(); item.Name = name; item.Description = "default description"; item.Inventory = room.Inventory; context.Items.Add(item); context.SaveChanges(); Program.RefreshGameState(context); } }
private static void CreateMonsterType(string value) { using (var context = new TextEngineContext()) { var m = context.MonsterTypes.FirstOrDefault(mon => mon.Name.ToLower() == value.ToLower()); if (m != null) { Utility.Print(ConsoleColor.Yellow, "A monster with that name already exists!"); return; } m = new MonsterType(); m.Name = value; context.MonsterTypes.Add(m); context.SaveChanges(); } }
private static void SetDoorCondition(string[] args) { if (args.Length < 3) { Utility.Print(ConsoleColor.Yellow, "Invalid command."); return; } string dir = args[1]; string type = args[2]; string itemId = args[3]; using (var context = new TextEngineContext()) { switch (args[2]) { case "item": Item item = Item.FromID(context, args[3]); if (item == null) { Utility.Print(ConsoleColor.Yellow, "Invalid item!"); return; } Door door = Door.FromDir(context, dir); if (item == null) { Utility.Print(ConsoleColor.Yellow, "Invalid direction!"); return; } var doorItemCondition = new DoorItemCondition(); doorItemCondition.Item = item; doorItemCondition.Door = door; context.DoorItemConditions.Add(doorItemCondition); context.SaveChanges(); break; default: Utility.Print(ConsoleColor.Yellow, "Invalid command!"); break; } } }
public static void DrawMap() { using (var context = new TextEngineContext()) { int map_radius = 2; int z = Program.character.CurrentRoom.Z; int xMin = Program.character.CurrentRoom.X - map_radius; int xMax = Program.character.CurrentRoom.X + map_radius; int yMin = Program.character.CurrentRoom.Y - map_radius; int yMax = Program.character.CurrentRoom.Y + map_radius; for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { } // ending a row } } }
public static void Attack(string[] cmdParts) { if (cmdParts.Length < 2) { Utility.Print(ConsoleColor.Yellow, "Invalid command!"); return; } string monsterName = cmdParts[1].ToLower(); var monster = Program.character.CurrentRoom.Monsters.FirstOrDefault(m => m.MonsterType.Name.ToLower() == monsterName); if (monster == null) { Utility.Print(ConsoleColor.Yellow, "You don't see that monster here!"); return; } int attack = Program.Random.Next(1, 101); if (attack < 75) { int damage = Program.Random.Next(1, 10); Utility.Print(ConsoleColor.DarkRed, "You smash the " + monster.MonsterType.Name + " for " + damage + " points of damage!"); monster.MonsterType.HP -= damage; if (monster.MonsterType.HP <= 0) { monster.Die(); using (var context = new TextEngineContext()) { context.Monsters.Remove(monster); context.SaveChanges(); Program.RefreshGameState(context); } } } else { Utility.Print(ConsoleColor.DarkGreen, "You miss the " + monster.MonsterType.Name + "."); } }
public static void Get(string[] cmdParts) { string itemName = string.Join(" ", cmdParts.Skip(1)).ToLower(); using (var context = new TextEngineContext()) { var localItem = Program.character.CurrentRoom.Inventory.Items.FirstOrDefault(i => i.Name.ToLower() == itemName); if (localItem == null) { Utility.Print(ConsoleColor.Yellow, "You don't see that item here!"); return; } var item = context.Items.FirstOrDefault(i => i.ItemID == localItem.ItemID); var character = Character.GetInstance(context); item.Inventory = character.Inventory; context.SaveChanges(); Program.RefreshGameState(context); Utility.Print(ConsoleColor.DarkGreen, "Taken."); } }
public static void SpawnMonster(string[] cmdParts) { string monsterName = string.Join(" ", cmdParts.Skip(1)).ToLower(); using (var context = new TextEngineContext()) { var monsterType = context.MonsterTypes.FirstOrDefault(m => m.Name.ToLower() == monsterName); if (monsterType == null) { Utility.Print(ConsoleColor.Yellow, "Invalid monster type!"); return; } var monster = new Monster(); monster.MonsterType = monsterType; //Room room = Room.ByID(context, Program.character.CurrentRoom.RoomID); monster.Room = Program.character.CurrentRoom; context.Monsters.Add(monster); context.Inventories.Add(monster.Inventory); context.SaveChanges(); Program.RefreshGameState(context); } }
private static void CreateRoom(Direction direction) { if (direction == Direction.None) { Utility.Print(ConsoleColor.Yellow, "Invalid direction."); return; } // check for existing room var door = Program.character.CurrentRoom.Doors.Where(d => d.Direction == direction).FirstOrDefault(); if (door != null) { Utility.Print(ConsoleColor.Yellow, "A room already exists in that location."); return; } // figure out the coordinates of the new room. // this seems a little clunky to me, but it will do for now. int x = Program.character.CurrentRoom.X; int y = Program.character.CurrentRoom.Y; int z = Program.character.CurrentRoom.Z; // autofill is REALLY handy here... switch (direction) { case Direction.North: ++y; break; case Direction.South: --y; break; case Direction.East: ++x; break; case Direction.West: --x; break; case Direction.Northwest: ++y; --x; break; case Direction.Southeast: --y; ++x; break; case Direction.Northeast: ++y; ++x; break; case Direction.Southwest: --y; --x; break; case Direction.Up: --z; break; case Direction.Down: ++z; break; } // create the room and doors using (var context = new TextEngineContext()) { Room r = new Room(); r.Name = "New Room"; r.Description = "New Room Description"; r.X = x; r.Y = y; r.Z = z; context.Rooms.Add(r); context.Inventories.Add(r.Inventory); // initial door Door d = new Door(); d.Direction = direction; d.SourceRoom = Program.character.CurrentRoom; d.TargetRoom = r; context.Doors.Add(d); // and back again Door d2 = new Door(); d2.Direction = Utility.DirectionOpposite(direction); d2.SourceRoom = r; d2.TargetRoom = Program.character.CurrentRoom; context.Doors.Add(d2); context.SaveChanges(); } Program.character.CurrentRoom.Look(); }
static void Main(string[] args) { Program.Random = new Random(); // initialize database using (var context = new TextEngineContext()) { // TODO: remove this //context.Database.EnsureDeleted(); if (context.Database.EnsureCreated()) { context.Seed(context); } RefreshGameState(context); } Program.character.CurrentRoom.Look(); for (var play = true; play;) { Console.WriteLine(); Console.Write("[HP=" + Program.character.HP + "]> "); string cmd = Console.ReadLine().Trim(); Console.WriteLine(); // split on one or more whitespace characters var cmdParts = Regex.Split(cmd, @"\s+"); var action = cmdParts[0].ToLower(); var cmdArgs = cmdParts.Skip(1); // on empty input, default to "look" if (string.IsNullOrEmpty(action)) { action = "l"; } switch (action) { #if TEXTENGINEADMIN case "create": AdminCommands.Create(cmdParts); break; case "set": AdminCommands.Set(cmdParts); break; case "list": AdminCommands.ListMonsterTypes(); break; case "spawn": AdminCommands.SpawnMonster(cmdParts); break; case "give": AdminCommands.Give(cmdParts); break; #endif case "n": case "north": case "s": case "south": case "e": case "east": case "w": case "west": case "nw": case "northwest": case "ne": case "northeast": case "sw": case "southwest": case "se": case "southeast": case "u": case "up": case "d": case "down": character.CurrentRoom.Move(Utility.StringToDirection(action)); break; case "l": case "look": PlayerCommands.Look(cmdParts); break; case "i": case "inventory": Program.character.Inventory.ShowInventory(); break; case "g": case "get": case "t": case "take": PlayerCommands.Get(cmdParts); break; case "drop": PlayerCommands.Drop(cmdParts); break; case "a": case "attack": PlayerCommands.Attack(cmdParts); break; case "q": case "quit": play = false; break; case "h": case "help": PlayerCommands.Help(); break; case "m": case "map": PlayerCommands.DrawMap(); break; default: Console.WriteLine("I beg your pardon?"); break; } #if !TEXTENGINEADMIN Utility.ProcessCombat(); #endif if (Program.character.HP < 0) { Utility.Print(ConsoleColor.Red, "YOU HAVE DIED."); play = false; } } Console.WriteLine("Thanks for playing!"); }