예제 #1
0
        // Event Handlers
        protected virtual void OnChangeScene(ChangeSceneEventArgs e)
        {
            EventHandler <ChangeSceneEventArgs> handler = ChangeSceneEvent;

            // Raise the event
            handler?.Invoke(this, e);
        }
예제 #2
0
        private static void OnChangeScene(object sender, ChangeSceneEventArgs e)
        {
            TextAsset progressFile = Engine.AssetLoader.Get <TextAsset>("progress.json");
            TextAsset nextRoom     = Engine.AssetLoader.Get <TextAsset>(e.NextScenePath);

            // Read tmx map file and create a TiledMap
            TextAsset tmxMap   = Engine.AssetLoader.Get <TextAsset>("Rooms/3x2.tmx");
            XMLReader reader   = new XMLReader(tmxMap.Content);
            TiledMap  tiledMap = new TiledMap(reader);

            MainScene scene = new MainScene(progressFile, nextRoom, tiledMap);

            GameContext.Scene       = scene;
            scene.ChangeSceneEvent += OnChangeScene;

            Engine.SceneManager.SetScene(scene);
        }
예제 #3
0
        public void Update()
        {
            if (CodeVariant == 2)
            {
                if (SecondaryLayer.CurrentTrack?.File.Name == "frozen cave loop 3.wav")
                {
                    SecondaryLayer.LoopingCurrent = true;
                }
            }

            // Update the player
            Player.Update();

            // Update the camera
            Vector2 windowSize = Engine.Host.Window.Size; // Change to render size?
            // The center of the player on the X axis only
            float playerXCenter = Player.X + Player.Size.X / 2;

            if (playerXCenter < Engine.Renderer.Camera.X)
            {
                // Check left camera side
                if (Engine.Renderer.Camera.X - (windowSize.X / 2) > 0 + Player.VelocityX)
                {
                    // Engine.Renderer.Camera.X -= VelocityX;
                    Engine.Renderer.Camera.X = playerXCenter;

                    // Move the foreground decorations faster than the player
                    foreach (Decoration dec in ForegroundDecorations)
                    {
                        dec.X += Player.VelocityX + dec.VelocityOffsetX;
                    }
                }
                // If not fix the camera along the edge of the room
                else
                {
                    Engine.Renderer.Camera.X = 0 + (windowSize.X / 2);
                }
            }
            else if (playerXCenter > Engine.Renderer.Camera.X)
            {
                // Check right camera side
                if (Engine.Renderer.Camera.X + (windowSize.X / 2) < LoadedRoom.Size.X - Player.VelocityX)
                {
                    // Engine.Renderer.Camera.X += VelocityX;
                    Engine.Renderer.Camera.X = playerXCenter;

                    // Move the foreground decorations faster than the player
                    foreach (Decoration dec in ForegroundDecorations)
                    {
                        dec.X -= Player.VelocityX + dec.VelocityOffsetX;
                    }
                }
                // If not fix the camera along the edge of the room
                else
                {
                    Engine.Renderer.Camera.X = LoadedRoom.Size.X - (windowSize.X / 2);
                }
            }

            float playYCenterWithOffset = Player.Center.Y - 216;

            if (playYCenterWithOffset < Engine.Renderer.Camera.Y)
            {
                // When the vertical velocity is negative treat it as 0 in the calculations
                float velocityY = Player.VelocityY > 0 ? Player.VelocityY : 0;

                // If the upper border of the camera has enough room to move up with velocityY, then move it
                if (Engine.Renderer.Camera.Y - (windowSize.Y / 2) > 0 + velocityY)
                {
                    Engine.Renderer.Camera.Y = playYCenterWithOffset; // CameraOffsetY
                }
                // If not fix the camera along the edge of the room
                else
                {
                    Engine.Renderer.Camera.Y = 0 + (windowSize.Y / 2);
                }
            }
            else if (playYCenterWithOffset > Engine.Renderer.Camera.Y)
            {
                // When falling the camera always follows the player
                Engine.Renderer.Camera.Y = playYCenterWithOffset; // CameraOffsetY
            }

            // Interaction
            if (Player.IsInteracting && CurrentInteration == null)
            {
                CurrentInteration = new Interaction(Player, Player.InteractTarget);
            }
            else if (!Player.IsInteracting && CurrentInteration != null)
            {
                CurrentInteration = null;
            }
            else if (Player.IsInteracting && CurrentInteration != null)
            {
                CurrentInteration.Update();
            }

            // Update all non-player units
            for (int i = 0; i < NonPlayerUnits.Count; i++)
            {
                NonPlayerUnits[i].Update();
            }

            // Check for collision with Scene changers
            SceneChanger sceneChanger = CollisionUtils.IntersectsWithSceneChangers(Player.CollisionBox.ToRectangle());

            if (sceneChanger != null)
            {
                ChangeSceneEventArgs eventArgs = new ChangeSceneEventArgs();
                eventArgs.NextScenePath = sceneChanger.NextScene;
                this.OnChangeScene(eventArgs);
            }

            // Debug
            // Mouse click to add to DebugObjects
            if (Engine.InputManager.IsMouseKeyDown(MouseKey.Left))
            {
                AddDebugObject();
            }
            else if (Engine.InputManager.IsMouseKeyDown(MouseKey.Right) && DebugObjects.Count > 0)
            {
                DebugObjects.RemoveAt(DebugObjects.Count - 1);
            }

            // Camera Zoom Out
            if (Engine.InputManager.IsKeyDown(Key.R))
            {
                if (Engine.Renderer.Camera.Zoom == 1f)
                {
                    Engine.Renderer.Camera.Zoom = 0.4f;
                }
                else if (Engine.Renderer.Camera.Zoom == 0.4f)
                {
                    Engine.Renderer.Camera.Zoom = 0.1f;
                }
                else if (Engine.Renderer.Camera.Zoom == 0.1f)
                {
                    Engine.Renderer.Camera.Zoom = 1f;
                }
            }

            // Quit on Escape press
            if (Engine.InputManager.IsKeyHeld(Key.Escape))
            {
                Engine.Quit();
            }
        }