Old version
Home Up Event Sequence Simple Example Ramps curved surfaces Old version

 

A new approach to Gamemaker Platform games

Platform games are popular, and there seems to be no shortage of platform "engines", however I was unable to locate one with the exact feature set I wanted. Also having found an engine it is usually impractical to drop it into a game as it requires special actions in several events.

What is needed is a "Move_Platform" command that would automatically make an object fall until it landed on something solid. 

To achieve this I propose adding a script to the "End Step" event, after collisions but just before drawing takes place.

The code will replace the standard behaviour with "move_contact" actions that will make the object free fall normally, "land" on solid objects and further to this it will follow ramps of up to 45 degrees up or down.

It will be up to the developer to add keyboard actions, set a gravity, friction, select sprites etc. As the keyboard handling is separated from the engine it is easy to create "monsters" under computer control.

Also the code requires a flag called "jump" which should be set in the create event

//Roll back movement
//This is necessary as GM may or may not have carried out "Solid" behaviour
x=xprevious
y=yprevious
//Check if we're on the ground
if not (jump or place_free(x,y+1))
{
  //We're on the ground so check we haven't just walked off a cliff
  if not place_free(x+hspeed,y+abs(hspeed)+1)
  {
      //Search for the ground level
      x+=hspeed;
      y+=abs(hspeed)   
      move_outside_solid(90,1+2*abs(hspeed))  
      //Roughly set vspeed for the benefit of any AIs that may use it
      vspeed=(y-yprevious)
      //Check if we hit a wall.
      if not place_free(x,y)
      {
          x=xprevious
          y=yprevious
          if hspeed<>0
          {
            move_contact_solid(180*(hspeed<0),abs(hspeed))
          }  
          hspeed=0
          vspeed=0
      }
  }
  else
  {
    //Lookout!! Cliff!!
    vspeed=0
    //Just move out horizontally
    move_contact_solid(180*(hspeed<0),abs(hspeed))
    jump=true
  }  
}
else
{
    //Freefall code
    //Move horizontally
    if hspeed<>0
    {
      move_contact_solid(180*(hspeed<0),abs(hspeed))
      if not (place_free(x+sign(hspeed),y) or place_free(x+sign(hspeed),y-2))
      {
        hspeed=0
      }
    }
    //Move vertically
    if vspeed<>0
    {
      move_contact_solid(90+180*(vspeed>0),abs(vspeed))
      if not place_free(x,y+sign(vspeed))
      {
        if vspeed>0
        {
          jump=0
        }
        vspeed=0
      }
    }
}

It will be seen that this code "glues" the player to the platform at times when possibly they ought to free-fall. I find the effect can be pleasing though.

No extra effort is required to run uphill (yet).

Most of the free-fall code could be skipped when there is no collision.

The "jump" variable may not be necessary. It does help to "unstick" the player from the platform.

Also at this time there is no support for special (non-solid) platforms. More to follow...

Demo: 

ramp_platform.gmd

Platform design continued...