ftp.nice.ch/Attic/openStep/games/Empire.0.6.m.NIS.bs.tgz#/Empire.0.6/src/Human.subproj/HDirectionState.m

This is HDirectionState.m in view mode; [Download] [Up]

//
// $Id: HDirectionState.m,v 1.2 1997/10/31 03:38:02 nygard Exp $
//

//
//  This file is a part of Empire, a game of exploration and conquest.
//  Copyright (C) 1996  Steve Nygard
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//  You may contact the author by:
//     e-mail:  nygard@telusplanet.net
//

#import "Empire.h"

RCSID ("$Id: HDirectionState.m,v 1.2 1997/10/31 03:38:02 nygard Exp $");

#import "HDirectionState.h"

#import "Human.h"
#import "MapView.h"
#import "Unit.h"

#import "../Orders.subproj/Orders.h"

//======================================================================
// Allow the user to select a direction for continuously moving the
// currently selected unit.  The user may use the keyboard, or click
// in the adjacent squares to select a direction.
//======================================================================

@implementation HDirectionState

//======================================================================
// Subclass responsibilities
//======================================================================

- (NSString *) stateName
{
    return @"Direction";
}
  
//----------------------------------------------------------------------

- (NSString *) stateKey
{
    return HS_DIRECTION;
}
  
//----------------------------------------------------------------------

- (void) stateEntered:(Human *)context
{
    MapView *mapView = [context mapView];
  
    // Set cursor
    [mapView useDirectionCursor];
    [mapView setCursorEnabled:YES];

    // Disable both buttons
    [context enableContinueButton:NO];
    [context enableEndTurnButton:NO];
}
  
//----------------------------------------------------------------------

- (void) mouseDown:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    int delta_row, delta_col;
    Unit *unit = [context selectedUnit];
    Direction delta_to_dir[9] = {d_northwest, d_north, d_northeast, d_west, d_none, d_east, d_southwest, d_south, d_southeast};
    Direction dir;
    EMMapLocation unitLocation = [unit unitLocation];

    delta_row = target.row - unitLocation.row;
    delta_col = target.column - unitLocation.column;
  
    if (delta_row >= -1 && delta_row <= 1 && delta_col >= -1 && delta_col <= 1)
    {
        dir = delta_to_dir[3 * (delta_row + 1) + delta_col + 1];

        if (dir != d_none)
        {
            Order *order = [[[ODirection alloc] initForUnit:unit direction:dir] autorelease];
            [unit setOrder:order];
            [context setStatusLine:[unit statusLine]];
        }

        [context restoreState];
    }
}
  
//----------------------------------------------------------------------

- (void) keyDown:(Human *)context:(NSEvent *)theEvent
{
    NSString *eventCharacters = [theEvent characters];
    unichar uc = [eventCharacters characterAtIndex:0];
    Direction dir = d_none;

    switch (uc)
    {
      case '1':
          dir = d_southwest;
          break;
      
      case NSDownArrowFunctionKey:
      case '2':
          dir = d_south;
          break;
      
      case '3':
          dir = d_southeast;
          break;
      
      case NSLeftArrowFunctionKey:
      case '4':
          dir = d_west;
          break;
      
      case NSRightArrowFunctionKey:
      case '6':
          dir = d_east;
          break;
      
      case '7':
          dir = d_northwest;
          break;
      
      case NSUpArrowFunctionKey:
      case '8':
          dir = d_north;
          break;
      
      case '9':
          dir = d_northeast;
          break;
      
      default:
          [super keyDown:context:theEvent];
          break;
    }

    if (dir != d_none)
    {
        Unit *unit = [context selectedUnit];
        Order *order = [[[ODirection alloc] initForUnit:unit direction:dir] autorelease];
        [unit setOrder:order];
        [context setStatusLine:[unit statusLine]];
    }

    [context restoreState];
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.