Jump to content
  • Advertisement
Sign in to follow this  
PureSnowX

Problem getting my 2D Camera object to work as intended

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


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;
}
}
}


WorldScreen class


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();
}
}


and the TileMap.Draw Method:

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);
]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!