Exemplo n.º 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            CurrentFrameIndex++;
            DebugDrawCommands.Clear();

            float deltaSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Perf.BeginSample(PerformanceSlots.InputUpdate);
            Input.Update(deltaSeconds);
            Perf.EndSample(PerformanceSlots.InputUpdate);

            if (Input.PlatformInput.WantsExit)
            {
                Exit();
            }

            if (Input.PlatformInput.ToggleFullscreen)
            {
                graphics.ToggleFullScreen();
            }

#if DEBUG
            deltaSeconds *= Input.DebugInput.SpeedMultiplier;

            {
                ActiveCamera.Movement.MovementVector = Input.DebugMovement;
            }

            if (Input.DebugInput.ToggleMainDrawing)
            {
                MainDrawingEnabled = !MainDrawingEnabled;
            }

            if (Input.DebugInput.ToggleDebugDrawing)
            {
                DebugDrawingEnabled = !DebugDrawingEnabled;
            }

            if (Input.DebugInput.TogglePhysicsDebugView)
            {
                PhysicsDebugView.Enabled = !PhysicsDebugView.Enabled;
            }

            if (Input.DebugInput.ToggleCameraVisibilityBounds)
            {
                if (ActiveCamera.CameraComponent.VisibilityBounds == null)
                {
                    ActiveCamera.CameraComponent.VisibilityBounds = CurrentLevel.LevelBounds;
                }
                else
                {
                    ActiveCamera.CameraComponent.VisibilityBounds = null;
                }

                if (ActiveCamera.SpringArm.Target == null)
                {
                    ActiveCamera.SpringArm.Target = Owliver;
                }
                else
                {
                    ActiveCamera.SpringArm.Target = null;
                }
            }

            if (Input.DebugInput.ResetCameraPosition)
            {
                ActiveCamera.Spatial.Position = Owliver.GetWorldSpatialData().Position;
            }
#endif

            {
                Owliver.Movement.MovementVector = Input.CharacterMovement;
                Owliver.Input = Input.CharacterInput;
            }

            // TODO(manu): Make use of `Input.CompationInput`!

            // Add pending game objects.
            GameObjects.AddRange(GameObjectsPendingAdd);
            foreach (GameObject go in GameObjectsPendingAdd)
            {
                go.Initialize();
                go.PostInitialize();
            }
            GameObjectsPendingAdd.Clear();

            // Execute pre-physics update.
            foreach (GameObject go in GameObjects)
            {
                go.PrePhysicsUpdate(deltaSeconds);
            }

            // Physics simulation
            float simTime = _excessSimTime + deltaSeconds;
            Perf.BeginSample(PerformanceSlots.WorldStep);
            while (simTime > _secondsPerSimStep)
            {
                World.Step(_secondsPerSimStep);
                simTime -= _secondsPerSimStep;
            }
            Perf.EndSample(PerformanceSlots.WorldStep);
            _excessSimTime = simTime;

            CurrentLevel.Update(deltaSeconds);

            // Post-physics update.
            foreach (GameObject go in GameObjects)
            {
                go.Update(deltaSeconds);

                AABB aabb = Global.CreateInvalidAABB();
                foreach (SpatialComponent sc in go.GetComponents <SpatialComponent>())
                {
                    AABB other = sc.GetWorldSpatialData().AbsoluteAABB;
                    aabb.Combine(ref other);
                }

                DebugDrawCommands.Add(view => view.DrawPoint(aabb.Center, Conversion.ToMeters(3), Color.MonoGameOrange));
                DebugDrawCommands.Add(view => view.DrawAABB(ref aabb, Color.Lime));
            }

            // Remove pending game objects.
            GameObjects.RemoveAll(go => GameObjectsPendingRemove.Contains(go));
            foreach (GameObject go in GameObjectsPendingRemove)
            {
                go.Destroy();
            }
            GameObjectsPendingRemove.Clear();

            base.Update(gameTime);

            Perf.AdvanceFrame();
        }
