/* SpaceThing.m -- implementation for SpaceThing objects
   Copyright (C) 1992, 1993 David A. Strout

   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, 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.  */

/* Written by David Strout <dstrout@isi.edu>. */


#import "SpaceThing.h"
#import "Thinker.h"
#import <appkit/NXImage.h>


@implementation SpaceThing

- findImageNamed:(const char *)name
/* Loads the tiff 'name' into a newly alloc'd NXImage and returns that image's id */
{
  char buf[1024];
  id ret = [NXImage findImageNamed:name];
  if (ret) return ret;
  sprintf(buf,"%s/%s.tiff",[[NXApp delegate] moduleDirectory:"Battle"],name);
  ret = [[NXImage alloc] initFromFile:buf];
  [(NXImage *)ret setName:name];
  return ret;
}

- init
/* Unless someone tells us otherwise, assume we're on a full screen */
{
  if(!maxX) maxX=1120;
  if(!maxY) maxY=832;
  [super init];
  return self;
}

- setViewXSize:(float)x YSize:(float)y
{
  maxX=x;maxY=y;
  [self checkMove];
  return self;
}

- drawSelf
/* This should be over-ridden in subclasses */
{
  
  return self;
}

- oneStep
/* This is called by BattleView's oneStep method, not by BackSpace directly */
{
  if(ONEIN(NEW_HEAD)) [self newVector];
  [self checkMove];
  [self drawSelf];
  return self;
}

- newVector
{
  heading=RANDINT(7);
  speed=RANDINT(5)+2;	
  return self;
}

- checkMove
/* If we have gone beyond the edge of the view, move back to the edge and pick a new vector */
{
  if(location.x>maxX) {
    location.x=maxX;
    [self newVector];
  };
  
  if(location.x<0) {
    location.x=0;
    [self newVector];
  };
  
  if(location.y>maxY) {
    location.y=maxY;
    [self newVector];
  };
  
  if(location.y<0) {
    location.y=0;
    [self newVector];
  };
  
  return self;
}

- (float) xLoc
{
  return location.x;
}

- (float) yLoc
{
  return location.y;
}

@end
