コード例 #1
0
ファイル: Game.cs プロジェクト: JohnathanBlaikie/AIETankGame
        public void Update()
        {
            currentTime = stopwatch.ElapsedMilliseconds;
            deltaTime   = (currentTime - lastTime) / 1000.0f;
            Timer coolDown = new Timer();

            timer += deltaTime;
            if (timer >= 1)
            {
                fps    = frames;
                frames = 0;
                timer -= 1;
            }

            frames++;
            #region Movement
            if (IsKeyDown(KeyboardKey.KEY_A))
            {
                tankObject.Rotate(-deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_D))
            {
                tankObject.Rotate(deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_W))
            {
                //MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                //    tankObject.LocalTransform.m[0],
                //    tankObject.LocalTransform.m[1], 1) * deltaTime * 100;
                //tankObject.Translate(facing.x, facing.y);
                MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                    tankObject.LocalTransform.m1,
                    tankObject.LocalTransform.m2, 1) * deltaTime * 100;
                tankObject.Translate(facing.x, facing.y);
            }
            if (IsKeyDown(KeyboardKey.KEY_S))
            {
                //MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                //tankObject.LocalTransform.m[0],
                //tankObject.LocalTransform.m[1], 1) * deltaTime * -100;
                //tankObject.Translate(facing.x, facing.y);
                MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                    tankObject.LocalTransform.m1,
                    tankObject.LocalTransform.m2, 1) * deltaTime * -100;
                tankObject.Translate(facing.x, facing.y);
            }
            if (IsKeyDown(KeyboardKey.KEY_Q))
            {
                turretObject.Rotate(-deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_E))
            {
                turretObject.Rotate(deltaTime);
            }
            #endregion Movement

            if (IsKeyPressed(KeyboardKey.KEY_SPACE))
            {
                Projectile shell = new Projectile(turretObject.GlobalTransform.m5, -turretObject.GlobalTransform.m4);
                shell.SetPosition(turretObject.GlobalTransform.m7, turretObject.GlobalTransform.m8);
                shellObject.AddChild(shell);


                //MathHelpers.Vector3 facing = new MathHelpers.Vector3 (
                //    tankObject.LocalTransform.m1,
                //    tankObject.LocalTransform.m2, 1) *deltaTime * -1000;
                //tankObject.Translate(facing.x, facing.y);
            }

            if (boxCollider.Overlaps(playerCollider))
            {
                boxColor = Color.DARKGREEN;
            }
            else
            {
                boxColor = Color.GREEN;
            }

            for (int i = 0; i < shellObject.GetChildCount(); i++)
            {
                if (shellObject.GetChild(i).proDel)
                {
                    shellObject.RemoveChild(shellObject.GetChild(i));
                }
            }
            for (int i = 0; i < shellObject.GetChildCount(); i++)
            {
                Projectile tShell = (Projectile)shellObject.GetChild(i);
                if (!boxCollider.Overlaps(playerCollider))
                {
                    if (tShell.projectileCollider.Overlaps(boxCollider))
                    {
                        boxColor = Color.RED;
                        break;
                    }
                    else
                    {
                        boxColor = Color.GREEN;
                    }
                }
            }

            tankObject.UpdateTransform();
            //tankObject.Update(deltaTime);



            playerCollider.Resize(new MathHelpers.Vector3(tankObject.GlobalTransform.m7 - (tankSprite.Width / 2), tankObject.GlobalTransform.m8 - (tankSprite.Height / 2), 0),
                                  new MathHelpers.Vector3(tankObject.GlobalTransform.m7 + (tankSprite.Width / 2), tankObject.GlobalTransform.m8 + (tankSprite.Height / 2), 0));
            //DrawRectangle(90, 90, 90, 10, Color.RED);
            Vector2 v2 = new Vector2(900, 24);
            //DrawLineStrip(ref v2, 255, Color.VIOLET);
            lastTime = currentTime;

            foreach (SceneObject i in Hierarchy)
            {
                i.Update(deltaTime);
            }
        }