Exemplo n.º 2
0
        public static GameObject CreateKnown(KnownGameObject type)
        {
            GameObject go = new GameObject();

            switch (type)
            {
            case KnownGameObject.Owliver:
            {
                go = new Owliver();
            }
            break;

            case KnownGameObject.Shop:
            {
                go = new Shop();
            }
            break;

            case KnownGameObject.Slurp:
            {
                go = new Slurp();
            }
            break;

            case KnownGameObject.Tankton:
            {
                go = new Tankton();
            }
            break;

            case KnownGameObject.DeathConfetti:
            {
                go = new DeathConfetti();
            }
            break;

            case KnownGameObject.Projectile:
            {
                go = new Projectile();
            }
            break;

            case KnownGameObject.BackgroundScreen:
            {
                go = new BackgroundScreen();
            }
            break;

            case KnownGameObject.Gate:
            {
                go = new Gate();
            }
            break;

            case KnownGameObject.Flora_Fir:
            case KnownGameObject.Flora_FirAlt:
            case KnownGameObject.Flora_Conifer:
            case KnownGameObject.Flora_ConiferAlt:
            case KnownGameObject.Flora_Oak:
            case KnownGameObject.Flora_Orange:
            case KnownGameObject.Flora_Bush:
            {
                FloraType floraType = (FloraType)(type - KnownGameObject.Flora_Fir);
                go = new Flora()
                {
                    TreeType = floraType,
                };
            }
            break;

            case KnownGameObject.Bonbon_Gold:
            case KnownGameObject.Bonbon_Red:
            {
                BonbonType bonbonType = (BonbonType)(type - KnownGameObject.Bonbon_Gold);
                go = new BonbonPickup()
                {
                    BonbonType = bonbonType,
                };
            }
            break;

            case KnownGameObject.Key_Gold:
            {
                KeyType keyType = (KeyType)(type - KnownGameObject.Key_Gold);
                go = new KeyPickup()
                {
                    KeyType = keyType,
                };
            }
            break;

            case KnownGameObject.ShopItem_FruitBowl:
            case KnownGameObject.ShopItem_FishingRod:
            case KnownGameObject.ShopItem_Stick:
            {
                ShopItemType itemType = (ShopItemType)(type - KnownGameObject.ShopItem_FruitBowl);
                go = new ShopItem()
                {
                    ItemType = itemType
                };
            }
            break;

            case KnownGameObject.Random_FirTree:
            {
                FloraType floraType = _random.Choose(FloraType.Fir, FloraType.Conifer);
                go = new Flora()
                {
                    TreeType = floraType,
                };
            }
            break;

            case KnownGameObject.Random_FirTreeAlt:
            {
                FloraType floraType = _random.Choose(FloraType.FirAlt, FloraType.ConiferAlt);
                go = new Flora()
                {
                    TreeType = floraType,
                };
            }
            break;

            case KnownGameObject.Random_OakTree:
            {
                FloraType floraType = _random.Choose(FloraType.Oak, FloraType.Orange);
                go = new Flora()
                {
                    TreeType = floraType,
                };
            }
            break;

            default:
                throw new ArgumentException("Unknown game object type.");
            }

            int instanceID = _knownCreationCount[(int)type]++;

            go.Name = $"{type}_{instanceID}";

            return(go);
        }
Exemplo n.º 3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            WorldRenderer.Initialize(GraphicsDevice);
            UIRenderer.Initialize(GraphicsDevice);

            PhysicsDebugView.LoadContent(GraphicsDevice, Content);

            CurrentLevel = new Level(Content)
            {
                ContentNameFormat_Ground    = "level01/level1_ground_{0}{1}",
                ContentNameFormat_Collision = "level01/static_collision/static_collision_{0}{1}",
                ContentNameFormat_Layout    = "level01/layout/layout_{0}{1}",
            };

            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 7; y++)
                {
                    CurrentLevel.CreateScreen(x, y);
                }
            }

            {
                Owliver = new Owliver();
                Owliver.Spatial.Position += Conversion.ToMeters(450, 600);
                AddGameObject(Owliver);

                CurrentLevel.CullingCenter = Owliver;
            }

            CurrentLevel.LoadContent();

            {
                ActiveCamera = new CameraObject();

                ActiveCamera.CameraComponent.VisibilityBounds = CurrentLevel.LevelBounds;
                ActiveCamera.CameraComponent.OnGraphicsDeviceReset(GraphicsDevice);
                Window.ClientSizeChanged += (o, e) =>
                {
                    ActiveCamera.CameraComponent.OnGraphicsDeviceReset(GraphicsDevice);
                };

                ActiveCamera.SpringArm.BeforeInitialize += () =>
                {
                    ActiveCamera.SpringArm.Target = Owliver;
                };

                AddGameObject(ActiveCamera);
            }

