2015-02-06 55 views
-1

RWGameData.h
#import <Foundation/Foundation.h> 

@class RWGameData; 
@protocol RWGameStateProtocol <NSObject> 
-(void)StateUpdateForGameData:(RWGameData*)data; 
@end 

@interface RWGameData : NSObject <NSCoding> 

@property (weak) id<RWGameStateProtocol> delegate; 

@property (assign, nonatomic) long regularBubbleCount; 
@property (assign, nonatomic) long premiumBubbleCount; 

@property (assign, nonatomic) long megaBubbleUpgradeTier; 
@property (assign, nonatomic) long bubbleFactoryUpgradeTier; 
@property (assign, nonatomic) long bubblersUpgradeTier; 
@property (assign, nonatomic) long mysteryBubbleUpgradeTier; 
@property (assign, nonatomic) long bubbleBankUpgradeTier; 

@property (assign, nonatomic) int megaBubblePopValue; 
@property (assign, nonatomic) int bubbleFactoryTickValue; 

@property (assign, nonatomic) long bubbleBankCapacity; 

@property (assign, nonatomic) int collectionBallsQuantity; 
@property (assign, nonatomic) int collectionGlowsticksQuantity; 
@property (assign, nonatomic) int collectionFlowersQuantity; 
@property (assign, nonatomic) int collectionStuffedAnimalsQuantity; 
@property (assign, nonatomic) int collectionEasterEggsQuantity; 

@property (assign, nonatomic) int currentXP; 
@property (assign, nonatomic) int targetXP; 
@property (assign, nonatomic) int level; 

@property (assign, nonatomic) BOOL dataIsInitialized; 

+(instancetype)sharedGameData; 
-(void)reset; 
-(void)save; 

-(void)timerSetup; 
-(void)timerCalled; 

@end 

RWGameData.m
#import "RWGameData.h" 

@implementation RWGameData 

static NSString* const SSGameDataRegularBubbleCountKey = @"regularBubbleCount"; 
static NSString* const SSGameDataPremiumBubbleCountKey = @"premiumBubbleCount"; 

static NSString* const SSGameDataMegaBubbleUpgradeTierKey = @"megaBubbleUpgradeTier"; 
static NSString* const SSGameDataBubbleFactoryUpgradeTierKey = @"bubbleFactoryUpgradeTier"; 
static NSString* const SSGameDataBubblersUpgradeTierKey = @"bubblersUpgradeTier"; 
static NSString* const SSGameDataMysteryBubbleUpgradeTierKey = @"mysteryBubbleUpgradeTier"; 
static NSString* const SSGameDataBubbleBankUpgradeTierKey = @"bubbleBankUpgradeTier"; 

static NSString* const SSGameDataMegaBubblePopValueKey = @"megaBubblePopValueKey"; 
static NSString* const SSGameDataBubbleFactoryTickValueKey = @"bubbleFactoryTickValueKey"; 

static NSString* const SSGameDataBubbleBankCapacityKey = @"bubbleBankCapacityKey"; 

static NSString* const SSGameDataCollectionBallsQuantityKey = @"collectionBallsQuantityKey"; 
static NSString* const SSGameDataCollectionGlowsticksQuantityKey = @"collectionGlowsticksQuantityKey"; 
static NSString* const SSGameDataCollectionFlowersQuantityKey = @"collectionFlowersQuantityKey"; 
static NSString* const SSGameDataCollectionStuffedAnimalsQuantityKey = @"collectionStuffedAnimalsQuantityKey"; 
static NSString* const SSGameDataCollectionEasterEggsQuantityKey = @"collectionEasterEggsQuantityKey"; 

/*static NSString* const SSGameDataCollectionBallsModifierKey = @"collectionBallsModifierKey"; 
static NSString* const SSGameDataCollectionGlowsticksModifierKey = @"collectionGlowsticksModifierKey"; 
static NSString* const SSGameCollectionFlowersModifierKey = @"collectionFlowersModifierKey"; 
static NSString* const SSGameCollectionStuffedAnimalsModifierKey = @"collectionStuffedAnimalsModifierKey"; 
static NSString* const SSGameCollectionEasterEggsModifierKey = @"collectionEasterEggsModifierKey";*/ 

static NSString* const SSGameDataCurrentXPKey = @"currentXPKey"; 
static NSString* const SSGameDataTargetXPKey = @"targetXPKey"; 
static NSString* const SSGameDataLevelKey = @"levelKey"; 

static NSString* const SSGameDataIsInitializedKey = @"dataIsInitializedKey"; 

+ (instancetype)sharedGameData { 
    static id sharedInstance = nil; 

    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedInstance = [self loadInstance]; 
    }); 

    return sharedInstance; 
} 

