Exemple #1
0
        static bool device_OnEvent(Event evnt)
        {
            if (evnt.Type == EventType.Mouse &&
                evnt.Mouse.Type == MouseEventType.Move)
            {
                Vector2Di m = new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y);
                Line3Df   l = device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(m);
                Plane3Df  p = new Plane3Df(new Vector3Df(0, 0, 0), new Vector3Df(100, 0, 0), new Vector3Df(0, 0, 100));
                Vector3Df i = p.GetIntersectionWithLimitedLine(l.Start, l.End);
                if (i != null)
                {
                    camera.Target = game.CenterOfTheBoard + new Vector3Df(
                        (m.Y - device.VideoDriver.ScreenSize.Height / 2) / 100.0f,
                        0,
                        (m.X - device.VideoDriver.ScreenSize.Width / 2) / 100.0f);

                    i.Y           += 25;           // we want light to be a little bit above
                    light.Position = i;
                }
            }

            if (window == null &&
                evnt.Type == EventType.Mouse &&
                (evnt.Mouse.Type == MouseEventType.LeftDown || evnt.Mouse.Type == MouseEventType.RightDown))
            {
                text.Visible = false;                 // if user started to play - remove the gui text
                game.MouseClick(evnt.Mouse.X, evnt.Mouse.Y, evnt.Mouse.Type == MouseEventType.RightDown);

                if (game.StateOfTheGame != Game.State.Playing)
                {
                    text.Visible = true;
                    text.Text    = game.StateOfTheGame == Game.State.Won ? TextWon : TextLost;
                }

                return(true);
            }

            if (evnt.Type == EventType.Key &&
                evnt.Key.PressedDown &&
                evnt.Key.Key == KeyCode.Esc)
            {
                if (window != null)
                {
                    window.Remove();
                    window = null;
                    return(true);
                }

                GUIEnvironment gui = device.GUIEnvironment;

                window = gui.AddWindow(new Recti(100, 100, 400, 400), true, "GAME MENU");

                gui.AddButton(new Recti(20, 40, window.ClientRect.Width - 20, 60), window, 1510, "NEW GAME 5x5");
                gui.AddButton(new Recti(20, 60, window.ClientRect.Width - 20, 80), window, 1520, "NEW GAME 10x10");
                gui.AddButton(new Recti(20, 80, window.ClientRect.Width - 20, 100), window, 1530, "NEW GAME 15x15");
                gui.AddButton(new Recti(20, 100, window.ClientRect.Width - 20, 120), window, 1540, "NEW GAME 20x20");

                gui.AddCheckBox(optionShadows, new Recti(20, 140, window.ClientRect.Width - 20, 160), "SHOW REALTIME SHADOWS", window, 1710);
                gui.AddCheckBox(optionBackground, new Recti(20, 160, window.ClientRect.Width - 20, 180), "SHOW BACKGROUND", window, 1720);
                gui.AddCheckBox(optionFPS, new Recti(20, 180, window.ClientRect.Width - 20, 200), "SHOW FPS", window, 1730);

                gui.AddButton(new Recti(20, 260, window.ClientRect.Width - 20, 280), window, 1590, "EXIT GAME");

                return(true);
            }

            if (window != null &&
                evnt.Type == EventType.GUI)
            {
                if (evnt.GUI.Caller == window &&
                    evnt.GUI.Type == GUIEventType.ElementClosed)
                {
                    window.Remove();
                    window = null;
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1510 &&
                    evnt.GUI.Type == GUIEventType.ButtonClicked)
                {
                    window.Remove();
                    window = null;
                    game.NewGame(5, 5);
                    setupCameraPositionAndTarget();
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1520 &&
                    evnt.GUI.Type == GUIEventType.ButtonClicked)
                {
                    window.Remove();
                    window = null;
                    game.NewGame(10, 10);
                    setupCameraPositionAndTarget();
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1530 &&
                    evnt.GUI.Type == GUIEventType.ButtonClicked)
                {
                    window.Remove();
                    window = null;
                    game.NewGame(15, 15);
                    setupCameraPositionAndTarget();
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1540 &&
                    evnt.GUI.Type == GUIEventType.ButtonClicked)
                {
                    window.Remove();
                    window = null;
                    game.NewGame(20, 20);
                    setupCameraPositionAndTarget();
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1590 &&
                    evnt.GUI.Type == GUIEventType.ButtonClicked)
                {
                    device.Close();
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1710 &&
                    evnt.GUI.Type == GUIEventType.CheckBoxChanged)
                {
                    optionShadows     = (evnt.GUI.Caller as GUICheckBox).Checked;
                    light.CastShadows = optionShadows;
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1720 &&
                    evnt.GUI.Type == GUIEventType.CheckBoxChanged)
                {
                    optionBackground = (evnt.GUI.Caller as GUICheckBox).Checked;
                    device.SceneManager.GetSceneNodeFromID(7777).Visible = optionBackground;
                    return(true);
                }

                if (evnt.GUI.Caller.ID == 1730 &&
                    evnt.GUI.Type == GUIEventType.CheckBoxChanged)
                {
                    optionFPS = (evnt.GUI.Caller as GUICheckBox).Checked;
                    return(true);
                }
            }

            return(false);
        }
