コード例 #1
0
        public static bool InBounds(this IGame2DProperties obj, BoundF b)
        {
            var inside = false;

            if (obj.Location.X >= b.Min.X && obj.Location.X <= b.Max.X)
            {
                if (obj.Location.Y >= b.Min.Y && obj.Location.Y <= b.Max.Y)
                {
                    inside = true;
                }
            }
            return(inside);
        }
コード例 #2
0
        void fitCamera()
        {
            if (m_Items == null)
            {
                return;
            }
            // Get all points
            List <PointF> ptOnScreen = new List <PointF>();

            for (int i = 0; i < m_Items.Length; i++)
            {
                PointF cartStart2d = m_Projector.OnlyRotationProject(((LineItem)m_Items[i]).Start);
                //PointF startgraphics = m_CartToGdiTransform.ToGdi( cart2d );
                ptOnScreen.Add(cartStart2d);

                PointF cartEnd2d = m_Projector.OnlyRotationProject(((LineItem)m_Items[i]).End);
                //PointF startgraphics = m_CartToGdiTransform.ToGdi( cart2d );
                ptOnScreen.Add(cartEnd2d);
            }
            // Calc Max and Min
            BoundF bound2d = new BoundF(ptOnScreen);

            // Get XY ratio
            float ratioX = Size.Width / bound2d.LengthX;
            float ratioY = Size.Height / bound2d.LengthY;

            // Do set a little ratio to make it fit full screen
            const float fitRatio = 0.95f;

            float ratio = fitRatio * Math.Min(ratioX, ratioY);

            m_Projector.Scale = ratio;

            // Move bounding box center to screen center.
            float  ratioAfterScaled = m_Projector.Scale;
            PointF center2d         = bound2d.Center;

            // Because screen is display after scale, translate it into unscaled position
            PointF unscaleScrCenter = new PointF(
                Width * 0.5f / ratioAfterScaled,
                Height * 0.5f / ratioAfterScaled);
            float x = unscaleScrCenter.X - center2d.X;
            float y = unscaleScrCenter.Y - center2d.Y;

            m_Projector.SetOffset(x, y, 0);
            return;
        }
コード例 #3
0
 public static IGame2DProperties KeepInBounds(this IGame2DProperties obj, BoundF b)
 {
     if (obj.Location.X < b.Min.X)
     {
         obj.Location.X = b.Max.X;
     }
     else if (obj.Location.X > b.Max.X)
     {
         obj.Location.X = b.Min.X;
     }
     if (obj.Location.Y < b.Min.Y)
     {
         obj.Location.Y = b.Max.Y;
     }
     else if (obj.Location.Y > b.Max.Y)
     {
         obj.Location.Y = b.Min.Y;
     }
     return(obj);
 }
コード例 #4
0
ファイル: DestroyManager.cs プロジェクト: Hemlon/Framework-2D
        public static Dictionary <string, IGameBehavior> DestroyOutOfBounds(ref Dictionary <string, IGameBehavior> lst, BoundF b)
        {
            foreach (var item in lst.OfType <IGame2DProperties>())
            {
                if (item.InBounds(b).IsNotTrue())
                {
                    ((IKillable)item).ToBeDestroyed = true;
                }
            }

            DestroyFromList(ref lst);
            return(lst);
        }
