-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppController.m
More file actions
321 lines (274 loc) · 11.9 KB
/
AppController.m
File metadata and controls
321 lines (274 loc) · 11.9 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Project: DSA-SpielHelfer
Author: Sebastian Reitenbach
Created: 2024-09-07 23:14:39 +0200 by sebastia
Application Controller
*/
#import "AppController.h"
#import "DSACharacterGenerationController.h"
#import "DSANPCGenerationController.h"
#import "DSAAdventureGenerationController.h"
#import "DSADocumentController.h"
#import "DSACharacterWindowController.h"
#import "DSAAdventureWindowController.h"
#import "DSACharacterDocument.h"
#import "DSAAdventureDocument.h"
#import "DSACharacter.h"
#import "DSAAdventure.h"
#import "DSAMapViewController.h"
#import "DSAEquipmentListViewController.h"
#import "DSANameGenerationController.h"
#import "DSALocations.h"
#import "DSABattleWindowController.h"
#import "DSALocalMapViewController.h"
#import "DSAAdventureGroup.h"
#import "DSAMapCoordinate.h"
@implementation AppController
+ (void) initialize
{
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
/*
* Register your app's defaults here by adding objects to the
* dictionary, eg
*
* [defaults setObject:anObject forKey:keyForThatObject];
*
*/
[[NSUserDefaults standardUserDefaults] registerDefaults: defaults];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"AppController initialized!");
}
- (id) init
{
if ((self = [super init]))
{
}
return self;
}
- (void) dealloc
{
}
- (void) showPrefPanel: (id)sender
{
NSLog(@"AppController: showPrefPanel called!");
}
- (void)setupApplication
{
// Setup global settings, preferences, or shared resources here
NSLog(@"AppController: setupApplication was called");
}
- (IBAction)openMap:(id)sender
{
NSLog(@"AppController openMap called!!!");
// Initialize and retain the DSAMapViewController
self.mapViewController = [DSAMapViewController sharedMapController];
//self.mapViewController = [[DSAMapViewController alloc] init];
// Show the window
[self.mapViewController showWindow:self];
}
- (IBAction)newBattle:(id)sender
{
NSLog(@"AppController newBattle called!!!");
// Initialize and retain the DSAMapViewController
self.battleViewController = [[DSABattleWindowController alloc] init];
// Show the window
[self.battleViewController showWindow:self];
}
- (IBAction)showEquipmentList:(id)sender
{
NSLog(@"AppController showEquipmentList called!!!");
// Initialize and retain the DSAEquipmentListViewController
self.equipmentListViewController = [[DSAEquipmentListViewController alloc] init];
NSLog(@"AppController showEquipmentList the equipmentListViewController is now initialized");
// Show the window
[self.equipmentListViewController showWindow:self];
}
- (IBAction)showNameGenerator:(id)sender
{
NSLog(@"AppController showNameGenerator called!!!");
// Initialize and retain the DSANameGenerationController
self.nameGenerationController = [[DSANameGenerationController alloc] init];
// Show the window
[self.nameGenerationController showWindow:self];
}
- (IBAction)showLocalMaps:(id)sender
{
NSLog(@"AppController showLocalMaps called!!!");
// Initialize and retain the DSANameGenerationController
self.localMapViewController = [[DSALocalMapViewController alloc] initWithMode: DSALocalMapViewModeGameMaster adventure: nil];
// Show the window
[self.localMapViewController showWindow:self];
}
#pragma mark Character Generation
- (IBAction)newCharacterGeneration:(id)sender
{
NSLog(@"AppController: newCharacterGeneration was called");
// Instantiate the CharacterGenerationController when the menu item is clicked
self.characterGenController = [[DSACharacterGenerationController alloc] init];
// Set up the completion handler to transition to the character management document
__weak typeof(self) weakSelf = self;
self.characterGenController.completionHandler = ^(DSACharacter *newCharacter)
{
[weakSelf createNewCharacterDocument:newCharacter];
};
// Start character generation process
[self.characterGenController startCharacterGeneration: sender];
}
- (void)createNewCharacterDocument:(DSACharacter *)newCharacter
{
NSError *error = nil;
DSADocumentController *docController = [DSADocumentController sharedDocumentController];
// Create a new CharacterDocument with the generated character
NSLog(@"AppController createNewCharacterDocument! calling makeUntitledDocumentOfType....");
DSACharacterDocument *newDocument = [docController makeUntitledDocumentOfType:@"DSACharacter" error:&error];
if (newDocument)
{
newDocument.model = newCharacter;
[docController addDocument:newDocument];
[newDocument makeWindowControllers]; // Create the window controller
//[newDocument makeWindowControllersForNewDocument]; // Create the window controller
[newDocument showWindows]; // Show the document window
// Mark the document as dirty
[newDocument updateChangeCount:NSChangeDone];
}
}
#pragma mark NPC Generation
- (IBAction)newNPCGeneration:(id)sender
{
NSLog(@"AppController: newNPCGeneration was called");
// Instantiate the CharacterGenerationController when the menu item is clicked
self.npcGenController = [[DSANPCGenerationController alloc] init];
// Set up the completion handler to transition to the character management document
__weak typeof(self) weakSelf = self;
self.npcGenController.completionHandler = ^(DSACharacter *newCharacter)
{
[weakSelf createNewNpcDocument:newCharacter];
};
// Start character generation process
[self.npcGenController startNpcGeneration: sender];
}
- (void)createNewNpcDocument:(DSACharacter *)newCharacter
{
NSError *error = nil;
DSADocumentController *docController = [DSADocumentController sharedDocumentController];
// Create a new CharacterDocument with the generated character
NSLog(@"AppController createNewNpcDocument! calling makeUntitledDocumentOfType....");
DSACharacterDocument *newDocument = [docController makeUntitledDocumentOfType:@"DSACharacter" error:&error];
if (newDocument)
{
newDocument.model = newCharacter;
[docController addDocument:newDocument];
[newDocument makeWindowControllers]; // Create the window controller
//[newDocument makeWindowControllersForNewDocument]; // Create the window controller
[newDocument showWindows]; // Show the document window
// Mark the document as dirty
[newDocument updateChangeCount:NSChangeDone];
}
}
#pragma mark Adventure Generation
- (IBAction)newAdventureGeneration:(id)sender {
NSLog(@"AppController: newAdventureGeneration was called");
// Instantiate the adventure generation controller
self.adventureGenController = [[DSAAdventureGenerationController alloc] init];
// Set up completion handler to create the adventure document after selecting a location
__weak typeof(self) weakSelf = self;
self.adventureGenController.completionHandler = ^(NSString *selectedLocation) {
if (selectedLocation) { // Only proceed if a valid selection was made
[weakSelf createNewAdventureDocument:selectedLocation];
} else {
NSLog(@"Adventure generation was cancelled.");
}
};
// Start adventure generation process
[self.adventureGenController startAdventureGeneration:sender];
}
- (void)createNewAdventureDocument:(NSString *)selectedLocation {
NSError *error = nil;
DSADocumentController *docController = [DSADocumentController sharedDocumentController];
NSLog(@"AppController createNewAdventureDocument! calling makeUntitledDocumentOfType....");
DSAAdventureDocument *newDocument = [docController makeUntitledDocumentOfType:@"DSAAdventure" error:&error];
DSALocations *locations = [DSALocations sharedInstance];
NSLog(@"AppController createNewAdventureDocument selectedLocation: %@", selectedLocation);
if (newDocument) {
// Neue leere Gruppe erstellen
DSAAdventureGroup *initialGroup = [[DSAAdventureGroup alloc] init];
DSAPosition *startingPosition;
if (selectedLocation)
{
DSALocation *location = [locations locationWithName:selectedLocation ofType:@"local"];
NSLog(@"AppController createNewAdventureDocument got location: %@", location);
if ([location isMemberOfClass:[DSALocalMapLocation class]]) {
DSALocalMapLocation *localLocation = (DSALocalMapLocation *)location;
DSALocalMapTile *startingTempleTile = [self getLocalMapTileOfStartingTempleForLocation:localLocation.locationMap];
NSLog(@"AppController: createNewAdventureDocument: starting temple tile: %@", startingTempleTile);
location.mapCoordinate = startingTempleTile.tileCoordinate;
startingPosition = [DSAPosition positionWithMapCoordinate: startingTempleTile.tileCoordinate
globalLocationName: localLocation.globalLocationName
localLocationName: localLocation.name
context: nil];
}
NSLog(@"AppController createNewAdventureDocument: startingPosition: %@", startingPosition);
// Setze Location in der ersten Gruppe
initialGroup.position = [startingPosition copy];
}
else
{
NSLog(@"AppController createNewAdventureDocument: selectedLocation was nil, aborting!");
abort();
}
// Setze die Gruppenliste mit einer aktiven (leeren) Gruppe
newDocument.model.groups = [NSMutableArray arrayWithObject:initialGroup];
NSLog(@"AppController createNewAdventureDocument: activeGroup currentPosition: %@", newDocument.model.activeGroup.position);
// Dokument öffnen
[docController addDocument:newDocument];
[newDocument makeWindowControllers];
[newDocument showWindows];
[newDocument updateChangeCount:NSChangeDone];
}
NSLog(@"AppController createNewAdventureDocument: newDocument.model: %@", newDocument.model);
if (error) {
NSLog(@"Error creating Document: %@", error);
}
}
- (DSALocalMapTile *) getLocalMapTileOfStartingTempleForLocation: (DSALocalMap *) localMap
{
for (NSString *god in @[@"Praios", @"Rondra", @"Efferd", @"Travia", @"Boron", @"Hesinde", @"Firun", @"Tsa", @"Phex", @"Peraine", @"Ingerimm", @"Rahja"])
{
NSLog(@"AppController getLocalMapTileOfStartingTempleForLocation: checking god %@", god);
for (DSALocalMapLevel *mapLevel in localMap.mapLevels)
{
//NSLog(@"AppController getLocalMapTileOfStartingTempleForLocation: checking map level %@", mapLevel);
NSArray *mapTiles = mapLevel.mapTiles;
for (NSArray *mapRow in mapTiles)
{
for (DSALocalMapTile *tile in mapRow)
{
//NSLog(@"AppController getLocalMapTileOfStartingTempleForLocation: checking map tile of type %@", tile.type);
if ([tile.type isEqualToString: @"Tempel"])
{
if ([[(DSALocalMapTileBuildingTemple *)tile god] isEqualToString: god])
{
return tile;
}
}
}
}
}
}
return nil;
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
//NSLog(@"AppController validateMenuItem %@ %lu", [menuItem title], (unsigned long)[menuItem tag]);
// TAGS: 200: create new adventure
if ([menuItem tag] == 200)
{ // Tag for the "Level Up" menu item
//NSLog(@"AppController validateMenuItem for menuItem tag 200");
if ([DSAAdventureManager sharedManager].currentAdventure != nil) // we already have an adventure loaded
{
//NSLog(@"AppController validateMenuItem for menuItem tag 200, returning NO");
return NO;
}
}
return YES; // [super validateMenuItem:menuItem];
}
@end