OctopusKit OctopusKit Logo

A 2D game engine in 100% Swift for iOS, macOS and tvOS.

OctopusKit wraps and extends Apple’s frameworks:
GameplayKit’s flexible Entity-Component-System architecture lets you dynamically compose game behavior.
SpriteKit gives you full access to textures, physics and GPU shaders.
SwiftUI’s declarative syntax lets you quickly design fluid, scalable HUDs.
Metal under the hood ensures the best native performance.
• OS-agnostic components let you compile natively for iOS and macOS and handle mouse + touch input with the same code, without needing Catalyst etc.

QuickStart Demo

  1. Examples
  2. Overview
  3. Design Goals
  4. Getting Started
  5. Etcetera

OctopusKit is a constant work in progress and my first ever open-source project. I’m still learning as I go, so it may change rapidly without maintaining backwards compatibility or updating the documentation.

This project is a result of trying to make my own games as a hobby. I fell in love with Swift but couldn’t find any engines that supported it or had the kind of architecture that I wanted to work with, so I started making my own.

Any advice on how to improve the API, coding style, git workflow, or open-source best-practices, would be appreciated! – ShinryakuTako


🚀 Eager to dive in? Add OctopusKit as a Swift Package Manager dependency to a SwiftUI project, and use the QuickStart template (which also serves as a little demo.)

🎨 Using with SwiftUI

import SwiftUI
import OctopusKit
struct ContentView: View {

    // The coordinator object manages your game's scenes and global state, 
    // and can be observed and controlled from SwiftUI.
    var gameCoordinator = OKGameCoordinator(states: [
        Gameplay() ])
    var body: some View {
            .statusBar(hidden: true)

👾 Creating an animated sprite

var character = OKEntity(components: [
    // Start with a blank texture.
    SpriteKitComponent(node: SKSpriteNode(color: .clear, size: CGSize(widthAndHeight: 42))),
    // Load texture resources.
    TextureDictionaryComponent(atlasName: "PlayerCharacter"),
    // Animate the sprite with textures whose names begin with the specified prefix.
    TextureAnimationComponent(initialAnimationTexturePrefix: "Idle") ])

🕹 Adding player control

// Add a component to the scene that will be updated with input events.
// Other components that handle player input will query this component.

// This lets us handle asynchronous events in sync with the frame-update cycle.
// A shared event stream is more efficient than forwarding events to every entity.

// PointerEventComponent is an OS-agnostic component for touch or mouse input.

let sharedPointerEventComponent = PointerEventComponent()

    // A relay component adds a reference to a component from another entity,
    // and also fulfills the dependencies of other components in this entity.
    RelayComponent(for: sharedPointerEventComponent),
    // This component checks the entity's PointerEventComponent (provided here by a relay)
    // and syncs the entity's position to the touch or mouse location in every frame.
    PointerControlledPositioningComponent() ])

🕹 Dynamically removing player control or changing to a different input method

character.removeComponent(ofType: PointerControlledPositioningComponent.self)

    // Add a physics body to the sprite.
    RelayComponent(for: sharedKeyboardEventComponent),
    // Apply a force to the sprite's body based on keyboard input in every frame.
    KeyboardControlledForceComponent() ])

🧩 A custom game-specific component

class AngryEnemyComponent: OKComponent {
    override func didAddToEntity(withNode node: SKNode) {
        node.colorTint = .angryMonster
    override func update(deltaTime seconds: TimeInterval) {
        guard let behaviorComponent = coComponent(EnemyBehaviorComponent.self) else { return }
    override func willRemoveFromEntity(withNode node: SKNode) {
        node.colorTint = .mildlyInconveniencedMonster

🛠 Using a custom closure to change the animation based on player movement

// Add a component that executes the supplied closure every frame.
character.addComponent(RepeatingClosureComponent { component in
    // Check if the entity of this component has the required dependencies at runtime.
    // This approach allows dynamic behavior modification instead of halting the game.
    if  let physicsBody = component.coComponent(PhysicsComponent.self)?.physicsBody,
        let animationComponent = component.coComponent(TextureAnimationComponent.self)
        // Change the animation depending on whether the body is stationary or mobile.
        animationComponent.textureDictionaryPrefix = physicsBody.isResting ? "Idle" : "Moving"

// This behavior could be better encapsulated in a custom component,
// with many different game-specific animations depending on many conditions.

🎎 Loading a scene built in the Xcode Scene Editor and creating multiple entities from sprites identified by a shared name

// Load a ".sks" file as a child node.

if  let editorScene = SKReferenceNode(fileNamed: "EditorScene.sks") {

// Search the entire tree for all nodes named "Turret",
// and give them properties of "tower defense" turrets,
// and make them independently draggable by the player.

for turretNode in scene["//Turret"] {

    // Create a new entity for each node found.
    scene.addEntity(OKEntity(components: [
        SpriteKitComponent(node: turretNode),
        RelayComponent(for: sharedPointerEventComponent),
        // Hypothetical game-specific components.
        // Track the first touch or mouse drag that begins inside the sprite.
        // Let the player select and drag a specific sprite.
        // This differs from the PointerControlledPositioningComponent in a previous example, 
        // which repositions nodes regardless of where the pointer began.
        PointerControlledDraggingComponent() ]))

// Once the first monster wave starts, you could replace PointerControlledDraggingComponent 
// with PointerControlledShootingComponent to make the turrets immovable but manually-fired.


OctopusKit uses an “Entity-Component-System” architecture, where:

See the Architecture documentation for a detailed breakdown of the object hierarchy.

Your primary workflow will be writing component classes for each “part” of the graphics and gameplay, then combining them to build entities which appear onscreen, or abstract entities that handle data on the “backend.”

e.g. say a ParallaxBackgroundEntity containing a CloudsComponent, a HillsComponent and a TreesComponent, or a GameSessionEntity containing a WorldMapComponent and a MultiplayerSyncComponent.

Performance: Although extensive benchmarks have not been done yet, OK can display over 3000 sprites on an iPhone X at 60 frames per second; each sprite represented by an entity with multiple components being updated every frame, and responding to touch input.

Design Goals

Getting Started

  1. Read the QuickStart and Usage Guide. You will need Xcode 11, iOS 13 and macOS Catalina (though OK may work on older versions with some manual modifications.)

    Skill Level: Intermediate: Although OK is not presented in a form designed for absolute beginners, mostly because I’m too lazy to write documentation from step zero, it’s not “advanced” level stuff either; if you’ve read the Swift Language Book and have attempted to make a SpriteKit game in Xcode, you are ready to use OK!

    You should also read about the “Composition over inheritance” and “Entity–component–system” patterns if you’re not already familiar with those concepts, although OK’s implementation of these may be different than what you expect.

    Also see Apple’s tutorials for SwiftUI.

  2. For a detailed overview of the engine’s architecture, see Architecture.

  3. Stuck? See Tips & Troubleshooting.

  4. Wondering whether something was intentionally done the way it is, or why? Coding Conventions & Design Decisions may have an explanation.

  5. Want to keep tabs on what’s coming or help out with the development of missing features? See the TODO & Roadmap.


OctopusKit © 2019 Invading OctopusApache License 2.0