-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBPSceneManager.m
133 lines (111 loc) · 3.9 KB
/
BPSceneManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#import "BPSceneManager.h"
#import "BPUtl.h"
#import "BPScene.h"
NSString *sceneRelativeDirectoryPath = @"Data/Scenes";
NSString *metadataFileName = @"metadata.json";
static BPSceneManager *gDefaultSceneManager = nil;
@interface BPSceneManager(PrivateMethods)
-(BPScene*)getRandomScene;
@end
@implementation BPSceneManager
@synthesize scenes=_scenes;
@synthesize currentScene=_currentScene;
@synthesize nextScene=_nextScene;
@synthesize previousScene=_previousScene;
-(void)dealloc {
[_scenes release];
[_sceneQueue release];
[_sceneHistory release];
[_currentScene release];
[_nextScene release];
[_previousScene release];
[super dealloc];
}
-(id)init {
if (self = [super init]) {
_scenes = nil;
_sceneQueue = nil;
_sceneHistory = [[[NSMutableArray alloc] init] retain];
_currentHistoryIndex = -1;
_currentScene = nil;
_nextScene = nil;
_previousScene = nil;
}
return self;
}
+(BPSceneManager*)defaultSceneManager {
if (!gDefaultSceneManager) {
gDefaultSceneManager = [[[[self class] alloc] init] retain];
}
return gDefaultSceneManager;
}
-(void)loadScenes {
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *sceneDirectoryPath = [bundlePath stringByAppendingPathComponent:sceneRelativeDirectoryPath];
NSString *metadataFilePath = [sceneDirectoryPath stringByAppendingPathComponent:metadataFileName];
NSArray *scenesMetadata = (NSArray*)[BPUtl getObjectFromJSONFile:metadataFilePath];
NSMutableArray *tmpScenes = [[NSMutableArray alloc] init];
for (NSDictionary *sceneMetadata in scenesMetadata) {
NSString *imageDescriptionText = [sceneMetadata valueForKey:@"image_description_text"];
NSString *imageFileName = [sceneMetadata valueForKey:@"image_file_name"];
NSString *imageSoundDescriptionFileName = [sceneMetadata valueForKey:@"image_sound_description_file_name"];
NSString *imageSoundEffectFileName = [sceneMetadata valueForKey:@"image_sound_effect_file_name"];
BPScene *scene = [[BPScene alloc] init];
scene.description = imageDescriptionText;
scene.imageFilePath = [sceneDirectoryPath stringByAppendingPathComponent:imageFileName];
scene.imageSoundDescriptionFilePath = [sceneDirectoryPath stringByAppendingPathComponent:imageSoundDescriptionFileName];
scene.imageSoundEffectFilePath = [sceneDirectoryPath stringByAppendingPathComponent:imageSoundEffectFileName];
[tmpScenes addObject:scene];
}
_scenes = [NSArray arrayWithArray:tmpScenes];
_scenes = [_scenes sortedArrayUsingSelector:@selector(compare:)];
[_scenes retain];
_sceneQueue = [[NSMutableArray arrayWithArray:_scenes] retain];
}
-(BPScene*)nextScene:(BPScene*)_scene {
if ((_scene != nil) && ([self.currentScene.description isEqualToString:_scene.description])) {
return _scene;
}
BPScene *scene = nil;
if (_currentHistoryIndex == -1 || _currentHistoryIndex == ([_sceneHistory count] - 1)) {
scene = (_scene == nil) ?[self getRandomScene] : _scene;
[_sceneHistory addObject:scene];
_currentHistoryIndex = [_sceneHistory count] - 1;
} else {
if (_scene == nil) {
_currentHistoryIndex += 1;
scene = [_sceneHistory objectAtIndex:_currentHistoryIndex];
} else {
scene = _scene;
[_sceneHistory addObject:scene];
_currentHistoryIndex = [_sceneHistory count] - 1;
}
}
self.currentScene = scene;
return scene;
}
-(BOOL)hasPreviousScene {
return _currentHistoryIndex > 0;
}
-(BPScene*)previousScene {
BPScene *scene = nil;
if (_currentHistoryIndex > 0) {
_currentHistoryIndex -= 1;
scene = [_sceneHistory objectAtIndex:_currentHistoryIndex];
self.currentScene = scene;
}
return scene;
}
-(BPScene*)getRandomScene {
// queue is empty so repopulate with all scenes
if ([_sceneQueue count] == 0) {
[_sceneQueue addObjectsFromArray:_scenes];
}
srandom(time(NULL));
int sceneQueueCount = [_sceneQueue count];
int index = random() % sceneQueueCount;
BPScene *scene = [_sceneQueue objectAtIndex:index];
[_sceneQueue removeObjectAtIndex:index];
return scene;
}
@end