Ejemplo n.º 1
0
        /// <summary>
        /// Instantiates all the classes needed for the game
        /// </summary>
        public override void Initialize()
        {
            //generate Level from file
            var parser = new LevelParser();
            level = parser.GenerateLevel(ScreenManager.Content.Load<List<String[]>>(@"Levels\level1"));

            //create a camer, for we want to see the game
            camera = new Camera(0.4f, Vector2.Zero, 0, true, null);

            Point size = new Point(49, 49);

            PlayerController playerController = new PlayerController(Direction.None, "PacMa", 1, PlayerIndex.One,
                                                                     MovObjType.LocalPacMan, ScreenManager.Input);

            //Create a player object
            PacMan player = new PacMan(@"Sprites\PacMan", level.getCell(1, 1), level,
                                                new PlayerController(Direction.None, "Player 1",1, PlayerIndex.One, MovObjType.LocalPacMan, ScreenManager.Input), Direction.None, 3f, size);

            //Create all ghosts for the game

            Ghost blinky = new Ghost(@"Sprites\GhostBase", level.getCell(26, 1), level, Direction.Down, 3f, size, Color.Red, new Blinky(player, new Point(0, 0), new Point(15, 15), new Point(16, 16)));
            Ghost pinky = new Ghost(@"Sprites\GhostBase", level.getCell(26, 29), level, Direction.None, 3f, size, Color.Pink, new Pinky(player, new Point(0, 28), new Point(15, 15), new Point(16, 16)));
            Ghost inky = new Ghost(@"Sprites\GhostBase", level.getCell(26, 14), level, Direction.None, 3f, size, Color.Blue,
                                   new Inky(player, blinky, new Point(31, 0), new Point(15, 15), new Point(16, 16)));
            Ghost clyde = new Ghost(@"Sprites\GhostBase", level.getCell(12, 17), level, Direction.None, 3f, size, Color.Yellow,
                                    new Clyde(player, new Point(31, 28), new Point(15, 15), new Point(16, 16)));

            //Initialize the list of gui elements
            guiElements = new List<GUIElement>();
            GUIString stringElement = new GUIString(player, Color.White, new Vector2(0, 0), new Vector2(10, 10));

            guiElements.Add(stringElement);

            //Store everything inside the gameStateManager
            gameStateManager = new GameStateManager();
            gameStateManager.GameState = GameState.Loading;

            gameStateManager.AddPlayers(player, blinky, pinky, inky, clyde);

            gameStateManager.Level = level;

            //            movOb.Add(player);
            //            movOb.Add(blinky);
            //            movOb.Add(pinky);
            //            movOb.Add(inky);
            //            movOb.Add(clyde);

            //Initialize the gameplay behaviours
            playBehaviour = new PlayBehaviour();
            deathBehaviour = new DeathBehaviour();

            gameStateManager.GetAllOfType<PacMan>();
            gameStateManager.GetAllOfType<Ghost>();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a new blinky AI
 /// </summary>
 /// <param name="pacMan">A pacman</param>
 /// <param name="scatterTarget">the cell this ghost targets in scatter mode</param>
 /// <param name="exitPoint">the cell where this ghost leaves the house</param>
 /// <param name="homeTarget">the cell where this ghost has his home</param>
 public Blinky(PacMan pacMan, Point scatterTarget, Point exitPoint, Point homeTarget)
     : base(scatterTarget, exitPoint, homeTarget, MovObjType.Blinky)
 {
     this.pacMan = pacMan;
     this.ghostBehaviour = EGhostBehaviour.Exiting;
 }
Ejemplo n.º 3
0
        private void msgTicker_Tick(object sender, EventArgs e)
        {
            //Console.WriteLine(gameTime.TotalGameTime.TotalSeconds+" server");
            if (server != null)
            {
                NetIncomingMessage msg;
                NetOutgoingMessage response;
                NetConnection msgCon;
                string msgString;
                int msgInt;

                // check for new messages (all messages in queu per update cycle)
                while ((msg = networkManager.getMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        // Client sucht nach einem Server
                        case NetIncomingMessageType.DiscoveryRequest:
                            this.WriteToLog("Discoveryanfrage von: " + msg.SenderEndpoint.Address.ToString());
                            // Create a response
                            response = networkManager.createMessage();

                            // Send the response to the sender of the request
                            try
                            {
                                server.SendDiscoveryResponse(response, msg.SenderEndpoint);
                                this.WriteToLog("Antwort an " + msg.SenderEndpoint + " geschickt");
                            }
                            catch (Exception)
                            {
                                this.WriteToLog("Antwort an " + msg.SenderEndpoint + " konnte nicht gesendet werden");
                            }
                            break;

                        // Es handelt sich um ein Datenpaket
                        case NetIncomingMessageType.Data:
                            msgString = msg.ReadString();

                            switch (msgString)
                            {
                                // Client startete Spiel und wartet nun auf Initialisierung vom Server
                                case "startGame":
                                    string fileName = @"\Levels\level1.csv";
                                    gameStateManager.Level = new LevelProcessor().LoadMap(fileName);
                                    this.WriteToLog("Level " + fileName + " loaded");

                                    msgInt = msg.ReadInt32();
                                    foreach (Client c in connectedClients)
                                    {
                                        if (c.getConnection() == msg.SenderConnection)
                                        {
                                            c.setFigureType(msgInt);

                                            switch (c.getFigureType())
                                            {
                                                case 1:
                                                    NetworkController networkController =
                                                        new NetworkController(c.getUID(), EmptyController.Empty(c.getUID()),
                                                                              networkManager);

                                                    PacMan pacMan = new PacMan(string.Empty,
                                                                               gameStateManager.Level.getCell(1, 1),
                                                                               gameStateManager.Level, EmptyController.Empty(c.getUID()),
                                                                               Direction.None, 0.9f, new Point(49, 49));

                                                    gameStateManager.AddPlayers(pacMan);
                                                    break;

                                                default:
                                                    WriteToLog("Ghost isn't implemented yet");
                                                    break;
                                            }

                                            // Remove Client from pending list
                                            pendingClients.Remove(c.getConnection());
                                            //gameStateManager.AddPlayers(new PacMan());
                                            this.WriteToLog("Set figureType " + msgInt + " to Client " + c.getConnection().RemoteEndpoint.Address);
                                        }
                                    }

                                    gameLoop = new GameLoop(5000);

                                    gameLoop.StartingSimulation += StartSimulation;
                                    gameLoop.FinishedSimulation += FinishedSimulation;

                                    //gameTime.Start();

                                    break;

                                // Got direction update from client deploy it to the rest
                                case "newDirection":
                                    msgInt = msg.ReadInt32();
                                    long msgLong = msg.ReadInt64();
                                    msgCon = msg.SenderConnection;

                                    // calculate the UID from the sender of the message
                                    string msgUID = msgCon.RemoteUniqueIdentifier.ToString().Substring(msgCon.RemoteUniqueIdentifier.ToString().Length - 5);

                                    MovableObject toChange = gameStateManager.getFromId(Convert.ToInt32(msgUID));

                                    WriteToLogDebug("UID " + toChange.ID + "s current direction: " + (Direction)msgInt);

                                    inputQueue.Add(new Command((Direction)msgInt, Convert.ToInt32(msgUID),
                                                               gameTime.TotalGameTime.Milliseconds));

                                    //toChange.Direction = (Direction)msgInt;

                                    // send direction updates to all clients except the sender
                                    foreach (Client c in connectedClients)
                                    {
                                        if (c.getConnection() != msgCon)
                                        {
                                            response = networkManager.createMessage();
                                            response.Write("updateDirection;" + msgUID + ";" + msgInt);
                                            networkManager.sendMessage(response, c.getConnection());
                                            this.WriteToLog("Sent direction update for uid " + msgUID + " to " + c.getConnection().RemoteEndpoint.Address + "Time: " + gameTime.TotalGameTime.TotalMilliseconds);
                                        }
                                    }

                                    MovableObject changed = gameStateManager.getFromId(Convert.ToInt32(msgUID));

                                    //WriteToLogDebug("UID " + changed.ID + "s current direction: " + changed.Direction);

                                    break;

                                    // client is asking for the list of connected clients
                                case "getPlayers":
                                    this.WriteToLog("Client asked for Players");
                                    string players = "";
                                    // create a string with all connected clients (example 15482/1;56847/2)
                                    foreach (Client c in connectedClients)
                                    {
                                        if (players == "")
                                        {
                                            players = c.getUID() + "/" + c.getFigureType() + ";";
                                        }
                                        else
                                        {
                                            players = players + c.getUID() + "/" + c.getFigureType()+";";
                                        }
                                    }
                                    response = networkManager.createMessage();
                                    // add "getPlayser" to the string for message identification on the client and send the message
                                    response.Write("getPlayers;"+players);
                                    networkManager.sendMessage(response, msg.SenderConnection);
                                    this.WriteToLog("List of players " + players);
                                    break;

                                case "latencyTime":
                                    //this.WriteToLogDebug("Got timesync request");
                                    response = networkManager.createMessage();
                                    response.Write(msgString);
                                    networkManager.sendMessage(response, msg.SenderConnection);
                                    break;

                                case "syncTime":
                                    response = networkManager.createMessage();
                                    response.Write(msgString);
                                    // serialize the current servertime and send it to the requesting client
                                    response.Write(networkManager.Serialize(gameTime.TotalGameTime));
                                    this.WriteToLogDebug("Sent server time: " + gameTime.TotalGameTime.TotalSeconds);
                                    networkManager.sendMessage(response, msg.SenderConnection);
                                    break;
                            }

                            break;
                        case NetIncomingMessageType.DebugMessage:
                        case NetIncomingMessageType.WarningMessage:
                        case NetIncomingMessageType.StatusChanged:
                            msgString = msg.ReadString();
                            msgCon = msg.SenderConnection;
                            // Weil bei der connect-Nachricht vom Client spezielle escape-Zeichen
                            // mitgesendet werden, muss der String angepasst werden, damit er
                            // verglichen werden kann
                            string corrString = msgString.Substring(msgString.Length - 2);
                            if (corrString.CompareTo("Co") == 0)
                            {
                                try
                                {
                                    response = networkManager.createMessage();
                                    response.Write("connected");
                                    networkManager.sendMessage(response, msgCon);
                                    // create new client
                                    Client c = new Client(msgCon);
                                    // add new client to the list of connected clients
                                    connectedClients.Add(c);
                                    // add new client to the list of pending clients (clients in this list are still configuring their game options)
                                    pendingClients.Add(msgCon);

                                    this.WriteToLog("Sent connection approval to" + msg.SenderEndpoint + " with UID: "+c.getUID());
                                }
                                catch (Exception ex)
                                {
                                    System.Console.WriteLine(ex.ToString());
                                }
                            }
                            break;
                        case NetIncomingMessageType.ErrorMessage:
                            this.WriteToLog("Error: " + msg.ReadString());
                            break;
                        default:
                            this.WriteToLog("Unhandled type: " + msg.MessageType + " -> " + msg.ReadString());
                            break;
                    }
                    server.Recycle(msg);
                }

                // All Players are ready, send start message to all connected clients
                if (pendingClients.Count == 0 && gameRunning == false && connectedClients.Count > 0)
                {
                    response = networkManager.createMessage();
                    response.Write("start");

                    foreach (Client c in connectedClients)
                    {
                        networkManager.sendMessage(response, c.getConnection());
                    }
                    gameRunning = true;
                }
            }

            gameTime.Reset();
        }
Ejemplo n.º 4
0
 public Clyde(PacMan pacMan, Point scatterTarget, Point exitPoint, Point homeTarget)
     : base(scatterTarget, exitPoint, homeTarget, Enums.MovObjType.Clyde)
 {
     this.pacMan = pacMan;
 }
Ejemplo n.º 5
0
 public Inky(PacMan pacMan, Ghost blinky, Point scatterTarget, Point exitPoint, Point homeTarget)
     : base(scatterTarget, exitPoint, homeTarget, MovObjType.Inky)
 {
     this.pacMan = pacMan;
     this.blinky = blinky;
 }