Slick Forums

Discuss the Slick 2D Library
It is currently Mon May 20, 2013 10:11 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Jan 17, 2012 1:35 pm 
Offline
User avatar

Joined: Tue Jan 17, 2012 1:29 pm
Posts: 5
Hi all,

Maybe i missed it in the search, but i could not find anything explaining how i can solve the following problem:

I have a TiledMap with things like buildings and stuff, The player needs to be rendered in front of the building when the player Y is higher then the building Y, but when the player Y is lower then the Building Y the player needs to be renderen behind the building. Two screenshots for explaining:
This is correct player is on front of the building, so the player needs to be rendered on top:
http://cl.ly/3t3W0t1t2N1F2x1w132w

This one is incorrect, player needs to be behind the building:
http://cl.ly/391m0s342Q3E0C2v1y2F

How can i fix this issue with the Tiled system build into Slick?


Top
 Profile  
 
PostPosted: Tue Jan 17, 2012 2:54 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
You need to render your tiledmap line by line. There is a render() method implementation with additional parameters where you can force line by line rendering (in TiledMap and in Layer).
It might be sufficient to do this only for your object layer.
Then you need to calculate (based on the y values) between which lines of your object layer you render your player.

Hope that helps,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
PostPosted: Tue Jan 17, 2012 4:16 pm 
Offline
User avatar

Joined: Tue Jan 17, 2012 1:29 pm
Posts: 5
Hi,

I found the render method you where taking about, but how can i render an entity between those lines? or do i have to call that render method in a for loop?


Top
 Profile  
 
PostPosted: Tue Jan 17, 2012 4:47 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
Exactly. For your specific layers that deal with your y ordering you'll have to render line by line to put your player (or other game objects) in between those lines at their proper positions.

I think in my SpiderTrap game I do render the layers line by line in a loop - just in case you'll need some snippet code. Source code on my site (see signature). Should be in class AreaMap if I remember correctly...
...yup, I just checked. It's in class AreaMap which is a subclass of TiledMap.

Here is the relevant snippet:
Code:
   public void draw(Graphics g) {
      int targetCX = 0;
      int targetCY = 0;

      if (focusObj != null) {
         // set center of areamap to focus obj
         targetCX = (int) focusObj.getPosition().x;
         targetCY = (int) focusObj.getPosition().y;
      } else {
         // set center of areamap to start position
         targetCX = (int) startPos.x * SpiderTrap.TILESIZE;
         targetCY = (int) startPos.y * SpiderTrap.TILESIZE;
      }
      // now smoothly move camera on realCX, realCY to targetCX, targetCY, using cameraDelta
      if (Math.abs(targetCX-realCX) > cameraDelta) {
         if (targetCX > realCX)
            realCX += cameraDelta;
         else
            realCX -= cameraDelta;
      } else
         realCX = targetCX;
      if (Math.abs(targetCY-realCY) > cameraDelta) {
         if (targetCY > realCY)
            realCY += cameraDelta;
         else
            realCY -= cameraDelta;
      } else
         realCY = targetCY;
      
      int sx = (realCX - SpiderTrap.WIDTH_HALF) / SpiderTrap.TILESIZE;
      int sy = (realCY - SpiderTrap.HEIGHT_HALF) / SpiderTrap.TILESIZE;
      int ox = (realCX - SpiderTrap.WIDTH_HALF) % SpiderTrap.TILESIZE;
      int oy = (realCY - SpiderTrap.HEIGHT_HALF) % SpiderTrap.TILESIZE;

      int x = -ox;
      int y = -oy;
      int height = SpiderTrap.HEIGHT / SpiderTrap.TILESIZE;
//      int width = SpiderTrap.WIDTH / SpiderTrap.TILESIZE;
      int width = (SpiderTrap.WIDTH / SpiderTrap.TILESIZE) + 1;

      // draw ground layer and entities
      drawLayer(GROUNDLAYER, x, y, sx, sy, realCX, realCY, width, g, groundEntities);
      // draw splatters below all objects
      g.translate(-realCX + SpiderTrap.WIDTH_HALF, -realCY + SpiderTrap.HEIGHT_HALF);
//      splatSystem.render();
      g.resetTransform();
      // draw object layer and entities
      drawLayer(OBJECTLAYER, x, y, sx, sy, realCX, realCY, width, g, objectEntities);
      g.translate(-realCX + SpiderTrap.WIDTH_HALF, -realCY + SpiderTrap.HEIGHT_HALF);
      bulletSystem.render();
      explosionSystem.render();
      effectSystem.render();
      g.resetTransform();
      // draw top layer and entities
      drawLayer(TOPLAYER, x, y, sx, sy, realCX, realCY, width, g, topEntities);
      g.translate(-realCX + SpiderTrap.WIDTH_HALF, -realCY + SpiderTrap.HEIGHT_HALF);
      ftbEmitter.renderSystem(g);
      g.resetTransform();
   }
   
   private void drawLayer(int layerIndex, int x, int y, int sx, int sy, int cx, int cy, int width, Graphics g, ArrayList<Entity> entities) {
      // draw the layer
      Layer layer = (Layer) layers.get(layerIndex);
      for (int ty = 0; ty < height; ty++) {
//         layer.render(x, y, sx, sy, width, ty, true, tileWidth, tileHeight);
         layer.render(x, y, sx, sy, width, ty, true);
      }
      // draw the entities on this layer
      for (Entity entity : entities) {
         float xp = -cx + SpiderTrap.WIDTH_HALF + entity.getX();
         float yp = -cy + SpiderTrap.HEIGHT_HALF + entity.getY();
         
         if ((xp > -50) && (yp > -50) && (xp < SpiderTrap.WIDTH + 50) && (yp < SpiderTrap.HEIGHT + 50)) {
            g.translate(-cx + SpiderTrap.WIDTH_HALF, -cy + SpiderTrap.HEIGHT_HALF);
            entity.draw(g);
            g.resetTransform();
         }
      }
      
   }


Hope that helps,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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