Queex
05-11-09, 12:14 AM
Some of the more reusable things I'm using. They may come in handy for someone, or wiser heads may spot problems with them.
Thread on background colour changes (http://www.yolkfolk.com/bb/showthread.php?t=1171).
Thread on floaty text (http://www.yolkfolk.com/bb/showthread.php?t=1209).
Function that gives an item to Dizzy, or drops it at his feet if his inventory is full:
// Give to player or drop on ground if inventory full
func GiveOrDrop(idx)
{
if(InventoryAdd(idx)==0){
ObjSet(idx,O_X,PlayerGet(P_X)-8);
ObjSet(idx,O_Y,PlayerGet(P_Y)-4);
ObjSet(idx,O_DISABLE,0);
}
}
A line to add to DoPickupObject(idx) for items to avoid embarrassing disappearances behind scenery when they're dropped.
ObjSet(idx, O_LAYER,5); // move object to player layer
Two simple AI routines- they don't do anything that can't also be achieved with AIUpdateTrain, but I find them handy for very simple movement patterns without having to set up waypoints.
/////////////////////////////////////////////////////////////////////////////////
// IN: int; idx; object index
// Walker AI
// Moves left and right between two positions.
// O_USER = left x value
// O_USER+1 = right x value
// O_USER+2 = speed
// O_USER+3 = disable_flip, 0 = do flip, 1 = no flip
// O_STATUS = direction 0=right, 1=left (also determines flip)
///////////////////////////////////////////////////////////////////////////////
func AIUpdateWalker(idx)
{
if(idx==-1) return;
if(ObjGet(idx,O_DISABLE)) return;
if(!IsUpdate(ObjGet(idx,O_DELAY))) return;
x = ObjGet(idx,O_X);
speed=ObjGet(idx,O_USER+2);
if(speed==0) speed=4;//default speed
if(ObjGet(idx,O_STATUS)==0) //right
{
tx=ObjGet(idx,O_USER+1);
if(x<tx)
x+=speed;
else {
ObjSet(idx,O_STATUS,1);
if(ObjGet(idx,O_USER+3)==0)
ObjSet(idx,O_FLIP,1);
}
} else {
tx=ObjGet(idx,O_USER);
if(x>tx)
x-=speed;
else {
ObjSet(idx,O_STATUS,0);
if(ObjGet(idx,O_USER+3)==0)
ObjSet(idx,O_FLIP,0);
}
}
ObjSet(idx,O_X,x);
}
/////////////////////////////////////////////////////////////////////////////////
// IN: int; idx; object index
// Walker AI
// Moves left and right between two positions.
// O_USER = top y value
// O_USER+1 = bottom y value
// O_USER+2 = speed
// O_STATUS = direction 0=down, 1=up
///////////////////////////////////////////////////////////////////////////////
func AIUpdateDescender(idx)
{
if(idx==-1) return;
if(ObjGet(idx,O_DISABLE)) return;
if(!IsUpdate(ObjGet(idx,O_DELAY))) return;
y = ObjGet(idx,O_Y);
speed=ObjGet(idx,O_USER+2);
if(speed==0) speed=4;//default speed
if(ObjGet(idx,O_STATUS)==0) //down
{
ty=ObjGet(idx,O_USER+1);
if(y<ty)
y+=speed;
else {
ObjSet(idx,O_STATUS,1);
if(ObjGet(idx,O_USER+3)==0)
ObjSet(idx,O_FLIP,1);
}
} else {
ty=ObjGet(idx,O_USER);
if(y>ty)
y-=speed;
else {
ObjSet(idx,O_STATUS,0);
if(ObjGet(idx,O_USER+3)==1)
ObjSet(idx,O_FLIP,0);
}
}
ObjSet(idx,O_Y,y);
}
Thread on background colour changes (http://www.yolkfolk.com/bb/showthread.php?t=1171).
Thread on floaty text (http://www.yolkfolk.com/bb/showthread.php?t=1209).
Function that gives an item to Dizzy, or drops it at his feet if his inventory is full:
// Give to player or drop on ground if inventory full
func GiveOrDrop(idx)
{
if(InventoryAdd(idx)==0){
ObjSet(idx,O_X,PlayerGet(P_X)-8);
ObjSet(idx,O_Y,PlayerGet(P_Y)-4);
ObjSet(idx,O_DISABLE,0);
}
}
A line to add to DoPickupObject(idx) for items to avoid embarrassing disappearances behind scenery when they're dropped.
ObjSet(idx, O_LAYER,5); // move object to player layer
Two simple AI routines- they don't do anything that can't also be achieved with AIUpdateTrain, but I find them handy for very simple movement patterns without having to set up waypoints.
/////////////////////////////////////////////////////////////////////////////////
// IN: int; idx; object index
// Walker AI
// Moves left and right between two positions.
// O_USER = left x value
// O_USER+1 = right x value
// O_USER+2 = speed
// O_USER+3 = disable_flip, 0 = do flip, 1 = no flip
// O_STATUS = direction 0=right, 1=left (also determines flip)
///////////////////////////////////////////////////////////////////////////////
func AIUpdateWalker(idx)
{
if(idx==-1) return;
if(ObjGet(idx,O_DISABLE)) return;
if(!IsUpdate(ObjGet(idx,O_DELAY))) return;
x = ObjGet(idx,O_X);
speed=ObjGet(idx,O_USER+2);
if(speed==0) speed=4;//default speed
if(ObjGet(idx,O_STATUS)==0) //right
{
tx=ObjGet(idx,O_USER+1);
if(x<tx)
x+=speed;
else {
ObjSet(idx,O_STATUS,1);
if(ObjGet(idx,O_USER+3)==0)
ObjSet(idx,O_FLIP,1);
}
} else {
tx=ObjGet(idx,O_USER);
if(x>tx)
x-=speed;
else {
ObjSet(idx,O_STATUS,0);
if(ObjGet(idx,O_USER+3)==0)
ObjSet(idx,O_FLIP,0);
}
}
ObjSet(idx,O_X,x);
}
/////////////////////////////////////////////////////////////////////////////////
// IN: int; idx; object index
// Walker AI
// Moves left and right between two positions.
// O_USER = top y value
// O_USER+1 = bottom y value
// O_USER+2 = speed
// O_STATUS = direction 0=down, 1=up
///////////////////////////////////////////////////////////////////////////////
func AIUpdateDescender(idx)
{
if(idx==-1) return;
if(ObjGet(idx,O_DISABLE)) return;
if(!IsUpdate(ObjGet(idx,O_DELAY))) return;
y = ObjGet(idx,O_Y);
speed=ObjGet(idx,O_USER+2);
if(speed==0) speed=4;//default speed
if(ObjGet(idx,O_STATUS)==0) //down
{
ty=ObjGet(idx,O_USER+1);
if(y<ty)
y+=speed;
else {
ObjSet(idx,O_STATUS,1);
if(ObjGet(idx,O_USER+3)==0)
ObjSet(idx,O_FLIP,1);
}
} else {
ty=ObjGet(idx,O_USER);
if(y>ty)
y-=speed;
else {
ObjSet(idx,O_STATUS,0);
if(ObjGet(idx,O_USER+3)==1)
ObjSet(idx,O_FLIP,0);
}
}
ObjSet(idx,O_Y,y);
}