Skip to content

Instantly share code, notes, and snippets.

@elliotfiske
Created March 22, 2017 05:35
Show Gist options
  • Save elliotfiske/b3fb26a2c3fbef336d7270bbd7978665 to your computer and use it in GitHub Desktop.
Save elliotfiske/b3fb26a2c3fbef336d7270bbd7978665 to your computer and use it in GitHub Desktop.
Demonstrates serialization of monster body parts
//
// MCPart+NSCoding.swift
// MonsterMe
//
// Created by Elliot Fiske on 3/15/15.
// Copyright (c) 2015 Monster Create. All rights reserved.
//
// Implements NSCoding for MCParts - lets us convert an MCPart object to an NSData object and back again
import Foundation
import SpriteKit
extension MCPart {
override func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.uuid, forKey: "uuid")
aCoder.encodeObject(self.partType.rawValue, forKey: "type")
aCoder.encodeCGPoint(self.position, forKey: "position")
aCoder.encodeCGPoint(self.anchorPoint, forKey: "anchor")
aCoder.encodeFloat(Float(self.userAngle), forKey: "userAngle")
aCoder.encodeFloat(Float(self.orbitalAngle), forKey: "orbitalAngle")
aCoder.encodeFloat(Float(self.scaleFactor), forKey: "scaleFactor")
aCoder.encodeBool(self.hidden, forKey: "hidden")
aCoder.encodeObject(self.savedColor, forKey: "color")
aCoder.encodeInteger(self.colorable.rawValue, forKey: "colorable")
aCoder.encodeObject(self.textureName, forKey: "textureName")
aCoder.encodeBool(self.flipped, forKey: "flipped")
if mirroredPart != nil {
aCoder.encodeObject(self.mirroredPart!.uuid, forKey: "mirroredUuid")
}
if let leg = self as? MCPartLeg {
aCoder.encodeCGPoint(leg.baseAnchor, forKey: "baseAnchor")
}
}
/**
* Encode this part to an NSDictionary that we can convert
* to JSON.
*/
func encodeWithJson(partDict: NSMutableDictionary) {
partDict["uuid"] = self.uuid.UUIDString
partDict["type"] = self.partType.rawValue
partDict["x"] = self.position.x
partDict["y"] = self.position.y
partDict["anchorX"] = self.anchorPoint.x
partDict["anchorY"] = self.anchorPoint.y
partDict["userAngle"] = self.userAngle
partDict["orbitalAngle"] = self.orbitalAngle
partDict["scaleFactor"] = self.scaleFactor
partDict["hidden"] = self.hidden
encodeColor(partDict)
partDict["colorable"] = self.colorable.rawValue
partDict["textureName"] = self.textureName
partDict["flipped"] = self.flipped
if mirroredPart != nil {
partDict["mirroredUuid"] = self.mirroredPart!.uuid.UUIDString
}
if let leg = self as? MCPartLeg {
partDict["baseAnchorX"] = leg.baseAnchor.x
partDict["baseAnchorY"] = leg.baseAnchor.y
}
}
/**
* Nab color comps and stick em in the dictionary
*/
func encodeColor(partDict: NSMutableDictionary) {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0 // unused, since always 1
self.color.getRed(&r, green: &g, blue: &b, alpha: &a)
partDict["colorR"] = r
partDict["colorG"] = g
partDict["colorB"] = b
}
/**
* Extract those sweet sweet CGFloats and put them back into the coder
*/
class func decodeColor(json: [String : JSON]) -> UIColor {
var r = json["colorR"]!.cgFloatValue
var g = json["colorG"]!.cgFloatValue
var b = json["colorB"]!.cgFloatValue
var color = UIColor(red: r, green: g, blue: b, alpha: 1.0)
return color
}
/**
* Convert a dictionary of part info to the sweet, sweet part itself
*/
convenience init?(dict: [String : JSON]) {
let anchor = CGPoint(x: dict["anchorX"]!.cgFloatValue, y: dict["anchorY"]!.cgFloatValue)
let texName = dict["textureName"]!.stringValue
self.init(textureName: texName, anchor: anchor)
let newUUID: NSUUID = NSUUID(UUIDString: dict["uuid"]!.stringValue)!
uuid = newUUID
userAngle = dict["userAngle"]!.cgFloatValue
orbitalAngle = dict["orbitalAngle"]!.cgFloatValue
scaleFactor = dict["scaleFactor"]!.cgFloatValue
savedColor = MCPart.decodeColor(dict)
let rawColorable = dict["colorable"]!.intValue
colorable = PartColorableType(rawValue: rawColorable)!
hidden = dict["hidden"]!.boolValue
let pos = CGPoint(x: dict["x"]!.cgFloatValue, y: dict["y"]!.cgFloatValue)
position = pos
updateRotation()
updateColorBlendFactor()
if let eye = self as? MCPartEye {
eye.pupilNode.color = savedColor
eye.colorBlendFactor = 0
}
else {
color = savedColor
}
flipped = dict["flipped"]!.boolValue
scalePart(scaleFactor)
zPosition = self.partType.defaultZPos()
if let mirrUUIDString = dict["mirroredUuid"]?.string {
mirroredUuid = NSUUID(UUIDString: mirrUUIDString)!
}
if dict["partType"]!.stringValue == "leg" {
let baseAnchor = CGPoint(x: dict["baseAnchorX"]!.cgFloatValue, y: dict["baseAnchorY"]!.cgFloatValue)
aCoder.encodeCGPoint(baseAnchor, forKey: "baseAnchor")
}
if dict["partType"]!.stringValue == "eye" {
aCoder.encodeObject(dict["pupilTextureName"]!.stringValue, forKey: "pupilTextureName")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment