Coder Social home page Coder Social logo

boyquotes / 2d-state-machine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bastianosoriol/2d-state-machine

0.0 0.0 0.0 11 KB

State Machine Plugin for 2d games in godot. It comes with the implementation of 4-way movement.

GDScript 100.00%

2d-state-machine's Introduction

2d-state-machine

State Machine Plugin for 2d games in godot. It comes with the implementation of 4-way movement.

To use this plugin correctly, the following steps must be followed:

1.- You must create the script of the character node to which you want to implement the state machine, like the next example.

player_script

2.- Verify that you have the plugin enabled. (Project/Projects Settings/Plugins)

config_plugin

3.- Add StateMachinePlayer node to your character.

add_state_machine_player

Note: If you want to create a second state machine different from the first one, you must create a different node with its respective script where to define the new states.

4.- Add the states as child nodes to StateMachinePlayer node and for each one there must be a script created. Each of the state node scripts must be extended by state

add_child_nodes

5.- In the script that is included with the StateMachinePlayer node, you must define the states that your character will have and what the initial state will be.

define_state_machine

6.- You only have to edit the scripts of the child nodes with the "enter" and "physics" functions.

extends state

var name_state

func enter(): ----> THIS FUNCTION IS EXECUTED WHEN YOU ENTER THE STATE
	name_state = self.get_name()
	
func physics(delta): ----> THIS FUNCTION IS IN CHARGE OF MAKING THE CORRESPONDING VALIDATIONS TO CHANGE FROM ONE STATE TO ANOTHER
	get_direction()
	move()
  
  direction_validation(name_state)

Below an example of the state called "Right"

state_child_script

Plugin Functions

The following functions can be used to construct the states.:

  • "get_direction()" : This function gives you the orientation in which the character will move, depending on the key that is pressed.
  • "move()" : This function gives the speed defined in the player script to the character.
  • "walk_animation(texture, is_flip_h)" : This function is used to run an animation, the first argument it receives is the character texture for that state. And the second argument is optional, which is a boolean to flip the sprite, which has a false value by default.
  • "stop_walk()" : It's used to stop the sprite and leave it in its first frame.
  • "direction_validation(state_name)" : This function validates changes in the direction of the character while it is in a state, in this way the changes of state are made. The only parameter that this function receives is the name of the node of the state that needs to be validated.

2d-state-machine's People

Contributors

bastianosoriol avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.