コード例 #1
0
ファイル: Game.cs プロジェクト: SamuelDaman/Assessment-2
        /// <summary>
        /// This function initialiazes the bullets so they can be fired.
        /// </summary>
        public void Shoot()
        {
            SceneObject        bullet       = new SceneObject();
            SpriteObject       sprite       = new SpriteObject();
            List <SceneObject> HitBoxPoints = new List <SceneObject>(4);

            //This is where the bullets' sprites are initialized.
            sprite.Load("barrelBlue.png");
            sprite.SetRotate(-90 * (float)(Math.PI / 180f));
            sprite.SetPosition(0, sprite.Width / 2f);

            //This is where the bullets' corners are listed for the purpose of creating an AABB.
            for (int i = 0; i < HitBoxPoints.Capacity; i++)
            {
                HitBoxPoints.Add(new SceneObject());
            }
            HitBoxPoints[0].SetPosition(0, -sprite.Width / 2f);
            HitBoxPoints[1].SetPosition(sprite.Height, -sprite.Width / 2f);
            HitBoxPoints[2].SetPosition(sprite.Height, sprite.Width / 2f);
            HitBoxPoints[3].SetPosition(0, sprite.Width / 2f);

            //This is where the bullets are put togather.
            bullet.AddChild(sprite);
            bullet.SetRotate(turretObject.GlobalTransform.GetRotateZ());
            bullet.SetPosition(turretObject.GlobalTransform.m7, turretObject.GlobalTransform.m8);
            for (int i = 0; i < HitBoxPoints.Count; i++)
            {
                bullet.AddChild(HitBoxPoints[i]);
            }
            bulletObject.Add(bullet);
        }
コード例 #2
0
        public void Init(List<Body> bodys, List<Barrel> barels, int player)
        {
            bodies = bodys; barrels = barels;
            bodyPick = 0;
            gunPick = 0;
            SetBody(0);
            SetGun(0);
            reload = reloading;
            armor = maxArmor;

            if (player == 1) { tankObject.SetRotate(0); }
            if (player == 2) { tankObject.SetRotate(180 * (float)(Math.PI / 180.0f)); }
            tankSprite.SetRotate(-90 * (float)(Math.PI / 180.0f));
            tankSprite.SetPosition(-tankSprite.Width / 2.0f, tankSprite.Height / 2.0f);

            turretSprite.SetRotate(-90 * (float)(Math.PI / 180.0f));
            turretSprite.SetPosition(-10, turretSprite.Width / 2.0f);

            tankObject.AddChild(tankSprite);
            tankObject.AddChild(turretObject);
            turretObject.AddChild(turretSprite);
            turretObject.AddChild(turretSprite);

            tankObject.AddChild(tankTL);
            tankObject.AddChild(tankTR);
            tankObject.AddChild(tankBL);
            tankObject.AddChild(tankBR);
            tankTL.SetPosition(-tankSprite.Height / 2, -tankSprite.Width / 2);
            tankTR.SetPosition(tankSprite.Height / 2, -tankSprite.Width / 2);
            tankBR.SetPosition(tankSprite.Height / 2, tankSprite.Width / 2);
            tankBL.SetPosition(-tankSprite.Height / 2, tankSprite.Width / 2);

            tankObject.myPoints.Add(tankTL.position);
            tankObject.myPoints.Add(tankTR.position);
            tankObject.myPoints.Add(tankBR.position);
            tankObject.myPoints.Add(tankBL.position);

            turretSprite.AddChild(shotFX);
            shotSprite.Load("Resources/shotLarge.png");
            shotFX.AddChild(shotSprite);
            shotFX.SetPosition(turretSprite.Width / 2.0f, turretSprite.Height);
            shotSprite.SetPosition(-(shotSprite.Width / 2.0f), 0);
        }
コード例 #3
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);
            }
        }