• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[HOW TO] State Machine

C

Claudiu

Guest
Hi to all! I want to know how to start to implement finite state machine in my game. Until now i done some codes on work very well but i know will be hard if i continue like that and will be better to organize in scripts for each state. For now i have : idle, walk, run, jump, fall and landing animation. i will like if someone can help how to make script from my events. Thanks and those are my codes.


Code:
///Initialize Variables
grav = 3.5;
hsp = 0;
vsp = 0;
spd = 0;
facing = 0;
doubletap = 0;
walkspeed = 8;
runspeed = 24;
jumpspeed = 48;
land_timer = 0;

Code:
///Control the Player

//Get the player input
var key_right = keyboard_check(vk_right) || gamepad_button_check(0, gp_padr) > 0;
var key_right_pressed = keyboard_check_pressed(vk_right) || gamepad_button_check_pressed(0, gp_padr) > 0;
var key_left = keyboard_check(vk_left) || gamepad_button_check(0, gp_padl) > 0;
var key_left_pressed = keyboard_check_pressed(vk_left) || gamepad_button_check_pressed(0, gp_padl) > 0;
var key_jump = keyboard_check_pressed(vk_space) || gamepad_button_check_pressed(0, gp_face1);
var key_jump_held = keyboard_check(vk_space) || gamepad_button_check(0, gp_face1);
var key_block = keyboard_check(ord("Q")) || gamepad_button_check(0, gp_shoulderlb) > 0;
var key_block_init = keyboard_check_pressed(ord("Q")) || gamepad_button_check_pressed(0, gp_shoulderlb) > 0;
var key_block_end = keyboard_check_released(ord("Q")) || gamepad_button_check_released(0, gp_shoulderlb) >0;

//Horizontal Movement
hsp = 0;

doubletap--;

if key_right && !key_left {
    if key_right_pressed {
        if doubletap > 0 and place_meeting(x,y+1,par_wall)
            spd = runspeed;
        else
            spd = walkspeed;
        doubletap = 10;
    }
    if place_free(x + walkspeed, y) {
        facing = 0;
        hsp = spd;
    }
}

if key_left && !key_right {
    if key_left_pressed {
        if doubletap > 0 and place_meeting(x,y+1,par_wall)
            spd = runspeed;
        else
            spd = walkspeed;
        doubletap = 10;
    }
    if place_free(x - walkspeed, y) {
        facing = 1;
        hsp = -spd;
    }
}

if key_left && key_right
    spd = walkspeed;

//Vertical Movement
if vsp < 48
    vsp += grav;

if place_meeting(x, y + 1, par_wall)
    vsp = key_jump * -jumpspeed

if !key_jump_held
    vsp = max(vsp, 0);

if land_timer > 0 {
    land_timer--;
    hsp = 0;
}

//Horizontal and Slopes Collision
if place_meeting(x + hsp, y, par_wall) {
    yplus = 0;
    while (place_meeting(x+hsp,y-yplus,par_wall) && yplus <= abs(1*hsp)) yplus +=1;
    if place_meeting(x+hsp,y-yplus,par_wall) {
    while !place_meeting(x + sign(hsp), y, par_wall)
        x += sign(hsp);
    hsp = 0;
}
    else{
        y -= yplus;
    }
}
x += hsp;
if !place_meeting(x,y,par_wall) && vsp >= 0 && place_meeting(x,y+2+abs(hsp),par_wall) {
    while(!place_meeting(x,y+1,par_wall)) {
    y += 1;
    }
}

//Vertical and Slopes Collision
if place_meeting(x, y + vsp, par_wall) {
    while !place_meeting(x, y + sign(vsp), par_wall)
        y += sign(vsp);
    vsp = 0;
}
y += vsp;


Code:
///Animation

//Right Animation
if facing == 0 { // He facing to right
    if place_meeting(x, y + 1, par_wall) {
        switch sprite_index {
        case spr_alucard_fall_r:
            sprite_index = spr_alucard_fall_end_r; // He landing to right
            exit;
        case spr_alucard_fall_end_r:
            land_timer = 2;
            if image_index < image_number - 1
                exit;
        }
        if hsp == 0 { // He relax to right
            if sprite_index != spr_alucard_relax_r
                image_index = 0;
            sprite_index = spr_alucard_relax_r;
        } else if hsp == 8 { // He walk to right
            if sprite_index != spr_alucard_walk_r
                image_index = 0;
            sprite_index = spr_alucard_walk_r;
        } else {
            if hsp > 8 && sprite_index != spr_alucard_run_r // He run to right
                image_index = 0;
            sprite_index = spr_alucard_run_r;
        }
        image_speed = 1;
    }
    if !place_meeting(x, y + 1, par_wall) {
        if vsp < 0 { // He jump to the right
            if sprite_index != spr_alucard_jump_r
                image_index = 0;
             sprite_index = spr_alucard_jump_r;
             image_speed = 1;
        }
        if vsp > 0 { // He fall to the right
            if sprite_index != spr_alucard_fall_r
                image_index = 0;
            sprite_index = spr_alucard_fall_r;
            image_speed = 1;
        }
    }
}

//Left Animation
if facing == 1 { // He facing to left
    if place_meeting(x, y + 1, par_wall) {
        switch sprite_index {
        case spr_alucard_fall_l:
            sprite_index = spr_alucard_fall_end_l; //He landing to left
            exit;
        case spr_alucard_fall_end_l:
            land_timer = 2;
            if image_index < image_number - 1
                exit;
        }
        if hsp == 0 { // He relax to left
            if sprite_index != spr_alucard_relax_l
                image_index = 0;
            sprite_index = spr_alucard_relax_l;
        } else if hsp == -8 { // He walk to left
            if sprite_index != spr_alucard_walk_l
                image_index = 0;
            sprite_index = spr_alucard_walk_l;
        } else {
            if hsp > -8 && sprite_index != spr_alucard_run_l // He run to left
                image_index = 0;
            sprite_index = spr_alucard_run_l;
        }
        image_speed = 1;
    }
    if !place_meeting(x, y + 1, par_wall) {
        if vsp < 0 { // He jump to the left
            if sprite_index != spr_alucard_jump_l
                image_index = 0;
            sprite_index = spr_alucard_jump_l;
            image_speed = 1;
        }
        if vsp > 0 { // He fall to the left
            if sprite_index != spr_alucard_fall_l
                image_index = 0;
            sprite_index = spr_alucard_fall_l;
            image_speed = 1;
        }
    }
}
 
C

Claudiu

Guest
Thanks! But i think you do complex staff. For now i will be glad if i can transform my animation for in to state : idle, walk, run, jump, fall, land. If you can give me a help hand i will appreciate it.
 
D

Docker

Guest
I use a state machine in my Lemmings video. You can watch it here:

Someone in the YYG staff is the reason for GTA and Lemmings existing... learn something new every day! Will give these videos a watch when I'm not swamped with uni work.
 
C

Claudiu

Guest
My game is a platformer with rpg elements and sprites are for right and left to look more real" looks like a 2.5 D models. If someone will make scripts from my codes I will know what to do forward I just only need a guide first.
 
Top