Exemple #2
0
		static bool device_OnEvent(Event evnt)
		{
			if (evnt.Type == EventType.Mouse &&
				evnt.Mouse.Type == MouseEventType.Move)
			{
				Vector2Di m = new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y);
				Line3Df l = device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(m);
				Plane3Df p = new Plane3Df(new Vector3Df(0, 0, 0), new Vector3Df(100, 0, 0), new Vector3Df(0, 0, 100));
				Vector3Df i;
				if (p.GetIntersectionWithLimitedLine(l.Start, l.End, out i))
				{
					camera.Target = game.CenterOfTheBoard + new Vector3Df(
						(m.Y - device.VideoDriver.ScreenSize.Height / 2) / 100.0f,
						0,
						(m.X - device.VideoDriver.ScreenSize.Width / 2) / 100.0f);

					i.Y += 25; // we want light to be a little bit above
					light.Position = i;
				}
			}

			if (window == null &&
				evnt.Type == EventType.Mouse &&
				(evnt.Mouse.Type == MouseEventType.LeftDown || evnt.Mouse.Type == MouseEventType.RightDown))
			{
				text.Visible = false; // if user started to play - remove the gui text
				game.MouseClick(evnt.Mouse.X, evnt.Mouse.Y, evnt.Mouse.Type == MouseEventType.RightDown);

				if (game.StateOfTheGame != Game.State.Playing)
				{
					text.Visible = true;
					text.Text = game.StateOfTheGame == Game.State.Won ? TextWon : TextLost;
				}

				return true;
			}

			if (evnt.Type == EventType.Key &&
				evnt.Key.PressedDown &&
				evnt.Key.Key == KeyCode.Esc)
			{
				if (window != null)
				{
					window.Remove();
					window = null;
					return true;
				}

				GUIEnvironment gui = device.GUIEnvironment;

				window = gui.AddWindow(new Recti(100, 100, 400, 400), true, "GAME MENU");

				gui.AddButton(new Recti(20, 40, window.ClientRect.Width - 20, 60), window, 1510, "NEW GAME 5x5");
				gui.AddButton(new Recti(20, 60, window.ClientRect.Width - 20, 80), window, 1520, "NEW GAME 10x10");
				gui.AddButton(new Recti(20, 80, window.ClientRect.Width - 20, 100), window, 1530, "NEW GAME 15x15");
				gui.AddButton(new Recti(20, 100, window.ClientRect.Width - 20, 120), window, 1540, "NEW GAME 20x20");

				gui.AddCheckBox(optionShadows, new Recti(20, 140, window.ClientRect.Width - 20, 160), "SHOW REALTIME SHADOWS", window, 1710);
				gui.AddCheckBox(optionBackground, new Recti(20, 160, window.ClientRect.Width - 20, 180), "SHOW BACKGROUND", window, 1720);
				gui.AddCheckBox(optionFPS, new Recti(20, 180, window.ClientRect.Width - 20, 200), "SHOW FPS", window, 1730);

				gui.AddButton(new Recti(20, 260, window.ClientRect.Width - 20, 280), window, 1590, "EXIT GAME");

				return true;
			}

			if (window != null &&
				evnt.Type == EventType.GUI)
			{
				if (evnt.GUI.Caller == window &&
					evnt.GUI.Type == GUIEventType.ElementClosed)
				{
					window.Remove();
					window = null;
					return true;
				}

				if (evnt.GUI.Caller.ID == 1510 &&
					evnt.GUI.Type == GUIEventType.ButtonClicked)
				{
					window.Remove();
					window = null;
					game.NewGame(5, 5);
					setupCameraPositionAndTarget();
					return true;
				}

				if (evnt.GUI.Caller.ID == 1520 &&
					evnt.GUI.Type == GUIEventType.ButtonClicked)
				{
					window.Remove();
					window = null;
					game.NewGame(10, 10);
					setupCameraPositionAndTarget();
					return true;
				}

				if (evnt.GUI.Caller.ID == 1530 &&
					evnt.GUI.Type == GUIEventType.ButtonClicked)
				{
					window.Remove();
					window = null;
					game.NewGame(15, 15);
					setupCameraPositionAndTarget();
					return true;
				}

				if (evnt.GUI.Caller.ID == 1540 &&
					evnt.GUI.Type == GUIEventType.ButtonClicked)
				{
					window.Remove();
					window = null;
					game.NewGame(20, 20);
					setupCameraPositionAndTarget();
					return true;
				}

				if (evnt.GUI.Caller.ID == 1590 &&
					evnt.GUI.Type == GUIEventType.ButtonClicked)
				{
					device.Close();
					return true;
				}

				if (evnt.GUI.Caller.ID == 1710 &&
					evnt.GUI.Type == GUIEventType.CheckBoxChanged)
				{
					optionShadows = (evnt.GUI.Caller as GUICheckBox).Checked;
					light.CastShadows = optionShadows;
					return true;
				}

				if (evnt.GUI.Caller.ID == 1720 &&
					evnt.GUI.Type == GUIEventType.CheckBoxChanged)
				{
					optionBackground = (evnt.GUI.Caller as GUICheckBox).Checked;
					device.SceneManager.GetSceneNodeFromID(7777).Visible = optionBackground;
					return true;
				}

				if (evnt.GUI.Caller.ID == 1730 &&
					evnt.GUI.Type == GUIEventType.CheckBoxChanged)
				{
					optionFPS = (evnt.GUI.Caller as GUICheckBox).Checked;
					return true;
				}
			}

			return false;
		}
