/// <summary> /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// </summary> public override void Initialize() { cursorTexture = mGame.Content.Load<Texture2D>("reg_cursor"); grabTexture = mGame.Content.Load<Texture2D>("grab_cursor"); wCursor = new WorldObject(cursorTexture); wCursor.setTexture(cursorTexture); base.Initialize(); }
public void createGrid() { float widthLeft = Width; float heightLeft = Height; for (float i = 0; i < widthLeft; i += gridTexture.Width) { for (float j = 0; j < heightLeft; j += gridTexture.Height) { gridObj = new WorldObject(); gridObj.setTexture(gridTexture); gridObj.PositionInWorldSpace = new Vector2(i + gridTexture.Width/2, j + gridTexture.Height/2); gridList.Add(gridObj); } } }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { KeyboardState currentKeys = Keyboard.GetState(); currentMouseState = Mouse.GetState(); cursorPos.X = currentMouseState.X + camera.xOffset; cursorPos.Y = currentMouseState.Y + camera.yOffset; if (currentMouseState.LeftButton == ButtonState.Pressed && currentKeys.IsKeyUp(Keys.LeftShift)) { wCursor.setTexture(grabTexture); //Check if cursor is trying to drag an object already on screen foreach (WorldObject obj in world.worldObjectList) { if (wCursor.Collides(obj) && objDragged == null) { objDragged = obj; } } //Check if cursor is clicking an icon of object in objMenu if (wCursor.Collides(objMenu.currObject) && objDragged == null) { objDragged = new WorldObject(); objDragged.setTexture(objMenu.currObject.Texture); objDragged.TextureName = objMenu.currObject.TextureName; } //Causes selected object to follow cursor if (objDragged != null) { objDragged.PositionInWorldSpace = cursorPos; } } //If leftshift and left mouse are pressed, delete object in world else if (currentMouseState.LeftButton == ButtonState.Pressed && currentKeys.IsKeyDown(Keys.LeftShift)) { foreach (WorldObject obj in world.worldObjectList) { if (wCursor.Collides(obj) && objDragged == null) { world.deleteList.Add(obj); } } } else { //Drop object and place in list of object in world if (objDragged != null && !world.worldObjectList.Contains(objDragged)) world.worldObjectList.Add(objDragged); objDragged = null; wCursor.setTexture(cursorTexture); } wCursor.PositionInWorldSpace = cursorPos; base.Update(gameTime); }
public override void Update(GameTime gameTime) { prevState = currState; currState = Keyboard.GetState(); //Tab to iterate through object list if(currState.IsKeyDown(Keys.Tab) && prevState.IsKeyUp(Keys.Tab) && currState.IsKeyUp(Keys.LeftShift)) { currIcon = (currIcon + 1) % objIconList.Count; } //Shift-tab to iterate backwards through object list if ((currState.IsKeyDown(Keys.Tab) && currState.IsKeyDown(Keys.LeftShift)) && (prevState.IsKeyUp(Keys.Tab))) { currIcon = (currIcon - 1); if (currIcon < 0) currIcon = objIconList.Count - 1; } currObject = objIconList[currIcon]; if (cameraPos != camera.Position) { cameraPos = camera.Position; foreach (WorldObject obj in objIconList) { float newX = (camera.Position.X - camera.world.Width/2) + (obj.Texture.Width/2) * obj.objScale; float newY = (camera.Position.Y - camera.world.Height/2) + 50; Vector2 pos = new Vector2(newX, newY); obj.PositionInWorldSpace = pos; } } iconObj.PositionInWorldSpace = new Vector2(currObject.PositionInWorldSpace.X, currObject.PositionInWorldSpace.Y); base.Update(gameTime); }
private void LoadMenu(ContentManager content, String wordFile) { using (XmlReader reader = XmlReader.Create(new StreamReader(wordFile))) { XDocument xml = XDocument.Load(reader); XElement root = xml.Root; foreach (XElement elem in root.Elements()) { WorldObject obj = new WorldObject(); foreach (XElement innerElem in elem.Elements()) { XMLParse.AddValueToClassInstance(innerElem, obj); } obj.setTexture(content.Load<Texture2D>(obj.TextureName)); obj.objScale = 1f/(obj.Texture.Width) * 100; objIconList.Add(obj); } } //init icon background iconObj = new WorldObject(); iconObj.setTexture(iconBack); iconObj.color = Color.Gray; iconObj.objScale = 1f / (iconObj.Texture.Width) * 100; iconObj.PositionInWorldSpace = iconPos; }
public bool Collides(WorldObject other) { Rectangle myAABB = AABB(); Rectangle otherAABB = other.AABB(); if (myAABB.Intersects(otherAABB)) { // No rotation case if (RotationInWorldSpace == 0 && other.RotationInWorldSpace == 0) { // Find the top, left, bottom, and right of the rectangle defined by the // interesction of the two AABBs int left = Math.Max(myAABB.Left, otherAABB.Left); int top = Math.Max(myAABB.Top, otherAABB.Top); int bottom = Math.Min(myAABB.Bottom, otherAABB.Bottom); int right = Math.Min(myAABB.Right, otherAABB.Right); // Go through every point in this intersection rectangle, and determine if the // corresponding points in the other two textures are both non-transparent for (int i = left; i < right; i++) { for (int j = top; j < bottom; j++) { int index1 = i - myAABB.Left + (j - myAABB.Top) * Texture.Width; int index2 = i - otherAABB.Left + (j - otherAABB.Top) * other.Texture.Width; if (data[index1].A > 0 && other.data[index2].A > 0) { return true; } } } return false; } // Rotation casef else { // First, find the 4 corners of the other object in my texture space Vector2 upperLeft = WorldSpaceToTextureSpace(other.TextureSpaceToWorldSpace(new Vector2(0, 0))); Vector2 upperRight = WorldSpaceToTextureSpace(other.TextureSpaceToWorldSpace(new Vector2(other.Texture.Width, 0))); Vector2 lowerRight = WorldSpaceToTextureSpace(other.TextureSpaceToWorldSpace(new Vector2(other.Texture.Width, other.Texture.Height))); Vector2 lowerLeft = WorldSpaceToTextureSpace(other.TextureSpaceToWorldSpace(new Vector2(0, other.Texture.Height))); // Next, find the AABB that contains these points, in my texture space int top = (int)min4(upperLeft.Y, upperRight.Y, lowerLeft.Y, lowerRight.Y); int bottom = (int)max4(upperLeft.Y, upperRight.Y, lowerLeft.Y, lowerRight.Y); int left = (int)min4(upperLeft.X, upperRight.X, lowerLeft.X, lowerRight.X); int right = (int)max4(upperLeft.X, upperRight.X, lowerLeft.X, lowerRight.X); // Next, find the intersection of this AABB and my texture in my texture space int top2 = Math.Max(top, 0); int bottom2 = Math.Min(bottom, Texture.Height); int left2 = Math.Max(0, left); int right2 = Math.Min(right, Texture.Width); // Go though each point in this intersection AABB (which has been defined in *my* texture space), // and find the corresponding point in the other texture. Check if both points are non-transparent. for (int i = left2; i < right2; i++) { for (int j = top2; j < bottom2; j++) { Vector2 pointInMyTextureSpace = new Vector2(i, j); Vector2 pointInOtherTextureSpace = other.WorldSpaceToTextureSpace(TextureSpaceToWorldSpace(pointInMyTextureSpace)); if (nonTransparentPixel(pointInMyTextureSpace) && other.nonTransparentPixel(pointInOtherTextureSpace)) { return true; } } } return false; } } return false; }
public void Draw(WorldObject obj) { Vector2 objPosInCameraSpace = obj.PositionInWorldSpace - Position; objPosInCameraSpace = new Vector2(objPosInCameraSpace.X * (float)Math.Cos(-Rotation) - objPosInCameraSpace.Y * (float)Math.Sin(-Rotation), objPosInCameraSpace.X * (float)Math.Sin(-Rotation) + objPosInCameraSpace.Y * (float)Math.Cos(-Rotation)); objPosInCameraSpace *= Zoom; Vector2 objPosInScreenSpace = objPosInCameraSpace + new Vector2(Game1.Width / 2, Game1.Height / 2); float rotationInScreenSpace = obj.RotationInWorldSpace - Rotation; if (obj.objScale != 0) { mSpriteBatch.Draw(obj.Texture, objPosInScreenSpace, null, obj.color, rotationInScreenSpace, obj.TextureOrigin, obj.objScale, SpriteEffects.None, 0); } else { if (obj.spEffect == SpriteEffects.None) mSpriteBatch.Draw(obj.Texture, objPosInScreenSpace, null, obj.color, rotationInScreenSpace, obj.TextureOrigin, Zoom, SpriteEffects.None, 0); else mSpriteBatch.Draw(obj.Texture, objPosInScreenSpace, null, obj.color, rotationInScreenSpace, obj.TextureOrigin, Zoom, obj.spEffect, 0); } }