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

2013/9/23

help with optimizing code

1
2
wabuilderman wabuilderman

2013/9/23

#
I have my terraria game, its not great, but it atleast does something... the problem is that it is not very efficiant. Any ideas as far as how to optimize it? Scenario:Terraria Greenfoot Remake Note: I am currently at a stand still. anything else i add at this point begins to create lag. i still need to update lighting allow for creation of blocks. i really need to optimize my code.
It'd help if we could take a look at your code, and see how we can help. Just update your scenario and check the "Publish Source Code" button.
danpost danpost

2013/9/23

#
No ideas until I am able to download it (please include source code on 'sharing').
I found the source of your problems! You called produceSunLight() Every single frame, TWICE (Once by the world, and once by sun) when it only needs to be declared once the whole time. After getting rid of your world's act method, and changing the act method in sun to:
private boolean boo = true;

public void act() 
    {
        if (boo)
        {
            produceSunLight();
            boo = false;
        }
    }    
Your game now runs in hyper active mode!!! I also cleaned up your selector and Player to get rid of most of those big if statements (runs exactly the same). Here's your Player: Player Here's your Selector: Selector Your welcome :D
wabuilderman wabuilderman

2013/9/23

#
thanks!
wabuilderman wabuilderman

2013/9/23

#
oh... can you please paste the player and selector code here? i am having issues with the website that you linked to.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Selector here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Selector extends Actor
{
    private MouseInfo mouse;
    private PlayerWorld world;
    
    public Selector()
    {
        getImage().scale(10,10);
    }
    
    /**
     * Act - do whatever the Selector wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        mouse = Greenfoot.getMouseInfo();
        if (world == null)
            world = (PlayerWorld) getWorld();
        if (mouse != null)
        {
           this.setLocation(mouse.getX(), mouse.getY());
           if(Greenfoot.mouseClicked(null))
            {
                if(mouse.getButton() == 1)
                {
                    if(getObjectsInRange(100, Player.class).isEmpty() == false)
                    {
                        if(this.getIntersectingObjects(Block.class) != null)
                        {
                           getWorld().removeObject(this.getOneIntersectingObject(Block.class));
                        }
                    }
                }
                if(mouse.getButton() == 3)
                {
                    if(getObjectsInRange(100, Player.class).isEmpty() == false)
                    {
                        world.placeBlock(new Dirt(), getX(), getY());
                    }
                }
            }
        }
    }    
    
    public void goTo(int x, int y)
    {
        this.setLocation(x, y);
    }
}
wabuilderman wabuilderman

2013/9/23

#
thanks. and the player?
Also, I see you had that produceSunLight() method running constantly in case the player tries to break a block, you want it to be like terraria that the blocks gain more "light". Well, for the basic stages that you are at now, I recommend that the blocks actually "light" themselves, by counting how many blocks are above them, and to make the game run a bit faster, have that only called when the mouse is clicked (when blocks are destroyed/added) and that the blocks below the completely black ones to notice that the block above them is completely black and do the same, instead of counting the rest of the blocks.
here's the player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    public boolean moveRight = true;
    public boolean moveLeft = true;
    public boolean jumping = false;
    public int jumpTime = 0;
    protected double exactX;
    protected double exactY;
    public boolean facingRight = true;
    public boolean facingLeft = false;

    public Player()
    {
        getImage().scale(getImage().getWidth() + 3, getImage().getHeight() + 10);
    }

    public void act()
    {
        if(!onGround())
        {
            if (!jumping)
            {
                setLocation(getX(), getY() + 10);
            }
        }

        if(jumping)
        {
            if(jumpTime < 6)
            {
                jumpTime++;
                setLocation(getX(), getY() - 17);
            }
            else
            {
                jumpTime = 0;
                jumping = false;
            }
        }

        if(onGround())
        {
            jumping = Greenfoot.isKeyDown("space");
        }

        moveRight = !objectToRight();

        moveLeft = !objectToLeft();

        if(moveRight && (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")))
        {
            setLocation(getX() + 5, getY());
            if(facingRight == false)
            {
                getImage().mirrorHorizontally();
                facingRight = true;
                facingLeft = false;
            }
        }

        if(moveLeft && (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")))
        {
            setLocation(getX() - 5, getY());
            if(facingLeft == false)
            {
                getImage().mirrorHorizontally();
                facingRight = false;
                facingLeft = true;
            }
        }
    }

    public void setLocation(double x, double y) 
    {  
        this.exactX = x;  
        this.exactY = y;  
        super.setLocation((int) x, (int) y);  
    }  

    public double getExactX() 
    {  
        return exactX;  
    }  

    public double getExactY() 
    {  
        return exactY;  
    }

    public boolean onGround()
    {
        return getOneObjectAtOffset(0,getImage().getHeight() / 2, Block.class) != null;
    }

    public boolean ObjectAbove()
    {
        return getOneObjectAtOffset(0, getImage().getHeight() + 3, Block.class) != null;
    }

    public boolean objectToLeft()
    {
        return getOneObjectAtOffset(-1, 4, Block.class) != null;
    }

    public boolean objectToRight()
    {
        return getOneObjectAtOffset(-1,4, Block.class) != null;
    }
}
And you can add on later to the method I suggested with light by having blocks look around them for blocks with more light (like if you dig a hole straight down, only the blocks below the player will gain light with my previous method) and guess what their light should be.
wabuilderman wabuilderman

2013/9/23

#
the problem with doing the "blocks lighting themselves" is that i want to plan longer term... example: torches, that would give light to blocks, rather than having the block detect light sources. On that matter, when i call produceSunLight(), for some reason or another, it doesn't work, my blocks aren't updated with light, thus i have some pretty dull lighting.
wabuilderman wabuilderman

2013/9/23

#
you don't have to answer that.. it was the in the wrong place
That's why I suggested this for your early stage. Another thing you could do with the torches, and other light elements, is have the blocks look for those objects around them as well, and determine how much light they should add to their image because of those objects.
wabuilderman wabuilderman

2013/9/23

#
well, tourches and other more advanced light sources aren't going to be too far down the road, thus, i don't want to completely redesign my light system just to make the game run faster if i am going to have to then re-invent the wheel when i get those more advanced lights. (when i say "other more advanced light sources" i mean i am going to add a possible "colored" light)
There are more replies on the next page.
1
2