Example #1
0
        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;
        }
Example #2
0
        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;
        }
Example #3
0
        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;
        }
Example #4
0
        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;
        }
Example #5
0
 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));
     }
 }
Example #6
0
        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);
            }
        }
Example #7
0
 public bool IsWalkable(Map map)
 {
     return CellType == CellType.Ground && GetCharacter(map)==null;
 }
Example #8
0
 public bool IsSpawnable(Map map)
 {
     return IsWalkable(map) && Gold == 0 && GetPotion(map)==null;
 }
Example #9
0
 public Player GetPlayer(Map map)
 {
     return GetCharacter(map) as Player;
 }
Example #10
0
 public Monster GetMonster(Map map)
 {
     return GetCharacter(map) as Monster;
 }