Difference between revisions of "Pushing Objects"

From AGI Wiki
Jump to navigationJump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[AGI Tutorials|Tutorials Table of Contents]]<br />
+
[[Tutorials and Guides|Tutorials and Guides Table of Contents]]<br />
  
 
<div align="center"><span style="font-size: 22pt">Pushing Objects</span><br />
 
<div align="center"><span style="font-size: 22pt">Pushing Objects</span><br />
Line 6: Line 6:
 
&nbsp;
 
&nbsp;
  
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.
+
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.<br />
 +
 
  
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
Line 95: Line 96:
 
&nbsp;
 
&nbsp;
  
[[AGI Tutorials|Tutorials Table of Contents]]
+
[[Tutorials and Guides|Tutorials and Guides Table of Contents]]
  
 
&nbsp;
 
&nbsp;
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 +
[[Category:References]]
 +
[[Category:Objects]]

Latest revision as of 16:30, 26 December 2013

Tutorials and Guides Table of Contents

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) { 
    load.pic(room_no); 
    draw.pic(room_no); 
    discard.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.
    load.view(2); 
    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.

The basic code itself is packaged here pushobj. Download it by right-clicking the hyperlink.

 

Tutorials and Guides Table of Contents