-(void)reset { 
    self.regularBubbleCount = 0; 
    self.premiumBubbleCount = 0; 

    self.megaBubbleUpgradeTier = 0; 
    self.bubbleFactoryUpgradeTier = 0; 
    self.bubblersUpgradeTier = 0; 
    self.mysteryBubbleUpgradeTier = 0; 
    self.bubbleBankUpgradeTier = 0; 

    self.megaBubblePopValue = 1; 
    self.bubbleFactoryTickValue = 1; 

    self.bubbleBankCapacity = 500; 

    self.collectionBallsQuantity = 0; 
    self.collectionGlowsticksQuantity = 0; 
    self.collectionFlowersQuantity = 0; 
    self.collectionStuffedAnimalsQuantity = 0; 
    self.collectionEasterEggsQuantity = 0; 

    self.currentXP = 0; 
    self.targetXP = 83; 
    self.level = 1; 

    self.dataIsInitialized = true; 
} 

- (void)encodeWithCoder:(NSCoder *)encoder 
{ 
    [encoder encodeDouble:self.regularBubbleCount forKey: SSGameDataRegularBubbleCountKey]; 
    [encoder encodeDouble:self.premiumBubbleCount forKey: SSGameDataPremiumBubbleCountKey]; 

    [encoder encodeDouble:self.megaBubbleUpgradeTier forKey: SSGameDataMegaBubbleUpgradeTierKey]; 
    [encoder encodeDouble:self.bubbleFactoryUpgradeTier forKey: SSGameDataBubbleFactoryUpgradeTierKey]; 
    [encoder encodeDouble:self.bubblersUpgradeTier forKey: SSGameDataBubblersUpgradeTierKey]; 
    [encoder encodeDouble:self.mysteryBubbleUpgradeTier forKey: SSGameDataMysteryBubbleUpgradeTierKey]; 
    [encoder encodeDouble:self.bubbleBankUpgradeTier forKey: SSGameDataBubbleBankUpgradeTierKey]; 

    [encoder encodeDouble:self.megaBubblePopValue forKey: SSGameDataMegaBubblePopValueKey]; 
    [encoder encodeDouble:self.bubbleFactoryTickValue forKey: SSGameDataBubbleFactoryTickValueKey]; 

    [encoder encodeDouble:self.bubbleBankCapacity forKey: SSGameDataBubbleBankCapacityKey]; 

    [encoder encodeDouble:self.collectionBallsQuantity forKey: SSGameDataCollectionBallsQuantityKey]; 
    [encoder encodeDouble:self.collectionGlowsticksQuantity forKey: SSGameDataCollectionGlowsticksQuantityKey]; 
    [encoder encodeDouble:self.collectionFlowersQuantity forKey: SSGameDataCollectionFlowersQuantityKey]; 
    [encoder encodeDouble:self.collectionStuffedAnimalsQuantity forKey: SSGameDataCollectionStuffedAnimalsQuantityKey]; 
    [encoder encodeDouble:self.collectionEasterEggsQuantity forKey: SSGameDataCollectionEasterEggsQuantityKey]; 

    [encoder encodeDouble:self.currentXP forKey: SSGameDataCurrentXPKey]; 
    [encoder encodeDouble:self.targetXP forKey: SSGameDataTargetXPKey]; 
    [encoder encodeDouble:self.level forKey:SSGameDataLevelKey]; 

    [encoder encodeBool:self.dataIsInitialized forKey: SSGameDataIsInitializedKey]; 
} 

- (instancetype)initWithCoder:(NSCoder *)decoder 
{ 
    self = [self init]; 
    if (self) { 
     _regularBubbleCount = [decoder decodeDoubleForKey: SSGameDataRegularBubbleCountKey]; 
     _premiumBubbleCount = [decoder decodeDoubleForKey: SSGameDataPremiumBubbleCountKey]; 

     _megaBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMegaBubbleUpgradeTierKey]; 
     _bubbleFactoryUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryUpgradeTierKey]; 
     _bubblersUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubblersUpgradeTierKey]; 
     _mysteryBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMysteryBubbleUpgradeTierKey]; 
     _bubbleBankUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleBankUpgradeTierKey]; 

     _megaBubblePopValue = [decoder decodeDoubleForKey: SSGameDataMegaBubblePopValueKey]; 
     _bubbleFactoryTickValue = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryTickValueKey]; 

     _bubbleBankCapacity = [decoder decodeDoubleForKey: SSGameDataBubbleBankCapacityKey]; 

     _collectionBallsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionBallsQuantityKey]; 
     _collectionGlowsticksQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionGlowsticksQuantityKey]; 
     _collectionFlowersQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionFlowersQuantityKey]; 
     _collectionStuffedAnimalsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionStuffedAnimalsQuantityKey]; 
     _collectionEasterEggsQuantity = [decoder decodeDoubleForKey: SSGameDataCollectionEasterEggsQuantityKey]; 

     _currentXP = [decoder decodeDoubleForKey: SSGameDataCurrentXPKey]; 
     _targetXP = [decoder decodeDoubleForKey: SSGameDataTargetXPKey]; 
     _level = [decoder decodeDoubleForKey: SSGameDataLevelKey]; 

     _dataIsInitialized = [decoder decodeBoolForKey: SSGameDataIsInitializedKey]; 
    } 
    return self; 
} 

+(NSString*)filePath 
{ 
    static NSString* filePath = nil; 
    if (!filePath) { 
     filePath = 
     [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] 
     stringByAppendingPathComponent:@"gamedata"]; 
    } 
    return filePath; 
} 

