Git Product home page Git Product logo

fsm's Introduction

fsm

Table of Contents

  1. License
  2. Introduction
  3. Usage
  4. [Documentation] (#documentation)

License

Copyright © 2013 Arturo Vergara

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.

Introduction

fsm provides a simple finite state machine intended for game development.

To install: $ go get -u github.com/ArturoVM/fsm

Usage

To use fsm, you first need to declare your own constant state types. For example:

// states.go

package main

import "github.com/ArturoVM/fsm"

const (
    StateOne fsm.StateType = iota
    StateTwo
    StateThree
    StateQuit
)

After that, it's only a matter of writing your own states conforming to the fsm.GameState interface (see the documentation for more information) and initializing a package-wide *fsm.StateMachine instance, and you're good to go:

// main.go

package main

import "github.com/ArturoVM/fsm"

type SomeState struct {
   stateType fsm.StateType
}

func (s *SomeState) Init() {
   s.stateType = StateOne
}

func (s *SomeState) Update() {
    // do some complicated logic
}

func (s *SomeState) Draw() {
    // do some drawing
}

func (s *SomeState) Type() fsm.StateType {
    return s.stateType
}

// Game loop

var FSM *fsm.StateMachine // package-wide StateMachine instance

func m_init() {
    FSM = new(fsm.StateMachine)
    var someState SomeState
    FSM.Init(&someState)
}

func update() {
    FSM.Update() // Crucial! See documentation.
    FSM.CurrentState().Update()
}

func draw() {
    FSM.CurrentState().Draw()
}

func main() {
    m_init()
    for FSM.CurrentState().Type() != StateQuit {
        update()
        draw()
    }
}

Documentation

You can find detailed documentation for this package, here

fsm's People

Stargazers

 avatar

Watchers

 avatar

Forkers

pablomelo

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.