Many of the variables in my 2D mining game were declared as constants like so:
DWConstants.h
static const int kBlockSpriteWidth = 64;
static const float kBufferFactor = 2.5;
static const int kMaxBlockDamage = 3;
This was all well and good for initial testing, but I soon found out that there were quite a few variables that I wanted to be able to adjust based on what device the game was running on. I thought about a couple approaches to this, and I decided on this one. The values of the variables are set based on the memory of the device. I would love to know what you think.
DWMemoryConstants.h
#import <Foundation/Foundation.h>
@interface DWMemoryConstants : NSObject
@property int startingChunks;
@property int chunkSize;
@property int chunksToGenerate;
@property float maxZoom;
@end
DWMemoryConstants.m
#import "DWMemoryConstants.h"
@implementation DWMemoryConstants
-(instancetype) init {
self = [super init];
if (self) {
unsigned long long memorySize = [NSProcessInfo processInfo].physicalMemory;
unsigned long long memSize = memorySize/1000/1000;
if (memSize < 256) {
_startingChunks = 0;
_chunkSize = 0;
_chunksToGenerate = 0;
_maxZoom = 0.00;
} else if (memSize >= 256 && memSize < 800) {
//ipod 5 is in this range
_startingChunks = 8;
_chunkSize = 16;
_chunksToGenerate = 4;
_maxZoom = 0.45;
} else if (memSize >= 800 && memSize < 1800) {
//ipad 4 will fall in this range
_startingChunks = 10;
_chunkSize = 18;
_chunksToGenerate = 6;
_maxZoom = 0.25;
} else if (memSize >= 1800 && memSize < 2800) {
//previous typical settings
_startingChunks = 10;
_chunkSize = 24;
_chunksToGenerate = 8;
_maxZoom = 0.05;
} else if (memSize >= 2800) {
_startingChunks = 12;
_chunkSize = 30;
_chunksToGenerate = 10;
_maxZoom = 0.04;
}
}
return self;
}
@end
As always, I am open to any sort of feedback about the code.
nonatomic
qualifier to the properties can actually have a performance impact. \$\endgroup\$