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

2016/2/7

The sokoban game

TomazVDSN TomazVDSN

2016/2/7

#
We are almost there 1) ScenarioLoader using strings is working very well 2) Player is blocked by the Wall
import greenfoot.*; 
public class Player extends MovementController
{
    private Display display;

    public void act(){
        moveWith("right", "left", "up", "down");
        stopIfCollideWith(Wall.class);
    }
}
When Player hits the wall I setLocation (get_x(), get_y()), get_x() and get_y() memories from previous location of the Player.
public void stopIfCollideWith(Class myClass){

        Object obj= getWorld().getObjects(Player.class).get(0);
        Player gB = (Player)obj;

        Actor actor = getOneIntersectingObject(myClass);
        if(actor!=null){
            setLocation(gB.get_x(), gB.get_y());
        }
    }
3) Player is moving the Blocks to right, left, up, and down. Please take look the whole code I published The problem now is, when the Player move the block against the wall block and player should stop. I have try many things with a different result. I tried to stop the Player if Block bump the Wall, instead it flips over the player.
    public void checkBumpWith(Class myClass){
            Actor actor1 = getOneIntersectingObject(myClass);
            if(actor1!=null && !blockBumpedWall()){
                this.moveIt(identifyCollisionSide(actor1));
            }
    }
Please, somebody could help me a little here. Thanks.
danpost danpost

2016/2/7

#
TomazVDSN wrote...
The problem now is, when the Player move the block against the wall block and player should stop. I have try many things with a different result. I tried to stop the Player if Block bump the Wall, instead it flips over the player.
I am not exactly sure what you mean by "flips over the player"; EDIT: after seeing the scenario, I understand -- also there is an issue with block over block movement. Anyway, being this is a gridded world where each step requires a distinct move in one of four directions, it is better to check for the possibility to move before moving (rather than afterwards). The way I would go about it would be to do something like the following:
// pseudo-coded (undefined variables used)

// in player class (after determining the direction to move and not running directly into wall)
Actor block = getOneObjectAtOffset(xDirection, yDirection, Block.class);
if (block == null || block.canMove(direction)) move(direction);

// in block class
public boolean canMove(int direction)
{
    Actor wall = getOneObjectAtOffset(xDirection, yDirection, Wall.class);
    if (wall != null) return false;
    move(direction);
    return true;
}
I just wanted to get across a way to have the movement of the player restricted by the object it is trying to push. Also, not included was whether a block is trying to push another block. If you are interested, I could post a sample scenario that is intended to show this in use. It would be very basic -- with very little other code.
TomazVDSN TomazVDSN

2016/2/8

#
danpost wrote...
it is better to check for the possibility to move before moving (rather than afterwards).
I do agree, let me try to do that. Yes please , if you could publish a sample scenario I will study it, it would be of a great help for me. Regards, Tomaz
danpost danpost

2016/2/8

#
TomazVDSN wrote...
Yes please , if you could publish a sample scenario I will study it, it would be of a great help for me.
The sample sokoban movement demo scenario is here.
TomazVDSN TomazVDSN

2016/2/9

#
danpost, Thank you!, I will take a look !
You need to login to post a reply.