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.isAnimated) mSpriteBatch.Draw(obj.Texture, objPosInScreenSpace, new Rectangle(obj.animatedObject.mCurrentFrame * obj.animatedObject.Width, 0, obj.animatedObject.Width, obj.animatedObject.mTexture.Height), obj.color, Rotation, obj.TextureOrigin, Zoom, 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); if (textTime < 3500 && obj.isTalking) { mSpriteBatch.DrawString(sFont, obj.DialogueText.Replace("@", System.Environment.NewLine), new Vector2(objPosInScreenSpace.X + 20, objPosInScreenSpace.Y - 70), Color.White, 0, Vector2.Zero, 0.2f, SpriteEffects.None, 0); } else { obj.isTalking = false; } }
private void LoadWorld(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)); if (obj.CanFall) obj.color = Color.Black; if (obj.DoesWalk) { obj.setWalkTexture(content.Load<Texture2D>(obj.WalkingTextureName)); obj.walkAnimList = new List<Texture2D>(); obj.walkAnimList.Add(obj.Texture); obj.walkAnimList.Add(obj.WalkingTexture); } worldObjectList.Add(obj); } } }
/// <summary> /// Checks if sprite is it collides with other objects /// </summary> /// <param name="playerObj"></param> /// <param name="position"></param> /// <returns></returns> public bool checkFalling(WorldObject playerObj, ref Vector2 position) { bool falling = true; foreach (WorldObject obj in mWorld.worldObjectList) { if (playerObj.Collides(obj) && !obj.IsTransparent) { if (obj.DoesHurt) { position = playerData[0].startingPoint; } else if (obj.CanFall) { obj.isFalling = true; obj.color = Color.Blue; falling = false; } else { if (playerObj.PositionInWorldSpace.Y <= obj.PositionInWorldSpace.Y - obj.TextureOrigin.Y) { if (obj.IsMoving) { //position = obj.PositionInWorldSpace - obj.WorldSpaceToObjectSpace(position); position.X += 1f * obj.direction; } falling = false; } } } else { obj.color = Color.White; } } return falling; }
public Vector2 playerFall(GameTime gameTime, WorldObject playerObj) { Vector2 place = playerInWorld.PositionInWorldSpace; place.Y += jumpSpeed; float distToTravel = jumpSpeed; foreach (WorldObject obj in mWorld.worldObjectList) { if (playerObj.PositionInWorldSpace.Y <= obj.PositionInWorldSpace.Y - obj.TextureOrigin.Y) { if (playerObj.Collides(obj) && !obj.IsTransparent) { isFalling = false; //landSound.Play(); playerInWorld.Texture = playerTexture; place.Y = obj.PositionInWorldSpace.Y - obj.TextureOrigin.Y; if (!playerObj.Collides(obj)) place.Y = 100; return playerObj.PositionInWorldSpace; } } } return place; }
public void objInteract(WorldObject playerObject) { foreach (WorldObject obj in mWorld.worldObjectList) { if (playerObject.Collides(obj)) { if (obj.TextureName == "treasure") gameOver(1); if (obj.TextureName == "bottomPort") { playerInWorld.PositionInWorldSpace = obj.PosToPort; } if (obj.DoesTalk) { obj.isTalking = true; camera.textTime = 0; } } } }
public void LoadContent(ContentManager content) { playerData = content.Load<PlayerData[]>("PlayerSprite"); playerTexture = content.Load<Texture2D>(playerData[0].spriteName); playerTexture.Name = "regular"; playerTextureWalk = content.Load<Texture2D>(playerData[0].spriteWalking); playerJumpText = content.Load<Texture2D>(playerData[0].spriteJumping); playerTextureWalk.Name = playerData[0].spriteWalking; playerFlipped = content.Load<Texture2D>(playerData[0].spriteFlipped); jumpSound = content.Load<SoundEffect>(playerData[0].jumpSound); landSound = content.Load<SoundEffect>(playerData[0].landSound); mFont = content.Load<SpriteFont>("SpriteFont1"); topSpeed = playerData[0].topSpeed; playerSpeed = playerData[0].speed; jumpSpeed = playerData[0].jumpSpeed; jumpDistance = playerData[0].jumpDistance; currentKeys = Keyboard.GetState(); walkAnimList = new List<Texture2D>(); walkAnimList.Add(playerTexture); walkAnimList.Add(playerTextureWalk); playerInWorld = new WorldObject(playerTexture, playerData[0].startingPoint); playerInWorld.TextureOrigin = playerData[0].center; }
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 case 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; }