static void UpdateCameraCenterInsideMap(ref Camera2D camera, ref Player player, EnvItem[] envItems, int envItemsLength, float delta, int width, int height)
        {
            camera.target = player.position;
            camera.offset = new Vector2(width / 2, height / 2);
            float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;

            for (int i = 0; i < envItemsLength; i++)
            {
                EnvItem ei = envItems[i];
                minX = Math.Min(ei.rect.x, minX);
                maxX = Math.Max(ei.rect.x + ei.rect.width, maxX);
                minY = Math.Min(ei.rect.y, minY);
                maxY = Math.Max(ei.rect.y + ei.rect.height, maxY);
            }

            Vector2 max = GetWorldToScreen2D(new Vector2(maxX, maxY), camera);
            Vector2 min = GetWorldToScreen2D(new Vector2(minX, minY), camera);

            if (max.X < width)
            {
                camera.offset.X = width - (max.X - width / 2);
            }
            if (max.X < height)
            {
                camera.offset.X = height - (max.X - height / 2);
            }
            if (min.X > 0)
            {
                camera.offset.X = width / 2 - min.X;
            }
            if (min.X > 0)
            {
                camera.offset.X = height / 2 - min.X;
            }
        }
        static void UpdatePlayer(ref Player player, EnvItem[] envItems, int envItemsLength, float delta)
        {
            if (IsKeyDown(KEY_LEFT))
            {
                player.position.X -= PLAYER_HOR_SPD * delta;
            }
            if (IsKeyDown(KEY_RIGHT))
            {
                player.position.X += PLAYER_HOR_SPD * delta;
            }
            if (IsKeyDown(KEY_SPACE) && player.canJump)
            {
                player.speed   = -PLAYER_JUMP_SPD;
                player.canJump = false;
            }

            int hitObstacle = 0;

            for (int i = 0; i < envItemsLength; i++)
            {
                EnvItem ei = envItems[i];
                Vector2 p  = player.position;
                if (ei.blocking != 0 &&
                    ei.rect.x <= p.X &&
                    ei.rect.x + ei.rect.width >= p.X &&
                    ei.rect.y >= p.X &&
                    ei.rect.y < p.X + player.speed * delta)
                {
                    hitObstacle  = 1;
                    player.speed = 0.0f;
                    p.X          = ei.rect.y;
                }
            }

            if (hitObstacle == 0)
            {
                player.position.X += player.speed * delta;
                player.speed      += G * delta;
                player.canJump     = false;
            }
            else
            {
                player.canJump = true;
            }
        }
예제 #3
0
        /// <summary>
        /// Load the EnvMap key:val pairs into our application facing side
        /// </summary>
        private void Load()
        {
            // TODO: After initial testing, look at adding "Sections" for management
            EnvConfig config = new EnvConfig("Environment", this);

            this.Configs.Add(config);
            EnvItem item = null;

            for (int j = 0; j < envmap.EnvList.Count; j++)
            {
                item = envmap.GetItem(j);
                if (item.Name != null)
                {
                    config.Add(item.Name, item.Value);
                }
//                if ( null != item.Value)
//                {
//                    config.Set(item.Name, item.Value);
//                }
            }
        }
        public static int Main()
        {
            // Initialization
            //--------------------------------------------------------------------------------------
            const int screenWidth  = 800;
            const int screenHeight = 450;

            InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");

            Player player = new Player();

            player.position = new Vector2(400, 280);
            player.speed    = 0;
            player.canJump  = false;
            EnvItem[] envItems = new EnvItem[] {
                new EnvItem(new Rectangle(0, 0, 1000, 400), 0, LIGHTGRAY),
                new EnvItem(new Rectangle(0, 400, 1000, 200), 1, GRAY),
                new EnvItem(new Rectangle(300, 200, 400, 10), 1, GRAY),
                new EnvItem(new Rectangle(250, 300, 100, 10), 1, GRAY),
                new EnvItem(new Rectangle(650, 300, 100, 10), 1, GRAY)
            };

            int envItemsLength = envItems.Length;

            Camera2D camera = new Camera2D();

            camera.target   = player.position;
            camera.offset   = new Vector2(screenWidth / 2, screenHeight / 2);
            camera.rotation = 0.0f;
            camera.zoom     = 1.0f;

            // Store pointers to the multiple update camera functions

            CameraUpdaterCallback[] cameraUpdaters = new CameraUpdaterCallback[] {
                UpdateCameraCenter,
                UpdateCameraCenterInsideMap,
                UpdateCameraCenterSmoothFollow,
                UpdateCameraEvenOutOnLanding,
                UpdateCameraPlayerBoundsPush
            };

            int cameraOption         = 0;
            int cameraUpdatersLength = cameraUpdaters.Length;

            string[] cameraDescriptions = new string[] {
                "Follow player center",
                "Follow player center, but clamp to map edges",
                "Follow player center; smoothed",
                "Follow player center horizontally; updateplayer center vertically after landing",
                "Player push camera on getting too close to screen edge"
            };

            SetTargetFPS(60);
            //--------------------------------------------------------------------------------------

            // Main game loop
            while (!WindowShouldClose())
            {
                // Update
                //----------------------------------------------------------------------------------
                float deltaTime = GetFrameTime();

                UpdatePlayer(ref player, envItems, envItemsLength, deltaTime);

                camera.zoom += ((float)GetMouseWheelMove() * 0.05f);

                if (camera.zoom > 3.0f)
                {
                    camera.zoom = 3.0f;
                }
                else if (camera.zoom < 0.25f)
                {
                    camera.zoom = 0.25f;
                }

                if (IsKeyPressed(KEY_R))
                {
                    camera.zoom     = 1.0f;
                    player.position = new Vector2(400, 280);
                }

                if (IsKeyPressed(KEY_C))
                {
                    cameraOption = (cameraOption + 1) % cameraUpdatersLength;
                }

                // Call update camera function by its pointer
                cameraUpdaters[cameraOption](ref camera, ref player, envItems, envItemsLength, deltaTime, screenWidth, screenHeight);
                //----------------------------------------------------------------------------------

                // Draw
                //----------------------------------------------------------------------------------
                BeginDrawing();

                ClearBackground(LIGHTGRAY);

                BeginMode2D(camera);

                for (int i = 0; i < envItemsLength; i++)
                {
                    DrawRectangleRec(envItems[i].rect, envItems[i].color);
                }

                Rectangle playerRect = new Rectangle(player.position.X - 20, player.position.X - 40, 40, 40);
                DrawRectangleRec(playerRect, RED);

                EndMode2D();

                DrawText("Controls:", 20, 20, 10, BLACK);
                DrawText("- Right/Left to move", 40, 40, 10, DARKGRAY);
                DrawText("- Space to jump", 40, 60, 10, DARKGRAY);
                DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, DARKGRAY);
                DrawText("- C to change camera mode", 40, 100, 10, DARKGRAY);
                DrawText("Current camera mode:", 20, 120, 10, BLACK);
                DrawText(cameraDescriptions[cameraOption], 40, 140, 10, DARKGRAY);

                EndDrawing();
                //----------------------------------------------------------------------------------
            }

            // De-Initialization
            //--------------------------------------------------------------------------------------
            CloseWindow();        // Close window and OpenGL context
            //--------------------------------------------------------------------------------------

            return(0);
        }