Platformer Engine

Instructions: - arrow keys and WASD to move\n- spikes and lava hurt\n- don't get crushed by moving platforms\n\n- 900 views, pretty cool

@Stage\n\n@blank\n\nwhen flag clicked\nhide\n\ngo to [front v] layer\nshow\n\n@Player\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\nwhen flag clicked\nshow variable [coins v]\nshow variable [scene # v]\nhide list [collected v]\nset [acceleration v] to [1.5]\nset [resistance v] to [0.8]\nset [gravity v] to [-1.5]\nset [jump force v] to [14]\nset [jump duration v] to [6]\nbroadcast (Setup v) and wait\nReset and Begin Level\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\nControls - Up and Down\nControls - Left and Right\nMove - in steps (([abs v] of (speed x) ) + ([abs v] of (speed y) ))\nSet Costume\n\ndefine Controls - Up and Down\nif <<key (up arrow v) pressed?> or <key (w v) pressed?>> then\n    if <<(wall slide) > [0]> and <(jumping) = [0]>> then\n        set [jumping v] to [1]\n        set [falling v] to [3]\n        set [speed y v] to (JUMP FORCE)\n        turn right (180) degrees\n        set [speed x v] to (((direction) / (90)) * (7))\n        set [long jump v] to [12]\n    end\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)\nif <(wall slide) > [0]> then\n    Check Can Wall Slide\n    if <(speed y) < [0]> then\n        set [speed y v] to ((speed y) * (0.6))\n    end\nend\n\ndefine Controls - Left and Right\nif <(long jump) > [0]> then\n    change [long jump v] by (-1)\n    stop [this script v]\nend\nset [key x v] to (<<key (d v) pressed?> or <key (right arrow v) pressed?>> - <<key (a v) pressed?> or <key (left arrow v) pressed?>>)\nif <(KEY X) = [0]> then\n    if <<(falling) > [2]> and <(wall slide) < [0]>> then\n        set [speed x v] to ((speed x) * (0.98))\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 x v] to [0]\nset [speed y v] to [0]\nset [falling v] to [99]\nset [long jump v] to [0]\nset [wall slide v] to [0]\nset [jumping v] to [0]\npoint in direction (90)\nBegin scene # [1] go to x, y: [-123] [30]\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: [] [-160]\n    Fix Collision in Direction [90]\nend\nif <(y position) < [-180]> then\n    Begin scene # ((SCENE #) - (100)) go to x, y: [] [180]\nend\nif <<touching (dangerlava v)?> or <touching (dangerspikes 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 (1024)\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 v2 v)\nset rotation style [don't rotate v]\nset [platform dx v] to [0]\nset [platform dy v] to [0]\n\ndefine Set Costume\nset rotation style [left-right v]\nif <(wall slide) > [0]> then\n    switch costume to (hangon01 v)\n    stop [this script v]\nend\nif <(falling) > [1]> then\n    if <(speed y) > [0]> then\n        switch costume to (jump01 v)\n    else\n        switch costume to (run_j2 v)\n    end\n    stop [this script v]\nend\nif <(frame) > [0]> then\n    switch costume to ((1) + ([floor v] of ((frame) mod (16)) ))\nelse\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)\nend\n\ndefine Collide X - Slope or Wall\nCheck Can Wall Slide\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\ndefine Check Can Wall Slide\nif <(falling) < [2]> then\n    set [wall slide v] to [0]\n    stop [this script v]\nend\nswitch costume to (hitbox wallslide v)\nset rotation style [left-right v]\nCheck Touching Solid\nset [wall slide v] to (touching)\nswitch costume to (hitbox v2 v)\nset rotation style [don't rotate v]\n\nwhen I receive [lose life v]\nstop [other scripts in sprite v]\nstart sound [Oops v]\nrepeat (10)\n    change [ghost v] effect by (10)\nend\nwait (1.5) seconds\nReset and Begin Level\n\nswitch backdrop to <touching (level v)?>\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 (0)\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\ngo to [front v] layer\n\n@Level\n\nwhen flag clicked\nshow\ngo to x: (0) y: (0)\ngo to [back v] layer\n\nwhen I receive [change scene v]\nswitch costume to (join [Scene] (SCENE #))\nerase all\nStamp Outline\nStamp Shadow\n\ndefine Stamp Shadow\nset [brightness v] effect to (-100)\nset [ghost v] effect to (90)\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 (-100)\nrepeat (8)\n    move (3) steps\n    stamp\n    move (-3) steps\n    turn right (45) degrees\nend\nclear graphic effects\n\n@DangerSpikes\n\ndefine Stamp Outline\nset rotation style [don't rotate v]\nset [brightness v] effect to (-100)\nrepeat (8)\n    move (3) steps\n    stamp\n    move (-3) steps\n    turn right (45) degrees\nend\nclear graphic effects\n\ndefine Stamp Shadow\nset [brightness v] effect to (-100)\nset [ghost v] effect to (90)\nchange y by (-20)\nstamp\nchange y by (20)\nclear graphic effects\n\nwhen flag clicked\nshow\ngo to x: (0) y: (0)\ngo to [back v] layer\n\nwhen I receive [change scene v]\nswitch costume to (blank v)\nswitch costume to (join [Scene] (SCENE #))\nStamp Outline\nStamp Shadow\n\n@DangerLava\n\nwhen flag clicked\nshow\ngo to x: (0) y: (0)\ngo to [back v] layer\n\nwhen I receive [change scene v]\nswitch costume to (blank v)\nswitch costume to (join [Scene] (SCENE #))\nforever\n    set y to ((3) * ([sin v] of ((timer) * (180)) ))\nend\n\n@Platform\n\ndefine Stamp Shadow\nset [brightness v] effect to (-100)\nset [ghost v] effect to (90)\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 (-100)\nrepeat (8)\n    move (3) steps\n    stamp\n    move (-3) steps\n    turn right (45) degrees\nend\nclear graphic effects\n\nwhen I receive [change scene v]\nhide\ngo to [back v] layer\nif <(SCENE #) = [105]> then\n    Door at [-1] [65] key [Key 1]\nend\n\ndefine Animate Scene 1\ngo to x: (151) y: (-102)\nforever\n    glide (2) secs to x: (151) y: (24)\n    wait (1) seconds\n    glide (2) secs to x: (151) y: (-102)\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 (player v)?> then\n    set [platform dx v] to ((new x) - (last x))\n    set [platform dy v] to ((new y) - (last y))\nend\ngo to x: (new x) y: (new y)\n\ndefine Animate Scene 3\ngo to x: (-98) y: (78)\nforever\n    glide (2) secs to x: (-98) y: (150)\n    wait (1) seconds\n    glide (2) secs to x: (-98) y: (78)\n    wait (1) seconds\nend\n\ndefine Animate Scene 102\ngo to x: (88) y: (-51)\nforever\n    glide (2) secs to x: (-103) y: (-51)\n    wait (1) seconds\n    glide (2) secs to x: (88) y: (-51)\n    wait (1) seconds\nend\n\ndefine Animate Scene 2\ngo to x: (122) y: (-102)\nforever\n    glide (2) secs to x: (-125) y: (24)\n    wait (1) seconds\n    glide (2) secs to x: (122) y: (-102)\n    wait (1) seconds\nend\n\ndefine Animate Scene 105\ngo to x: (134) y: (13)\nforever\n    glide (2) secs to x: (-134) y: (13)\n    wait (1) seconds\n    glide (2) secs to x: (134) y: (13)\n    wait (1) seconds\nend\n\ndefine Door at (x) (y) key (key name)\nshow\nswitch costume to (key name)\ngo to x: (x) y: (y)\nforever\n    wait until <touching (player v)?>\n    if <[collected v] contains (key name)?> then\n        repeat (24)\n            change y by (5)\n        end\n        start sound [Crunch2 v]\n        wait (1) seconds\n        repeat (24)\n            change y by (-5)\n        end\n        start sound [Crunch v]\n    else\n        start sound [High Conga v]\n    end\n    wait until <not <touching (player v)?>>\nend\n\n@Collectibles\n\nwhen I receive [change scene v]\nif <(SCENE #) = (my scene)> then\n    show\nelse\n    hide\nend\n\nwhen I start as a clone\nrepeat until <touching (player v)?>\n    change y by ((0.5) * ([sin v] of (frame) ))\n    change [frame v] by (5)\nend\nset [my scene v] to []\nif <(costume [name v]) = [coin]> then\n    start sound [Coin v]\n    change [coins v] by (1)\nelse\n    start sound [Collect v]\n    add (costume [name v]) to [collected v]\nend\nrepeat (20)\n    change [ghost v] effect by (5)\n    change y by (2)\nend\n\nwhen I receive [setup v]\ndelete all of [collected v]\nset [frame v] to [0]\nhide\nPlace [coin] scene [1] xy [149] [-63]\nPlace [coin] scene [101] xy [64] [54]\nPlace [coin] scene [102] xy [-7] [10]\nPlace [Key 1] scene [2] xy [9] [-3]\nPlace [coin] scene [103] xy [139] [51]\nPlace [coin] scene [103] xy [90] [51]\nPlace [coin] scene [3] xy [-152] [154]\nPlace [coin] scene [104] xy [-74] [-66]\nPlace [coin] scene [104] xy [-5] [-32]\nPlace [coin] scene [104] xy [88] [7]\nPlace [coin] scene [105] xy [205] [67]\nPlace [coin] scene [106] xy [-99] [124]\n\ndefine Place (costume name) scene (scene) xy (x) (y)\nset [coins v] to [0]\nset [my scene v] to (scene)\nswitch costume to (costume name)\ngo to x: (x) y: (y)\ncreate clone of (_myself_ v)\nset [my scene v] to []\n\n