IJInventoryItem.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // IJInventoryItem.m
  3. // InsideJob
  4. //
  5. // Created by Adam Preble on 10/7/10.
  6. // Copyright 2010 Adam Preble. All rights reserved.
  7. //
  8. #import "IJInventoryItem.h"
  9. @implementation IJInventoryItem
  10. @synthesize itemId, slot, damage, count;
  11. + (id)emptyItemWithSlot:(uint8_t)slot
  12. {
  13. IJInventoryItem *obj = [[[[self class] alloc] init] autorelease];
  14. obj.slot = slot;
  15. return obj;
  16. }
  17. - (NSString *)itemName
  18. {
  19. NSString *name = [[IJInventoryItem itemIdLookup] objectForKey:[NSNumber numberWithShort:self.itemId]];
  20. if (name)
  21. return name;
  22. else
  23. return [NSString stringWithFormat:@"%d", self.itemId];
  24. }
  25. - (NSImage *)image
  26. {
  27. NSSize itemImageSize = NSMakeSize(32, 32);
  28. NSPoint atlasOffset;
  29. NSUInteger itemsPerRow = 9;
  30. NSUInteger pixelsPerColumn = 36;
  31. NSUInteger pixelsPerRow = 56;
  32. int index;
  33. if (self.itemId <= 85)
  34. {
  35. if (self.itemId <= 20)
  36. index = self.itemId - 1; // first item is 1
  37. else if (self.itemId == 35)
  38. index = self.itemId - (35 - 20);
  39. else if (self.itemId >= 37)
  40. index = self.itemId - (37 - 21);
  41. atlasOffset = NSMakePoint(36, 75);
  42. }
  43. else if (self.itemId >= 256 && self.itemId <= 346)
  44. {
  45. index = self.itemId - 256;
  46. atlasOffset = NSMakePoint(445, 23+52);
  47. }
  48. else if (self.itemId >= 2556 && self.itemId <= 2557)
  49. {
  50. index = self.itemId - 2556;
  51. atlasOffset = NSMakePoint(445+pixelsPerColumn, 23+52);
  52. }
  53. else
  54. {
  55. NSLog(@"%s error: unrecognized item id %d", __PRETTY_FUNCTION__, self.itemId);
  56. return nil;
  57. }
  58. atlasOffset.x += pixelsPerColumn * (index % itemsPerRow);
  59. atlasOffset.y += pixelsPerRow * (index / itemsPerRow);
  60. NSRect atlasRect = NSMakeRect(atlasOffset.x, atlasOffset.y, itemImageSize.width, itemImageSize.height);
  61. NSImage *atlas = [NSImage imageNamed:@"DataValuesV110Transparent.png"];
  62. NSImage *output = [[NSImage alloc] initWithSize:itemImageSize];
  63. atlasRect.origin.y = atlas.size.height - atlasRect.origin.y;
  64. [NSGraphicsContext saveGraphicsState];
  65. [output lockFocus];
  66. [atlas drawInRect:NSMakeRect(0, 0, itemImageSize.width, itemImageSize.height)
  67. fromRect:atlasRect
  68. operation:NSCompositeCopy
  69. fraction:1.0];
  70. [output unlockFocus];
  71. [NSGraphicsContext restoreGraphicsState];
  72. return output;
  73. }
  74. + (NSDictionary *)itemIdLookup
  75. {
  76. static NSDictionary *lookup = nil;
  77. if (!lookup)
  78. {
  79. NSError *error = nil;
  80. NSString *lines = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Items" withExtension:@"csv"]
  81. encoding:NSUTF8StringEncoding
  82. error:&error];
  83. NSMutableDictionary *building = [NSMutableDictionary dictionary];
  84. [lines enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
  85. NSArray *components = [line componentsSeparatedByString:@","];
  86. NSNumber *itemId = [NSNumber numberWithShort:[[components objectAtIndex:0] intValue]];
  87. NSString *name = [components objectAtIndex:1];
  88. [building setObject:name forKey:itemId];
  89. }];
  90. lookup = [[NSDictionary alloc] initWithDictionary:building];
  91. }
  92. return lookup;
  93. }
  94. @end