home *** CD-ROM | disk | FTP | other *** search
- /*
- * You may freely copy, distribute and reuse the code in this example.
- * NeXT disclaims any warranty of any kind, expressed or implied, as to its
- * fitness for any particular use.
- */
-
-
-
- /* always import your particular class .h file!! */
- #import "Fishie.h"
- #import <stdio.h>
- #import <stdlib.h>
-
- #define STARTSIZE 64
-
-
- @implementation Fishie
-
- /* general methods */
- - init
- {
- printf("\t >() \t making a Fishie\n");
- [super init]; /* ALWAYS init your inherited instance vars first!! */
-
- printf("\t >() \t setting the instance variables for the Fishie\n");
- [self setSize:STARTSIZE]; /* initial fishie size */
- [self setHLoc:0];
- [self setVLoc:0];
- [self setIsHappy:YES];
-
- printf("\t >() \t look Ma, i made a Fishie!\n\n");
- return self;
- }
-
-
-
- /* state-returning methods */
- - (int) size
- {
- printf("\t >() \t you asked for it...here's size: %d\n", size);
- return size;
- }
-
-
- - (int) hLoc
- {
- printf("\t >() \t you asked for it...here's hLoc: %d\n", hLoc);
- return hLoc;
- }
-
-
- - (int) vLoc
- {
- printf("\t >() \t you asked for it...here's vLoc: %d\n", vLoc);
- return vLoc;
- }
-
-
- - (BOOL) isHappy
- {
- printf("\t >() \t you asked for it...here's isHappy: %d\n", isHappy);
- return isHappy;
- }
-
-
-
-
- /* state-changing methods */
- - setSize:(int) val
- {
- printf("\t >() \t I'm changing size\n");
- size = val;
- return self;
- }
-
-
- - setHLoc:(int) val
- {
- printf("\t >() \t I'm changing hLoc\n");
- hLoc = val;
- return self;
- }
-
-
- - setVLoc:(int) val
- {
- printf("\t >() \t I'm changing vLoc\n");
- vLoc = val;
- return self;
- }
-
-
- - setIsHappy:(BOOL) val
- {
- printf("\t >() \t I'm changing isHappy\n");
- isHappy = val;
- return self;
- }
-
-
-
- /* special-purpose methods */
- /* swimming moves the fish left/right by the Fishie's size or keeps it
- stationary; ditto for vertical motion; up and right motion forces
- happiness; left and down motion forces unhappiness */
- - swim
- {
- int hInc, vInc;
-
- printf("\t >() \t making a Fishie swim\n");
-
- hInc = rand()%3 - 1; /* horizontal increment is -1,0,or 1 */
- vInc = rand()%3 - 1; /* vertical increment is -1,0,or 1 */
-
- if (hInc > 0 && vInc > 0) /* double positive motion makes happiness */
- [self setIsHappy:YES];
- else if (hInc < 0 && vInc < 0) /* opposite brings sadness */
- [self setIsHappy:NO];
-
- [self incHBy:[self size]*hInc]; /* new pos is same or
- left/right shift of size units */
- [self incVBy:[self size]*vInc];
-
- printf("\t >() \t FINished making a Fishie swim (pun intended)\n\n");
- return self;
- }
-
-
- - incHBy:(int) val
- {
- printf("\t >() \t I'm incrementing hLoc\n");
- [self setHLoc:[self hLoc]+val];
- return self;
- }
-
-
- - incVBy:(int) val
- {
- printf("\t >() \t I'm incrementing vLoc\n");
- [self setVLoc:[self vLoc]+val];
- return self;
- }
-
-
- @end
-