예제 #1
0
        public void Update()
        {
            currentTime = stopwatch.ElapsedMilliseconds;
            deltaTime   = (currentTime - lastTime) / 1000.0f;

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

            //The controls for the tank
            if (rl.IsKeyDown(KeyboardKey.KEY_A))
            {
                tankObject.Rotate(-deltaTime);
            }
            if (rl.IsKeyDown(KeyboardKey.KEY_D))
            {
                tankObject.Rotate(deltaTime);
            }
            if (rl.IsKeyDown(KeyboardKey.KEY_W))
            {
                Vector3 facing = new Vector3(tankObject.LocalTransform.m1,
                                             tankObject.LocalTransform.m2, 1) *
                                 deltaTime * 100;

                tankObject.Translate(facing.x, facing.y);
            }
            if (rl.IsKeyDown(KeyboardKey.KEY_S))
            {
                Vector3 facing = new Vector3(tankObject.LocalTransform.m1,
                                             tankObject.LocalTransform.m2, 1) *
                                 deltaTime * -100;

                tankObject.Translate(facing.x, facing.y);
            }
            //The controls for the turret
            if (rl.IsKeyDown(KeyboardKey.KEY_Q))
            {
                turrentObject.Rotate(-deltaTime);
            }
            if (rl.IsKeyDown(KeyboardKey.KEY_E))
            {
                turrentObject.Rotate(deltaTime);
            }
            //The control for the bullet
            if (rl.IsKeyPressed(KeyboardKey.KEY_SPACE))
            {
                //Make the bullet appear, set the position, and update per frame when it becomes active
                turrentObject.AddChild(bulletObject);
                bulletObject.SetPosition(65, -5.5f);
                bulletObject.Update(deltaTime);
                bulletObject.active = true;

                float xPos = bulletObject.GlobalTransform.m7;
                float yPos = bulletObject.GlobalTransform.m8;

                float xR  = turrentSprite.GlobalTransform.m1;
                float yR  = turrentSprite.GlobalTransform.m4;
                float rot = (float)Math.Atan2(xR, yR);

                //Make the bullet remove from the turrent
                turrentObject.RemoveChild(bulletObject);

                //Set the rotation of the turrent from the globalTransform
                bulletObject.SetRotate(rot);
                //Set the position of the turrent from the globalTransform
                bulletObject.SetPosition(xPos, yPos);

                bulletTime = 2;
            }

            lastTime = currentTime;

            //The collision boundries to make the bullet disappear
            if (bulletObject.GlobalTransform.m7 > 795 || bulletObject.GlobalTransform.m7 < 5 || bulletObject.GlobalTransform.m8 > 445 || bulletObject.GlobalTransform.m8 < 5)
            {
                bulletObject.active = false;
            }

            //The method for when the bullet is active so the bullet appears
            if (/*bulletTime > 0 && */ bulletObject.active)
            {
                //bulletTime -= deltaTime;
                bulletSprite.Draw();
                //To make the bullet go foward
                Vector3 facing = new Vector3(bulletObject.LocalTransform.m1,
                                             bulletObject.LocalTransform.m2, 1) *
                                 deltaTime * 100;

                bulletObject.Translate(facing.x, facing.y);
            }
        }