public LineOfSight(Vector2 fovCameraPostion, WorldEntity detectableEntity, WorldEntity[] worldEntites, float fovAngleInRadians, Vector2 fovCameraDirection) { _fovCameraPosition = fovCameraPostion; _detectableEntity = detectableEntity; _worldEntities = worldEntites; _fovAngleInRadians = fovAngleInRadians; _fovCameraDirection = fovCameraDirection; _result = new Vector2(); _newPixelPosition = new Vector2(); }
/// <summary> /// Handles the mouse, gamepad and keyboard controls /// </summary> /// public void ControlUpdate() { KeyboardState keyboard = Keyboard.GetState(); GamePadState gamePad = GamePad.GetState(PlayerIndex.One); MouseState mouse = Mouse.GetState(); _elapsed = 0.0f; if (gamePad.Buttons.Back == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Escape)) { this.Exit(); } if (keyboard.IsKeyDown(Keys.Left) || gamePad.DPad.Left == ButtonState.Pressed) { _fovCameraPosition.X -= FOV_CAMERA_SPEED; _fovCameraDirectionFacing.X -= FOV_CAMERA_SPEED; _fovCameraLeftAngleBoundary.X -= FOV_CAMERA_SPEED; _fovCameraRightAngleBoundary.X -= FOV_CAMERA_SPEED; } if (keyboard.IsKeyDown(Keys.Right) || gamePad.DPad.Right == ButtonState.Pressed) { _fovCameraPosition.X += FOV_CAMERA_SPEED; _fovCameraDirectionFacing.X += FOV_CAMERA_SPEED; _fovCameraLeftAngleBoundary.X += FOV_CAMERA_SPEED; _fovCameraRightAngleBoundary.X += FOV_CAMERA_SPEED; } if (keyboard.IsKeyDown(Keys.Up) || gamePad.DPad.Up == ButtonState.Pressed) { _fovCameraPosition.Y -= FOV_CAMERA_SPEED; _fovCameraDirectionFacing.Y -= FOV_CAMERA_SPEED; _fovCameraLeftAngleBoundary.Y -= FOV_CAMERA_SPEED; _fovCameraRightAngleBoundary.Y -= FOV_CAMERA_SPEED; } if (keyboard.IsKeyDown(Keys.Down) || gamePad.DPad.Down == ButtonState.Pressed) { _fovCameraPosition.Y += FOV_CAMERA_SPEED; _fovCameraDirectionFacing.Y += FOV_CAMERA_SPEED; _fovCameraLeftAngleBoundary.Y += FOV_CAMERA_SPEED; _fovCameraRightAngleBoundary.Y += FOV_CAMERA_SPEED; } if (keyboard.IsKeyDown(Keys.Delete)) { _elapsed += -0.01f; } if (keyboard.IsKeyDown(Keys.PageDown)) { _elapsed = 0.01f; } _fovCameraLeftAngleBoundary = RotatePointOnZAxis(_fovCameraLeftAngleBoundary, _elapsed); _fovCameraRightAngleBoundary = RotatePointOnZAxis(_fovCameraRightAngleBoundary, _elapsed); _fovCameraDirectionFacing = RotatePointOnZAxis(_fovCameraDirectionFacing, _elapsed); _RotationAngle += _elapsed; float circle = MathHelper.Pi * 2; _RotationAngle = _RotationAngle % circle; _cursorPosition.X = (float)mouse.X; _cursorPosition.Y = (float)mouse.Y; if (!_leftMouseBtnHeld) { if (mouse.LeftButton == ButtonState.Pressed) { //loop through each object and see if the cursor is on the object //just using a basic square shape so it is not pixel perfect foreach (WorldEntity entity in _worldEntites) { if (_cursorPosition.X > entity.Position.X && _cursorPosition.X < entity.Position.X + entity.Texture.Width) { if (_cursorPosition.Y > entity.Position.Y && _cursorPosition.Y < entity.Position.Y + entity.Texture.Height) { if (keyboard.IsKeyDown(Keys.S)) { //Set what was the detectable entity to false _currentDetectableEntity.IsDetectable = false; //Assign new entity and set detectable to true _currentDetectableEntity = entity; _currentDetectableEntity.IsDetectable = true; //update the line of sight to know which object to detect _lineOfSight.DetectableEntity = _currentDetectableEntity; } _currentlySelectedEntity.Selected = false; _currentlySelectedEntity = entity; //these values are used later to to keep the cursur position relative to the oject when dragging _currentlySelectedEntity.XDiff = _cursorPosition.X - _currentlySelectedEntity.Position.X; _currentlySelectedEntity.YDiff = _cursorPosition.Y - _currentlySelectedEntity.Position.Y; _currentlySelectedEntity.Selected = true; _leftMouseBtnHeld = true; break; } } } } } //------------------- if (!_rightMouseBtnHeld) { if (mouse.RightButton == ButtonState.Pressed) { foreach (WorldEntity entity in _worldEntites) { if (_cursorPosition.X > entity.Position.X && _cursorPosition.X < entity.Position.X + entity.Texture.Width) { if (_cursorPosition.Y > entity.Position.Y && _cursorPosition.Y < entity.Position.Y + entity.Texture.Height) { _currentlySelectedEntity.Selected = false; _currentlySelectedEntity = entity; _currentlySelectedEntity.Selected = true; _rightMouseBtnHeld = true; break; } } } } } // y - entity.position.Y; if (_leftMouseBtnHeld) { //following required for allowing the user to hold down left then press 'S' if (keyboard.IsKeyDown(Keys.S)) { _currentDetectableEntity.IsDetectable = false; _currentDetectableEntity = _currentlySelectedEntity; _currentDetectableEntity.IsDetectable = true; _lineOfSight.DetectableEntity = _currentDetectableEntity; } _currentlySelectedEntityPosition.X = _cursorPosition.X - _currentlySelectedEntity.XDiff; _currentlySelectedEntityPosition.Y = _cursorPosition.Y - _currentlySelectedEntity.YDiff; _currentlySelectedEntity.Position = _currentlySelectedEntityPosition; } if (_rightMouseBtnHeld) { _currentlySelectedEntity.Rotate = 0.01f; } if (mouse.LeftButton == ButtonState.Released) { _leftMouseBtnHeld = false; } if (mouse.RightButton == ButtonState.Released) { _rightMouseBtnHeld = false; } if (mouse.RightButton == ButtonState.Released && mouse.LeftButton == ButtonState.Released) { _currentlySelectedEntity.Selected = false; } }
/// <summary> /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// /// We load the textures here, therefore we also load the verticies of the /// world entities, and create the entities before adding them to a container /// </summary> /// <param name="loadAllContent">Which type of content to load.</param> /// protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // Load textures greenSquareTexture = _content.Load <Texture2D>("Content/GreenSquare"); purpleSquareTexture = _content.Load <Texture2D>("Content/PurpleSquare"); triangleTexture = _content.Load <Texture2D>("Content/Triangle"); concaveCircleTexture = _content.Load <Texture2D>("Content/CircularConcave"); lineTexture = _content.Load <Texture2D>("Content/Line"); cursorCrosshairTexture = _content.Load <Texture2D>("Content/CursorCrosshair"); _cursorOrigin = new Vector2(cursorCrosshairTexture.Width * 0.5f, cursorCrosshairTexture.Height * 0.5f); //Sets up the vertices of the entity based on the dimensions of each texture //vertex code generated by Shape Vertices tool Vector2[] squareVertices = new Vector2[4]; squareVertices[0] = new Vector2(0.0f, 50.0f); squareVertices[1] = new Vector2(50.0f, 50.0f); squareVertices[2] = new Vector2(50.0f, 0.0f); squareVertices[3] = new Vector2(0.0f, 00.0f); _greenSquareEntity = new WorldEntity(greenSquareTexture, squareVertices); _greenSquareEntity.Position = new Vector2(500.0f, 300.0f); _greenSquareEntity.Centroid = new Vector2(25.0f, 25.0f); //By default the green square will be the entity to detect _greenSquareEntity.IsDetectable = true; Vector2[] triangleVertices = new Vector2[3]; triangleVertices[0] = new Vector2(0.0f, 49.0f); triangleVertices[1] = new Vector2(23.0f, 3.0f); triangleVertices[2] = new Vector2(49.0f, 49.0f); triangleEntity = new WorldEntity(triangleTexture, triangleVertices); triangleEntity.Centroid = new Vector2(23.0f, 34.0f); triangleEntity.Position = new Vector2(300.0f, 100.0f); Vector2[] concaveCircleVertices = new Vector2[27]; concaveCircleVertices[0] = new Vector2(3.0f, 38.0f); concaveCircleVertices[1] = new Vector2(1.0f, 32.0f); concaveCircleVertices[2] = new Vector2(0.0f, 26.0f); concaveCircleVertices[3] = new Vector2(0.0f, 21.0f); concaveCircleVertices[4] = new Vector2(1.0f, 14.0f); concaveCircleVertices[5] = new Vector2(4.0f, 8.0f); concaveCircleVertices[6] = new Vector2(8.0f, 5.0f); concaveCircleVertices[7] = new Vector2(12.0f, 2.0f); concaveCircleVertices[8] = new Vector2(16.0f, 0.0f); concaveCircleVertices[9] = new Vector2(20.0f, 0.0f); concaveCircleVertices[10] = new Vector2(28.0f, 0.0f); concaveCircleVertices[11] = new Vector2(34.0f, 1.0f); concaveCircleVertices[12] = new Vector2(40.0f, 4.0f); concaveCircleVertices[13] = new Vector2(44.0f, 8.0f); concaveCircleVertices[14] = new Vector2(47.0f, 14.0f); concaveCircleVertices[15] = new Vector2(49.0f, 20.0f); concaveCircleVertices[16] = new Vector2(49.0f, 27.0f); concaveCircleVertices[17] = new Vector2(48.0f, 32.0f); concaveCircleVertices[18] = new Vector2(45.0f, 37.0f); concaveCircleVertices[19] = new Vector2(44.0f, 32.0f); concaveCircleVertices[20] = new Vector2(42.0f, 28.0f); concaveCircleVertices[21] = new Vector2(36.0f, 22.0f); concaveCircleVertices[22] = new Vector2(29.0f, 19.0f); concaveCircleVertices[23] = new Vector2(20.0f, 19.0f); concaveCircleVertices[24] = new Vector2(13.0f, 22.0f); concaveCircleVertices[25] = new Vector2(7.0f, 28.0f); concaveCircleVertices[26] = new Vector2(5.0f, 34.0f); _concaveCircleEntity = new WorldEntity(concaveCircleTexture, concaveCircleVertices); _concaveCircleEntity.Centroid = new Vector2(25.0f, 25.0f); _concaveCircleEntity.Position = new Vector2(100.0f, 100.0f); _purpleSquareEntity = new WorldEntity(purpleSquareTexture, squareVertices); _purpleSquareEntity.Centroid = new Vector2(25.0f, 25.0f); _purpleSquareEntity.Position = new Vector2(460.0f, 240.0f); //container to hold all world world entities _worldEntites = new WorldEntity[4]; _worldEntites[0] = _purpleSquareEntity; _worldEntites[1] = triangleEntity; _worldEntites[2] = _concaveCircleEntity; _worldEntites[3] = _greenSquareEntity; // _currentlySelectedEntity = _greenSquareEntity; _currentlySelectedEntity.Selected = true; _currentDetectableEntity = _greenSquareEntity; _currentlySelectedEntity.IsDetectable = true; _spriteBatch = new SpriteBatch(_graphics.GraphicsDevice); _lineOfSight = new LineOfSight(_fovCameraPosition, _greenSquareEntity, _worldEntites, MathHelper.ToRadians(FOV_ANGLE_DEGREES / 2), _fovCameraDirectionFacing); } }