Exemple #3
0
        static void Main(string[] args)
        {
            // Initialize device.

            DriverType driverType;

            if (!AskUserForDriver(out driverType))
            {
                return;
            }

            IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(640, 480));

            if (device == null)
            {
                return;
            }

            // Add event handling.

            device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent);

            // Save important pointers.

            VideoDriver  driver = device.VideoDriver;
            SceneManager smgr   = device.SceneManager;
            Logger       logger = device.Logger;

            // Initialize joysticks and print info about them.

            List <JoystickInfo> joystickList = device.ActivateJoysticks();

            if (joystickList != null)
            {
                logger.Log("Joystick support is enabled and " + joystickList.Count.ToString() + " joystick(s) are present.");

                foreach (JoystickInfo j in joystickList)
                {
                    logger.Log("Joystick " + j.Joystick.ToString() + ":");
                    logger.Log("\tName: \"" + j.Name + "\"");
                    logger.Log("\tAxisCount: " + j.AxisCount.ToString());
                    logger.Log("\tButtonCount: " + j.ButtonCount.ToString());
                    logger.Log("\tPovHat: " + j.PovHat.ToString());
                }
            }
            else
            {
                logger.Log("Joystick support is not enabled.");
            }

            device.SetWindowCaption("Mouse and joystick - Irrlicht Lime - " + joystickList.Count.ToString() + " joystick(s)");

            // Create an arrow mesh and move it around either with the joystick axis/hat,
            // or make it follow the mouse pointer (when no joystick movement).

            SceneNode node = smgr.AddMeshSceneNode(
                smgr.AddArrowMesh(
                    "Arrow",
                    new Color(255, 0, 0),
                    new Color(0, 255, 0),
                    16, 16,
                    2.0f, 1.3f,
                    0.1f, 0.6f
                    )
                );

            node.SetMaterialFlag(MaterialFlag.Lighting, false);

            CameraSceneNode camera = smgr.AddCameraSceneNode();

            camera.Position = new Vector3Df(0, 0, -10);

            // As in example #4, we'll use framerate independent movement.
            uint        then          = device.Timer.Time;
            const float MovementSpeed = 5.0f;

            // Run main cycle.

            while (device.Run())
            {
                // Work out a frame delta time.
                uint  now            = device.Timer.Time;
                float frameDeltaTime = (float)(now - then) / 1000.0f;                 // in seconds
                then = now;

                bool      movedWithJoystick = false;
                Vector3Df nodePosition      = node.Position;

                if (joystickList.Count > 0)
                {
                    float moveHorizontal = 0.0f;                   // range is -1.0 for full left to +1.0 for full right
                    float moveVertical   = 0.0f;                   // range is -1.0 for full down to +1.0 for full up

                    // We receive the full analog range of the axes, and so have to implement our own dead zone.
                    // This is an empirical value, since some joysticks have more jitter or creep around the center
                    // point than others. We'll use 5% of the range as the dead zone, but generally you would want
                    // to give the user the option to change this.
                    float DeadZone = 0.05f;

                    moveHorizontal = joystickState.Axis[0] / 32767.0f;                     // "0" for X axis
                    if (Math.Abs(moveHorizontal) < DeadZone)
                    {
                        moveHorizontal = 0.0f;
                    }

                    moveVertical = joystickState.Axis[1] / -32767.0f;                     // "1" for Y axis
                    if (Math.Abs(moveVertical) < DeadZone)
                    {
                        moveVertical = 0.0f;
                    }

                    // POV will contain 65535 if POV hat info no0t supported, so we can check its range.
                    ushort povDegrees = (ushort)(joystickState.POV / 100);
                    if (povDegrees < 360)
                    {
                        if (povDegrees > 0 && povDegrees < 180)
                        {
                            moveHorizontal = +1.0f;
                        }
                        else if (povDegrees > 180)
                        {
                            moveHorizontal = -1.0f;
                        }

                        if (povDegrees > 90 && povDegrees < 270)
                        {
                            moveVertical = -1.0f;
                        }
                        else if (povDegrees > 270 || povDegrees < 90)
                        {
                            moveVertical = +1.0f;
                        }
                    }

                    // If we have any movement, apply it.
                    if (Math.Abs(moveHorizontal) > 0.0001f || Math.Abs(moveVertical) > 0.0001f)
                    {
                        float m = frameDeltaTime * MovementSpeed;
                        nodePosition      = new Vector3Df(moveHorizontal * m, moveVertical * m, nodePosition.Z);
                        movedWithJoystick = true;
                    }
                }

                // If the arrow node isn't being moved with the joystick, then have it follow the mouse cursor.
                if (!movedWithJoystick)
                {
                    // Create a ray through the mouse cursor.
                    Line3Df ray = smgr.SceneCollisionManager.GetRayFromScreenCoordinates(mouseState.Position, camera);

                    // And intersect the ray with a plane around the node facing towards the camera.
                    Plane3Df  plane = new Plane3Df(nodePosition, new Vector3Df(0, 0, -1));
                    Vector3Df mousePosition;
                    if (plane.GetIntersectionWithLine(ray.Start, ray.Vector, out mousePosition))
                    {
                        // We now have a mouse position in 3d space; move towards it.
                        Vector3Df toMousePosition   = mousePosition - nodePosition;
                        float     availableMovement = frameDeltaTime * MovementSpeed;

                        if (toMousePosition.Length <= availableMovement)
                        {
                            nodePosition = mousePosition;                             // jump to the final position
                        }
                        else
                        {
                            nodePosition += toMousePosition.Normalize() * availableMovement;                             // move towards it
                        }
                    }
                }

                node.Position = nodePosition;

                // Turn lighting on and off depending on whether the left mouse button is down.
                node.SetMaterialFlag(MaterialFlag.Lighting, mouseState.IsLeftButtonDown);

                // Draw all.
                driver.BeginScene(true, true, new Color(113, 113, 133));
                smgr.DrawAll();
                driver.EndScene();
            }

            // Drop the device.

            device.Drop();
        }
