June 7, 2014

Hello, Swift

I’ve finished reimplementing the framework and a couple of the basic systems using Swift. I’m replacing my renderer, physics, and collisions with Sprite Kit, but I’m taking pains to make sure that I don’t break the entity component system paradigm. Between that and the quirks I’ve found in a very beta programming language, it has been difficult, but rewarding.

First sprite in the Swift engine.

First sprite in the Swift engine.

I have Sprite Kit working for rendering right now using a LGSprite component wrapping an SKSpriteNode. I’ve created the LGPosition as a component that maps x and y coordinates to the SKSpriteNode‘s position property. That way, although the engine itself will depend on Sprite Kit right now, games written on top of it won’t know the difference. Systems can reference the position of the entity without caring whether the position is part of a SKNode or not.

Here’s the main code for the example shown in the screenshot:

self.add(
	LGSpriteSystem(scene: self),
	LGPositionSystem()
)

let player = LGEntity()
player.put(
	LGPosition(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)),
	LGSprite(spriteSheet: LGSpriteSheet(texture: SKTexture(imageNamed: "Player"), rows: 1, cols: 9)),
	LGNode(sknode: SKSpriteNode())
)

self.add(player)

I’ve open sourced the current iteration of the engine here on GitHub. My goal is to get it to the point the Objective-C version of the engine was at before explaining it in detail. Please check it out and let me know what you think! I’d love to have other developers contribute to this project.

Posted by Luke Godfrey

Luke Godfrey is a graduate student at the University of Arkansas, pursuing a PhD in Computer Science. He has created applications on a number of platforms, and is especially interested in mobile and web development.

View more posts from this author

Leave a Reply

Your email address will not be published. Required fields are marked *