Using the push-sprites hack


Animal Husbandry is a short game to showcase some of the features of the push-sprites hack in Bitsy.

Below I will explain how to achieve the key game behaviours using the hack.

game code       

push-sprites.js

behaviourYou can push the animals but not the sign.
explanationAll sprites except the sign are pushable by the player, so I added a flag to the sign name.
game data
SPR g
11111111
10000001
10000001
10000001
11111111
00011000
00011000
00000000
NAME sign_IMMOVABLE
DLG SPR_0
POS 0 3,2
hackOptions
playerPushesSprite: function (spr) {
    if(!spr.name) return true; else return spr.name.indexOf('IMMOVABLE') == -1; 
}

behaviourBigger animals can push smaller ones.
explanationSprites have names starting with the animal name, which is checked in the push logic.
game data
SPR s
00000000
00000000
00000000
01111100
11111110
11111110
11111100
10001000
NAME pig1
POS 0 9,7
hackOptions
spritePushesSprite: function (spr1,spr2) {
  if(spr1.name){
  //...
    if(spr1.name.startsWith("pig")){
      if(spr2.name){
        if( spr2.name.startsWith("pig") ||
            spr2.name.startsWith("dog") ||
            spr2.name.startsWith("sheep") ||
            spr2.name.startsWith("cat") ||
            spr2.name.startsWith("chicken") ){
              return true;
        }
      }
    }
  //...
  }
  return false;
}

behaviourAnimals turn in the direction they are pushed.
explanation

push-sprites includes some sprite-flipping code borrowed from the directional avatar hack.

I just need to specify that horizontal flipping is turned on.

hackOptions
horizontalFlipsAllowed: true,
verticalFlipsAllowed: false

behaviourPutting animals on the targets opens the door.
explanation

The target positions are specified in the hackOptions and linked to a _true dialog that will fire when they are all covered by a sprite.

The dialog fires on every move as long as the targets are covered, so I use a variable to flag when the door is already open. The sound effect only occurs when the door opens.

The _false dialog replaces the hedge when the condition is no longer satisfied.

game data
DLG 0_complete_true
"""
{
  - open_0 == 1 ?
  - else ?
        (js "room[0].tilemap[6][15] = null")
        (soundeffectNow "pig3")
        {open_0 = 1}
}
"""
DLG 0_complete_false
"""
{
  - open_0 == 0 ?
  - else ?
        (js "room[0].tilemap[6][15] = 'a'")
        {open_0 = 0}
}
"""
hackOptions
conditions: {
  '0_complete':{
    'anything':[[[0,5,10],[0,7,10],[0,9,10]]]
  },
  //...
}

behaviourSome targets need specific animals.
explanation

The hackOptions provide a lot of expressive power to set up specific target conditions.

Here, I provide the animal names (sprite prefixes) for individual targets that must all be covered before the door will open.

hackOptions
conditions: {
  //...
  '1_complete':{
    'chicken':[[[1,9,5]]],
    'cat':[[[1,9,7]]],
    'dog':[[[1,9,9]]]
  },
  //...
}

behaviourThe long grass stops the sheep.
explanation

Items are not pushable themselves, but they can be set up to block sprites.

hackOptions
itemStopsSprite: function (itm,spr) {
  return true; 
}

behaviourThe sheep can pass the electric fence, but the player cannot.
explanation

Although the fence is marked as a wall, sprites can be pushed onto specified tiles.

game data
TIL v
11111111
00000000
00000000
00000000
00000000
00000000
00000000
00000000
>
01010101
10101010
00000000
00000000
00000000
00000000
00000000
00000000
NAME electric_NONSTOPPING
WAL true
hackOptions
tileStopsSprite: function (til,spr) {
  if(til.name && til.name.indexOf('NONSTOPPING') !== -1) return false; else return til.isWall;
}

behaviourSprites can be pushed from one room to another.
explanation

I have set hackOptions to allow all sprites to move through all exits. This behaviour can be specified in more detail if required.

hackOptions
spriteCanExit: function (spr,ext){
    return true;
}

Files

AnimalHusbandry.zip Play in browser
Nov 12, 2019

Leave a comment

Log in with itch.io to leave a comment.