コード例 #1
0
        public void Draw()
        {
            BeginDrawing();

            ClearBackground(Color.RAYWHITE);



            // Green/red square
            DrawRectangle(120, 120, 80, 80, boxColor);

            if (showHitboxCorners)
            {
                Color cornerColor = new Color();

                // For loops a finite 4 times- once for each corner.
                // Sphere colliders are ommited from this since their hitbox is their size.
                for (int i = 0; i < 4; i++)
                {
                    // This will create a checker pattern, purple being min/max.
                    if (i % 2 == 0)
                    {
                        cornerColor = Color.GREEN;
                    }
                    else
                    {
                        cornerColor = Color.DARKPURPLE;
                    }

                    // Player's collider
                    DrawCircle((int)playerCollider.Corners()[i].x, (int)playerCollider.Corners()[i].y, 6, cornerColor);

                    // Red/green box's collider
                    DrawCircle((int)boxCollider.Corners()[i].x, (int)boxCollider.Corners()[i].y, 6, cornerColor);

                    // Destructable items colliders
                    for (int j = 0; j < destructableHolder.GetChildCount(); j++)
                    {
                        Destructable temp = (Destructable)destructableHolder.GetChild(j);

                        if (temp.destHP > 0)
                        {
                            DrawCircle((int)temp.destCollider.Corners()[i].x, (int)temp.destCollider.Corners()[i].y, 6, cornerColor);
                        }
                    }

                    // Walls Colliders
                    for (int j = 0; j < wallHolder.GetChildCount(); j++)
                    {
                        Wall temp = (Wall)wallHolder.GetChild(j);

                        DrawCircle((int)temp.wallCollider.Corners()[i].x, (int)temp.wallCollider.Corners()[i].y, 6, cornerColor);
                    }
                }
            }

            foreach (SceneObject s in Hierarchy)
            {
                s.Draw();
            }

            // TEXT SHOULD BE DRAWN ABOVE ALL OTHER ITEMS; PLACE TEXT BELOW HERE

            DrawText($"FPS: {fps.ToString()}", 10, 10, 12, rainbow);
            if (showHitboxCorners)
            {
                DrawText("HITBOX VIEW", 60, 10, 12, rainbow);
            }

            DrawText($"Time Left: {remainingTime}", 245, 20, 18, Color.RED);

            DrawText("SCORE:", 540, 10, 14, Color.DARKGRAY);
            DrawText($"{score}", 595, 10, 14, rainbow);

            DrawText("Highscores:", 10, 400, 20, rainbow);
            for (int i = 0; i < highscores.Length; i++)
            {
                DrawText($"{i + 1}: {highscores[i]}", 20, 425 + (15 * i), 14, Color.DARKGRAY);
            }

            DrawText("Press P to toggle hitbox logic.", 350, 460, 12, Color.DARKGRAY);
            if (collisionToggle)
            {
                DrawText("Transforming", 540, 460, 12, rainbow);
            }
            else
            {
                DrawText("Static", 565, 460, 12, rainbow);
            }

            EndDrawing();
        }
コード例 #2
0
        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
        }