Open Surge Forum

A fun 2D retro platformer inspired by Sonic games and a game creation system

You are not logged in.

Announcement

Our community has moved to Discord! https://discord.gg/w8JqM7m

#1 2018-08-22 12:47:32

SynfigMaster91
Member
Registered: 2017-10-10
Posts: 144

Swimming Character script

Hi, guys! Here's a obj script to make your character swim underwater. Also, when your character touches an enemy, it gets hurt.

object ".player_companion"
{
    requires 0.2.0
    always_active

    state "main"
    {
        create_child ".player_spindash_controller"
        create_child ".player_nolightwalk_controller"
        create_child ".player_swim"
        destroy
    }
}


object ".player_spindash_controller"
{
    requires 0.2.0
    always_active

    state "main"
    {
        hide
        observe_player "player"
        change_state "stand by"
        weak_player
    }

    state "stand by"
    {
        on_player_duck "duck"
    }

    state "duck"
    {
        let "$p = 0"
        on_button_pressed "fire1" "charge"
        on_player_duck "duck"
        change_state "stand by"
    }

    state "charge"
    {
        set_player_animation "SD_player" "6"
        strong_player
        springfy_player // so that the camera won't go down, since the player is ducking
        disable_player_movement
        play_sample "charge"
        let "$p = min($p+2, 8)"
        change_state "hold"
    }

    state "hold"
    {
        //textout menu.small 0 0 $p
        set_player_animation "SD_player" "6"
        let "$p -= 0.234 * floor(8*$p) * dt()"

        // check if there's a platform underneath the player
        attach_to_player 0 20
        unless "obstacle_exists(0,0)" "cancel"

        // create cool particles
        let "$_pixelparticle_anim = 1 + random(3)"
        let "$_pixelparticle_xvel = -player_direction() * (120 + random(60))"
        let "$_pixelparticle_yvel = -60 - random(120)"
        create_child .pixelparticle -player_direction()*7 -2
        let "$_pixelparticle_anim = 1 + random(3)"
        let "$_pixelparticle_xvel = -player_direction() * (120 + random(60))"
        let "$_pixelparticle_yvel = -60 - random(120)"
        create_child .pixelparticle -player_direction()*7 -2
        let "$_pixelparticle_anim = 1 + random(3)"
        let "$_pixelparticle_xvel = -player_direction() * (120 + random(60))"
        let "$_pixelparticle_yvel = -60 - random(120)"
        create_child .pixelparticle -player_direction()*7 -2

        // check if the user wants to charge more, or if we can release the player
        on_button_pressed "fire1" "charge"
        on_button_down "down" "hold"
        change_state "release"
    }

    state "cancel"
    {
        enable_player_movement
        weak_player
        change_state "stand by"
    }

    state "release"
    {
        play_sample "release"
        enable_player_movement
        weak_player
        set_player_xspeed "(480 + 30 * floor($p)) * player_direction()"
        roll_player
        change_state "stand by"
    }
}

object ".player_nolightwalk_controller" // his lighting sneakers can't shine when in the air
{
    requires 0.2.0
    always_active

    state "main"
    {
        hide
        observe_player "player"
        change_state "stand by"
        weak_player
    }

    state "stand by"
    {
        on_player_in_the_air "air"
    }

    state "air"
    {
        on_player_walk "walking in the air"
        on_player_run "running in the air"
        on_player_brake "braking in the air"
        on_player_in_the_air "air"
        change_state "stand by"
    }

    state "walking in the air"
    {
        on_player_spring "stand by"
        on_player_jump "stand by"
        on_player_gethit "stand by"
        on_player_death "stand by"

        set_player_animation "SD_player" 18
        on_player_in_the_air "walking in the air"
        change_state "stand by"
    }

    state "running in the air"
    {
        on_player_spring "stand by"
        on_player_jump "stand by"
        on_player_gethit "stand by"
        on_player_death "stand by"

        set_player_animation "SD_player" 19
        on_player_in_the_air "running in the air"
        change_state "stand by"
    }

    state "braking in the air"
    {
        on_player_spring "stand by"
        on_player_jump "stand by"
        on_player_gethit "stand by"
        on_player_death "stand by"

        set_player_animation "SD_player" 20
        on_player_in_the_air "braking in the air"
        change_state "stand by"
    }
}

object ".player_swim"
{
    requires 0.2.0
    always_active

    state "main"
    {
        observe_player "player"
        hide
        change_state "out of water"
    }
    
    state "out of water"
    {
        on_player_underwater "dive"
    }
    
    state "dive"
    {
        on_button_pressed "fire6" "swimming"
    }

    state "swimming"
    {
        hide

        // got hit?
        on_player_gethit "getting hit underwater"

        // killed?
        on_player_death "destroy"

        // landed on the floor?
        attach_to_player 0 0
        if "obstacle_exists(0,32) and player_yspeed() > 0" "underwater but not swimming"

        // play the swimming animation
        springfy_player
        set_player_animation SD_PIXEL 0
        set_animation SD_player 21
        look_at_walking_direction
        show
        on_button_pressed "fire6" "swim"

        // got off water?
        on_player_underwater "swimming"
        hide
        change_state "out of water"
    }

    state "swim"
    {
        springfy_player
        attach_to_player 0 0
        set_player_animation SD_PIXEL 0
        set_player_yspeed -90
        change_state "swimming"
    }

    state "underwater but not swimming"
    {
        on_player_jump "dive"
        on_player_underwater "underwater but not swimming"
        change_state "out of water"
    }

    state "getting hit underwater"
    {
        // wait until the getting hit animation stops
        on_player_gethit "getting hit underwater"
        change_state "underwater but not swimming"
    }

    state "destroy"
    {
        destroy
    }

}

The script is released under Creative Commons Attribution 3.0 and you can modify it for whatever you guys want.

Made by me with help of Alexandre Martins and some other members of the community.

Offline

#2 2018-08-22 12:49:22

SynfigMaster91
Member
Registered: 2017-10-10
Posts: 144

Re: Swimming Character script

Also I forgot to mention... replace the 'player' name for the name of your character.

Also I recommend to make a backup copy of the pixel.png file in the images folder and changing the black pixel dot to magenta with some image manipulation program (like GIMP)

Offline

Board footer

Powered by FluxBB  hosted by tuxfamily.org