A very simplistic state machine system for Swift while mainly used with Raylib on Swift

Overview

Seuwichi 스위치 - Plain simple State Machine.

About

A very simplistic state machine system for Swift while mainly used with Raylib on Swift.

Seuwichi allows you to easily create, manage and transition states at ease without any complexities. It was created with ease of usability in mind.

Seuwichi is a Korean word for "Switch" (for example: light switch.). The reason for the name is rather simple, a switch allows us to change from a one state to another.

Instructions

Setting up Seuwichi is very easy and using it is even easier!

Make sure you added Seuwichi as your dependency package and imported to your file!

.package(url: "https://github.com/conifer-dev/Seuwichi.git", .branch("main"))
import Seuwichi

Firstly, our state machine has to be initialised! Once initialised, the state machine is set to a void state that represents an empty state that will be no longer part of the state machine upon changing the state.

var myStateMachine = StateMachine() // Initialisation of our state machine

Now we have to create a new state and add it to our state machine. It's very easy to do!

class MenuState: State {
    func update() { 
        // handle your update here... 
    }
    
    func render() {
        // handle your render here... 
    }

    func onEnter() {
        // any functionalities/initialisation you want to happen when you enter/switch to the state...
    }

    func onExit() {
        // any functionalities/initialisation you want to happen when you exit/switch from the state...
    }
}

class GameState: State {
    func update() { 
        // handle your update here... 
    }
    
    func render() {
        // handle your render here... 
    }

    func onEnter() {
        // any functionalities/initialisation you want to happen when you enter/switch to the state...
    }

    func onExit() {
        // any functionalities/initialisation you want to happen when you exit/switch from the state...
    }
}

Here we have two newly created (and empty for example purposes) states using the "State" protocol, every state has to adopt the State protocol! Now we have to add them to our state machine!

myStateManager.insertState(gameState(), withID: "gameState") // Adding our newly created state to our state machine. All states are stored within a dictionary.
myStateManager.insertState(menuState(), withID: "menuState")

Now that our state has been successfully added, we're ready to change our states! To change our state all we need to do is pass through a .changeState() function to our state machine.

myStateMachine.changeState(id: "gameStart") // .changeState() function takes in a single parameter "id" that looks into our dictionary of states previously added.
// Beware that attempting to change to a state that does not exist will result in an error!

And you're done! You've successfully created a state machine, added a state to the machine and changed the state! It's as simple as that. Down below we will explain thorougly each function (and those not mentioned above) and what they do.

State Machine Functions

Function Example Description
.insertState(_ state: State, withID id: String) myStateMachine.changeState(id: "gameStart") Adds a newly created state to our state machine.
.removeState(id: String) myStateMachine.removeState("menuState") Removes the passed state ID from our state machine.
.clearStates() myStateMachine.clearStates() Removes all entries of States previously added to our state machine.
.changeState(id: String) myStateMachine.changeState(id: "gameState") Changing to a passed through game state (ID). This will automatically call states own .onEnter() function. Passing through nonexistent ID will result in an error! Make sure you added your state to the state machine prior to changing.

State Functions

Function Description
.update() All updates within the state are called here e.g. player movement.
.render() All rendering within the state are called here e.g. player sprite or a menu state.
.onEnter() onEnter function is called in on every state change, therefore its useful to use onEnter to initialise your code or set your animation to reflect what animation state your entity will be in.
.onExit() The opposite of onEnter(). This function will be also called during the change of states

Closing notes

This state machine is very simple, and I hope that many new programmers/game devs can look at the code and understand how to create one themselves, but also use it if they feel like it. This was mainly created for my own purposes as I re-create one of my Love2D games in Raylib on Swift.. Please feel free to send PRs on how to improve Seuwichi and perhaps how to make it even more Swifty.

Creating this state machine taught me a little bit more about protocols which was also the objective of this project.

You might also like...
LIDAR Wearable Navigational System for the Visually Impaired - 👁️
LIDAR Wearable Navigational System for the Visually Impaired - 👁️

A Novel Implementation of LiDAR Mesh Classification and Image Classifiers In Assistive Technology for the Visually Impaired. Background Information 25

Ulangi is a language flashcards app with spaced repetition system and more.

Ulangi Open-source language learning tools Introduction Ulangi makes it easy for you to manage and create flash cards to learn languages. It comes wit

