Exemplo n.º 1
0
        public static bool HasCollided(
            long object1XMillis,
            long object1YMillis,
            DTImmutableList <ObjectBox> object1Boxes,            /* nullable */
            long object2XMillis,
            long object2YMillis,
            DTImmutableList <ObjectBox> object2Boxes /* nullable */)
        {
            if (object1Boxes == null || object2Boxes == null)
            {
                return(false);
            }
            if (object1Boxes.Count == 0 || object2Boxes.Count == 0)
            {
                return(false);
            }

            for (int i = 0; i < object1Boxes.Count; i++)
            {
                ObjectBox obj1Box = object1Boxes[i];
                for (int j = 0; j < object2Boxes.Count; j++)
                {
                    ObjectBox obj2Box = object2Boxes[j];

                    long obj1Left  = object1XMillis + obj1Box.LowerXMillis;
                    long obj1Right = object1XMillis + obj1Box.UpperXMillis;

                    long obj2Left  = object2XMillis + obj2Box.LowerXMillis;
                    long obj2Right = object2XMillis + obj2Box.UpperXMillis;

                    bool noXCollision = obj1Right < obj2Left || obj2Right < obj1Left;

                    if (!noXCollision)
                    {
                        long obj1Bottom = object1YMillis + obj1Box.LowerYMillis;
                        long obj1Top    = object1YMillis + obj1Box.UpperYMillis;

                        long obj2Bottom = object2YMillis + obj2Box.LowerYMillis;
                        long obj2Top    = object2YMillis + obj2Box.UpperYMillis;

                        bool noYCollision = obj1Top < obj2Bottom || obj2Top < obj1Bottom;

                        if (!noYCollision)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        public void Render(TranslatedDTDanmakuDisplay display)
        {
            if (this.shouldPlayPlayerShootSound)
            {
                if (this.playerShootSoundCooldownInMillis <= 0)
                {
                    display.PlaySound(sound: DTDanmakuSound.PlayerShoot, volume: this.soundVolume);
                    this.playerShootSoundCooldownInMillis = 10;
                }
                this.shouldPlayPlayerShootSound = false;
            }

            foreach (DTDanmakuSound sound in this.soundsThatNeedToBePlayed)
            {
                display.PlaySound(sound: sound, volume: this.soundVolume);
            }
            this.soundsThatNeedToBePlayed = new List <DTDanmakuSound>();

            // Draw background
            display.DrawRectangle(
                x: 0,
                y: 0,
                width: 1000,
                height: 700,
                color: new DTColor(r: 132, g: 245, b: 255),                 // teal
                fill: true);

            EnemyObjectRenderer.Render(
                enemyObjects: this.enemyObjects,
                enemyObjectType: EnemyObjectType.Enemy,
                display: display,
                spriteNameToImageDictionary: this.spriteNameToImageDictionary);

            Player.RenderPlayer(
                player: this.player,
                display: display,
                debugMode: this.debugMode,
                debug_renderHitboxes: this.debug_renderHitboxes);

            PowerUp.RenderPowerUps(
                powerUps: this.powerUps,
                display: display);

            PlayerBullet.RenderPlayerBullets(
                playerBullets: this.playerBullets,
                display: display);

            EnemyObjectRenderer.Render(
                enemyObjects: this.enemyObjects,
                enemyObjectType: EnemyObjectType.EnemyBullet,
                display: display,
                spriteNameToImageDictionary: this.spriteNameToImageDictionary);

            // Placeholders shouldn't have any sprites, so this should be a no-op
            EnemyObjectRenderer.Render(
                enemyObjects: this.enemyObjects,
                enemyObjectType: EnemyObjectType.Placeholder,
                display: display,
                spriteNameToImageDictionary: this.spriteNameToImageDictionary);

            Player.RenderPlayerLifeIcons(
                numberOfLivesRemaining: this.player.numLivesLeft,
                display: display);

            this.bossHealthBar.RenderBossHealthBar(display: display);

            /*
             *      Note that since these debug things bypass regular game logic, they may break other stuff or crash the program
             *      (Should be used for debugging / development only)
             */
            if (this.debugMode)
            {
                if (this.debug_renderHitboxes)
                {
                    foreach (EnemyObject enemy in this.enemyObjects)
                    {
                        if (enemy.CollisionBoxes != null)
                        {
                            for (int i = 0; i < enemy.CollisionBoxes.Count; i++)
                            {
                                display.DrawRectangle(
                                    x: (int)((enemy.XMillis + enemy.CollisionBoxes[i].LowerXMillis) / 1000L),
                                    y: (int)(700 - ((enemy.YMillis + enemy.CollisionBoxes[i].UpperYMillis) / 1000L)),
                                    width: (int)((enemy.CollisionBoxes[i].UpperXMillis - enemy.CollisionBoxes[i].LowerXMillis) / 1000L + 1),
                                    height: (int)((enemy.CollisionBoxes[i].UpperYMillis - enemy.CollisionBoxes[i].LowerYMillis) / 1000L + 1),
                                    color: new DTColor(r: 0, g: 0, b: 255, alpha: 50),
                                    fill: true);
                            }
                        }
                        if (enemy.DamageBoxes != null)
                        {
                            for (int i = 0; i < enemy.DamageBoxes.Count; i++)
                            {
                                display.DrawRectangle(
                                    x: (int)((enemy.XMillis + enemy.DamageBoxes[i].LowerXMillis) / 1000L),
                                    y: (int)(700 - ((enemy.YMillis + enemy.DamageBoxes[i].UpperYMillis) / 1000L)),
                                    width: (int)((enemy.DamageBoxes[i].UpperXMillis - enemy.DamageBoxes[i].LowerXMillis) / 1000L + 1),
                                    height: (int)((enemy.DamageBoxes[i].UpperYMillis - enemy.DamageBoxes[i].LowerYMillis) / 1000L + 1),
                                    color: new DTColor(r: 0, g: 0, b: 255, alpha: 50),
                                    fill: true);
                            }
                        }
                    }
                    foreach (PlayerBullet playerBullet in this.playerBullets)
                    {
                        for (int i = 0; i < playerBullet.CollisionBoxes.Count; i++)
                        {
                            ObjectBox playerBulletObjectBox = playerBullet.CollisionBoxes[i];
                            display.DrawRectangle(
                                x: (int)((playerBullet.xMillis + playerBulletObjectBox.LowerXMillis) / 1000L),
                                y: (int)(700 - ((playerBullet.yMillis + playerBulletObjectBox.UpperYMillis) / 1000L)),
                                width: (int)((playerBulletObjectBox.UpperXMillis - playerBulletObjectBox.LowerXMillis) / 1000L + 1),
                                height: (int)((playerBulletObjectBox.UpperYMillis - playerBulletObjectBox.LowerYMillis) / 1000L + 1),
                                color: new DTColor(r: 255, g: 0, b: 0, alpha: 50),
                                fill: true);
                        }
                    }
                }
            }

            /*
             *      Note that since these debug things bypass regular game logic, they may break other stuff or crash the program
             *      (Should be used for debugging / development only)
             */
            if (this.debugMode)
            {
                display.DebugPrint(
                    x: 10,
                    y: 10 + 30,
                    debugText: "Number of objects: " + (this.powerUps.Count + this.playerBullets.Count + this.enemyObjects.Count));
            }
        }