Platformer Engine

Instructions: Here's griffpatch's platformer engine that I use for my Pokemon Metroidvanias! Feel free to use it in your projects!\n\nScizor's here too. Yay :D

@Stage\n\nwhen flag clicked\nswitch backdrop to (false v)\n\n@Scizor\n\nwhen flag clicked\ndelete all of [attack list v]\nadd [Slash] to [attack list v]\npoint in direction (90)\nset [gravity v] to [-1.4]\nset [jump force v] to [12]\nset [acceleration v] to [1.4]\nset [resistance v] to [0.85]\nset [jump duration v] to [6]\nset [attack v] to [1]\nReset and Begin Level\n\ndefine Move - In steps (steps)\nchange [falling v] by (1)\nrepeat (steps)\n    set [last value v] to (x position)\n    change x by ((speed x) / (steps))\n    Check Touching Solid\n    if <(touching) > [0]> then\n        Collide X - Slope or Wall?\n    end\n    set [last value v] to (y position)\n    change y by ((speed y) / (steps))\n    Check Touching Solid\n    if <(touching) > [0]> then\n        Collide Y - Ceiling or Floor?\n    end\nend\n\ndefine Check Touching Solid\nif <<touching (level v)?> or <touching (platform v)?>> then\n    set [touching v] to [1]\nelse\n    set [touching v] to [0]\nend\n\nwhen I receive [tick - player v]\nMoved - by Moving Platform\nswitch backdrop to <touching (level v)?>\nControls - Up and Down\nControls - Left and Right\nMove - In steps (([abs v] of (speed y) ) + ([abs v] of (speed x) ))\nSet Costume\n\ndefine Controls - Up and Down\nif <key (up arrow v) pressed?> then\n    if <<(falling) < [3]> and <(jumping) = [0]>> then\n        set [jumping v] to [1]\n        set [falling v] to [3]\n    end\n    if <<(jumping) > [0]> and <(jumping) < (Jump Duration)>> then\n        change [jumping v] by (1)\n        set [speed y v] to (Jump Force)\n    end\nelse\n    set [jumping v] to [0]\nend\nchange [speed y v] by (Gravity)\n\ndefine Controls - Left and Right\nset [key x v] to (<key (right arrow v) pressed?> - <key (left arrow v) pressed?>)\nif <(Key X) = [0]> then\n    if <(falling) > [2]> then\n        set [speed x v] to ((speed x) * (0.95))\n        stop [this script v]\n    end\n    if <([abs v] of (speed x) ) < [1]> then\n        set [frame v] to [0]\n        set [speed x v] to [0]\n    else\n        change [frame v] by (0.5)\n    end\nelse\n    change [speed x v] by ((Key X) * (Acceleration))\n    point in direction ((Key X) * (90))\n    change [frame v] by (1)\nend\nset [speed x v] to ((speed x) * (Resistance))\n\ndefine Reset and Begin Level\nset [speed y v] to [0]\nset [speed x v] to [0]\nset [falling v] to [99]\nset [jumping v] to [0]\npoint in direction (90)\nBegin Scene# [1] go to x, y: [-150] [-82]\n\nwhen I receive [game loop v]\nset [ghost v] effect to (0)\nforever\n    broadcast (Tick - First v)\n    broadcast (Tick - Platforms v)\n    broadcast (tick - player v)\n    broadcast (Tick - Last v)\nend\n\nwhen I receive [tick - last v]\nif <(x position) > [235]> then\n    Begin Scene# ((Scene #) + (1)) go to x, y: [-235] []\nend\nif <(x position) < [-235]> then\n    Begin Scene# ((Scene #) + (-1)) go to x, y: [235] []\nend\nif <(y position) > [180]> then\n    Begin Scene# ((Scene #) + (100)) go to x, y: [] [-180]\nend\nif <(y position) < [-180]> then\n    Begin Scene# ((Scene #) + (-100)) go to x, y: [] [180]\nend\nif <touching (danger v)?> then\n    broadcast (Lose Life v)\nend\n\ndefine Begin Scene# (scene#) go to x, y: (x) (y)\nset [scene # v] to (scene#)\nif <(x) > []> then\n    set x to (x)\nend\nif <(y) > []> then\n    set y to (y)\nend\nbroadcast (Change Scene v)\nstop [other scripts in sprite v]\nwait (0) seconds\nswitch costume to (hitbox v)\nset rotation style [don't rotate v]\nif <(x) > []> then\n    Fix Collision in Direction [0]\nelse\n    Fix Collision in Direction [90]\nend\nSet Costume\nbroadcast (game loop v)\n\ndefine Fix Collision in Direction (dir)\nset [temp v] to (direction)\nset [distance v] to [1]\npoint in direction (dir)\nrepeat (128)\n    Check Touching Solid\n    if <(touching) < [1]> then\n        point in direction (temp)\n        stop [this script v]\n    end\n    move (distance) steps\n    turn right (180) degrees\n    change [distance v] by (1)\nend\n\nwhen I receive [tick - first v]\nswitch costume to (hitbox v)\nset rotation style [don't rotate v]\nset [platform dx v] to []\nset [platform dy v] to []\n\ndefine Set Costume\nset rotation style [left-right v]\nif <(falling) > [1]> then\n    if <(speed y) > [0]> then\n        switch costume to (jump v)\n    else\n        switch costume to (fall v)\n    end\n    stop [this script v]\nend\nif <(frame) > [0]> then\n    switch costume to ((12) + ([floor v] of ((frame) mod (4)) ))\nelse\n    if <key (x v) pressed?> then\n        if <(Slash) = [0]> then\n            broadcast (Slash v)\n        end\n        switch costume to (slash 1 v)\n    else\n        switch costume to (hit angle 0 v)\n        repeat (5)\n            Check Touching Solid\n            if <(touching) < [1]> then\n                switch costume to ((costume [number v]) + (5))\n                stop [this script v]\n            end\n            next costume\n        end\n        switch costume to (stand v)\n    end\nend\n\nforever\n    change [frame v] by (0.25)\n    Set Costume\n    wait (0.1) seconds\nend\n\ndefine Collide X - Slope or Wall?\nchange y by (1)\nCheck Touching Solid\nif <(touching) > [0]> then\n    change y by (1)\n    Check Touching Solid\n    if <(touching) > [0]> then\n        change y by (-2)\n        set x to (last value)\n        set [speed x v] to [0]\n        stop [this script v]\n    end\n    set [speed x v] to ((speed x) * (0.8))\nend\nset [speed x v] to ((speed x) * (0.95))\nSlip\n\ndefine Collide Y - Ceiling or Floor?\nset y to (last value)\nif <(speed y) > [0]> then\n    set [speed y v] to [0]\n    stop [this script v]\nend\nif <(falling) > [0]> then\n    set [falling v] to [0]\n    Slip\nend\nset [speed y v] to ((speed y) * (0.8))\n\ndefine Slip\nchange y by (-2)\nchange x by (1)\nCheck Touching Solid\nif <(touching) < [1]> then\n    set [falling v] to [9]\n    change [speed x v] by (1)\n    stop [this script v]\nend\nchange x by (-2)\nCheck Touching Solid\nif <(touching) < [1]> then\n    set [falling v] to [9]\n    change [speed x v] by (-1)\n    stop [this script v]\nend\nchange y by (2)\nchange x by (1)\n\nwhen I receive [lose life v]\nstop [other scripts in sprite v]\nswitch costume to (lose v)\nrepeat (10)\n    change [ghost v] effect by (10)\nend\nwait (0.5) seconds\nReset and Begin Level\n\ndefine Moved - by Moving Platform\nchange x by (Platform DX)\nchange y by (Platform DY)\nCheck Touching Solid\nif <(touching) > [0]> then\n    Find Closest Space to (x position) (y position) Max [8]\n    if <(touching) > [0]> then\n        broadcast (Lose Life v)\n    end\nend\n\ndefine Find Closest Space to (x) (y) Max (max)\nset [temp v] to (direction)\nset [distance v] to [1]\npoint in direction (dir)\nrepeat (max)\n    repeat (16)\n        go to x: (x) y: (y)\n        move (distance) steps\n        Check Touching Solid\n        if <(touching) < [1]> then\n            point in direction (temp)\n            stop [this script v]\n        end\n        turn right (22.5) degrees\n    end\n    change [distance v] by (1)\nend\ngo to x: (x) y: (y)\npoint in direction (temp)\n\nif <(speed x) = [0]> then\n    set [frame v] to [0]\n    switch costume to (stand v)\nend\n\nset [color v] effect to (40)\n\n@Level\n\nwhen flag clicked\ngo to x: (0) y: (0)\nshow\nerase all\nStamp Shadow\nStamp Outline\n\nwhen I receive [change scene v]\nswitch costume to (join [Scene] (Scene #))\nerase all\nStamp Shadow\nStamp Outline\n\ndefine Stamp Shadow\nset [brightness v] effect to (-100)\nset [ghost v] effect to (85)\nchange y by (-20)\nstamp\nchange y by (20)\nclear graphic effects\n\ndefine Stamp Outline\nset rotation style [don't rotate v]\nset [brightness v] effect to (-45)\nrepeat (8)\n    move (3) steps\n    stamp\n    move (-3) steps\n    turn right (45) degrees\nend\nclear graphic effects\n\n@DANGER\n\nwhen flag clicked\ngo to x: (0) y: (0)\nshow\n\nwhen I receive [change scene v]\nswitch costume to (blank v)\nswitch costume to (join [Scene] (Scene #))\n\n@Water\n\nwhen flag clicked\ngo to x: (0) y: (0)\nhide\n\nwhen I receive [change scene v]\nshow\nswitch costume to (blank v)\nswitch costume to (join [Scene] (Scene #))\nif <(Scene #) = [102]> then\n    forever\n        set y to ((5) * ([sin v] of ((timer) * (180)) ))\n    end\nend\n\n@Platform\n\nwhen I receive [change scene v]\nhide\nif <(Scene #) = [1]> then\n    show\n    Animate Platform\nend\n\ndefine Animate Platform\ngo to x: (51) y: (-83)\nforever\n    glide (2) secs to x: (51) y: (132)\n    wait (1) seconds\n    glide (2) secs to x: (51) y: (-83)\n    wait (1) seconds\nend\n\ngo to x: (109) y: (-6)\nforever\n    glide (2) secs to x: (-59) y: (-6)\n    wait (1) seconds\n    glide (2) secs to x: (109) y: (-6)\n    wait (1) seconds\nend\n\nwhen I receive [tick - platforms v]\nTick (x position) (y position)\nset [last x v] to (x position)\nset [last y v] to (y position)\n\ndefine Tick (new x) (new y)\nif <(new y) > (last y)> then\n    change y by (1)\nelse\n    set y to ((last y) + (1))\nend\nif <touching (scizor v)?> then\n    set [platform dx v] to ((new x) - (last x))\n    set [platform dy v] to ((new y) - (last y))\nelse\nend\ngo to x: (new x) y: (new y)\n\n