OctopusKit uses an “Entity-Component-System”Ā architecture, where:
-
š¬ A game is organized intoĀ StatesĀ such asĀ MainMenu,Ā PlayingĀ andĀ Paused. Each state is associated with aĀ SwiftUIĀ view which displays the user interface, and aĀ SpriteKit SceneĀ that presents the gameplay for that state usingĀ Entities,Ā ComponentsĀ andĀ Systems.
You can divide your game into as many or as few states as you want. e.g. A single “PlayState” which also handles the main menu, pausing, cutscenes etc.
States, Scenes, and SwiftUI views may have many-to-many relationships that may change during runtime.
-
š¾Ā EntitiesĀ are simply collections ofĀ Components. They contain no logic, except for convenience constructors which initialize groups of related components.
-
š§©Ā ComponentsĀ (which could also be called Behaviors, Effects, Features, or Traits) are the core concept in OctopusKit, containing the properties as well as the logic* which make up each visual or abstract element of the game. A component runs its code when it’s added to an entity, when a frame is updated, and/or when it’s removed from an entity. Components may query their entity for other components and affect each other’s behavior to form dynamic dependencies during runtime. The engine comes with a library of customizable components for graphics, gameplay, physics etc.
-
āĀ SystemsĀ are simply collections of componentsĀ of a specific class.Ā They don’t perform any logic*, but they’re arranged by aĀ SceneĀ in an array to execute components from all entities in a deterministic order every frame, so that components which rely on other components are updated after their dependencies.
*Ā These definitions may differ from other engines, like Unity, where all the logic is contained within systems.
-
šĀ User InterfaceĀ elements like buttons, lists and HUDs are designed inĀ SwiftUI. This allows fluid animations, sharp text, vector shapes, live previews, automatic data-driven updates, and over 1,500 high-quality icons from Apple’sĀ SF Symbols.
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 5000 sprites on an iPhone XS 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
Tailored for Swift: Swift, Swift, Swift! The framework must follow theĀ established guidelinesĀ for Swift API design. Everything must make sense within Swift and flow seamlessly with Swift idioms as much as possible.
-
Vitamin 2D: At the moment, OK is primarily a framework for 2D games, but it does not prevent you from using technologies like SceneKit or low-level Metal views, and it may be used for non-game apps.
-
Shoulders of Ettins: The engine leverages SpriteKit, GameplayKit, SwiftUI and other technologies provided by Apple. It should not try to “fight” them, replace them, or hide them behind too many abstractions.
OK is mostly implemented through custom subclasses and extensions of SpriteKit and GameplayKit classes, without “obscuring” them or blocking you from interacting with the base classes. This allows you to adopt this framework incrementally, and lets you integrate your game with the Xcode IDE tools such as the Scene Editor where possible.
The tight coupling with Apple APIs also ensures that your game is future-proof; whenever Apple improves these frameworks, OctopusKit and your games should also get some benefits “for free.” For example, when Metal was introduced, SpriteKit was updated to automatically use Metal instead of OpenGL under the hood, giving many existing games a performance boost.Ā (WWDC 2016, Session 610)
-
Code Comes First: OK is primarily a “programmatical” engine; almost everything is done in code. This also helps with source control. The Xcode Scene Editor is relegated to “second-class citizen” status because of its incompleteness and bugs (as of May 2018, Xcode 9.4), but it is supported wherever convenient. See the next point.
š” You can design high-level layouts/mockups in the Scene Editor, using placeholder nodes with names (identifiers.) You may then create entities from those nodes and add components to them in code.
Now with SwiftUI, programming for Apple platforms is heading towards a focus on code instead of visual editors anyway.
-
Customizability & Flexibility: The engine strives to be flexible and gives you the freedom to structure your game in various ways. Since you have full access to the engine’s source code, you can modify or extend anything to suit the exact needs of each project.
You can use any of the following approaches to building your scenes, in order of engine support:
- Perform the creation and placement of nodes mostly in code. Use the Xcode Scene Editor infrequently, to design and preview a few individual elements such as entities with specific positions etc., not entire scenes, and useĀ
SKReferenceNode
Ā to load them in code.
- Use the Xcode Scene Editor as your starting point, to create template scenes that may be loaded as top-levelĀ
SKReferenceNode
Ā instances of anĀOKScene
. This approach allows a modicum of “WYSIWYG” visual design and previewing.
- Create a scene almost entirely in the Xcode Scene Editor, adding any supported components, actions, physics bodies, navigation graphs and textures etc. right in the IDE.
Set the custom class of the scene asĀOKScene
Ā or a subclass of it. Load the scene by callingĀOKViewController.loadAndPresentScene(fileNamed:withTransition:)
, e.g. during theĀdidEnter.from(_:)
Ā event of anĀOKGameState
.
- You don’tĀ haveĀ to use any of the architectures and patterns suggested here; you don’t have to use game states, and your game objects don’t even have to inherit from any OK classes. You could use your own architecture, and just use OK for a few helper methods etc., keeping only what you need from this framework and excluding the rest from compilation.
- Perform the creation and placement of nodes mostly in code. Use the Xcode Scene Editor infrequently, to design and preview a few individual elements such as entities with specific positions etc., not entire scenes, and useĀ
-
Self-Containment: You should not need to download or keep up with any other third-party libraries if your own project does not require them; everything that OK uses is within OK or Apple frameworks, so it comes fully usable out-of-the-box.
Getting Started
Read the QuickStartĀ andĀ Usage Guide.Ā You will need Xcode 12, iOS 14 and macOS Big Sur (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.
-
For a detailed overview of the engine’s architecture, seeĀ Architecture.
-
Stuck? SeeĀ Tips & Troubleshooting.
-
Wondering whether something was intentionally done the way it is, or why?Ā Coding Conventions & Design DecisionsĀ may have an explanation.
-
Want to keep tabs on what’s coming or help out with the development of missing features? See theĀ TODO & Roadmap.
Etcetera
Contributors & Supporters ā¤ļø
-
This project may be referred to as OctopusKit, “OK” or “OKIO” (for “OctopusKit by Invading Octopus”) but “IOOK” sounds weird.
-
The naming is a combination of inspiration from companies like Rogue Amoeba, the .io domain, and the animeĀ Shinryaku! Ika Musume.
-
The space before the lastĀ
])
Ā in the Examples section is for clarity. š -
License:Ā Apache 2.0
-
Incorporates shaders fromĀ ShaderKitĀ Ā© Paul Hudson, licensed under MIT License (see headers in relevant files).
-
TellĀ me how awesome or terrible everything is:Ā Discord,Ā TwitterĀ or š ¾ctopusš ŗit@š nvadingš ctopus.āā
I rarely check these though, so the best way to ask a question may be via opening an issue on the GitHub repository.
-
SupportĀ my decadent lifestyle so I can focus on making unsaleable stuff:Ā My Patreon
-
This project is not affiliated in any way with Apple.
OctopusKitĀ Ā© 2023Ā Invading OctopusĀ ā¢Ā Apache License 2.0