@interface BGResourcePreloader : NSObject <AVAudioPlayerDelegate> + (instancetype)shared; // - (void)preloadAudioResource:(NSString *)name; // name // type // nil - - (AVAudioPlayer *)playerFromGameConfigForResource:(NSString *)name; // name // type. - (AVAudioPlayer *)playerForResource:(NSString *)name; @end
// // BGResourcePreloader.m // Miner // // Created by AndrewShmig on 4/5/14. // Copyright (c) 2014 Bleeding Games. All rights reserved. // #import "BGResourcePreloader.h" #import "BGSettingsManager.h" @implementation BGResourcePreloader { NSMutableDictionary *_data; } #pragma mark - Class methods static BGResourcePreloader *shared; + (instancetype)shared { static dispatch_once_t once; dispatch_once(&once, ^{ shared = [[self alloc] init]; shared->_data = [[NSMutableDictionary alloc] init]; }); return shared; } #pragma mark - Instance methods - (void)preloadAudioResource:(NSString *)name { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSString *soundPath = [[NSBundle mainBundle] pathForResource:name ofType:nil]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil]; [player prepareToPlay]; _data[name] = player; }); } - (AVAudioPlayer *)playerFromGameConfigForResource:(NSString *)name { // if ([BGSettingsManager sharedManager].soundStatus == BGMinerSoundStatusOff) return nil; return [self BGPrivate_playerForResource:name]; } - (AVAudioPlayer *)playerForResource:(NSString *)name { return [self BGPrivate_playerForResource:name]; } #pragma mark - AVAudioDelegate - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player { [player stop]; player.currentTime = 0.0; } #pragma mark - Private method - (AVAudioPlayer *)BGPrivate_playerForResource:(NSString *)name { return (AVAudioPlayer *) _data[name]; } @end
#pragma mark - Touches - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self updateSegmentedControlUsingTouches:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [self updateSegmentedControlUsingTouches:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self updateSegmentedControlUsingTouches:touches]; } #pragma mark - Private method - (void)updateSegmentedControlUsingTouches:(NSSet *)touches { UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self]; for (NSUInteger i = 0; i < _selectedSegments.count; i++) { CGRect rect = ((UIImageView *) _selectedSegments[i]).frame; if (CGRectContainsPoint(rect, touchPoint)) { if (self.selectedSegmentIndex != i) { // - // [[[BGResourcePreloader shared] playerFromGameConfigForResource:@"buttonTap.mp3"] play]; } self.selectedSegmentIndex = i; break; } } [_target performSelector:_action withObject:@(_selectedSegmentIndex)]; }
#pragma mark - Touches - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { BGLog(); [self updateActiveRegionUsingTouches:touches]; if ((self.isOn && self.activeRegion == BGUISwitchLeftRegion) || (!self.isOn && self.activeRegion == BGUISwitchRightRegion)) { [super touchesMoved:touches withEvent:event]; [self playSwitchSound]; [_target performSelector:_action withObject:self]; self.on = !self.on; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { BGLog(); [self updateActiveRegionUsingTouches:touches]; if ((self.isOn && self.activeRegion == BGUISwitchLeftRegion) || (!self.isOn && self.activeRegion == BGUISwitchRightRegion)) { [super touchesEnded:touches withEvent:event]; [self playSwitchSound]; [_target performSelector:_action withObject:self]; self.on = !self.on; } } - (void)updateActiveRegionUsingTouches:(NSSet *)touches { UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self]; CGRect leftRect = CGRectMake(0, 0, self.bounds.size.width / 2, self.bounds.size.height); CGRect rightRect = CGRectMake(self.bounds.size.width / 2, 0, self.bounds.size.width / 2, self.bounds.size.height); if (CGRectContainsPoint(leftRect, touchPoint)) { _activeRegion = BGUISwitchLeftRegion; } else if (CGRectContainsPoint(rightRect, touchPoint)) { _activeRegion = BGUISwitchRightRegion; } else { _activeRegion = BGUISwitchNoneRegion; } }
static NSString* const kBGSettingManagerUserDefaultsStoreKeyForMainSettings = @"kBGSettingsManagerUserDefaultsStoreKeyForMainSettings"; static NSString* const kBGSettingManagerUserDefaultsStoreKeyForDefaultSettings = @"kBGSettingsManagerUserDefaultsStoreKeyForDefaultSettings"; // Class allows to work with app settings in a simple and flexible way. @interface BGSettingsManager : NSObject // Delimiters for setting paths. Defaults to "." (dot) character. @property (nonatomic, readwrite, strong) NSCharacterSet *pathDelimiters; // Boolean value which specifies if exception should be thrown if settings path // doesn't exist or they are incorrect. Defaults to YES. @property (nonatomic, readwrite, assign) BOOL throwExceptionForUnknownPath; + (instancetype)shared; // creates default settings which are not used as main settings until // resetToDefaultSettings method is called // example: [[BGSettingsManager shared] createDefaultSettingsFromDictionary:@{@"user":@{@"login":@"Andrew", @"password":@"1234"}}] - (void)createDefaultSettingsFromDictionary:(NSDictionary *)settings; // resets main settings to default settings - (void)resetToDefaultSettings; // clears/removes all settings - main and default - (void)clear; // adding new setting value for settingPath // example: [... setValue:@YES forSettingsPath:@"user.personalInfo.married"]; - (void)setValue:(id)value forSettingsPath:(NSString *)settingPath; // return setting value with specified type - (id)valueForSettingsPath:(NSString *)settingsPath; - (BOOL)boolValueForSettingsPath:(NSString *)settingsPath; - (NSInteger)integerValueForSettingsPath:(NSString *)settingsPath; - (NSUInteger)unsignedIntegerValueForSettingsPath:(NSString *)settingsPath; - (CGFloat)floatValueForSettingsPath:(NSString *)settingsPath; - (NSString *)stringValueForSettingsPath:(NSString *)settingsPath; - (NSArray *)arrayValueForSettingsPath:(NSString *)settingsPath; - (NSDictionary *)dictionaryValueForSettingsPath:(NSString *)settingsPath; - (NSData *)dataValueForSettingsPath:(NSString *)settingsPath; @end
// // Copyright (C) 4/27/14 Andrew Shmig ( andrewshmig@yandex.ru ) // Russian Bleeding Games. All rights reserved. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #import "BGSettingsManager.h" @implementation BGSettingsManager { NSMutableDictionary *_defaultSettings; NSMutableDictionary *_settings; } #pragma mark - Class methods + (instancetype)shared { static dispatch_once_t once; static BGSettingsManager *shared; dispatch_once(&once, ^{ shared = [[self alloc] init]; shared->_pathDelimiters = [NSCharacterSet characterSetWithCharactersInString:@"."]; shared->_throwExceptionForUnknownPath = YES; [shared BGPrivateMethod_loadExistingSettings]; }); return shared; } #pragma mark - Instance methods - (void)createDefaultSettingsFromDictionary:(NSDictionary *)settings { _defaultSettings = [self BGPrivateMethod_deepMutableCopy:settings]; [self BGPrivateMethod_saveSettings]; } - (void)resetToDefaultSettings { _settings = [_defaultSettings mutableCopy]; [self BGPrivateMethod_saveSettings]; } - (void)clear { _settings = [NSMutableDictionary new]; _defaultSettings = [NSMutableDictionary new]; [self BGPrivateMethod_saveSettings]; } - (void)setValue:(id)value forSettingsPath:(NSString *)settingPath { NSArray *settingsPathComponents = [settingPath componentsSeparatedByCharactersInSet:self .pathDelimiters]; __block id currentNode = _settings; [settingsPathComponents enumerateObjectsUsingBlock:^(id pathComponent, NSUInteger idx, BOOL *stop) { id nextNode = currentNode[pathComponent]; BOOL nextNodeIsNil = (nextNode == nil); BOOL nextNodeIsDictionary = [nextNode isKindOfClass:[NSMutableDictionary class]]; BOOL lastPathComponent = (idx == [settingsPathComponents count] - 1); if ((nextNodeIsNil || !nextNodeIsDictionary) && !lastPathComponent) { [currentNode setObject:[NSMutableDictionary new] forKey:pathComponent]; } else if (idx == [settingsPathComponents count] - 1) { if ([value isKindOfClass:[NSNumber class]]) currentNode[pathComponent] = [value copy]; else currentNode[pathComponent] = [value mutableCopy]; } currentNode = currentNode[pathComponent]; }]; [self BGPrivateMethod_saveSettings]; } - (id)valueForSettingsPath:(NSString *)settingsPath { NSArray *settingsPathComponents = [settingsPath componentsSeparatedByCharactersInSet:self .pathDelimiters]; __block id currentNode = _settings; __block id valueForSettingsPath = nil; [settingsPathComponents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // we have a nil node for a path component which is not the last one // or a node which is not a leaf node if ((nil == currentNode && idx != [settingsPathComponents count]) || (currentNode != nil && ![currentNode isKindOfClass:[NSDictionary class]])) { [self BGPrivateMethod_throwExceptionForInvalidSettingsPath]; } NSString *key = obj; id nextNode = currentNode[key]; if (nil == nextNode) { *stop = YES; } else { if (![nextNode isKindOfClass:[NSMutableDictionary class]]) valueForSettingsPath = nextNode; } currentNode = nextNode; }]; return valueForSettingsPath; } - (BOOL)boolValueForSettingsPath:(NSString *)settingsPath { return [[self valueForSettingsPath:settingsPath] boolValue]; } - (NSInteger)integerValueForSettingsPath:(NSString *)settingsPath { return [[self valueForSettingsPath:settingsPath] integerValue]; } - (NSUInteger)unsignedIntegerValueForSettingsPath:(NSString *)settingsPath { return (NSUInteger) [[self valueForSettingsPath:settingsPath] integerValue]; } - (CGFloat)floatValueForSettingsPath:(NSString *)settingsPath { return [[self valueForSettingsPath:settingsPath] floatValue]; } - (NSString *)stringValueForSettingsPath:(NSString *)settingsPath { return (NSString *) [self valueForSettingsPath:settingsPath]; } - (NSArray *)arrayValueForSettingsPath:(NSString *)settingsPath { return (NSArray *) [self valueForSettingsPath:settingsPath]; } - (NSDictionary *)dictionaryValueForSettingsPath:(NSString *)settingsPath { return (NSDictionary *) [self valueForSettingsPath:settingsPath]; } - (NSData *)dataValueForSettingsPath:(NSString *)settingsPath { return (NSData *) [self valueForSettingsPath:settingsPath]; } - (NSString *)description { return [_settings description]; } #pragma mark - Private methods - (void)BGPrivateMethod_saveSettings { [[NSUserDefaults standardUserDefaults] setValue:_settings forKey:kBGSettingManagerUserDefaultsStoreKeyForMainSettings]; [[NSUserDefaults standardUserDefaults] setValue:_defaultSettings forKey:kBGSettingManagerUserDefaultsStoreKeyForDefaultSettings]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (void)BGPrivateMethod_loadExistingSettings { id settings = [[NSUserDefaults standardUserDefaults] valueForKey:kBGSettingManagerUserDefaultsStoreKeyForMainSettings]; id defaultSettings = [[NSUserDefaults standardUserDefaults] valueForKey:kBGSettingManagerUserDefaultsStoreKeyForDefaultSettings]; _settings = (settings ? settings : [NSMutableDictionary new]); _defaultSettings = (defaultSettings ? defaultSettings : [NSMutableDictionary new]); } - (NSMutableDictionary *)BGPrivateMethod_deepMutableCopy:(NSDictionary *)settings { NSMutableDictionary *deepMutableCopy = [settings mutableCopy]; [settings enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if ([obj isKindOfClass:[NSDictionary class]]) deepMutableCopy[key] = [self BGPrivateMethod_deepMutableCopy:obj]; else deepMutableCopy[key] = obj; }]; return deepMutableCopy; } - (void)BGPrivateMethod_throwExceptionForInvalidSettingsPath { if (self.throwExceptionForUnknownPath) [NSException raise:@"Invalid settings path." format:@"Some of your setting path components may intersect incorrectly or they don't exist."]; } @end
// CODE -- begin BGSettingsManager *settingsManager = [BGSettingsManager shared]; [settingsManager createDefaultSettingsFromDictionary:@{ @"user": @{ @"info":@{ @"name": @"Andrew", @"surname": @"Shmig", @"age": @22 } } }]; [settingsManager resetToDefaultSettings]; [settingsManager setValue:@"+7 920 930 87 56" forSettingsPath:@"user.info.contacts.phone"]; NSLog(@"%@", settingsManager); [settingsManager clear]; NSLog(@"%@", settingsManager); // CODE - end
2014-04-30 23:45:03.842 BGUtilityLibrary[13730:70b] { user = { info = { age = 22; contacts = { phone = "+7 920 930 87 56"; }; name = Andrew; surname = Shmig; }; }; } 2014-04-30 23:45:03.847 BGUtilityLibrary[13730:70b] { }
// NSArray *audioResources = @[@"switchON.mp3", @"switchOFF.mp3", @"flagTapOn.mp3", @"grassTap.mp3", @"buttonTap.mp3", @"flagTapOff.mp3", @"explosion.wav"]; for (NSString *audioName in audioResources) { [[BGResourcePreloader shared] preloadAudioResource:audioName]; }
- (void)generateFieldWithExcludedCellInCol:(NSUInteger)cellCol row:(NSUInteger)cellRow { BGLog(); // NSMutableArray *cells = [NSMutableArray new]; // _field = [NSMutableArray new]; for (NSUInteger i = 0; i < self.cols; i++) { [_field addObject:[NSMutableArray new]]; for (NSUInteger j = 0; j < self.rows; j++) { [_field[i] addObject:@(BGFieldEmpty)]; // , "" if (!(i == cellCol && j == cellRow)) [cells addObject:@(i * kBGPrime + j)]; } } // sranddev(); for (NSUInteger i = 0; i < self.bombs; i++) { NSUInteger index = arc4random() % [cells count]; NSUInteger randomCell = [cells[index] unsignedIntegerValue]; NSUInteger col = randomCell / kBGPrime; NSUInteger row = randomCell % kBGPrime; _field[col][row] = @(BGFieldBomb); // [cells removeObjectAtIndex:index]; } // _x = @[@0, @1, @1, @1, @0, @(-1), @(-1), @(-1)]; _y = @[@(-1), @(-1), @0, @1, @1, @1, @0, @(-1)]; for (NSUInteger i = 0; i < self.cols; i++) { for (NSUInteger j = 0; j < self.rows; j++) { NSInteger cellValue = [_field[i][j] integerValue]; NSInteger count = 0; if (cellValue == BGFieldEmpty) { for (NSUInteger k = 0; k < _x.count; k++) { NSInteger newY = i + [_x[k] integerValue]; NSInteger newX = j + [_y[k] integerValue]; if (newX >= 0 && newY >= 0 && newX < self.rows && newY < self.cols) { if ([_field[(NSUInteger) newY][(NSUInteger) newX] integerValue] == BGFieldBomb) { count++; } } } _field[i][j] = @(count); } } } }
Source: https://habr.com/ru/post/221519/
All Articles