Ejemplo n.º 1
0
        public void UpdatePlayer(KeyboardState keyboard, GameTime gameTime, ref Map map, ref Tank enemyTank)
        {
            lastPos = pos;
            moving  = 0;

            //movimento do tanke
            if (keyboard.IsKeyDown(Keys.W))
            {
                pos   -= dir * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                moving = 1;
            }

            if (keyboard.IsKeyDown(Keys.S))
            {
                pos   += dir * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                moving = 1;
            }

            if (keyboard.IsKeyDown(Keys.A))
            {
                yaw   += rotationSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                moving = 1;
            }

            if (keyboard.IsKeyDown(Keys.D))
            {
                yaw   -= rotationSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                moving = 1;
            }

            //colisoes
            if (Colides(ref enemyTank))
            {
                pos = enemyTank.pos + Vector3.Normalize(pos - enemyTank.pos) * 50 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }



            //otimizaçao da variavel encarregue pela rotaçao do tanke
            if (yaw > 2 * Math.PI || yaw < -2 * Math.PI)
            {
                yaw = 0;
            }

            //fazer restriçao das bordas do mapa
            if (pos.X > map.width - 2)
            {
                pos.X = map.width - 2f;
            }
            if (pos.X < 2)
            {
                pos.X = 2f;
            }
            if (pos.Z > map.height - 2)
            {
                pos.Z = map.height - 2f;
            }
            if (pos.Z < 2)
            {
                pos.Z = 2f;
            }

            //obter y e normal do tanke nuuma determinada posiçao
            pos.Y  = map.GetY(pos);
            normal = map.GetNormal(pos);

            //obter vetor right e dir
            Vector3 diretionHorizontal = Vector3.Transform(Vector3.UnitX, Matrix.CreateRotationY(yaw));
            Vector3 right = Vector3.Cross(diretionHorizontal, normal);

            dir = Vector3.Cross(normal, right);
            dir.Normalize();
            right.Normalize();

            //Criar matriz rotaçao(rotaçao do tank)
            Matrix rotationMatrix = Matrix.Identity;

            rotationMatrix.Forward = dir;
            rotationMatrix.Up      = normal;
            rotationMatrix.Right   = right;

            //Criar matriz translaçao (movimento do tank)
            Matrix translationMatrix = Matrix.CreateTranslation(pos);

            //Criar matriz escala (escala do tank)
            Matrix scaleMatrix = Matrix.CreateScale(scale);

            //adiciona modificaçoes ao tank
            tankModel.Root.Transform = scaleMatrix * rotationMatrix * translationMatrix;
            transform = tankModel.Root.Transform;


            //movimento da torre e do canhao
            if (keyboard.IsKeyDown(Keys.Left))
            {
                turretAng += MathHelper.ToRadians(1.0f);
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                turretAng -= MathHelper.ToRadians(1.0f);
            }
            if (keyboard.IsKeyDown(Keys.Up))
            {
                cannonAng += MathHelper.ToRadians(1.0f);
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                cannonAng -= MathHelper.ToRadians(1.0f);
            }

            //otimizaçao da variavel encarregue da rotaçao da torre
            if (turretAng > 2 * Math.PI || turretAng < -2 * Math.PI)
            {
                turretAng = 0;
            }

            //otimizaçao da variavel encarregue da rotaçao do canhao
            if (cannonAng < -Math.PI / 4f)
            {
                cannonAng = -(float)Math.PI / 4f;
            }
            if (cannonAng > 0)
            {
                cannonAng = 0;
            }

            //Aplicar as transformaçoes na torre e no canhao
            turretBone.Transform = Matrix.CreateRotationY(turretAng) * turretTransform;
            cannonBone.Transform = Matrix.CreateRotationX(cannonAng) * cannonTransform;



            //adiciona as mudanças "boneTransforms"
            tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms);

            //Fire Cannon
            if (keyboard.IsKeyUp(Keys.Space))
            {
                fierAux = true;
            }
            if (fierAux == true && keyboard.IsKeyDown(Keys.Space))
            {
                balas.Disparar(boneTransforms[cannonBone.Index].Translation, -Vector3.Normalize(boneTransforms[cannonBone.Index].Forward));
                fierAux = false;
            }

            balas.Update(gameTime, ref map, ref enemyTank);

            //Particles
            if (moving == 1)
            {
                po.SpawnParticles(gameTime, boneTransforms[rBWheelBone.Index].Translation + (Vector3.Normalize(boneTransforms[rBWheelBone.Index].Right) * 0.1f), tankModel.Root.Transform.Backward);
                po.SpawnParticles(gameTime, boneTransforms[lBWheelBone.Index].Translation + (Vector3.Normalize(boneTransforms[lBWheelBone.Index].Left) * 0.1f), tankModel.Root.Transform.Backward);
            }
            po.Update(gameTime, ref map);
        }
