Example #1
0
        public override void OnUpdate(float deltaTime)
        {
            lifetime -= 1 * deltaTime;
            if (lifetime <= 0)
            {
                removeMe = true;
            }

            projectileCollider.Resize(new MathFunctions.Vector3(GlobalTransform.m7, GlobalTransform.m8, 0), 6);

            MathFunctions.Vector3 facing = new MathFunctions.Vector3(direction.x, direction.y, 1) * deltaTime * speed;
            Translate(facing.x, facing.y);
        }
Example #2
0
 public Projectile(MathFunctions.Vector3 dir)
 {
     direction = dir;
 }
        public void Update()
        {
            #region Time Calculations
            deltaTime = gameTime.GetDeltaTime();

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

            // Updates the rainbow color every frame!
            rainbowColorF++;
            if (rainbowColorF <= 0 || rainbowColorF >= 360)
            {
                rainbowColorF = 0;
            }
            rainbow = ColorFromHSV(new Vector3(rainbowColorF, 1, 1));

            // In-game timer.
            // If the infinite time cheat is on, time will never advance. Once time is
            // resumed, the game will likely instantly end. Only intended for testing.
            if (!infiniteTime)
            {
                remainingTime = 45 - gameTime.Seconds; // THIS SHOULD BE 45
            }
            else
            {
                remainingTime = 1;
            }

            // Clamped to prevent the player from seeing "Time Remaining: [negitive number]".
            if (remainingTime <= 0)
            {
                remainingTime = 0;
            }
            #endregion

            #region Player Input
            lastPlayerTransform.Set(tankObject.GlobalTransform);

            // Player movement is restricted when colliding with a wall.
            // They can still move their turret and fire however.
            if (IsKeyDown(KeyboardKey.KEY_A) && !isCollidingWall)
            {
                tankObject.Rotate(-deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_D) && !isCollidingWall)
            {
                tankObject.Rotate(deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_W) && !isCollidingWall)
            {
                playerFacing = new MathFunctions.Vector3(tankObject.LocalTransform.m1, tankObject.LocalTransform.m2, 1) * deltaTime * playerSpeed;
                tankObject.Translate(playerFacing.x, playerFacing.y);
            }
            if (IsKeyDown(KeyboardKey.KEY_S) && !isCollidingWall)
            {
                playerFacing = new MathFunctions.Vector3(tankObject.LocalTransform.m1, tankObject.LocalTransform.m2, 1) * deltaTime * -playerSpeed;
                tankObject.Translate(playerFacing.x, playerFacing.y);
            }
            if (IsKeyDown(KeyboardKey.KEY_Q))
            {
                turretObject.Rotate(-deltaTime);
            }
            if (IsKeyDown(KeyboardKey.KEY_E))
            {
                turretObject.Rotate(deltaTime);
            }
            if (IsKeyPressed(KeyboardKey.KEY_SPACE))
            {
                // Create a new projectile going the direction of the turret's rotation.
                Projectile temp = new Projectile(turretObject.GlobalTransform.m5, -turretObject.GlobalTransform.m4);

                // Set the position to be near the end of the turret's barrel upon spawning.
                temp.SetPosition(turretObject.GlobalTransform.m7 + (turretObject.GlobalTransform.m5 * 30), turretObject.GlobalTransform.m8 + (-turretObject.GlobalTransform.m4 * 30));

                // Add it to the projectile holder.
                projectileHolder.AddChild(temp);
            }
            if (IsKeyPressed(KeyboardKey.KEY_P))
            {
                collisionToggle = !collisionToggle;
            }
            if (IsKeyPressed(KeyboardKey.KEY_O))
            {
                showHitboxCorners = !showHitboxCorners;
            }
            if (IsKeyPressed(KeyboardKey.KEY_I))
            {
                infiniteTime = !infiniteTime;
            }
            #endregion

            #region Collision Box Updates
            if (collisionToggle)
            {
                // For an AABB that resizes during transforms.
                for (int i = 0; i < playerCornerPoints.Length; i++)
                {
                    pCornersArray[i] = new MathFunctions.Vector3(playerCornerPoints[i].GlobalTransform.m7, playerCornerPoints[i].GlobalTransform.m8, 0);
                }
                playerCollider.Fit(pCornersArray);
            }
            else
            {
                // For a static AABB.
                playerCollider.Resize(new MathFunctions.Vector3(tankObject.GlobalTransform.m7 - (tankSprite.Width / 2), tankObject.GlobalTransform.m8 - (tankSprite.Height / 2), 0),
                                      new MathFunctions.Vector3(tankObject.GlobalTransform.m7 + (tankSprite.Width / 2), tankObject.GlobalTransform.m8 + (tankSprite.Height / 2), 0));
            }
            #endregion

            #region Projectiles
            // Check to see if the projectile acually needs to be deleted first.
            for (int i = 0; i < projectileHolder.GetChildCount(); i++)
            {
                if (projectileHolder.GetChild(i).removeMe)
                {
                    projectileHolder.RemoveChild(projectileHolder.GetChild(i));
                }
            }
            #endregion

            #region Collision Logic
            // PLAYER
            // Checking if the player is hitting any of the destructable objects.
            // This limits movement and pushes the player out of the collision box.
            for (int i = 0; i < destructableHolder.GetChildCount(); i++)
            {
                Destructable temp = (Destructable)destructableHolder.GetChild(i);

                if (playerCollider.Overlaps(temp.destCollider) && temp.destHP > 0)
                {
                    isCollidingWall = true;

                    tankObject.SetPosition(lastPlayerTransform.m7, lastPlayerTransform.m8);

                    // Break here to prevent any more checks setting isCollidingWall to false when you're hitting at least one object.
                    break;
                }
                else
                {
                    isCollidingWall = false;
                }
            }

            // Checking for the player hitting any of the walls.
            // This limits movement and pushes the player out of the collision box.
            for (int i = 0; i < wallHolder.GetChildCount(); i++)
            {
                Wall temp = (Wall)wallHolder.GetChild(i);

                if (playerCollider.Overlaps(temp.wallCollider))
                {
                    isCollidingWall = true;

                    tankObject.SetPosition(lastPlayerTransform.m7, lastPlayerTransform.m8);

                    // Break here to prevent any more checks setting isCollidingWall to false when you're hitting at least one object.
                    break;
                }
                else
                {
                    isCollidingWall = false;
                }
            }

            // Checking to see if the player is in the red/green box.
            // If so, the box will turn red.
            if (boxCollider.Overlaps(playerCollider))
            {
                boxColor = Color.RED;
            }
            else
            {
                boxColor = Color.GREEN;
            }

            // PROJECTILES
            for (int i = 0; i < projectileHolder.GetChildCount(); i++)
            {
                // Grab a Projectile from the holder
                Projectile temp = (Projectile)projectileHolder.GetChild(i);

                // Check against the Destructable items in the holder.
                // This takes away 1 HP from the itme and discards the projectile.
                for (int k = 0; k < destructableHolder.GetChildCount(); k++)
                {
                    Destructable tempDest = (Destructable)destructableHolder.GetChild(k);

                    if (temp.projectileCollider.Overlaps(tempDest.destCollider) && tempDest.destHP > 0)
                    {
                        projectileHolder.RemoveChild(temp);
                        tempDest.destHP--;
                    }
                }

                // Check against the Targets in the holder.
                // This will give the player a point and respawn the target elsewhere.
                for (int j = 0; j < targetHolder.GetChildCount(); j++)
                {
                    Target temptarget = (Target)targetHolder.GetChild(j);

                    if (temp.projectileCollider.Overlaps(temptarget.targetCollider))
                    {
                        temptarget.Respawn();
                        score++;
                        projectileHolder.RemoveChild(temp);
                    }
                }

                // Check to see if the projectile is in the green/red box.
                // But first- if the player already is in there, we won't check this.
                if (!boxCollider.Overlaps(playerCollider))
                {
                    if (temp.projectileCollider.Overlaps(boxCollider))
                    {
                        boxColor = Color.RED;
                        break;
                    }
                    else
                    {
                        boxColor = Color.GREEN;
                    }
                }
            }
            #endregion

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

            #region Debug - KEEP COMMENTED UNLESS TESTING

            #endregion
        }