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

2013/9/19

Help with dynamic grid lighting.

1
2
3
wabuilderman wabuilderman

2013/9/19

#
I have been working on a terraria like game, and i have everything that i had really set forth to do already done, execpet that there is no lighting. :( here is my code so far for my "light.class" and my "block.class":
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    /**
     * 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 inheirt(Block obj)
    {
        GreenfootImage image = obj.getImage();
        image.scale(10,10);
        setLight(obj, 200);
    }
    
    public void setLight(Block obj, int light)
    {
        darkness = 255 - light;
        baseImg = obj.getImage();
        lightMap.clear();
        lightMap.setColor(new java.awt.Color(0,0,0,darkness));
        lightMap.fillRect(0, 0, blockSize, blockSize);
        GreenfootImage tempImg = baseImg;
        tempImg.drawImage(lightMap, 0 ,0);
        obj.setImage(tempImg);
    }
    
    public void addLight(Block obj, int light)
    {
        darkness = darkness - light;
        lightMap.clear();
        lightMap.setColor(new java.awt.Color(0,0,0,darkness));
        lightMap.fillRect(0, 0, blockSize, blockSize);
        GreenfootImage tempImg = baseImg;
        tempImg.drawImage(lightMap, 0 ,0);
        obj.setImage(tempImg);
    }
}
and the other one:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class light here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class light extends Actor
{
    /**
     * Act - do whatever the light wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    
    public void produceRadiusLight()
    {
    }
    
    public void produceSunLight()
    {
        int x = 0;
        PlayerWorld world = ((PlayerWorld) getWorld());
        int worldWidth = world.sizeX/world.blockSize;
        while (x < worldWidth)
        {
           getTopBlock(x).setLight(getTopBlock(x), 255);
           x++;
        }
    }
    
    public Block getTopBlock(int x)
    {
        int y = 0;
        World world = getWorld();
        while(world.getObjectsAt(x, y, null).isEmpty())
        {
            y++;
        }
        return ((Block) (world.getObjectsAt(x, y, Block.class).get(0)));
    }
}
right now it just sets it springs a "null pointer exception" on my while loop. any Ideas?
wabuilderman wabuilderman

2013/9/19

#
PS: if you need my world generation code or some other tid bits, just ask.
danpost danpost

2013/9/19

#
Find where the 'getTopBlock(int)' method is called from and determine if the light object used to run the method on is in the world. Also, your 'while' statement should be:
while(world.getObjectsAt(x, y, Block.class).isEmpty())
otherwise, if any other actor object is encountered, it will error on the return statement. Another thing is that you MUST have at least one block at all possible 'x' values or you will run an infinite 'while' loop.
wabuilderman wabuilderman

2013/9/20

#
well, my terrain generation takes care of the infinite loop. but thanks a million, this problem really is difficult. i will try what you just said.
wabuilderman wabuilderman

2013/9/20

#
bad news, it still says the error is on line 40, the while loop, and it still is null pointer. any other ideas?
wabuilderman wabuilderman

2013/9/20

#
ok, a little trial and error, i found the problem is that world is null.
danpost danpost

2013/9/20

#
I am amazed that you are not getting an error when trying to create a block object to begin with. I would not think that line 12 would pass compilation when using 'getWorld' in the object constructor. Also, I hope that you only set the light of the block one time (during construction). Otherwise, you may not get the results you want. To allow the light on a block to be able to change properly, you will need to create a new GreenfootImage either by using a saved copy of the initial image before adding any light or by using the filename of the image file.
wabuilderman wabuilderman

2013/9/20

#
it is called during the constructor, but i need to know why i cant call the world.
wabuilderman wabuilderman

2013/9/20

#
.... i wonder if i should just start over on the light system....
wabuilderman wabuilderman

2013/9/20

#
here is what i want: but i don't know how to do this.
danpost danpost

2013/9/20

#
You may want to use something like my Spot-lights Demo. The main difference is you will be using step distances to determine the lighting where I used radial distance.
wabuilderman wabuilderman

2013/9/20

#
i don't quite see how i could apply that to this.
danpost danpost

2013/9/20

#
How, exactly, do you want your lighting to work. Is the lighting to follow specific actors? or once it is set, that is it?
wabuilderman wabuilderman

2013/9/20

#
i want to have it be something like this: one central "light process" that takes in lighting requests light process then makes neccessary changes to the actors.
wabuilderman wabuilderman

2013/9/20

#
i posted the game so you can see how my system works.
There are more replies on the next page.
1
2
3