开发者

Building Constructors for RPG game in Objective C

I have a character model class which has this structur开发者_JS百科e:

@interface CharacterModel : NSObject 
{
    // parent of this character
    CharacterModel *parentChar;

    // basic details
    NSString *fname, *sname, *nick;
    NSString *char_type; // categories of characters: dwarf, etc

    // health
    int health;

    // cash
    double cash;
    double graft;

    // flags
    bool is_cop, is_player, is_ai, is_playable;

    // Skills
    int skill_speed;
    int skill_stamina;
    int skill_aggr;
    int skill_another;
    int skill_somethingelse;
    // Total = 100

    // Hidden RPG skills
    int corruption;
    int greed;

    // Rep skills
    int reputation;

    // Misc. flags
    int active, picked, is_locked;
}

The problem are 2 things.

1) I would need to re-write this structure in the @property (nonotomic)... part of the .h file, and I would need to do it again for @synthesize part of the .m file

Is there a way to reduce the need to re-write stuff; can I put all this in a struct or something and then just @synthesize that?

2) The constructor will have a stupidly long function name.

I really, really do not want to be writing a constructor that has hundreds of variables/fields.

ie:

-(id)initCharacter:(NSString *)name, and every other class variable mentioned above ...

Is there are a way around this?

I was thinking of doing a NSMutableDictionary, but you would STILL need to write a constructor with every field you want somewhere.

Any help on this would be great.

Thanks


I really think you should take your design one step further. It is very inflexible to have explicitly defined all the skills and flags like that. Consider creating new classes called:

  • Skill
  • Attribute
  • Flag

Your character class will then have:

NSMutableArray* skills;
NSMutableArray* attributes;
NSMUtableArray* flags;

and obviously getters/setters and add/remove methods for each.

Not only will it make your class look neater but it will also save you a lot of typing.


If you can use the modern Objective-C runtime (available on the iPhone and for 64 bit OS X programs) you will have to write the property definitions twice. Just write the @property and the @synthesize parts, the instance variables will be created automatically. If you need to support the old runtime you just have to write it three times, there is nothing you can do about it.

To the constructor you probably should not pass values for every property. Initialize them to some sensible default values. Note that you don’t have to do anything if the default value is 0, 0.0, nil or NULL - alloc makes sure that all ivars are initialized to zero.

If you want to set all the properties from a NSDictionary you can use key-value-coding to set them instead of doing this manually:

for (NSString *key in dictionary) {
    id value = [dictionary objectForKey: key];
    [self setValue: value forKey: key];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