コード例 #5
0
        public override void Update(GameTime gameTime)
        {
            //  GameWindow.myText = new PointF(GameInput.GetMouseLocation().X - GameWindow.View.Location.X, GameInput.GetMouseLocation().Y - GameWindow.View.Location.Y).ToString();
            GameWindow.GameMessages[1] = ("objCount:" + gameObjects.Count.ToString());
            DestroyManager.DestroyFromList(ref gameObjects);
            DestroyManager.DestroyOutOfBounds(ref gameObjects, new Utilities.BoundF(0, 0, Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height));

            BoundF bound = new BoundF(
                0,                            // GameWindow.View.Size.Width ,
                0,                            // GameWindow.View.Size.Height / 4,
                GameWindow.DisplaySize.Width, // - GameWindow.View.Size.Width / 4,
                GameWindow.DisplaySize.Height // - GameWindow.View.Size.Height / 4
                );

            if (gameObjects.ContainsKey(mainPlayerID))
            {
                var fighter = mainPlayerID.GetByKey <string, IGameBehavior>(gameObjects) as StarFighter;

                if (GameInput.isKeyDown(Keys.Down))
                {
                    // if (GetAsyncKeyState(Keys.Down))
                    fighter.Deaccelerate();
                }
                if (GameInput.isKeyDown(Keys.Up))
                {
                    // if (GetAsyncKeyState(Keys.Up))
                    fighter.Accelerate();
                }
                if (GameInput.isKeyDown(Keys.Right))
                {
                    // if (GetAsyncKeyState(Keys.Right))
                    fighter.TurnRight();
                }
                if (GameInput.isKeyDown(Keys.Left))
                {
                    //  if (GetAsyncKeyState(Keys.Left))
                    fighter.TurnLeft();
                }

                if (gameActive)
                {
                    if (GameInput.isKeyDown(Keys.Space))
                    {
                        // if (GetAsyncKeyState(Keys.Space))
                        fighter.Shoot();
                    }
                }

                fighter.Location = ((IGame2DProperties)fighter)
                                   .KeepInBounds(bound
                                                 ).Location;
            }


            if (gameObjects.ContainsKey(secondPlayer))
            {
                var fighter2 = secondPlayer.GetByKey <string, IGameBehavior>(gameObjects) as StarFighter;

                if (GameInput.isKeyDown(Keys.W))
                {
                    fighter2.Accelerate();
                }
                if (GameInput.isKeyDown(Keys.S))
                {
                    fighter2.Deaccelerate();
                }
                if (GameInput.isKeyDown(Keys.D))
                {
                    fighter2.TurnRight();
                }
                if (GameInput.isKeyDown(Keys.A))
                {
                    fighter2.TurnLeft();
                }
                if (gameActive)
                {
                    if (GameInput.isKeyDown(Keys.Q))
                    {
                        fighter2.Shoot();
                    }
                }

                fighter2.Location = ((IGame2DProperties)fighter2).KeepInBounds(bound).Location;
            }


            foreach (var player in gameObjects.Values.OfType <StarFighter>())
            {
                foreach (var bullet in gameObjects.Values.OfType <Projectile>())
                {
                    var delx = player.Size.Width;
                    var dely = player.Size.Height;
                    if (bullet.InBounds(new BoundF(player.Location.X - delx, player.Location.Y - dely, player.Location.X + delx, player.Location.Y + dely)))
                    {
                        if (!bullet.ID.Contains(player.ID))
                        {
                            var id = bullet.ID.Split(',').Last();
                            bullet.ToBeDestroyed = true;
                            if (player.IsDead.IsNotTrue())
                            {
                                player.IsDead = true;
                                scores[id]++;
                            }
                        }
                    }
                }
            }

            angle += 1;
            if (angle > 360)
            {
                angle = 0;
            }
            var dx = 500 + 200 * Math.Cos(angle * Math.PI / 180f);

            GameWindow.View.Size = new SizeF((float)dx, (float)dx);

            var keys = gameObjects.Keys.ToArray <string>();

            for (int i = 0; i < keys.Count <string>(); i++)
            {
                var ass = gameObjects[keys[i]];
                if (ass.GetType() == typeof(Asteroid))
                {
                    ((IGame2DProperties)ass).Location = ((IGame2DProperties)ass).KeepInBounds(bound).Location;
                }
            }


            foreach (var obj in gameObjects)
            {
                obj.Value.Update(gameTime);
            }

            if (((StarFighter)gameObjects[mainPlayerID]).IsDead || ((StarFighter)gameObjects[secondPlayer]).IsDead)
            {
                gameActive = false;
                gameWaitTiming.WaitThenDoOnce(() =>
                {
                    foreach (var fighter in gameObjects.Values.OfType <StarFighter>())
                    {
                        var v            = new Vector2F(MathAssist.RandInt(0, (GameWindow.DisplaySize.Width - 50)), MathAssist.RandInt(0, GameWindow.DisplaySize.Height));
                        fighter.Location = v;
                        fighter.IsDead   = false;

                        fighter.StopMoving();
                    }
                    gameActive = true;
                    gameWaitTiming.Reset();
                });
            }


            if (GameInput.isKeyDown(Keys.Escape))
            {
                RunAgain = false;
                GameWindow.AudioPlayer.Item1.StopTrack("background");
                GameWindow.CurrentScene = NextScene;
                GameWindow.View.Init();
                gameObjects.Clear();
            }
        }