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

2013/5/29

Structure generation not working

Game/maniac Game/maniac

2013/5/29

#
I am trying to generate a tree but my code for structure generation isn't working and builds the tree completely wrong and I don't know How to fix it. Heres the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Worldy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Worldy extends World
{
    public int[][] block;
    public int[][] tree = {{-1, 9, 9, 9, -1, -1}, {9, 9, 9, 9, -1, -1}, {9, 9, 8, 8, 8, 8}, {9, 9, 9, 9, -1, -1}, {-1, 9, 9, 9, -1, -1}};
    public int[] spawn = {0, 0};
    
    public static int blockSize = 16;
    public static int pixelSize = 3;
    
    public int level = 0;
    public int surfaceLevel;
    public int sX = 0;
    public int sY = 0;
    public int randWidth = Greenfoot.getRandomNumber(6)+2;
    
    public Player player;
    
    public Worldy() {
        super(600, 400, 1, false);
        setLevel(0);
    }
    
    public void act() {
        if(level == 1) {
            render();
        }
    }
    
    public void setLevel(int level) {
        this.level = level;
        removeObjects(getObjects(null));
        if(level == 0) {
            setBackground("Menu.png");
            addObject(new Button("Start", 1), 300, 200);
        } else if(level == 1) {
            getBackground().setColor(new Color(130, 180, 255));
            getBackground().fill();
            generateWorld();
            render();
            player = new Player();
            addObject(player, spawn[0]*blockSize*pixelSize-sX, spawn[1]*blockSize*pixelSize-sY);
            addObject(new PlayerArm(), spawn[0]*blockSize*pixelSize-sX, spawn[1]*blockSize*pixelSize-sY);
            addObject(new FPS(), 50, 10);
        }
    }
    
    public void generateWorld() {
        block = new int[600][600];
        surfaceLevel = Greenfoot.getRandomNumber(200)+block[0].length/2;
        for(int x = 0; x < block.length; x++) {
            for(int y = 0; y < block[0].length; y++) {
                block[x][y] = -1;
            }
        }
        for(int x = 0; x < block.length; x++) {
            for(int y = 0; y < block[0].length; y++) {
                if(y==surfaceLevel) {
                    if(Greenfoot.getRandomNumber(50)==0) {
                        generateStructure(tree, x, y-1, false);
                        block[x][y] = 0;
                    } else {
                        block[x][y] = 1;
                    }
                    if(x==block.length/2) {
                        spawn[0] = x;
                        spawn[1] = surfaceLevel-2;
                    }
                } else if(y>surfaceLevel&&y<surfaceLevel+randWidth) {
                    block[x][y] = 0;
                } else if(y==block[0].length-1) {
                    block[x][y] = 3;
                } else if(y>surfaceLevel) {
                    int coalRarity = 2;
                    int coalLevel = block[0].length;
                    int copperRarity = 6;
                    int copperLevel = block[0].length-surfaceLevel;
                    int diamondRarity = 10;
                    int diamondLevel = 30;
                    if((Greenfoot.getRandomNumber(coalRarity*10)==0 || (x>0&&block[x-1][y] == 5&&Greenfoot.getRandomNumber(coalRarity)==0) || (y<block[0].length-1&&x>0&&block[x-1][y+1] == 5&&Greenfoot.getRandomNumber(coalRarity)==0) || (x>0&&block[x-1][y-1] == 5&&Greenfoot.getRandomNumber(coalRarity)==0) || (block[x][y-1] == 5&&Greenfoot.getRandomNumber(coalRarity)==0))&&y>block[0].length-coalLevel)
                        block[x][y] = 5;
                    else if((Greenfoot.getRandomNumber(copperRarity*10)==0 || (x>0&&block[x-1][y] == 6&&Greenfoot.getRandomNumber(copperRarity)==0) || (y<block[0].length-1&&x>0&&block[x-1][y+1] == 6&&Greenfoot.getRandomNumber(copperRarity)==0) || (x>0&&block[x-1][y-1] == 6&&Greenfoot.getRandomNumber(copperRarity)==0) || (block[x][y-1] == 6&&Greenfoot.getRandomNumber(copperRarity)==0))&&y>block[0].length-copperLevel)
                        block[x][y] = 6;
                    else if((Greenfoot.getRandomNumber(diamondRarity*10)==0 || (x>0&&block[x-1][y] == 7&&Greenfoot.getRandomNumber(diamondRarity)==0) || (y<block[0].length-1&&x>0&&block[x-1][y+1] == 7&&Greenfoot.getRandomNumber(diamondRarity)==0) || (x>0&&block[x-1][y-1] == 7&&Greenfoot.getRandomNumber(diamondRarity)==0) || (block[x][y-1] == 7&&Greenfoot.getRandomNumber(diamondRarity)==0))&&y>block[0].length-diamondLevel)
                        block[x][y] = 7;
                    else
                        block[x][y] = 2;
                } else {
                    block[x][y] = -1;
                }
            }
            int upDown = Greenfoot.getRandomNumber(4);
            if(upDown == 0) {
                surfaceLevel++;
            } else if(upDown == 1) {
                surfaceLevel--;
            }
            randWidth = Greenfoot.getRandomNumber(6)+2;
        }
    }
    
    public void generateStructure(int[][] structure, int bottomX, int bottomY, boolean solid) {
        int index = 0;
        for(int x = 0; x < block.length; x++) {
            for(int y = 0; y < block[0].length; y++) {
                if(x>bottomX-(double)(structure.length)/2&&x<bottomX+(double)(structure.length)/2&&y>bottomY-structure[0].length&&y<=bottomY) {
                    int bX = index%structure.length;
                    int bY = (index-(index%structure.length))/structure.length;
                    if(!solid&&block[x][y]==-1) {
                        block[x][y] = structure[bX][bY];
                    } else if(solid) {
                        block[x][y] = structure[bX][bY];
                    }
                    index++;
                }
            }
        }
    }
    
    public void render() {
        removeObjects(getObjects(Block.class));
        for(int x = 0; x < block.length; x++) {
            for(int y = 0; y < block[0].length; y++) {
                int trueX = (x*blockSize*pixelSize+(int)(blockSize/2)*pixelSize)-sX;
                int trueY = (y*blockSize*pixelSize+(int)(blockSize/2)*pixelSize)-sY;
                if(block[x][y] > -1 && trueX >= -(blockSize*pixelSize)/2 && trueX <= getWidth()+(blockSize*pixelSize)/2 && trueY >= -(blockSize*pixelSize)/2 && trueY <= getHeight()+(blockSize*pixelSize)/2) {
                    addObject(new Block(block[x][y]), trueX, trueY);
                }
            }
        }
    }
}
Game/maniac Game/maniac

2013/5/29

#
Heres what it generates:
Game/maniac Game/maniac

2013/5/29

#
fixed my problem! :D
You need to login to post a reply.