+(instancetype)loadInstance 
{ 
    NSData* decodedData = [NSData dataWithContentsOfFile: [RWGameData filePath]]; 
    if (decodedData) { 
     RWGameData* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData]; 
     return gameData; 
    } 

    return [[RWGameData alloc] init]; 
} 

-(void)save 
{ 
    NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self]; 
    [encodedData writeToFile:[RWGameData filePath] atomically:YES]; 
} 

- (void)timerSetup { // to be called from delegate didFinishLaunching…. 
    NSTimer *timer; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled) userInfo:nil repeats:YES]; 
} 

-(void)timerCalled 
{ 
    //NSLog(@"Timer Called");] 
    if ([RWGameData sharedGameData].bubbleFactoryUpgradeTier > 0) { 
     if ([RWGameData sharedGameData].regularBubbleCount < [RWGameData sharedGameData].bubbleBankCapacity) { 
      [RWGameData sharedGameData].regularBubbleCount += [RWGameData sharedGameData].bubbleFactoryTickValue; 
      [[RWGameData sharedGameData] save]; 


     } else { 
      NSLog(@"Capacity Reached! Capacity: %li", [RWGameData sharedGameData].bubbleBankCapacity); 
     } 
     [self.delegate StateUpdateForGameData:self]; 

    } NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount); 
} 

@end 

PrimaryViewController.h
#import <UIKit/UIKit.h> 
#import "RWGameData.h" 

@interface PrimaryViewController : UIViewController <RWGameStateProtocol> 

@property (strong, nonatomic) IBOutlet UILabel *regularBubLabel; 

@end 

我想能夠regularBubLabel的值從timerCalled方法內變化。感謝您的時間。在我的TabViewController中創建對選定選項卡的引用?

包含我的RWGameData類的完整請求。謝謝。

AppDelegate.h

#import <UIKit/UIKit.h> 
#import "RWGameData.h" 
#import "PrimaryViewController.h" 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong,nonatomic) RWGameData *gameData; 


@end 

後來看到的#RWGameData.m

+1

請澄清。 Xcode是IDE(專門爲特定環境編寫其他程序的計算機程序),而不是語言或操作系統。你想引用當前打開的Xcode標籤,還是UITabViewController的標籤? – CaptJak 2015-02-06 20:16:38

+0

@CaptJak更新。我指的是我的選項卡視圖控制器中的選定選項卡。 – 2015-02-06 20:21:54

+0

爲什麼你需要一個參考。你想從哪裏改變標籤的價值? – rdelmar 2015-02-06 20:23:50

回答

0

+(instancetype)loadInstance如果你能提供你想達到什麼樣的更多細節。

如果我理解正確的話,你想知道哪個標籤是有源 所有你需要做的就是讓tab.selectedIndex屬性爲您詮釋代表activTabView的指數,那麼你可以用它這樣做任何事情,這將返回作爲更改所選索引的文本

希望得到這個幫助。

+0

對不起,我很難解釋我想要什麼。從我的自定義類中,我試圖更改已從另一個類聲明並初始化的標籤的文本值。對於每秒通過,我的目標是能夠從我的自定義類更新此標籤。 – 2015-02-06 20:33:35

0

從你的代碼,我可以看到你的PrimaryViewController被確認爲RWGameData即

@interface PrimaryViewController : UIViewController <RWGameStateProtocol> 

所以協議,RWGameData的委託設置爲PrimaryViewController對象,然後實現在PrimaryViewController.m像

此委託方法
-(void)StateUpdateForGameData:(RWGameData*)data 
{ 
    //now update label 
    self.regularBubLabel.text = @"asdasd"; 
} 

編輯:

在PrimaryViewController.m viewDidLoad方法加入這行

[RWGameData sharedGameData].delegate = self; 

timercalled方法應該是這樣

-(void)timerCalled 
{ 
    //NSLog(@"Timer Called");] 
    if ([RWGameData sharedGameData].bubbleFactoryUpgradeTier > 0) { 
     if ([RWGameData sharedGameData].regularBubbleCount < [RWGameData sharedGameData].bubbleBankCapacity) { 
      [RWGameData sharedGameData].regularBubbleCount += [RWGameData sharedGameData].bubbleFactoryTickValue; 
      [[RWGameData sharedGameData] save]; 


     } else { 
      NSLog(@"Capacity Reached! Capacity: %li", [RWGameData sharedGameData].bubbleBankCapacity); 
     } 

    if(self.delegate && [self.delegate respondsToSelector:@selector(StateUpdateForGameData:)]) 
     [self.delegate StateUpdateForGameData:self]; 

    } NSLog(@"Regular Bubble Count: %li", [RWGameData sharedGameData].regularBubbleCount); 
} 
+0

我的課程似乎無法識別UIViewController。它說這是一個未申報的類型?我想我需要一個導入。 – 2015-02-06 20:42:24

+0

是導入您的customViewController類,如果可以,請發佈一些代碼。 – 2015-02-06 20:50:58

+0

已更新,以便於清晰。 – 2015-02-06 21:04:50