Skip to content

Instantly share code, notes, and snippets.

@pmark
Last active March 2, 2023 16:39
Show Gist options
  • Save pmark/6878d57423a8c8f4253762ea464d0dd3 to your computer and use it in GitHub Desktop.
Save pmark/6878d57423a8c8f4253762ea464d0dd3 to your computer and use it in GitHub Desktop.
DSL for game world and level declaration
class GameWorld {
var systems: [System] = []
var entities: [Entity] = []
var terrain: Terrain?
var level: Level?
var player: Player?
// Add a system to the game world
func addSystem(_ system: System) {
systems.append(system)
}
// Add an entity to the game world
func addEntity(_ entity: Entity) {
entities.append(entity)
}
// Set the terrain for the game world
func setTerrain(_ terrain: Terrain) {
self.terrain = terrain
}
// Set the level for the game world
func setLevel(_ level: Level) {
self.level = level
}
// Set the player for the game world
func setPlayer(_ player: Player) {
self.player = player
}
// Start the game world and all its systems
func start() {
systems.forEach { $0.start() }
}
// Stop the game world and all its systems
func stop() {
systems.forEach { $0.stop() }
}
}
// Define the game world
let gameWorld = GameWorld()
// Define the game level
let level = GameLevel()
// Define the terrain for the level
level.terrain = Terrain()
.withSize(width: 100, height: 50)
.withTexture(imageNamed: "grass_texture")
.withPhysics(enabled: true)
// Define the enemy spawning system for the level
level.enemySpawner = EnemySpawner()
.withEnemyType(type: .basicEnemy)
.withSpawnRate(rate: 2.0)
.withMaxEnemies(max: 10)
// Define the player character for the level
level.player = PlayerCharacter()
.withTexture(imageNamed: "player_texture")
.withHealth(max: 100)
.withSpeed(value: 5.0)
// Define the collectibles for the level
level.collectibles = Collectibles()
.withCollectibleType(type: .coin)
.withSpawnRate(rate: 1.0)
// Add the level to the game world
gameWorld.add(level: level)
// Define the physics system for the game world
gameWorld.physicsSystem = PhysicsSystem()
.withGravity(value: 9.8)
// Define the animation system for the game world
gameWorld.animationSystem = AnimationSystem()
// Define the input system for the game world
gameWorld.inputSystem = InputSystem()
.withSwipeGesture(direction: .up)
.withSwipeGesture(direction: .down)
.withSwipeGesture(direction: .left)
.withSwipeGesture(direction: .right)
.withTapGesture()
// Start the game
gameWorld.start()
import SceneKit
struct Level {
var terrain: Terrain
var player: Player
}
struct Terrain {
var node: SCNNode
}
struct Player {
var node: SCNNode
var speed: Float
var jumpHeight: Float
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment