This came out of an MPAGD forum request, and as I had already created the mechanic in Cocoa and the Time Machine and expanded/improved it in 24 Hour Parsley People, it wasn't too difficult to create a simplified version that you can use as a building block and then adapt and expand for your MPAGD games.
Our objective is to create a Crate that the player can push left and right, and jump on to reach higher platforms.
In addition we will make it subject to gravity, such that if the player pushes it off a platform it will drop to the platform beneath.
There is a stock Pushable Sprite script in standard MPAGD, but its not really suitable for a 2D platform game, originally I used it as a starting point but in the end I rewrote it and stripped it back (I was trying to save as many bytes as possible!.
So, here's what you should end up with...
OK, so the first thing were going to need is a couple of variables that will store the X an Y co-ords of the Player, in my demo I am using V & W, so in your player script add the following lines:
LET V = X LET W = Y
Now create an event for your crate, this is where all the work will take place, and add the following code (not substitute V & W if you already have other variable for your Player's X & Y values)
;======================================PUSHABLE CRATES
IF IMAGE 5 ; image 5 is my pushable crate
SPRITEINK 6
FALL ; let gravity do its thing
IF COLLISION PLAYER ; is the player touching the crate?
IF X < V ; is crate on left of player?
LET SETTINGA = LEFT ; if so we will push it left
ELSE ; if not
LET SETTINGA = RIGHT ; crate is on the right so we'll push it right
ENDIF
IF W > Y ; is player lower than the crate?
IF SETTINGA = LEFT ; and if crate is on the left
OTHER ; push fails so move the player back to the right
UNDOSPRITEMOVE
ENDSPRITE
ELSE
IF SETTINGA = RIGHT ; and if crate is on the right
OTHER ; push fails so move player to the left
UNDOSPRITEMOVE
ENDSPRITE
ENDIF
ENDIF
ENDIF
IF Y > W ; if player is higher than the crate
SUBTRACT W FROM Y ; how much higher is the player?
IF Y >= 9 ; if it is 9 or more pixels
OTHER ; switch to the player
IF CANGOUP ; if there is space
STOPFALL ; stop the player on the crate
SUBTRACT 1 FROM Y ; required to prevent player/crate overlapping
ENDIF
ENDSPRITE
ENDIF
ADD W TO Y ; reset the crates Y value
ELSE ; if player less than 9px above, landing fails
IF SETTINGA = LEFT ; if crate is on left
OTHER ; push has failed so move player to the right
UNDOSPRITEMOVE
ENDSPRITE
ELSE ; else crate is on right
OTHER ; push has failed so move player to the left
UNDOSPRITEMOVE
ENDSPRITE
ENDIF
ENDIF
IF W = Y ; if player is level with the crate push success!
IF SETTINGA = LEFT ; if crate is on the left of player
IF CANGOLEFT ; and there is room to the left
SPRITELEFT ; push the crate left
ELSE ; but if there is no room to push the crate
OTHER ; switch to the player
IF CANGORIGHT ; if there is space to the right
SPRITERIGHT ; move the player right
ENDIF
ENDSPRITE
ENDIF
ENDIF
IF SETTINGA = RIGHT ; if crate is on the right of player
IF CANGORIGHT ; and there is room to the right
SPRITERIGHT ; push the crate right
ELSE ; but if there is no room to push the crate
OTHER ; switch to the player
IF CANGOLEFT ; if there is space to the left
SPRITELEFT ; move the player left
ENDIF
ENDSPRITE
ENDIF
ENDIF
ENDIF
ENDIF
ENDIFThis is a much simplified version of what I used within my games, but is the core mechanic, obviously you can adapt it for your game. Things you can expand it with include:
What should happen if an enemy collides with a crate?
What should happen if a crate is pushed onto an enemy? (maybe squash them against the wall!)
What happens if a crate is pushed onto another crate (in Cocoa 2: 24 Hour Parsley People I have crates that can be stacked on top of each other)
Hopefully this building block mechanic will help you with your own game development and give you some idea for creating new, unique mechanics!
*UPDATE: I've changed the code above so that it now uses the UNDOSPRITEMOVE command rather than adding and subtracting from the Players X when a push fails, I think this is a better use of MPAGDs scripting language