A macOS menu bar app that enables system-wide navigation functionality for side buttons on third-party mice.
A macOS menu bar app that enables system-wide navigation functionality for side buttons on third-party mice.

SaneSideButtons macOS mostly ignores the M4/M5 mouse buttons, commonly used for navigation. Third-party apps can bind them to ⌘+[ and ⌘+], but this on

Gett's Design System code generator. Use Zeplin Styleguides as your R&D's Single Source of Truth.
Gett's Design System code generator. Use Zeplin Styleguides as your R&D's Single Source of Truth.

Prism is a Design System code generator developed by the team at Gett 🚕 . Synchronizing design teams with engineering teams is a huge challenge. As t

A Swift playground that comes pre-loaded with Plot, that can be used to explore the new component API.

PlotPlayground A Swift playground that comes pre-loaded with Plot, so that you can quickly try out the library and its new, SwiftUI-like API for build

ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects.
ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects.

ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects. The kit provides variety of customisations inorder to design a confetti which matches your project's UI. ConfettiKit makes your work of adding Confetti on your project with just one line of code.

A zero-code template app that demonstrates how to use TheraForge's APIs and can be used for fast prototyping
A zero-code template app that demonstrates how to use TheraForge's APIs and can be used for fast prototyping

TheraForge MagicBox 1.0.0-beta The Open TheraForge (OTF) MagicBox app is a template for creating digital health solutions that help people better mana

Alfred Workflow to be used with Wooshy, that brings any macOS windows to the foreground

Wooshy: Window to the Foreground! Switch apps with Alfred. Switch app windows with Wooshy: Window to the Foreground! ScreenFlow.mp4 Why Aren't you ann

A Swift package for encoding and decoding Swift Symbol Graph files.
A Swift package for encoding and decoding Swift Symbol Graph files.

SymbolKit The specification and reference model for the Symbol Graph File Format. A Symbol Graph models a module, also known in various programming la

Owner
Conifer Coniferoslav
Just another wannabe game-dev. Self-taught newbie
Conifer Coniferoslav
A ARM macOS Virtual Machine, using macOS 12's new Virtualization framework.

macOS Virtual Machine A ARM macOS Virtual Machine, using macOS 12's new Virtualization framework. I copied KhaosT's code from here, all I did is chang

Ming Chang 127 Nov 30, 2022
Multiple Arcade Machine Emulator for iOS, iPadOS, tvOS, macOS (Catalyst)

MAME4iOS Original Author: David Valdeita (Seleuco) This is a port of MAME for iOS, iPadOS, tvOS and macOS. MAME4iOS is designed to run for modern iOS

null 438 Jan 4, 2023
macOS Virtual Machine using Virtualization.framework

virtualOS Run a virtual macOS machine on your Apple Silicon computer. On first start, the latest macOS restore image is automatically downloaded from

null 102 Dec 24, 2022
Running GUI Linux in a virtual machine on a Mac

Running GUI Linux in a virtual machine on a Mac Install and run GUI Linux in a virtual machine using the Virtualization framework. การ build และใช้งาน

MrChoke 3 Jul 12, 2022
A compiler with very basic capabilities written in Swift

BasicCompiler A compiler with very basic capabilities written in Swift. This project is not intended for real-world use; it's implemented just for fun

null 2 Aug 23, 2022
React Native Todo List example app which uses Redux for managing app state

react-native-redux-todo-list A sample todo list app developed by using React Native and Redux. How to run the app Install react-native If you don't ha

null 43 Oct 11, 2022
Swift JSExport extensions for macOS system frameworks

framework-bridge Swift JSExport extensions for macOS system frameworks. Progress Foundation AppKit Usage Swift import JavaScriptCore import FrameworkB

John Susek 0 Dec 13, 2021
Snapify is an opensource tweak to make the messages app behave like Snapchat's sending system

Snapify A simple tweak to remove the send button in messages, replaced by keyboard key What is Snapify? Snapify is an opensource tweak to make the mes

Hallie 4 Oct 29, 2021
A repository to experiment around the use and generation of tokens for the JLL/T Design System

Basic Style Dictionary This example code is bare-bones to show you what this framework can do. If you have the style-dictionary module installed globa

null 0 Dec 7, 2021
A longstanding annoying bug in our beloved operating system macOS

Houston There is a longstanding annoying bug in our beloved operating system macOS. If you open an application while Mission Control is active, the ap

Christoffer Winterkvist 6 Dec 5, 2022