示例#1
0
        /// <summary>
        /// Once name has been recieved, creates a tank for them and sends startup info, then asks for commands.
        /// </summary>
        /// <param name="state"></param>
        private void sendStartUpInfo(SocketState state)
        {
            StringBuilder temp = new StringBuilder();

            temp.Append(state.ID.ToString() + "\n");
            temp.Append(world.GetSize().ToString() + "\n");
            foreach (Wall w in world.GetWalls())
            {
                temp.Append(JsonConvert.SerializeObject(w) + "\n");
            }

            if (state.ErrorOccured)
            {
                return;
            }

            ProcessMessage(state);
            state.OnNetworkAction = update;
            Networking.Send(state.TheSocket, temp.ToString());

            lock (users)
            {
                users.Add(state, (int)state.ID);
            }

            lock (state)
            {
                Networking.GetData(state);
            }
        }
示例#2
0
        /// <summary>
        /// This method is invoked when the DrawingPanel needs to be re-drawn
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            //Makes sure world is null, since it is null while loading game
            if (theWorld != null)
            {
                //Locks world since it accesses info from it
                lock (theWorld)
                {
                    //Get player's tank to center view
                    Tank player;
                    if (theWorld.GetTank(PlayerID, out player))
                    {
                        double playerX = player.location.GetX();
                        double playerY = player.location.GetY();

                        double ratio          = (double)ViewSize / (double)theWorld.GetSize();
                        int    halfSizeScaled = (int)(theWorld.GetSize() / 2.0 * ratio);

                        double inverseTranslateX = -WorldSpaceToImageSpace(theWorld.GetSize(), playerX) + halfSizeScaled;
                        double inverseTranslateY = -WorldSpaceToImageSpace(theWorld.GetSize(), playerY) + halfSizeScaled;

                        e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY);
                    }

                    //Draw background
                    e.Graphics.DrawImage(Background, new Rectangle(0, 0, theWorld.GetSize(), theWorld.GetSize()));

                    // Draw the tanks
                    foreach (Tank tank in theWorld.GetTanks())
                    {
                        //Draw tank, turret, and info if tank is alive and ingame
                        if (!tank.died && !tankTimer.ContainsKey(tank) && tank.hitPoints > 0)
                        {
                            DrawObjectWithTransform(e, tank, theWorld.GetSize(), tank.location.GetX(), tank.location.GetY(), tank.orientation.ToAngle(), TankDrawer);
                            DrawObjectWithTransform(e, tank, theWorld.GetSize(), tank.location.GetX(), tank.location.GetY(), tank.aiming.ToAngle(), TurretDrawer);
                            DrawObjectWithTransform(e, tank, theWorld.GetSize(), tank.location.GetX(), tank.location.GetY(), 0, InfoDrawer);
                        }
                        //Otherwise, add tank to tank timer, increment tank timer, or remove tank from tank timer if it is alive
                        else
                        {
                            //If tank is a live again, remove from tank timer
                            if (tank.hitPoints > 0)
                            {
                                tankTimer.Remove(tank);
                                break;
                            }
                            //If tank is already in tank timer, decrement tank timer or remove tank from timer
                            if (tankTimer.ContainsKey(tank))
                            {
                                if (tankTimer[tank] < 200)
                                {
                                    tankTimer[tank]++;
                                }
                                else
                                {
                                    tankTimer.Remove(tank);
                                }
                            }
                            //Add tank to timer if not in timer yet
                            else
                            {
                                tankTimer.Add(tank, 0);
                            }
                        }
                    }

                    // Draw the walls
                    foreach (Wall w in theWorld.GetWalls())
                    {
                        Vector2D location = GetWallLocation(w);
                        DrawObjectWithTransform(e, w, theWorld.GetSize(), location.GetX(), location.GetY(), 0, WallDrawer);
                    }

                    //Draw beams if existed for less than 60 frames
                    List <Beam> beams = new List <Beam>(beamsTimer.Keys);
                    foreach (Beam b in beams)
                    {
                        if (beamsTimer[b] > 60)
                        {
                            beamsTimer.Remove(b);
                        }
                        //Increments beamsTimer and draws beams
                        else
                        {
                            beamsTimer[b]++;
                            DrawObjectWithTransform(e, b, theWorld.GetSize(), b.origin.GetX(), b.origin.GetY(), b.direction.ToAngle(), BeamDrawer);;
                        }
                    }

                    // Draw the powerups if not dead
                    foreach (Powerup pow in theWorld.GetPowerups())
                    {
                        if (!pow.died)
                        {
                            DrawObjectWithTransform(e, pow, theWorld.GetSize(), pow.location.GetX(), pow.location.GetY(), 0, PowerupDrawer);
                        }
                    }

                    //Draw projectiles if not dead
                    foreach (Projectile p in theWorld.GetProjectiles())
                    {
                        if (!p.died)
                        {
                            DrawObjectWithTransform(e, p, theWorld.GetSize(), p.location.GetX(), p.location.GetY(), p.orientation.ToAngle(), ProjectileDrawer);
                        }
                    }

                    //Draw tank explosions with tanks in tankTimer
                    List <Tank> tanks = new List <Tank>(tankTimer.Keys);
                    foreach (Tank t in tanks)
                    {
                        DrawObjectWithTransform(e, t, theWorld.GetSize(), t.location.GetX(), t.location.GetY(), 0, ExplosionDrawer);
                    }
                }
            }
            // Do anything that Panel (from which we inherit) needs to do
            base.OnPaint(e);
        }
