Exemplo n.º 1
0
        static void Start()
        {
            const int Width  = 1366;
            const int Height = 768;

            Raylib.InitWindow(Width, Height, "Harvesturr");
            Raylib.SetTargetFPS(60);

            GUILoadStyle("jungle");

            GUIPanelColor = Raylib.Fade(Color.BLACK, 0.8f);
            GameCamera    = new Camera2D(new Vector2(Width, Height) / 2, Vector2.Zero, 0, 2);

            GameMap.Load("test");

            if (DebugPerformance)
            {
                const float Dist = 50;

                Rectangle Rect   = GameMap.GetBounds();
                Vector2   Pos    = new Vector2(Rect.x, Rect.y) + new Vector2(10);
                int       XCount = (int)(Rect.width / Dist);
                int       YCount = (int)(Rect.height / Dist);

                for (int X = 0; X < XCount; X++)
                {
                    for (int Y = 0; Y < YCount; Y++)
                    {
                        Spawn(new UnitConduit(Pos + new Vector2(X, Y) * Dist));
                    }
                }
            }
            else
            {
                for (int i = 0; i < 100; i++)
                {
                    Spawn(new UnitMineral(GameMap.RandomMineralPoint(), Utils.Random(0, 100) > 80));
                }
            }

            Spawn(new UnitAlienUfo(Vector2.Zero));

            GameTools.AddRange(IsGameToolAttribute.CreateAllGameTools());
            Resources = 50;

            if (DebugView)
            {
                Resources = int.MaxValue;
            }

            // Test

            /*UnitConduit ConduitA = new UnitConduit(new Vector2(314.844f, 167.216f));
             * UnitConduit ConduitB = new UnitConduit(new Vector2(380.955f, 166.661f));
             * UnitConduit ConduitC = new UnitConduit(new Vector2(348.513f, 215.875f));
             *
             * Spawn(ConduitA);
             * Spawn(ConduitB);
             * Spawn(ConduitC);
             * Spawn(new UnitEnergyPacket(ConduitA, ConduitB));*/

            GameTimer.Restart();

            while (!Raylib.WindowShouldClose())
            {
                float FrameTime = Raylib.GetFrameTime();
                Time = (float)GameTimer.Elapsed.TotalSeconds;

                ScreenWidth  = Raylib.GetScreenWidth();
                ScreenHeight = Raylib.GetScreenHeight();

                Lockstep(Time, 1.0f / 60, 1.0f / 10);

                /*if (FrameTime < 0.5f)
                 *      Update(FrameTime);
                 * else
                 *      Console.WriteLine("Skipping update, frame time {0} s", FrameTime);*/

                Raylib.BeginDrawing();
                Raylib.ClearBackground(Color.SKYBLUE);

                Raylib.BeginMode2D(GameCamera);
                CurrentDrawState = DrawState.WORLD;
                DrawWorld();
                Raylib.EndMode2D();

                CurrentDrawState = DrawState.SCREEN;
                DrawScreen();
                Raylib.EndDrawing();

                CurrentDrawState = DrawState.NONE;
            }

            Raylib.CloseWindow();
        }
Exemplo n.º 2
0
        static void Update(float Dt)
        {
            MousePosScreen  = Raylib.GetMousePosition();
            MousePosWorld   = Raylib.GetScreenToWorld2D(MousePosScreen, GameCamera);
            Zoom            = GameCamera.zoom;
            DrawZoomDetails = Zoom >= 2;

            float Amt = 100 * Dt;

            if (Raylib.IsKeyDown(KeyboardKey.KEY_W))
            {
                GameCamera.target += new Vector2(0, -Amt);
            }

            if (Raylib.IsKeyDown(KeyboardKey.KEY_A))
            {
                GameCamera.target += new Vector2(-Amt, 0);
            }

            if (Raylib.IsKeyDown(KeyboardKey.KEY_S))
            {
                GameCamera.target += new Vector2(0, Amt);
            }

            if (Raylib.IsKeyDown(KeyboardKey.KEY_D))
            {
                GameCamera.target += new Vector2(Amt, 0);
            }

            if (!IsMouseDragging)
            {
                int Wheel = Raylib.GetMouseWheelMove();
                if (Wheel != 0)
                {
                    GameCamera.zoom += Wheel / 10.0f;

                    if (GameCamera.zoom < 0.5f)
                    {
                        GameCamera.zoom = 0.5f;
                    }

                    if (GameCamera.zoom > 3)
                    {
                        GameCamera.zoom = 3;
                    }

                    Console.WriteLine("Zoom: {0}", GameCamera.zoom);
                }
            }

            if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_MIDDLE_BUTTON))
            {
                GameCamera.zoom = 2;
            }

            if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_RIGHT_BUTTON))
            {
                MouseDragStartMouse    = MousePosScreen;
                MouseDragStartLocation = GameCamera.target;
                IsMouseDragging        = true;
            }
            else if (Raylib.IsMouseButtonReleased(MouseButton.MOUSE_RIGHT_BUTTON))
            {
                IsMouseDragging = false;
            }

            if (IsMouseDragging)
            {
                Vector2 Delta = (MouseDragStartMouse - MousePosScreen) * (1.0f / GameCamera.zoom);
                GameCamera.target = MouseDragStartLocation + Delta;
            }

            GameMap.Update(Dt);

            for (int i = 0; i < GameUnits.Length; i++)
            {
                if (GameUnits[i] != null)
                {
                    if (GameUnits[i].Destroyed)
                    {
                        GameUnits[i] = null;
                        continue;
                    }

                    GameUnits[i].Update(Dt);
                }
            }

            ActiveGameTool?.Update(Dt);

            if (Utils.IsInside(new Rectangle(0, 0, ScreenWidth, ScreenHeight - GUIRectHeight), MousePosScreen) && GameMap.IsInBounds(MousePosWorld))
            {
                if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
                {
                    ActiveGameTool?.OnWorldMousePress(MousePosWorld, true);
                }

                if (Raylib.IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON))
                {
                    ActiveGameTool?.OnWorldMousePress(MousePosWorld, false);
                }
            }
        }