Simone Pistecchia
Simone Pistecchia

Reputation: 2840

Swift 2 Correct way to get level of any object game, protocol? struct? extension?

This is my first game, and I'm new on swift and sprite kit.

I must have a level for each class that needs get level. Like car lev1 car lev 2 etc. I have read about protocol extension etc, witch is the best way to approach level management?

I have tried to use LevelTraker as extension of this protocol:

protocol LevelTracker { 
     typealias    TypeUnit: TypeGame
     var         nameClass: String! {get set}           
     var     currentLevel : Int {get set} 
     mutating func levelIncreases()
}

but with extension, i must write 3 var each class that needs level. i try the same extension LevelTraker with struct LevelTraker:

func getClassName (theClass:AnyObject) -> String {
     let name = _stdlib_getDemangledTypeName(theClass); return name}

protocol TypeGame {}

enum transportType : TypeGame {
    case ground, sea, air
}

struct LevelTracker {
    var sender: AnyObject
    var TypeUnit: TypeGame

    private func getSaveFileWhitName() -> String {
        let saveWithName = getClassName(sender) + "." + String(TypeUnit)
        return saveWithName
    }

    var currentLevel : Int {
        get {
            let stringName = getSaveFileWhitName()
            let returnValue : Int = dataBase.read(stringName) as? Int ?? 1    //Check for first run of app, if = nil, set = 1
            return returnValue
        }
        set (newValue) {
            let stringName = getSaveFileWhitName()
            let level : Int = self.currentLevel
            let val = newValue
            if (newValue > level) {dataBase.write(val, key: stringName)}
        }
    }

    mutating func levelIncreases() {self.currentLevel++}
    ///SERVE SOLO PER SVILUPPO
    mutating func RESETLEVEL() {dataBase.write(1, key: getSaveFileWhitName())}

}

To use: (thanks @Krzak)

class car  {
    init () {
        let level = LevelTracker(sender: self, TypeUnit: transportType.ground).currentLevel
    }
}

But I don't want modify all init object that use level, and the super super class in common, some class don't have propriety level.

Upvotes: 0

Views: 227

Answers (3)

Krzak
Krzak

Reputation: 1461

The reason why you have compiler error is in your last line. You're missing the .ground

I'm not sure how you're thinking though that this will work, shouldn't it be var?

var level = LevelTracker(sender: self, TypeUnit: transportType.ground).currentLevel

Upvotes: 1

Simone Pistecchia
Simone Pistecchia

Reputation: 2840

I found a solution, I'm happy to have some comment.

protocol TypeGame {}

enum transportType : TypeGame {
    case car, bus, trak
}

protocol LevelTracker {
    var         nameClass: String! {get}
    var     currentLevel : Int {get set}
    mutating func levelIncreases()
}

extension LevelTracker {
    var currentLevel : Int {
        get {/*set to DB*/ return 1}
        set (newValue) {/*set to DB*/}
    }

    mutating func levelIncreases() {self.currentLevel++}}

A protocol only for transport object:

protocol Transport : LevelTracker {}

Ok, now my (simplified) class are:

class AllNode  {//SKSpriteNode
    init(){}
}

class TransportGame:AllNode, Transport {
    var nameClass : String! = "Transport"

    override init() {
        super.init()
        self.nameClass = nameClass + "." + getClassName(self)}
}

class Car : TransportGame {}
class miniCar : Car {}
class Bus: TransportGame {}
class Tree: AllNode {}

var carOne = Car()
let levelCar = carOne.currentLevel

var busOne = Bus()
let levelBue = busOne.currentLevel

var treeOne = Tree()
tree.currentLevel    //ERROR YUPPI!!!! :)

Now the tree class can't access to level! What do you think about this solution?

Upvotes: 0

Knight0fDragon
Knight0fDragon

Reputation: 16837

What I am reading it sounds like you are doing this:

class Level : AnyObject
{
   private func getSaveFileWhitName() -> String {
        let saveWithName = getClassName(sender) + "." + String(TypeUnit)
        return saveWithName
    }

    var currentLevel : Int {
        get {
            let stringName = getSaveFileWhitName()
            let returnValue : Int = dataBase.read(stringName) as? Int ?? 1    //Check for first run of app, if = nil, set = 1
            return returnValue
        }
        set (newValue) {
            let stringName = getSaveFileWhitName()
            let level : Int = self.currentLevel
            let val = newValue
            if (newValue > level) {dataBase.write(val, key: stringName)}
        }
    }

    mutating func levelIncreases() {self.currentLevel++}
    ///SERVE SOLO PER SVILUPPO
    mutating func RESETLEVEL() {dataBase.write(1, key: getSaveFileWhitName())}

}

class car : Level
{
    init () {
        let level = self.currentLevel
    }
}

Upvotes: 0

Related Questions