I define a tile with a rectangle. This is displayed below.
public class Tile : Sprite
{
Rectangle rectangle;
public Rectangle GetRectangle
{
get
{
return rectangle;
}
}
public Tile(Texture2D texture, Vector2 position, int tileWidth, int tileHeight) :
base(texture, position)
{
rectangle = new Rectangle((int)position.X, (int)position.Y, tileWidth, tileHeight);
}
}
These tiles are created by my level editor and placed on a map.
I have a player class here but I can't get it to behave with my tiles. Either I can get collisions working on the y, but then the character can't move on the x. Or I can get it moving on the x but if the player walks into tiles from the side the character won't respond.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
namespace NecromancerGame
{
public class Player : Sprite
{
private float speed;
private float jumpHeight;
Rectangle rectangle;
//private Vector2 velocity;
private float timer;
private float timeToChangeFrame;
private int frameNumber;
private int fps;
private int frameWidth;
private int frameHeight;
private bool facingRight;
private float gravity;
SpriteEffects effect;
public Vector2 GetPosition
{
get
{
return position;
}
}
public Player(Texture2D texture, Vector2 pos, int fps, float moveSpeed):
base(texture, pos)
{
int timer = 0;
int frameNumber = 0;
timeToChangeFrame = 0.20f;
this.fps = fps;
frameWidth = texture.Width / (fps + 1);
frameHeight = texture.Height;
facingRight = true;
speed = moveSpeed;
rectangle = new Rectangle((int)position.X, (int)position.Y, frameWidth, frameHeight);
gravity = Stats.gravityStrength;
origin = new Vector2(frameWidth / 2, frameHeight / 2);
}
public void StartJump()
{
velocity.Y = -Stats.jumpHeight;
}
public void ChangeDirection()
{
facingRight = !facingRight;
}
public void Update(GameTime gameTime, List<Tile> tileList)
{
Vector2 oldPosition = position;
bool colliding = false;
timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (timer > timeToChangeFrame)
{
frameNumber++;
if (frameNumber >= fps)
{
frameNumber = 0;
}
timer = 0;
}
if (facingRight)
{
velocity.X = speed;
effect = SpriteEffects.None;
}
else
{
velocity.X = -speed;
effect = SpriteEffects.FlipHorizontally;
}
velocity.Y += gravity;
Vector2 potentialPosition = oldPosition + velocity;
Rectangle potentialNewPosition = new Rectangle(rectangle.Center.X + (int)velocity.X, rectangle.Center.X + (int)velocity.Y, frameWidth, frameHeight);
for (int i = 0; i < tileList.Count; i++)
{
if (potentialNewPosition.Intersects(tileList[i].GetRectangle))
{
//Point point = potentialNewPosition.Center - tileList[i].GetRectangle.Center;
position = oldPosition;
//position = oldPosition;
//velocity.X = 0;
//position.X = oldPosition.X;
colliding = true;
}
}
position += velocity;
rectangle.X = (int)position.X;
rectangle.Y = (int)position.Y;
base.Update(gameTime);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch, frameNumber, frameWidth, frameHeight, effect);
}
}
}