#if true
            {
                var testSlurp = GameObjectFactory.CreateKnown(KnownGameObject.Slurp);
                testSlurp.Spatial.Position += Conversion.ToMeters(300, 250);
                AddGameObject(testSlurp);
            }

            {
                var testTankton = new Tankton();
                testTankton.Spatial.Position += Conversion.ToMeters(900, 350);
                AddGameObject(testTankton);
            }

            {
                Random rand = new Random();

                Global.SpawnGameObjectsInRingFormation(
                    center: Conversion.ToMeters(2400, 500),
                    radius: 2.5f,
                    numToSpawn: 15,
                    rand: rand,
                    types: new[] { KnownGameObject.Bonbon_Gold, KnownGameObject.Bonbon_Red });

                Global.SpawnGameObjectsInRingFormation(
                    center: Conversion.ToMeters(2400, 500),
                    radius: 1.0f,
                    numToSpawn: 5,
                    rand: rand,
                    types: new[] { KnownGameObject.Bonbon_Gold, KnownGameObject.Bonbon_Red });
            }

            {
                var testKey = new KeyPickup()
                {
                    KeyType = KeyType.Gold
                };
                testKey.Spatial.Position += Conversion.ToMeters(700, 720);
                AddGameObject(testKey);
            }

            {
                var testGate = new Gate();
                testGate.Spatial.Position += Conversion.ToMeters(2300, 1100);
                AddGameObject(testGate);
            }

            {
                var testShop = new Shop();
                testShop.Spatial.Position += Conversion.ToMeters(1300, 500);
                AddGameObject(testShop);

                var testFruitBowl = new ShopItem()
                {
                    ItemType = ShopItemType.FruitBowl
                };
                testFruitBowl.Price = ShopItemPriceType._20;
                testFruitBowl.AttachTo(testShop);
                testFruitBowl.Spatial.Position += Conversion.ToMeters(-110.0f, -90.0f);
                AddGameObject(testFruitBowl);

                var testRod = new ShopItem()
                {
                    ItemType = ShopItemType.FishingRod
                };
                testRod.Price = ShopItemPriceType._100;
                testRod.AttachTo(testShop);
                testRod.Spatial.Position += Conversion.ToMeters(128.0f, -80.0f);
                AddGameObject(testRod);
            }

            {
                var testSinger = new Singer();
                testSinger.Spatial.Position += Conversion.ToMeters(2600, 300);
                AddGameObject(testSinger);
            }

            {
                var testTrap = new SpikeTrap()
                {
                    Orientation = SpikeTrapOrientation.Vertical,
                    SensorReach = 4.0f,
                };
                testTrap.Spatial.Position += Conversion.ToMeters(1600, 500);
                AddGameObject(testTrap);
            }
#endif

            {
                var BackgroundMusic = Content.Load <Song>("snd/FiluAndDina_-_Video_Game_Background_-_Edit");
                MediaPlayer.IsRepeating = true;
#if false
                MediaPlayer.Play(BackgroundMusic);
#endif
            }

            Hud.Initialize();
            Hud.ResetLayout(GraphicsDevice.Viewport.Bounds);
            Window.ClientSizeChanged += (o, e) =>
            {
                Hud.ResetLayout(GraphicsDevice.Viewport.Bounds);
            };
        }