Makes Monster a CoreData entity

This commit is contained in:
2020-09-12 00:55:49 -07:00
parent f0e804ce59
commit fb6418cc80
5 changed files with 71 additions and 25 deletions

View File

@@ -7,6 +7,7 @@
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Ability.h"
#import "Action.h"
#import "DamageType.h"
@@ -18,13 +19,8 @@ NS_ASSUME_NONNULL_BEGIN
@class Skill;
@interface Monster : NSObject
@interface Monster : NSManagedObject
@property NSString *name;
@property NSString *size;
@property NSString *type;
@property NSString *tag;
@property NSString *alignment;
@property NSString *armorName;
@property NSString *otherArmorDescription;
@property NSString *hpText;
@@ -64,8 +60,8 @@ NS_ASSUME_NONNULL_BEGIN
+(int)abilityModifierForScore: (int)score;
+(int)hitDieForSize: (NSString*)size;
-(id)initWithJSONString:(NSString*)jsonString;
-(id)initWithJSONData:(NSData*)jsonData;
-(id)initWithJSONString:(NSString*)jsonString andContext:(NSManagedObjectContext*)context;
-(id)initWithJSONData:(NSData*)jsonData andContext:(NSManagedObjectContext*)context;
-(id)initWithMonster:(Monster*)monster;
-(NSString*)meta;
-(int)abilityScoreForAbilityScoreName: (NSString*)abilityScoreName;
@@ -130,3 +126,5 @@ NS_ASSUME_NONNULL_BEGIN
@end
NS_ASSUME_NONNULL_END
#import "Monster+CoreDataProperties.h"

View File

@@ -10,6 +10,38 @@
@implementation Monster
@synthesize armorName;
@synthesize blindsightDistance;
@synthesize burrowSpeed;
@synthesize challengeRating;
@synthesize charismaScore;
@synthesize climbSpeed;
@synthesize constitutionScore;
@synthesize customChallengeRating;
@synthesize customHP;
@synthesize customProficiencyBonus;
@synthesize customSpeed;
@synthesize customSpeedDescription;
@synthesize darkvisionDistance;
@synthesize dexterityScore;
@synthesize flySpeed;
@synthesize hitDice;
@synthesize hover;
@synthesize hpText;
@synthesize intelligenceScore;
@synthesize isBlind;
@synthesize naturalArmorBonus;
@synthesize otherArmorDescription;
@synthesize shieldBonus;
@synthesize speed;
@synthesize strengthScore;
@synthesize swimSpeed;
@synthesize telepathy;
@synthesize tremorsenseDistance;
@synthesize truesightDistance;
@synthesize understandsBut;
@synthesize wisdomScore;
+(int)abilityModifierForScore: (int)score {
@throw [[NSException alloc] initWithName:@"unimplemented" reason:@"Method not implemented." userInfo:nil];
}
@@ -26,14 +58,14 @@
return self;
}
-(id)initWithJSONString: (NSString*)jsonString {
-(id)initWithJSONString: (NSString*)jsonString andContext:(NSManagedObjectContext*)context {
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
return [self initWithJSONData:jsonData];
return [self initWithJSONData:jsonData andContext:context];
}
-(id)initWithJSONData: (NSData*)jsonData {
self = [super init];
-(id)initWithJSONData: (NSData*)jsonData andContext:(NSManagedObjectContext*)context {
self = [self initWithContext:context];
NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
@@ -42,12 +74,8 @@
return self;
}
-(id)initWithMonster:(Monster*)monster {
if (monster == nil) {
return [self init];
}
self = [super init];
-(id)initWithMonster:(Monster* _Nonnull)monster {
self = [self initWithContext:monster.managedObjectContext];
self.name = monster.name;

View File

@@ -1,4 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" sourceLanguage="Objective-C" usedWithCloudKit="true" userDefinedModelVersionIdentifier="">
<elements/>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="16119" systemVersion="19G2021" minimumToolsVersion="Automatic" sourceLanguage="Objective-C" usedWithCloudKit="YES" userDefinedModelVersionIdentifier="">
<entity name="Monster" representedClassName="Monster" syncable="YES" codeGenerationType="category">
<attribute name="alignment" optional="YES" attributeType="String"/>
<attribute name="name" attributeType="String" defaultValueString=""/>
<attribute name="size" optional="YES" attributeType="String"/>
<attribute name="subtype" optional="YES" attributeType="String"/>
<attribute name="type" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="Monster" positionX="-63" positionY="-18" width="128" height="118"/>
</elements>
</model>

View File

@@ -9,6 +9,7 @@
#import "LibraryViewController.h"
#import "Monster.h"
#import "MonsterViewController.h"
#import "AppDelegate.h"
@interface LibraryViewController ()
@@ -20,9 +21,12 @@
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
// Temporary setup of allMonsters until we bind to CoreData.
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}" andContext:context];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}" andContext:context];
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
}
@@ -32,7 +36,9 @@
}
- (IBAction)addNewMonster:(id)sender {
Monster *monster = [[Monster alloc] init];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
Monster *monster = [[Monster alloc] initWithContext:context];
monster.name = @"Unnamed Monster";
self.allMonsters = [self.allMonsters arrayByAddingObject:monster];
[self.monstersTable reloadData];

View File

@@ -9,6 +9,7 @@
#import "SearchViewController.h"
#import "MonsterViewController.h"
#import "Monster.h"
#import "AppDelegate.h"
@interface SearchViewController ()
@@ -21,8 +22,12 @@
- (void)viewDidLoad {
[super viewDidLoad];
Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"];
Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
AppDelegate *appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
Monster *pixie = [[Monster alloc] initWithContext:context];
pixie.name = @"Pixie";
Monster *acolyte = [[Monster alloc] initWithEntity:[NSEntityDescription entityForName:@"Monster" inManagedObjectContext:context] insertIntoManagedObjectContext:nil];
acolyte.name = @"Acolyte";
self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
self.foundMonsters= self.allMonsters;
}