Skip to content

Commit

Permalink
Merge pull request #1688 from srinner/CATTY-548
Browse files Browse the repository at this point in the history
CATTY-548 Refactor SetLookByIndex- and SetBackgroundByIndexBrick
  • Loading branch information
m-herold authored Nov 23, 2021
2 parents cdb953d + a9945f7 commit 8829853
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

@objcMembers class SetBackgroundByIndexBrick: Brick, BrickProtocol, BrickFormulaProtocol {

var backgroundIndex: Formula?
var backgroundIndex: Formula

override required init() {
self.backgroundIndex = Formula(integer: 1)
super.init()
}

Expand All @@ -39,7 +40,7 @@
}

override func getRequiredResources() -> Int {
backgroundIndex?.getRequiredResources() ?? ResourceType.noResources.rawValue
backgroundIndex.getRequiredResources()
}

override func brickCell() -> BrickCellProtocol.Type! {
Expand All @@ -55,7 +56,7 @@
}

func getFormulas() -> [Formula]! {
[backgroundIndex!]
[backgroundIndex]
}

override func setDefaultValuesFor(_ spriteObject: SpriteObject!) {
Expand Down
5 changes: 3 additions & 2 deletions src/Catty/DataModel/Bricks/Look/SetLookByIndexBrick.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

@objcMembers class SetLookByIndexBrick: Brick, BrickProtocol, BrickFormulaProtocol {

var lookIndex: Formula?
var lookIndex: Formula

override required init() {
self.lookIndex = Formula(integer: kBackgroundObjects)
super.init()
}

Expand Down Expand Up @@ -55,7 +56,7 @@
}

func getFormulas() -> [Formula]! {
[lookIndex!]
[lookIndex]
}

override func setDefaultValuesFor(_ spriteObject: SpriteObject!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
let spriteNode = object.spriteNode
else { fatalError("This should never happen!") }
return {
guard let backgroundIndex = self.backgroundIndex else { return }
let index = formulaInterpreter.interpretInteger(backgroundIndex, for: object)
let index = formulaInterpreter.interpretInteger(self.backgroundIndex, for: object)
guard let look = spriteNode.look(for: index) else { return }
var image = imageCache.cachedImage(forPath: self.path(for: look))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ extension SetLookByIndexBrick: CBInstructionProtocol {

func actionBlock(_ formulaInterpreter: FormulaInterpreterProtocol) -> () -> Void {
guard let object = self.script?.object,
let spriteNode = object.spriteNode,
let formula = self.lookIndex
let spriteNode = object.spriteNode
else { fatalError("This should never happen!") }

return {
let lookIndex = formulaInterpreter.interpretInteger(formula, for: object)
let lookIndex = formulaInterpreter.interpretInteger(self.lookIndex, for: object)
guard let lookAtIndex = spriteNode.look(for: lookIndex) else { return }

let cache = RuntimeImageCache.shared()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension SetBackgroundByIndexBrick: CBXMLNodeProtocol {
func xmlElement(with context: CBXMLSerializerContext) -> GDataXMLElement? {
let brick = super.xmlElement(for: "SetBackgroundByIndexBrick", with: context)
let formulaList = GDataXMLElement(name: "formulaList", context: context)
let formula = self.backgroundIndex?.xmlElement(with: context)
let formula = self.backgroundIndex.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement(name: "category", stringValue: "BACKGROUND_INDEX", context: nil))
formulaList?.addChild(formula, context: context)
brick?.addChild(formulaList, context: context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ extension SetLookByIndexBrick: CBXMLNodeProtocol {
CBXMLParserHelper.validate(xmlElement, forNumberOfChildNodes: 1, andFormulaListWithTotalNumberOfFormulas: 1)
let formula = CBXMLParserHelper.formula(in: xmlElement, forCategoryName: "LOOK_INDEX", with: context)
let brick = self.init()
brick.lookIndex = formula
brick.setFormula(formula, forLineNumber: 0, andParameterNumber: 0)
return brick
}

func xmlElement(with context: CBXMLSerializerContext) -> GDataXMLElement? {
let brick = super.xmlElement(for: "SetLookByIndexBrick", with: context)
let formulaList = GDataXMLElement(name: "formulaList", context: context)
let formula = self.lookIndex?.xmlElement(with: context)
let formula = self.lookIndex.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement(name: "category", stringValue: "LOOK_INDEX", context: nil))
formulaList?.addChild(formula, context: context)
brick?.addChild(formulaList, context: context)
Expand Down

0 comments on commit 8829853

Please sign in to comment.