Esto surgió de una petición en el foro de MPAGD y, como ya había creado la mecánica en Cocoa and the Time Machine y la había ampliado y mejorado en 24 Hour Parsley People, no fue muy difícil crear una versión simplificada que puedes usar como pieza básica y luego adaptar y ampliar para tus juegos de MPAGD.
Nuestro objetivo es crear una caja que el jugador pueda empujar a izquierda y derecha, y sobre la que pueda saltar para llegar a plataformas más altas.
Además, la haremos sujeta a la gravedad, de modo que si el jugador la empuja fuera de una plataforma, caerá a la plataforma de abajo.
En MPAGD estándar hay un script de sprite empujable, pero no es realmente adecuado para un juego de plataformas en 2D; originalmente lo usé como punto de partida, pero al final lo reescribí y lo recorté (¡intentaba ahorrar tantos bytes como fuera posible!).
Bien, esto es lo que deberías obtener...
Bien, lo primero que vamos a necesitar son un par de variables que guarden las coordenadas X e Y del jugador; en mi demo uso V & W, así que en el script de tu jugador añade las siguientes líneas:
LET V = X LET W = Y
Ahora crea un evento para tu caja: aquí se hará todo el trabajo. Añade el siguiente código (sustituye V & W si ya tienes otras variables para los valores X e Y de tu jugador)
;======================================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
ENDIFEsta es una versión muy simplificada de la que usé en mis juegos, pero es la mecánica básica; evidentemente puedes adaptarla a tu juego. Cosas con las que puedes ampliarla:
¿Qué debería pasar si un enemigo choca con una caja?
¿Qué debería pasar si se empuja una caja contra un enemigo? (¡quizá aplastarlo contra la pared!)
¿Qué pasa si se empuja una caja contra otra caja? (en Cocoa 2: 24 Hour Parsley People tengo cajas que se pueden apilar unas sobre otras)
Ojalá esta mecánica básica te ayude con tu propio desarrollo de juegos y te dé ideas para crear mecánicas nuevas y únicas.
*ACTUALIZACIÓN: he cambiado el código de arriba para que ahora use el comando UNDOSPRITEMOVE en lugar de sumar y restar a la X del jugador cuando un empujón falla; creo que así se aprovecha mejor el lenguaje de scripts de MPAGD
