iphone - Adding achievements mechanism - how do design - objective c -
so want add achievements game. there around 40 achievements can awarded while using app. (if while playing game, , while pressing menu buttons).
i'm not sure how should implement it, i'll let know options thought of far :
writing (achievements manager) class have addachievment() function.
inside every function in app can grant achievement can allocate achievemnt object , call addachievment() object. don't approach first, have add lot's of achievments code many many parts of app. (and checking not add same achievement more once).
one way improve maybe call addachievment() enum, , inside addachievment() implementation check each enum , allocate appropriate achievment object - however, function switch of 40 cases doesn't sound either.
2. every class can report achievements can write function per achievement return if achievement should granted. example class can report 2 achivments can write 2 functions :
-(bool) shouldgranta1 -(bool) shouldgranta2
when init class a, call achievements manger , add 2 function array of function hold. every time want check if should grant achievments call checkachievements() , run through function , add achievements function return true.
problem approach - let's in class reach place change value know can grant achievemetn. can call am's checkachievements() go through achivements functions, though class a's achivement granted. seems bit overhead. way solve ?
i love here other suggestion well. thanks!!
i not add achievement code existing game classes. no booleans or whatsoever because creates tight coupling between game classes , achievement system. better create separate "achievementmanager" manages several achievementlisteners, these listen state of objects , when relevant state changes unlock condition checked. think idea best illustrated in code.
for example if have achievement "player walks 100 kilometers". playerwalksachievementlistener this.
private achievementmanager manager; private player player. private vector2 previousplayerposition; private float distancewalked; update() { float dist = vector2.distance(player.position, previousplayerposition); if(dist > 0) { distancewalked += dist; checkunlockcondition(); } } checkunlockcondition() { if(distancewalked * conversionfactor > 100) { manager.unlockachivement(achievementid); } }
Comments
Post a Comment