Exemple #4
0
		static void Main(string[] args)
		{
			// Initialize device.
			
			DriverType driverType;
			if (!AskUserForDriver(out driverType))
				return;

			IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(640, 480));
			if (device == null)
				return;

			// Add event handling.

			device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent);

			// Save important pointers.

			VideoDriver driver = device.VideoDriver;
			SceneManager smgr = device.SceneManager;
			Logger logger = device.Logger;

			// Initialize joysticks and print info about them.

			List<JoystickInfo> joystickList = device.ActivateJoysticks();
			if (joystickList != null)
			{
				logger.Log("Joystick support is enabled and " + joystickList.Count.ToString() + " joystick(s) are present.");

				foreach (JoystickInfo j in joystickList)
				{
					logger.Log("Joystick " + j.Joystick.ToString() + ":");
					logger.Log("\tName: \"" + j.Name + "\"");
					logger.Log("\tAxisCount: " + j.AxisCount.ToString());
					logger.Log("\tButtonCount: " + j.ButtonCount.ToString());
					logger.Log("\tPovHat: " + j.PovHat.ToString());
				}
			}
			else
			{
				logger.Log("Joystick support is not enabled.");
			}

			device.SetWindowCaption("Mouse and joystick - Irrlicht Lime - " + joystickList.Count.ToString() + " joystick(s)");

			// Create an arrow mesh and move it around either with the joystick axis/hat,
			// or make it follow the mouse pointer (when no joystick movement).

			SceneNode node = smgr.AddMeshSceneNode(
				smgr.AddArrowMesh(
					"Arrow",
					new Color(255, 0, 0),
					new Color(0, 255, 0),
					16, 16,
					2.0f, 1.3f,
					0.1f, 0.6f
				)
			);

			node.SetMaterialFlag(MaterialFlag.Lighting, false);

			CameraSceneNode camera = smgr.AddCameraSceneNode();
			camera.Position = new Vector3Df(0, 0, -10);

			// As in example #4, we'll use framerate independent movement.
			uint then = device.Timer.Time;
			const float MovementSpeed = 5.0f;

			// Run main cycle.

			while (device.Run())
			{
				// Work out a frame delta time.
				uint now = device.Timer.Time;
				float frameDeltaTime = (float)(now - then) / 1000.0f; // in seconds
				then = now;

				bool movedWithJoystick = false;
				Vector3Df nodePosition = node.Position;

				if (joystickList.Count > 0)
				{
					float moveHorizontal = 0.0f; // range is -1.0 for full left to +1.0 for full right
					float moveVertical = 0.0f; // range is -1.0 for full down to +1.0 for full up

					// We receive the full analog range of the axes, and so have to implement our own dead zone.
					// This is an empirical value, since some joysticks have more jitter or creep around the center
					// point than others. We'll use 5% of the range as the dead zone, but generally you would want
					// to give the user the option to change this.
					float DeadZone = 0.05f;

					moveHorizontal = joystickState.Axis[0] / 32767.0f; // "0" for X axis
					if (Math.Abs(moveHorizontal) < DeadZone)
						moveHorizontal = 0.0f;

					moveVertical = joystickState.Axis[1] / -32767.0f; // "1" for Y axis
					if (Math.Abs(moveVertical) < DeadZone)
						moveVertical = 0.0f;

					// POV will contain 65535 if POV hat info no0t supported, so we can check its range.
					ushort povDegrees = (ushort)(joystickState.POV / 100);
					if (povDegrees < 360)
					{
						if (povDegrees > 0 && povDegrees < 180)
							moveHorizontal = +1.0f;
						else if (povDegrees > 180)
							moveHorizontal = -1.0f;

						if (povDegrees > 90 && povDegrees < 270)
							moveVertical = -1.0f;
						else if (povDegrees > 270 || povDegrees < 90)
							moveVertical = +1.0f;
					}

					// If we have any movement, apply it.
					if (Math.Abs(moveHorizontal) > 0.0001f || Math.Abs(moveVertical) > 0.0001f)
					{
						float m = frameDeltaTime * MovementSpeed;
						nodePosition = new Vector3Df(moveHorizontal * m, moveVertical * m, nodePosition.Z);
						movedWithJoystick = true;
					}
				}

				// If the arrow node isn't being moved with the joystick, then have it follow the mouse cursor.
				if (!movedWithJoystick)
				{
					// Create a ray through the mouse cursor.
					Line3Df ray = smgr.SceneCollisionManager.GetRayFromScreenCoordinates(mouseState.Position, camera);

					// And intersect the ray with a plane around the node facing towards the camera.
					Plane3Df plane = new Plane3Df(nodePosition, new Vector3Df(0, 0, -1));
					Vector3Df mousePosition;
					if (plane.GetIntersectionWithLine(ray.Start, ray.Vector, out mousePosition))
					{
						// We now have a mouse position in 3d space; move towards it.
						Vector3Df toMousePosition = mousePosition - nodePosition;
						float availableMovement = frameDeltaTime * MovementSpeed;

						if (toMousePosition.Length <= availableMovement)
							nodePosition = mousePosition; // jump to the final position
						else
							nodePosition += toMousePosition.Normalize() * availableMovement; // move towards it
					}
				}

				node.Position = nodePosition;

				// Turn lighting on and off depending on whether the left mouse button is down.
				node.SetMaterialFlag(MaterialFlag.Lighting, mouseState.IsLeftButtonDown);

				// Draw all.
				driver.BeginScene(true, true, new Color(113, 113, 133));
				smgr.DrawAll();
				driver.EndScene();
			}

			// Drop the device.

			device.Drop();
		}