示例#3
0
        /// <summary>
        /// This method is invoked when the DrawingPanel needs to be re-drawn
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            //Gets the world based on the world of the controller
            world = controller.GetWorld();

            //Gets the playerNum based on the controller's player number
            playerNum = controller.GetPlayerNum();

            //Only attempts to draw if the world contains the tanks
            if (world.GetTanks().Count > 0)
            {
                //Locks the thread with the world as the key to draw everything in one thread to avoid changes while drawing
                lock (world)
                {
                    //Locks the player's view in the center based on ther user's controlled tank, if the tank is not dead
                    if (world.GetTanks().TryGetValue(playerNum, out Tank tank) && !tank.GetDead())
                    {
                        //Sets the player's X and Y coordinate
                        playerX = tank.GetLocation().GetX();
                        playerY = tank.GetLocation().GetY();

                        //Centers the player's view
                        double ratio          = (double)viewSize / (double)world.Size;
                        int    halfSizeScaled = (int)(world.Size / 2.0 * ratio);

                        double inverseTranslateX = -WorldSpaceToImageSpace(world.Size, playerX) + halfSizeScaled;
                        double inverseTranslateY = -WorldSpaceToImageSpace(world.Size, playerY) + halfSizeScaled;

                        e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY);
                    }
                    else
                    {
                        //If the tank is not dead, then it sets the location as the last place of death of the tank and center's the player's view
                        double ratio          = (double)viewSize / (double)world.Size;
                        int    halfSizeScaled = (int)(world.Size / 2.0 * ratio);

                        double inverseTranslateX = -WorldSpaceToImageSpace(world.Size, playerX) + halfSizeScaled;
                        double inverseTranslateY = -WorldSpaceToImageSpace(world.Size, playerY) + halfSizeScaled;

                        e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY);
                    }



                    //Doesn't draw anything if the world size is less than equal to 0
                    if (world.Size <= 0)
                    {
                        return;
                    }
                    //Draws the background
                    DrawObjectWithTransform(e, null, world.Size, 0, 0, 0, BackgroundDrawer);

                    //Draws all the walls in the world
                    foreach (Wall w in world.GetWalls().Values.ToList())
                    {
                        for (double i = w.GetStartingPoint().GetX(); i <= w.GetEndingPoint().GetX(); i = i + 50)
                        {
                            for (double j = w.GetStartingPoint().GetY(); j <= w.GetEndingPoint().GetY(); j = j + 50)
                            {
                                //Draws all the walls in the condition that the starting point is smaller than ending points of the walls
                                DrawObjectWithTransform(e, w, world.Size, i, j, 0, WallDrawer);
                            }
                        }

                        for (double i = w.GetEndingPoint().GetX(); i <= w.GetStartingPoint().GetX(); i = i + 50)
                        {
                            for (double j = w.GetEndingPoint().GetY(); j <= w.GetStartingPoint().GetY(); j = j + 50)
                            {
                                //Draws all the walls in the condition that the starting point is greater than ending points of the walls
                                DrawObjectWithTransform(e, w, world.Size, i, j, 0, WallDrawer);
                            }
                        }
                    }

                    //Draws all the powerups
                    foreach (Powerups p in world.GetPowerups().Values)
                    {
                        //Draws the outside and inside circle for the powerups
                        DrawObjectWithTransform(e, p, world.Size, p.GetLocation().GetX(), p.GetLocation().GetY(), 0, PowerupDrawer);
                        DrawObjectWithTransform(e, p, world.Size, p.GetLocation().GetX(), p.GetLocation().GetY(), 0, PowerupDrawerInsideCircle);
                    }

                    //Draws all the tanks in the world
                    foreach (Tank t in world.GetTanks().Values)
                    {
                        //Draws the tank
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY(), t.GetOrientation().ToAngle(), TankDrawer);

                        //Draws the turret
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY(), t.GetAiming().ToAngle(), TurretDrawer);

                        //Draws the name
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY() + 60, 0, NameDrawer);

                        //Draws the health
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY() - 40, 0, HealthDrawer);
                    }

                    //Draws all the projectiles in the world
                    foreach (Projectile p in world.GetProjectile().Values.ToList())
                    {
                        //Draws the projectile
                        DrawObjectWithTransform(e, p, world.Size, p.GetLocation().GetX(), p.GetLocation().GetY(), p.GetOrientation().ToAngle(), ProjectileDrawer);
                    }

                    //Draws all the beams in the world
                    foreach (Beams b in world.GetBeams().Values.ToList())
                    {
                        //Draws the beams
                        DrawObjectWithTransform(e, b, world.Size, b.GetOrigin().GetX(), b.GetOrigin().GetY(), b.GetDirection().ToAngle(), BeamDrawer);
                        //Removes the beams after 20 iterations
                        beamFrameCount++;
                        if (beamFrameCount == 20)
                        {
                            world.GetBeams().Remove(b.GetBeamID());
                            beamFrameCount = 0;
                        }
                    }
                }
            }
            //Calls the base
            base.OnPaint(e);
        }