コード例 #2
0
        public void Update()
        {
            currentTime = stopwatch.ElapsedMilliseconds;
            deltaTime   = (currentTime - lastTime) / 1000.0f;
            Timer coolDown = new Timer();

            timer += deltaTime;
            if (timer >= 1)
            {
                fps    = frames;
                frames = 0;
                timer -= 1;
            }

            frames++;
            #region Movement
            if (IsKeyDown(KeyboardKey.KEY_A))
            {
                tankObject.Rotate(-deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_D))
            {
                tankObject.Rotate(deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_W))
            {
                //MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                //    tankObject.LocalTransform.m[0],
                //    tankObject.LocalTransform.m[1], 1) * deltaTime * 100;
                //tankObject.Translate(facing.x, facing.y);
                MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                    tankObject.LocalTransform.m1,
                    tankObject.LocalTransform.m2, 1) * deltaTime * 100;
                tankObject.Translate(facing.x, facing.y);
            }
            if (IsKeyDown(KeyboardKey.KEY_S))
            {
                //MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                //tankObject.LocalTransform.m[0],
                //tankObject.LocalTransform.m[1], 1) * deltaTime * -100;
                //tankObject.Translate(facing.x, facing.y);
                MathHelpers.Vector3 facing = new MathHelpers.Vector3(
                    tankObject.LocalTransform.m1,
                    tankObject.LocalTransform.m2, 1) * deltaTime * -100;
                tankObject.Translate(facing.x, facing.y);
            }
            if (IsKeyDown(KeyboardKey.KEY_Q))
            {
                turretObject.Rotate(-deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_E))
            {
                turretObject.Rotate(deltaTime);
            }
            #endregion Movement

            //Spawns a projectile that moves in the direction the barrel was pointed when pressed.
            if (IsKeyPressed(KeyboardKey.KEY_SPACE))
            {
                Projectile shell = new Projectile(turretObject.GlobalTransform.m5, -turretObject.GlobalTransform.m4);
                shell.SetPosition(turretObject.GlobalTransform.m7, turretObject.GlobalTransform.m8);
                shellObject.AddChild(shell);


                //MathHelpers.Vector3 facing = new MathHelpers.Vector3 (
                //    tankObject.LocalTransform.m1,
                //    tankObject.LocalTransform.m2, 1) *deltaTime * -1000;
                //tankObject.Translate(facing.x, facing.y);
            }

            //Checks to see if the player's collider interacts with the green box.
            if (boxCollider.Overlaps(playerCollider))
            {
                boxColor = Color.DARKGREEN;
            }
            else
            {
                boxColor = Color.GREEN;
            }

            //Goes through the children of shellObject and checks their lifespans.
            //If they expire they are removed from the shellObject.
            for (int i = 0; i < shellObject.GetChildCount(); i++)
            {
                if (shellObject.GetChild(i).proDel)
                {
                    shellObject.RemoveChild(shellObject.GetChild(i));
                }
            }

            for (int i = 0; i < shellObject.GetChildCount(); i++)
            {
                Projectile tShell = (Projectile)shellObject.GetChild(i);

                //If the green box isn't currently overlapping with the player
                //it will check if it's overlapping with a projectile; if it is, it will turn red.
                if (!boxCollider.Overlaps(playerCollider))
                {
                    if (tShell.projectileCollider.Overlaps(boxCollider))
                    {
                        boxColor = Color.RED;
                        break;
                    }
                    else
                    {
                        boxColor = Color.GREEN;
                    }
                }
            }

            tankObject.UpdateTransform();

            //Moves the collider in tandem with the tank by "resizing" it to the current dimensions
            //(and by extent, the position) of the player.
            playerCollider.Resize(new MathHelpers.Vector3(tankObject.GlobalTransform.m7 - (tankSprite.Width / 2), tankObject.GlobalTransform.m8 - (tankSprite.Height / 2), 0),
                                  new MathHelpers.Vector3(tankObject.GlobalTransform.m7 + (tankSprite.Width / 2), tankObject.GlobalTransform.m8 + (tankSprite.Height / 2), 0));

            Vector2 v2 = new Vector2(900, 24);
            lastTime = currentTime;

            //
            foreach (SceneObject i in Hierarchy)
            {
                i.Update(deltaTime);
            }
        }