public static Map Load(string chart) { var map = new Map(); int index = 0; for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { char c = chart[index++]; CellType cellType; if (c == '.') { cellType = CellType.Ground; } else if (c == '#') { cellType = CellType.Forest; } else { throw new ArgumentException($"Char '{c}' not valid. Map needs to be empty."); } Cell cell = new Cell(y, x, cellType); map.Cells[y, x] = cell; } } return map; }
public Potion GetPotion(Map map) { lock (map.potions) { foreach (var potion in map.potions) { if (potion.X == X && potion.Y == Y) { return potion; } } } return null; }
public static Map Load() { var map = new Map(); for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (random.NextDouble() <= FOREST_CHANCE) { map.Cells[y, x] = new Cell(y, x, CellType.Forest); } else { map.Cells[y, x] = new Cell(y, x, CellType.Ground); } } } for (int i = 0; i < random.Next(1, 4); i++) { map.Cells = map.DoSimulationStep(); } return map; }
public Character GetCharacter(Map map) { lock (map.players) { foreach (var c in map.players) { if (c.X == X && c.Y == Y) { return c; } } } lock (map.monsters) { foreach (var c in map.monsters) { if (c.X == X && c.Y == Y) { return c; } } } return null; }
static void StartGame(Request request) { if (request.Status == RequestStatus.START) { state = JsonConvert.DeserializeObject<State>(request.Data); map = Map.Load(state.MapString); Write(); gameStarted = true; protocol.Send(new Response(ResponseStatus.OK)); } }
static void StartGame() { if (!gameStarted) { gameStarted = true; map = Map.Load(); Client[] loggedInClients; lock (clients) { loggedInClients = clients.Where(c => c.IsLoggedIn).ToArray(); } var players = new List<Player>(); for (int i = 0; i < loggedInClients.Length; i++) { var client = loggedInClients[i]; var player = new Player(Player.IdCounter++, 20, 20, 0, Player.icons.ElementAt(i)); players.Add(player); client.Player = player; } Player[] playerArray = players.ToArray(); State state = new State(null, map.MapToString(), null, playerArray); foreach (var client in loggedInClients) { state.Player = client.Player; string data = JsonConvert.SerializeObject(state); Request(client, new Request(RequestStatus.START, DataType.JSON, data)); } map.Changed += (map, changes) => { UpdateState(changes); }; map.Start(playerArray); } }
public bool IsWalkable(Map map) { return CellType == CellType.Ground && GetCharacter(map)==null; }
public bool IsSpawnable(Map map) { return IsWalkable(map) && Gold == 0 && GetPotion(map)==null; }
public Player GetPlayer(Map map) { return GetCharacter(map) as Player; }
public Monster GetMonster(Map map) { return GetCharacter(map) as Monster; }