Esempio n. 1
0
        /// <summary>
        /// Tries to connect to the master server. If it is successful it will start a new thread
        /// which is the Recieve function, if not it throws an exception and stops the connect
        /// thread.
        /// </summary>
        public void ConnectToServer()
        {
            try
            {
                connection = new TcpClient();
                connection.Connect("whg.no", 7707);
                netmsgs = new Thread(new ThreadStart(Receive));
                netmsgs.IsBackground = true;
                netmsgs.Start();
                connected = true;
                toastnet.AddMessage("Connected to server!", 10, 10);
            }
            catch (Exception e)
            {
                connected = false;
                System.Diagnostics.Debug.WriteLine("No connection to server");

                //toastnet.AddMessage("Could not connect to server!", 10, 10);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(new Color(51, 0, 0));

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            // BUTTON TEXTURES
            // 0 - unit1 (placeholder button)
            // 1 - tower1
            // 2 - tower1upgrade1
            // 3 - tower2
            // 4 - tower2upgrade1
            // 5 - delete tower
            // 6 - new wave
            // 7 - new game menu
            // 8 - multiplayer menu
            // 9 - controls menu
            // 10 - exit menu
            // 11 - blackbox
            // 12 - resume
            // 13 - Save game

            if (gameState == GameState.Menu)
            {
                if (player.Name == null)
                {
                    graphics.GraphicsDevice.Clear(Color.DarkRed);
                    spriteBatch.Draw(ContentHolder.GuiSprites[3], new Rectangle((width / 2) - (ContentHolder.GuiSprites[3].Width / 2), 50, ContentHolder.GuiSprites[3].Width, ContentHolder.GuiSprites[3].Height), Color.White);
                    spriteBatch.DrawString(ContentHolder.Fonts[1], "Enter your desired username", new Vector2((width / 2) - 90, (height / 2) - 30), Color.White);
                    userName.Draw(spriteBatch);
                    if (input.IsKeyPressedOnce(Keys.Enter))
                    {
                        if (userName.Value != "")
                        {
                            player.Name = userName.Value;
                            if (network.Connected)
                            {
                                network.Deliver("username;" + userName.Value);
                            }
                        }
                        else
                        {
                            toast.AddMessage("You need a username");
                        }
                    }
                }
                else
                {
                    graphics.GraphicsDevice.Clear(Color.DarkRed);

                    // menu buttons
                    if (gameStateNumber)
                    {
                        menuButtons[0].TextureID = 7;
                    }
                    else
                    {
                        menuButtons[0].TextureID = 12;
                    }

                    foreach (Button menubtn in menuButtons)
                    {
                        menubtn.Draw(spriteBatch);
                    }

                    spriteBatch.Draw(ContentHolder.GuiSprites[3], new Rectangle((width / 2) - (ContentHolder.GuiSprites[3].Width / 2), 50, ContentHolder.GuiSprites[3].Width, ContentHolder.GuiSprites[3].Height), Color.White);
                }
            }
            else if (gameState == GameState.Running)
            {
                map.Draw(spriteBatch);
                pathview.Draw(spriteBatch);
                player.Draw(spriteBatch);
                waveHandler.Draw(spriteBatch);
                actionbar.Draw(spriteBatch, 0, ref player);
                topbar.Draw(spriteBatch, 1, ref player);

                if (player.SelectedTower != null)
                {
                    upgradeTowerButton.Draw(spriteBatch);
                }

                foreach (Button actionbtn in actionButtons)
                {
                    actionbtn.Draw(spriteBatch);
                }
            }
            else if (gameState == GameState.ServerList)
            {
                graphics.GraphicsDevice.Clear(Color.DarkRed);
                spriteBatch.Draw(ContentHolder.GuiSprites[3], new Rectangle((width / 2) - (ContentHolder.GuiSprites[3].Width / 2), 50, ContentHolder.GuiSprites[3].Width, ContentHolder.GuiSprites[3].Height), Color.White);
                lobbyName.Draw(spriteBatch);

                // createlobby button
                multiplayerButtons[0].Draw(spriteBatch);

                foreach (Gui entry in lobbyList)
                {
                    entry.Draw(spriteBatch, 1);
                }
                foreach (Button lobbybtn in joinLobbyButtons)
                {
                    lobbybtn.Draw(spriteBatch);
                }
            }
            else if (gameState == GameState.Lobby)
            {
                graphics.GraphicsDevice.Clear(Color.DarkRed);
                spriteBatch.Draw(ContentHolder.GuiSprites[3], new Rectangle((width / 2) - (ContentHolder.GuiSprites[3].Width / 2), 50, ContentHolder.GuiSprites[3].Width, ContentHolder.GuiSprites[3].Height), Color.White);
                chatBox.Draw(spriteBatch);
                int i = 0;
                foreach (String text in chatlog)
                {
                    spriteBatch.DrawString(ContentHolder.Fonts[1], text, new Vector2(100, 200 + i), Color.White);
                    i += 20;
                }
            }

            toast.Draw(spriteBatch);
            networkMessages.Draw(spriteBatch);
            spriteBatch.Draw(ContentHolder.GuiSprites[2], mousePosition, new Color(255, 255, 255, 255));

            spriteBatch.End();

            base.Draw(gameTime);
        }
Esempio n. 3
0
        public void Update(GameTime gameTime, ref PathView path)
        {
            this.enemies = waveHandler.GetCurrentWave().enemies;
            mouse.Update();

            // Finding out what cell in the array the mouse pointer is
            cellX = (int)(mouse.currentMouseState.X / 32);
            cellY = (int)((mouse.currentMouseState.Y - 24) / 32);

            // Only enter if you are releasing Left mouse and you are inside the array of the map
            if (mouse.ReleaseLeftClick && (map.Height - 1) >= cellY && (map.Width - 1) >= cellX && cellX >= 0 && cellY >= 0)
            {
                // Only enter if you have selected a tower you want to place and the round has not started
                if (newTowerType != 0 && !waveHandler.WaveStarted())
                {
                    // set the tower you want to upgrade to null
                    selectedTower = null;

                    // Only enter if there is something in the map array placed on the position you  have clicked
                    // and the newtowertype is 3(Which is the delete)
                    if (map.GetIndex(cellX, cellY) != 0 && towers[cellX, cellY] != null && newTowerType == 3)
                    {
                        // getting the type of tower you want to sell
                        int sellType = map.GetIndex(cellX, cellY);

                        // If tower is type one, which is a not an attacking tower you will get all you money back
                        // if not then 50% back
                        if (sellType == 1)
                        {
                            gold += Tower.GetCost(sellType);
                        }
                        else
                        {
                            gold += (int)(Tower.GetCost(sellType) * 0.5f);
                        }

                        // Add 0 to the position where the tower used to be.
                        map.AddToMap(cellX, cellY, 0);
                        // Remove the tower object by setting it to null
                        towers[cellX, cellY] = null;
                        // Sending the edited map in to the pathfinder again to get new path
                        Pathfinder     p       = new Pathfinder(map);
                        List <Vector2> newPath = p.FindPath(new Point(0, 0), new Point(map.Width - 1, map.Height - 1));
                        // send new path to the Pathview class to show new path
                        path.Path = newPath;
                        // send new path to the units so they know the new path when they start walking
                        Unit.Path = newPath;
                    }
                    // Only enter if there is nothing on that position of the map and you have the
                    // not pressed the delete tower button
                    else if (map.GetIndex(cellX, cellY) == 0 && newTowerType != 3)
                    {
                        // only enter if you have enough gold
                        if (gold >= Tower.GetCost(newTowerType))
                        {
                            gold -= Tower.GetCost(newTowerType);
                            // Add tower to the map
                            map.AddToMap(cellX, cellY, newTowerType);
                            Pathfinder     p = new Pathfinder(map);
                            List <Vector2> t = p.FindPath(new Point(0, 0), new Point(map.Width - 1, map.Height - 1));

                            // After checking new path. if count == 0 the path is blocked off and there is no way
                            // for the creeps to go. So remove the point added to the map refund money and tell the
                            // player
                            if (t.Count == 0)
                            {
                                map.AddToMap(cellX, cellY, 0);
                                toast.AddMessage("ಠ_ಠ Du är for dårlig at bygga tårn.");
                                gold += Tower.GetCost(newTowerType);
                            }
                            else
                            {
                                // Add new path to pathview and the units
                                path.Path = t;
                                Unit.Path = t;
                                // add new tower object to the array
                                towers[cellX, cellY] = new Tower(newTowerType, cellX, cellY);
                            }
                        }
                        else
                        {
                            // Player does not have enough gold tell them
                            toast.AddMessage("(╯°□°)╯︵ ʎǝuoɯ ǝɹoɯ ou");
                        }
                    }
                }
                else if (towers[cellX, cellY] != null && newTowerType == 0 && towers[cellX, cellY].Type != 1)
                {
                    // Select tower you want to upgrade
                    selectedTower = towers[cellX, cellY];
                }
            }
            if (mouse.ReleaseRightClick)
            {
                // Deselect towers you want to upgrade or build.
                newTowerType  = 0;
                selectedTower = null;
            }
            // Go through the array of the tower objects to tell them the new position of the enemies
            for (int y = 0; y < towers.GetLength(1); y++)
            {
                for (int x = 0; x < towers.GetLength(0); x++)
                {
                    if (towers[x, y] != null)
                    {
                        towers[x, y].Update(ref enemies, gameTime);
                    }
                }
            }
        }