Ejemplo n.º 2
0
        public bool Update(GameTime gt, ref Map map, ref Tank enemyTank)
        {
            //Phisics
            vel += acceleration * (float)gt.ElapsedGameTime.TotalSeconds;
            pos += vel;

            //destruir bala
            float dist = Vector3.Distance(enemyTank.pos, pos);

            lifeTime -= (float)gt.ElapsedGameTime.TotalSeconds;

            //limites do mapa
            if (pos.X > map.width - 2)
            {
                return(true);
            }
            if (pos.X < 2)
            {
                return(true);
            }
            if (pos.Z > map.height - 2)
            {
                return(true);
            }
            if (pos.Z < 2)
            {
                return(true);
            }

            // tempo de vida e colisao com o mapa
            if (pos.Y <= map.GetY(pos) || lifeTime <= 0)
            {
                return(true);
            }

            //colisao com tank enimigo
            if (dist <= 0.5f && enemyTank.alive)
            {
                enemyTank.hp -= 20;//Damage done
                return(true);
            }

            //obter vetor right e dir
            Vector3 right = Vector3.Cross(Vector3.Up, Vector3.Normalize(vel));

            right.Normalize();
            Vector3 normal = Vector3.Cross(right, Vector3.Normalize(vel));

            normal.Normalize();

            //Criar matriz rotaçao
            Matrix rotationMatrix = Matrix.Identity;

            rotationMatrix.Forward = right;
            rotationMatrix.Up      = normal;
            rotationMatrix.Right   = -Vector3.Normalize(vel);

            //Criar matriz translaçao
            Matrix translationMatrix = Matrix.CreateTranslation(pos);

            //Criar matriz escala
            Matrix scaleMatrix = Matrix.CreateScale(scale);

            //adiciona modificaçoes
            balaModel.Root.Transform = scaleMatrix * rotationMatrix * translationMatrix;
            transform = balaModel.Root.Transform;

            return(false);
        }
Ejemplo n.º 3
0
        public void UpdateEnemyBot(GameTime gameTime, ref Map map, ref Tank playerTank)
        {
            if (alive)
            {
                // if (Vector3.Distance(playerTank.pos, pos) >= 2)
                {
                    moving = 1;
                    float   aMax       = 4f;
                    float   velMax     = 4.5f;
                    Vector3 nextPosVec = -playerTank.dir * playerTank.speed * 3 * playerTank.moving;
                    Vector3 vecVSeek   = Vector3.Normalize(((playerTank.pos + nextPosVec)) - pos) * velMax;
                    Vector3 a          = Vector3.Normalize((vecVSeek - vNext)) * aMax;
                    vNext += a * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    pos    = pos + (vNext * (float)gameTime.ElapsedGameTime.TotalSeconds);
                }

                /* else
                 * {
                 *   moving = 1;
                 *   float aMax = 10f;
                 *   float velMax = 5f;
                 *   Vector3 vecVSeek = -Vector3.Normalize((playerTank.pos) - pos) * velMax;
                 *   Vector3 a = Vector3.Normalize(vecVSeek - vNext) * aMax;
                 *   vNext += a * (float)gameTime.ElapsedGameTime.TotalSeconds;
                 *   pos = pos + vNext * (float)gameTime.ElapsedGameTime.TotalSeconds;
                 * }*/

                //fazer restriçao das bordas do mapa
                if (pos.X > map.width - 2)
                {
                    pos.X = map.width - 2f;
                }
                if (pos.X < 2)
                {
                    pos.X = 2f;
                }
                if (pos.Z > map.height - 2)
                {
                    pos.Z = map.height - 2f;
                }
                if (pos.Z < 2)
                {
                    pos.Z = 2f;
                }

                ////obter y e normal do tanke nuuma determinada posiçao
                pos.Y  = map.GetY(pos);
                normal = map.GetNormal(pos);

                ////obter vetor right e dir
                Vector3 right = Vector3.Cross(-vNext, normal);
                dir = Vector3.Cross(normal, right);
                dir.Normalize();
                right.Normalize();

                ////Criar matriz rotaçao(rotaçao do tank)
                Matrix rotationMatrix = Matrix.Identity;
                rotationMatrix.Forward = dir;
                rotationMatrix.Up      = normal;
                rotationMatrix.Right   = right;

                ////Criar matriz translaçao (movimento do tank)
                Matrix translationMatrix = Matrix.CreateTranslation(pos);

                ////Criar matriz escala (escala do tank)
                Matrix scaleMatrix = Matrix.CreateScale(scale);

                ////adiciona modificaçoes ao tank
                tankModel.Root.Transform = scaleMatrix * rotationMatrix * translationMatrix;


                ////Aplicar as transformaçoes na torre e no canhao
                turretBone.Transform = Matrix.CreateRotationY(turretAng) * turretTransform;
                cannonBone.Transform = Matrix.CreateRotationX(cannonAng) * cannonTransform;

                ////adiciona as mudanças "boneTransforms"
                tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms);

                if (hp <= 0)
                {
                    alive = false;
                }

                if (moving == 1)
                {
                    po.SpawnParticles(gameTime, boneTransforms[rBWheelBone.Index].Translation + (Vector3.Normalize(boneTransforms[rBWheelBone.Index].Right) * 0.1f), tankModel.Root.Transform.Backward);
                    po.SpawnParticles(gameTime, boneTransforms[lBWheelBone.Index].Translation + (Vector3.Normalize(boneTransforms[lBWheelBone.Index].Left) * 0.1f), tankModel.Root.Transform.Backward);
                }
                po.Update(gameTime, ref map);
            }
        }