Slick Forums

Discuss the Slick 2D Library
It is currently Wed Apr 16, 2014 10:09 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sat Mar 17, 2012 1:32 pm 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
Hi all,

I was trying to create a basic game in which the camera follows an entity. where the entity remains on the center part of the screen/camera. i created an entity which will move
when the user press certain keys. I also created a component for the camera and here is the code

Code:
public class MapRenderComponent extends RenderComponent{

    TiledMapPlusCustom map = null;
    Entity playerLock = null;
   
    public MapRenderComponent(String id, TiledMapPlusCustom map, Entity playerLock){
        super(id);
        this.map = map;
        this.playerLock = playerLock;
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
       
        map.render(0, 0);
        //translate the graphics
        translate(gc,gr);
    }

    private void translate(GameContainer gc, Graphics gr){
        int transX;
        int transY;

        int halfWidth = gc.getWidth() / 2;
        int halfHeight = gc.getHeight() / 2;
        //position of player is half of the screen less than
        //some computation goes here

        //gr.translate(somevalue,somevalue);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}


most of the elements are translated to a new position but the map was not moving. is it because of the map.render(0,0); ?

also can anyone supply a sample for a camera class that follows an entity?


Top
 Profile  
 
PostPosted: Sat Mar 17, 2012 1:40 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
Here, near the bottom posted by koopa. With that you can use camera.centerOn(someEntity.x, someEntity.y).


Top
 Profile  
 
PostPosted: Sat Mar 17, 2012 1:42 pm 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
eulogy1337 wrote:
Here, near the bottom posted by koopa. With that you can use camera.centerOn(someEntity.x, someEntity.y).


thanks dude..

i was kinda looking in the search page here in the forum.. reading the thread now..


Top
 Profile  
 
PostPosted: Sun Mar 18, 2012 12:09 am 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
the camera works fine now.. but i got 1 question.. when i created a large map with the size of 75 tiles as height and 32x32 px it doesnt render the bottom part. is the tilemap class has a limit of 50 tiles only?


Top
 Profile  
 
PostPosted: Mon Mar 19, 2012 7:25 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
I don't know why this is happening. I have used maps over 100x100 size.

In your camera class (if you copied koopa's class), try commenting out this part:

Code:
      //if the camera is at the right or left edge lock it to prevent a black bar
      if(cameraX < 0) cameraX = 0;
      if(cameraX + gc.getWidth() > mapWidth) cameraX = mapWidth - gc.getWidth();
     
      //if the camera is at the top or bottom edge lock it to prevent a black bar
      if(cameraY < 0) cameraY = 0;
      if(cameraY + gc.getHeight() > mapHeight) cameraY = mapHeight - gc.getHeight();


And see if it still happens.


Top
 Profile  
 
PostPosted: Mon Mar 19, 2012 10:55 pm 
Offline
User avatar

Joined: Mon Feb 27, 2012 11:21 pm
Posts: 24
Location: Spain
I have a tutorial on this, maybe it helps you: http://shockper.com/2012/03/slick2d-mov ... 2/?lang=en

I know probably it's not the best way to do it, but well... it works

And here the code I used on it:

(The entity class it's really specific for the tutorial, use your own)

Code:
package game;
 
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
 
public class Entity {
 
   protected Vector2f pos; // Vector contains a value with components x & y
   protected Rectangle box;
   protected Image sprite;
 
   public Entity(float x, float y, int width, int height, Image sprite) {
      pos = new Vector2f(x,y);
      box = new Rectangle(x, y, width, height);
      this.sprite = sprite;
   }
 
   public void update(GameContainer gc, int mapWidth, int mapHeight, int delta) {
 
      Vector2f trans = new Vector2f(0, 0);
 
      Input input = gc.getInput();
 
      if (input.isKeyDown(Input.KEY_W)) // Delta is used to move things on a frame rate independent way
         trans.y = -0.5f * delta;
 
      if (input.isKeyDown(Input.KEY_S))
         trans.y = 0.5f * delta;
 
      if (input.isKeyDown(Input.KEY_D))
         trans.x = 0.5f * delta;
 
      if (input.isKeyDown(Input.KEY_A))
         trans.x = -0.5f * delta;
 
      if (trans.x != 0 && trans.y != 0) { // If both components aren't null, we reduce them to have constant speed on all directions
         trans.set(trans.x / 1.5f, trans.y / 1.5f);
      }
 
      if(pos.x+trans.x > 32 && pos.x+trans.x < (mapWidth-64)) // Is the player inside the map? (We add (subtract) because of the stone wall)
         pos.x += trans.x;
 
      if(pos.y+trans.y > 32 && pos.y+trans.y < (mapHeight-64))
         pos.y += trans.y;
 
 
   }
 
   public void render() {
      sprite.draw(pos.x, pos.y);
   }
 
   // Getters and Setters
   public Vector2f getPos() {
      return pos;
   }
 
   public float getX() {
      return pos.x;
   }
 
   public float getY() {
      return pos.y;
   }
 
   public void setPos(Vector2f pos) {
      this.pos = pos;
   }
 
   public Rectangle getBox() {
      return box;
   }
 
   public void setBox(Rectangle box) {
      this.box = box;
   }
 
   public Image getSprite() {
      return sprite;
   }
 
   public void setSprite(Image sprite) {
      this.sprite = sprite;
   }
}


Code:
package game;
 
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.tiled.TiledMap;
 
public class Camera {
 
   private int transX, transY;
   private int mapWidth, mapHeight;
   private Rectangle viewPort;
   /* We define a rectangle with the size of our screen, this represents our camera
    * "range", so everything inside the viewport will be drawn on the screen, we will
    * be able to move this rectangle across the map. */
 
 
   public Camera(TiledMap map, int mapWidth, int mapHeight) {
      transX = 0;
      transY = 0;
      viewPort = new Rectangle(0, 0, Game.WIDTH, Game.HEIGHT);
      this.mapWidth = mapWidth;
      this.mapHeight = mapHeight;
   }
 
   public void translate (Graphics g, Entity entity) {
 
   if(entity.getX()-Game.WIDTH/2+16 < 0)
          transX = 0;
       else if(entity.getX()+Game.WIDTH/2+16 > mapWidth)
          transX = -mapWidth+Game.WIDTH;
       else
          transX = (int)-entity.getX()+Game.WIDTH/2-16;
 
       if(entity.getY()-Game.HEIGHT/2+16 < 0)
          transY = 0;
       else if(entity.getY()+Game.HEIGHT/2+16 > mapHeight)
          transY = -mapHeight+Game.HEIGHT;
       else
          transY = (int)-entity.getY()+Game.HEIGHT/2-16;
 
       g.translate(transX, transY);
       viewPort.setX(-transX);
       viewPort.setY(-transY);
 
   }
}


Code:
package game;
 
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
 
public class Game extends BasicGame {
 
   //Constants
   static int WIDTH = 1024;
   static int HEIGHT = 760;
 
   static boolean fullscreen = false;
   static boolean showFPS = true;
   static String title = "Advanced Camera tutorial";
   static int fpslimit = 60;
 
 
   //Variables
   TiledMap map; //The file that contain the world we are
   Entity player; //The moving entity we will follow
   Camera camera; //The camera we are going to use
   int mapHeight, mapWidth;
 
   public Game(String title) {
      super(title);
   }
 
   public void init(GameContainer gc) throws SlickException {
      map = new TiledMap("res/testmap.tmx");
      mapWidth = map.getWidth() * map.getTileWidth(); // Map size = Tile Size * number of Tiles
      mapHeight = map.getHeight() * map.getTileHeight();
 
      player = new Entity(50, 50, 32, 32, new Image("res/emo.png"));
      camera = new Camera(map, mapWidth, mapHeight);
   }
 
   public void update(GameContainer gc, int delta) throws SlickException {
 
      player.update(gc, mapWidth, mapHeight, delta);
   }
 
   public void render(GameContainer gc, Graphics g) throws SlickException {
 
      camera.translate(g, player);
 
      map.render(0, 0);
      player.render();
   }
 
   public static void main(String[] args) throws SlickException {
      AppGameContainer app = new AppGameContainer(new Game(title));
      app.setDisplayMode(WIDTH, HEIGHT, fullscreen);
      app.setTargetFrameRate(fpslimit);
      app.setVSync(true);
      app.setShowFPS(showFPS);
      app.start();
   }
}

_________________
Sorry for my English, trying to improve!


Top
 Profile  
 
PostPosted: Tue Mar 20, 2012 5:01 am 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
its working fine now.. the problem was the variable for map size e.g. width and height.. the values for height used for width.. its working now.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: Jon and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group