Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
288 lines (231 sloc) 11.1 KB
//
// GamePlayViewController.m
// RPG Kit
//
// Created by Philip Regan on 4/21/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "GamePlayViewController.h"
@interface GamePlayViewController ()
#pragma mark - Data
@property (strong, nonatomic, readwrite) NSArray *accomplishments;
@property (strong, nonatomic, readwrite) NSArray *locations;
@end
@implementation GamePlayViewController
@synthesize gameDataController = _gameDataController;
@synthesize gameEngine = _gameEngine;
@synthesize currentLocation = _currentLocation;
@synthesize accomplishments = _accomplishments;
@synthesize locations = _locations;
// the following were autogenerated by Xcode
@synthesize locationTitleLabel;
@synthesize locationIntroLabel;
@synthesize accomplishmentsButton;
@synthesize accomplishmentsTable;
@synthesize locationsLabel;
@synthesize locationsTable;
#pragma mark - Object Lifecycle Stack
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self prepareViewController];
}
- (void)viewDidUnload
{
[self setLocationTitleLabel:nil];
[self setLocationIntroLabel:nil];
[self setAccomplishmentsButton:nil];
[self setAccomplishmentsTable:nil];
[self setLocationsLabel:nil];
[self setLocationsTable:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark NavigationController Stack
/*
These methods are required by the UINavigationController
*/
-(void)viewWillAppear:(BOOL)animated {
// set up the navigation controller
self.navigationController.delegate = self;
self.title = [self.gameDataController.currentUniverse valueForKey:OCAttributeTitleStory];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
[self prepareViewController];
}
-(void)viewWillDisappear:(BOOL)animated {
// update the navigation with the current title
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[self.currentLocation valueForKey:OCAttributeTitleStory]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
}
-(void)prepareViewController {
// start up a game engine so we have it at the ready
self.gameEngine = [[OCGameEngine alloc] init];
// get the data for this particular location
self.locations = [self.gameDataController getLinkedLocationsForLocation:self.currentLocation];
self.accomplishments = [self.gameDataController getAccomplishmentsForLocation:self.currentLocation];
if ( [self.accomplishments count] <= 0 ) {
self.accomplishmentsButton.enabled = NO;
self.accomplishmentsButton.alpha = (CGFLOAT_TYPE)0.5;
}
// get the narrarative for the current location
self.locationTitleLabel.text = [self.currentLocation valueForKey:OCAttributeTitleStory];
self.locationIntroLabel.text = [self.currentLocation valueForKey:OCAttributeIntroStory];
}
#pragma mark - Table View Stack
/*
Since we have two table views we switch between the two based on the tableview making
the call as opposed to creating two different classes. Is their functionality winds
up being anymore complex, we will need to push out the functionality into specific
methods
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ( tableView == accomplishmentsTable ) {
return [self.accomplishments count];
}
if ( tableView == locationsTable ) {
return [self.locations count];
}
return 1;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ( tableView == accomplishmentsTable ) {
if ( [self.accomplishments count] <= 0 ) {
return;
}
// standard table cell management
// create a resuse id
static NSString *gpacid = @"gamePlayAccomplishmentsTableCell";
// dequeue the cell should it exist
cell = [tableView dequeueReusableCellWithIdentifier:gpacid];
// make a cell if it doesn't
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:gpacid];
}
// functionality is pushed out since it is a non-trivial amount code that
// needs to be re-used
cell = [self populateAccomplishmentsCell:cell atIndexPath:indexPath];
}
if ( tableView == locationsTable ) {
// standard table cell management
static NSString *gplcid = @"gamePlayLocationsTableCell";
cell = [tableView dequeueReusableCellWithIdentifier:gplcid];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:gplcid];
}
cell.textLabel.text = [(NSManagedObject *)[self.locations objectAtIndex:indexPath.row] valueForKey:OCAttributeTitleStory];
cell.detailTextLabel.text = [[self.locations objectAtIndex:indexPath.row] valueForKey:OCAttributeIntroStory];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ( tableView == accomplishmentsTable ) {
// there are not always
if ( [self.accomplishments count] <= 0 ) {
return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
}
// standard table cell management
// create a resuse id
static NSString *gpacid = @"gamePlayAccomplishmentsTableCell";
// dequeue the cell should it exist
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:gpacid];
// make a cell if it doesn't
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:gpacid];
}
return [self populateAccomplishmentsCell:cell atIndexPath:indexPath];
}
if ( tableView == locationsTable ) {
static NSString *gplcid = @"gamePlayLocationsTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:gplcid];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:gplcid];
}
cell.textLabel.text = [(NSManagedObject *)[self.locations objectAtIndex:indexPath.row] valueForKey:OCAttributeTitleStory];
cell.detailTextLabel.text = [[self.locations objectAtIndex:indexPath.row] valueForKey:OCAttributeIntroStory];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
}
/*
private helper method to populate and style accomplishment cells for display
*/
-(UITableViewCell *)populateAccomplishmentsCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// populate the cell
cell.textLabel.text = [[self.accomplishments objectAtIndex:indexPath.row] valueForKey:OCAttributeTitleStory];
cell.detailTextLabel.text = [[self.accomplishments objectAtIndex:indexPath.row] valueForKey:OCAttributeIntroStory];
// style the cell
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// nothing in the accomplishments table is selectable
if ( tableView == accomplishmentsTable ) {
[self showAccomplishmentAtIndexPath:indexPath];
}
if ( tableView == locationsTable ) {
GamePlayViewController *gamePlayViewController = [[GamePlayViewController alloc] initWithNibName:@"GamePlayViewController" bundle:nil];
gamePlayViewController.gameDataController = self.gameDataController;
gamePlayViewController.currentLocation = [self.locations objectAtIndex:indexPath.row];
// push
[self.navigationController pushViewController:gamePlayViewController animated:YES];
}
}
#pragma mark - Alert View Stack
-(void)showAccomplishmentAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[self.accomplishments objectAtIndex:indexPath.row] valueForKey:OCAttributeTitleStory]
message:[[self.accomplishments objectAtIndex:indexPath.row] valueForKey:OCAttributeIntroStory]
delegate:self
cancelButtonTitle:NSLocalizedString(@"OCAlertViewCancelButtonTitle", @"")
otherButtonTitles:nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self.accomplishmentsTable deselectRowAtIndexPath:self.accomplishmentsTable.indexPathForSelectedRow animated:YES];
}
#pragma mark - User Action Stack
- (IBAction)attemptAccomplishments {
// time to roll some dice
BOOL result = [self.gameEngine playPlayer:[self.gameDataController.currentUniverse valueForKey:OCRelationshipPlayer]
againstAccomplishments:[self.currentLocation valueForKey:OCRelationshipChildAccomplishment]];
// tell the pplayer how they did
if ( result == YES ) {
[self showTitle:NSLocalizedString(@"OCAlertViewTitleWin", @"")
message:NSLocalizedString(@"OCAlertViewMessageWin", @"")
cancelButtonTitle:NSLocalizedString(@"OCAlertViewCancelButtonTitleWin", @"")];
} else {
[self showTitle:NSLocalizedString(@"OCAlertViewTitleLose", @"")
message:NSLocalizedString(@"OCAlertViewMessageLose", @"")
cancelButtonTitle:NSLocalizedString(@"OCAlertViewCancelButtonTitleLose", @"")];
}
}
-(void)showTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
[alertView show];
}
@end