This site requires JavaScript, please enable it in your browser!
Greenfoot back
wabuilderman
wabuilderman wrote ...

2013/9/23

help with optimizing code

1
2
Sorry for later response. You don't to reinvent the wheel, you can mix the two values of light of day/night cycle light, and torches together, along with light being modified by how deep the dirt is.
wabuilderman wabuilderman

2013/9/24

#
ok... problem... i made this cool code that i am call form each block type, but it slows down the game a ton:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Block here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Block extends Actor
{
    int blockSize = ((PlayerWorld) getWorld()).blockSize;
    public GreenfootImage lightMap = new GreenfootImage(blockSize, blockSize);
    public Color trans = new Color(0,0,0,0);
    GreenfootImage originalImg = new GreenfootImage(blockSize, blockSize);
    GreenfootImage grass = new GreenfootImage("grass.png");
    GreenfootImage dirt = new GreenfootImage("dirt.png");
    public Block()
    {
       lightMap.setColor(new java.awt.Color(0,0,0,darkness));
       lightMap.fillRect(0, 0, blockSize, blockSize);
       // in Block constructor  
       baseImg = getImage();  
    }

    /**
     * Act - do whatever the Block wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    }    
    
    public void trimEdge(Block obj)
    {
        if (obj.getClass() == grass.class)
        {
            originalImg.drawImage(grass,0,0);
        }
        else if (obj.getClass() == Dirt.class)
        {
            originalImg.drawImage(dirt,0,0);
        }
        else
        {
            originalImg.fill();
        }
        
        obj.setImage(originalImg);
        
        if(obj.getOneObjectAtOffset(blockSize, 0, Block.class) == null)
        {
            obj.setImage(trimRight(obj.getImage()));
        }
        
        if(obj.getOneObjectAtOffset(-blockSize, 0, Block.class) == null)
        {
            obj.setImage(trimLeft(obj.getImage()));
        }
        
        if(obj.getOneObjectAtOffset(0, -blockSize, Block.class) == null)
        {
            obj.setImage(trimTop(obj.getImage()));
        }
        
        if(obj.getOneObjectAtOffset(0, blockSize, Block.class) == null)
        {
            obj.setImage(trimBottom(obj.getImage()));
        }
    }
    
    public GreenfootImage trimTop(GreenfootImage img)
    {
        img.setColorAt(0, 0, trans);
        img.setColorAt(7, 0, trans);
        img.setColorAt(8, 0, trans);
        img.setColorAt(9, 0, trans);
        return img;
    }
    
    public GreenfootImage trimBottom(GreenfootImage img)
    {
        img.setColorAt(0, 9, trans);
        img.setColorAt(7, 9, trans);
        img.setColorAt(8, 9, trans);
        img.setColorAt(9, 9, trans);
        return img;
    }
    
    public GreenfootImage trimLeft(GreenfootImage img)
    {
        img.setColorAt(0, 0, trans);
        img.setColorAt(0, 7, trans);
        img.setColorAt(0, 8, trans);
        img.setColorAt(0, 9, trans);
        return img;
    }
    
    public GreenfootImage trimRight(GreenfootImage img)
    {
        img.setColorAt(9, 0, trans);
        img.setColorAt(9, 7, trans);
        img.setColorAt(9, 8, trans);
        img.setColorAt(9, 9, trans);
        return img;
    }
    
    public void inheirt(Block obj)
    {
        GreenfootImage image = obj.getImage();
        image.scale(10,10);
        //setLight(obj, 200);
    }
    
    // in Block class  
    // with instance fields  
    int darkness, lightness, tint;  
    GreenfootImage baseImg;  
    
    // the setLight method (renamed)  
    private void updateImage()  
    {  
        if (Math.abs(tint-(255-lightness)*darkness/255) < 8) return;  
        tint = (255-lightness)*darkness/255;  
        GreenfootImage image = new GreenfootImage(baseImg);  
        GreenfootImage tintImg = new GreenfootImage(image.getWidth(), image.getHeight());  
        tintImg.setColor(Color.BLACK);
        tintImg.fill(); // default color for new image is black  
        tintImg.setTransparency(tint);  
        image.drawImage(tintImg, 0 ,0);  
        setImage(image);  
    }  
    // with these methods  
    public void setDarkness(int dark)  
    {  
        if (dark < 0) dark = 0;  
        if (dark > 255) dark = 255;  
        darkness = dark;  
        updateImage();  
    }  
  
    public void setLightness(int light)  
    {  
        if  (light < 0) light = 0;  
        if (light > 255) light = 255;  
        lightness = light;  
        updateImage();  
    } 
}
any ideas? PS: the segment that is being called is the method "trimEdge(Block obj)"
danpost danpost

2013/9/25

#
danpost wrote...
Try limiting the range of darkness and lightness to between 0 and 100 and use the following for the value of tint: tint = 255-255*((100-darkness)+lightness)/200;
This was a change I made in one of the other discussion threads. change '(255-lightness)*darkness/255' in lines 123 and 124 with the expression on the right side of the 'tint' equation and change the limits on lightness and darkness to between 0 and 100.
You need to login to post a reply.
1
2