Pushing Objects

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Pushing Objects
By Andrew Baker

I was thinking about having a ladder in a room in a game that I could move around and then climb. I thought all day about how I would accomplish this when I figured out this much. In my example, the view in question (o1) is 10 x 20, as is the ego. This code produces a movable character that will push the block in the same direction he is going. The block was originally very sticky, but I fixed this by making it move faster than the ego, which makes it move a little choppy, but not distractingly so.

Code:
```// ************************************************************************
//
// Logic 2: Push block
//
// ************************************************************************

#include "defines.txt"

if (new_room) {
draw.pic(room_no);
set.horizon(37);

if ((prev_room_no == 1 ||
prev_room_no == 0)) {
position(ego,120,100);
status.line.on();
accept.input();
}

draw(ego);
show.pic();

block_x = 75; //these first two are the push_object's center dimensions
block_y = 75;
block_xr  = 85; //follows are the perimeter dimensions of the block.
block_yt = 65;  //r is right, t is top, l is left, b is bottom.
block_xl = 65;
block_yb = 85;

animate.obj(o1);  //this is the push block.
set.view(o1,2);
position.v(o1,block_x,block_y);
draw(o1);
ignore.objs(o1);

obj_speed = 2;  //and this is the code that makes sure the object is non-stick.
step.size(pushable_object, obj_speed);
}

get.posn(ego,ego_x,ego_y);

if (ego_x > block_xl &&             //Is the ego in the perimeter?
ego_y > block_yt     &&
ego_x < block_xr     &&
ego_y < block_yb) {
get.dir(ego, this_way);         //Then I simply set the object's direction to the ego's.
set.dir(o1, this_way);
get.posn(o1,block_x, block_y);  //Then I update the object's coordinates
block_xr = block_x;             //and the perimeter as well.
block_xr += 10;
block_yt = block_y;
block_yt -= 10;
block_xl = block_x;
block_xl -= 10;
block_yb = block_y;
block_yb += 10;
} else {
stop.motion(o1);  //And when the ego stops or leaves the push object's perimeter, the object
}                     //stops.

if (said("look")) {
print("Except for the stone block, this is an empty room.");
}

return();```

The push object is now stick-free for all intents and purposes. Not completely stick-free, but it won't glue itself to the ego the way it did in my original code.

It would be very simple to add a flag check in the push_object "if" statement so that you can control the "pushability" of the object. For example, if a very large boulder needs to be pushed to a particular area of the screen, but the ego first needs to lift weights until they are strong enough to push the boulder, then you would set up a flag, strong_enough. If strong_enough == FALSE, then the perimeter is ignored, but the object is observed (use observe.objs). If strong_enough == TRUE, then the perimeter is observed, but the object is ignored. It is essential to ignore the object when you want it to be pushed, or the block will not move properly.

It would also be a good idea to set or reset a flag when the boulder is in the final position so that it will stop moving.

Strangely enough, push objects cannot be forced off screen or across black control lines.