Sign in to follow this  
PureSnowX

Problem getting my 2D Camera object to work as intended

Recommended Posts

I'v been trying to get my camera to work as intended for my Rougelike 2D game created in XNA and C#.
But I can't get it right, the map is kinda off and I can't get the camera to center the object It's supposed to follow.


I'll post the code:

Camera class
[code]

class Camera
{
TileMap tileMap;
private bool followActorObject;
private Point location;
private Actor objectToFollow;
Viewport viewport;
public Camera(TileMap tileMap, Viewport viewport)
{
this.tileMap = tileMap;
this.viewport = viewport;
}

public void setObjectToFollow(Actor objectToFollow)
{
this.objectToFollow = objectToFollow;
followActorObject = true;
}
public void setPosition(int x, int y)
{
location.X = x;
location.Y = y;
}

public Point Position { get { return location; } }

public int getOffsetX()
{
return location.X;
}

public int getOffsetY()
{
return location.Y;
}

public void Update()
{
if (followActorObject)
{
location = objectToFollow.Position;
}
}
}[/code]

WorldScreen class
[code]

class WorldScreen : MainGameScreen
{
TileMap currentMap;
Dictionary<string, TileMap> mapList;
Camera cameraObject;
Player player;

public WorldScreen(TileMap tilemap)
{
this.currentMap = tilemap;
player = new Player(0, (int)tilemap.getPlayerSpawnPoint().X, (int)tilemap.getPlayerSpawnPoint().Y);
cameraObject = new Camera(currentMap, this.Viewport);
cameraObject.setObjectToFollow(player);
}

public override void Draw(SpriteBatch spritebatch, GameTime gameTime)
{
currentMap.DrawTileMap(spritebatch, cameraObject.Position.X, cameraObject.Position.Y);
player.Draw(spritebatch, gameTime, cameraObject.getOffsetX(), cameraObject.getOffsetY());
spritebatch.Begin();
spritebatch.DrawString(font, "Coordinates of the Camera Object X: " + player.Position.X.ToString() + " and Y: " + player.Position.Y.ToString(), new Vector2((3 * Tile.WIDTH)-cameraObject.getOffsetX(), (0 * Tile.HEIGHT)-cameraObject.getOffsetY()), Color.White);
spritebatch.DrawString(font, "Coordinates of the Player Drawn Object X: " + (player.Position.X*Tile.WIDTH).ToString() + " and Y: " + (player.Position.Y*Tile.HEIGHT).ToString(), new Vector2((3 * Tile.WIDTH) - cameraObject.getOffsetX(), (1 * Tile.HEIGHT) - cameraObject.getOffsetY()), Color.White);
spritebatch.End();
}

public override void Update(GameTime gameTime)
{

/* Poll for input */
Keyboard.getNewKeyboardState();

if (Keyboard.CheckKey(Keys.Up))
{
if(!currentMap.checkCollision((int)player.Position.X,(int)player.Position.Y-1))
player.setPositionY(-1);
}
else if (Keyboard.CheckKey(Keys.Right))
{
if (!currentMap.checkCollision((int)player.Position.X + 1, (int)player.Position.Y))
player.setPositionX(1);
}
else if (Keyboard.CheckKey(Keys.Down))
{
if (!currentMap.checkCollision((int)player.Position.X, (int)player.Position.Y + 1))
player.setPositionY(1);
}
else if (Keyboard.CheckKey(Keys.Left))
{
if (!currentMap.checkCollision((int)player.Position.X - 1, (int)player.Position.Y))
player.setPositionX(-1);
}
else if (Keyboard.CheckKey(Keys.I))
{
/*Open player objects inventory*/
}
else if (Keyboard.CheckKey(Keys.Space))
{
/* Skip a turn */
}

if (cameraObject != null)
cameraObject.Update();
}
}
[/code]

and the TileMap.Draw Method:
[code]
spritebatch.Draw(tileSet, new Rectangle((x *Tile.WIDTH) - (offsetX*Tile.WIDTH), (y * Tile.HEIGHT)-(offsetY*Tile.HEIGHT), Tile.WIDTH, Tile.HEIGHT), new Rectangle(tileX * Tile.WIDTH, tileY * Tile.HEIGHT, Tile.WIDTH, Tile.HEIGHT), Color.White);
][/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this