Posts Tagged ‘rendering’

Last games I made were all top-down, this had quite some advantages, for one you only need one set of animations/graphics for all directions and with an  isometric view, sprites can overlap.

I solved this in my code to seperate the static content from the dynamtic content (static vegetation, vs moving units/agents). ┬áIn my case there are less agents than vegetation. So I loop through the static objects and see what it’s y positions is. This makes sure you only have to sort your static objects once on their y position, and you’ll only have to keep track of the y position of your moving agents. Besides sorting agents on the same level, I’ve also got different Z levels for rendering, this what keeps the tree canopy above the trunk of tree with a higher Y value.

public void draw(Graphics graphics) throws SlickException 
{
 
 Iterator iter = agentViews.values().iterator();
 float agentY = 0;
 AgentView agView = null;
  // Get the first agent
 if(iter.hasNext())
 {
   agView = (AgentView) iter.next();
   agentY = agView.getModel().getY();
 }
 // Loop through all the static objects on the same level
 boolean processingAgents;
 for(AgentView vegView : vegetiationViewsL0) 
 {
 
 float y = vegView.getModel().getY();
 
 if(agView != null)
 {
    // As long as the y  of the static object is higher (so lower on the screen) it will draw it first
 if(y >= agentY )
 {
   vegView.draw(graphics);
 }
 else
 {
    // Else draw the agent and check if there are other agents with a higher y than the current static object
   agView.draw(graphics);

 processingAgents = true;
 while(processingAgents)
 {
   if(iter.hasNext())
   {
     agView = (AgentView)iter.next();
     agentY = agView.getModel().getY();
     if(agentY > y)
     {
       agView.draw(graphics);
     }
     else
     {
        processingAgents = false;
     } 
   }
   else
   {
     agView= null;
     processingAgents = false;
   } 
 }
 vegView.draw(graphics);
 //System.out.println("Tree" + y); 
 }
 }
 else
 {
 vegView.draw(graphics);
 }
 
 }

fhZLR3P