Author Topic: Pushing Objects  (Read 4109 times)

0 Members and 1 Guest are viewing this topic.

Offline Cloudee1

Pushing Objects
« on: December 06, 2011, 07:34:51 PM »
I was poking around a bit on Nick's agidev site and came across this little tid bit so I thought I would share it here... This is not my work, It is Andrew Bakers  ;)

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: [Select]
// Logic 2: Push block
// ************************************************************************
#include "defines.txt"
if (new_room) {
   if ((prev_room_no == 1 ||     
    prev_room_no == 0)) {     
 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.

 obj_speed = 2;  //and this is the code that makes sure the object is non-stick.
 step.size(pushable_object, obj_speed);
 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.");


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.
The basic code itself is atatched here pushobj.

Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

SMF 2.0.14 | SMF © 2017, Simple Machines
Simple Audio Video Embedder

Page created in 0.078 seconds with 23 queries.