clean some stuff
[exterlulz-kokogems.git] / src / Gem.m
blobef178cc6e675ee4143f139de39815636c8711eae
1 /* ----====----====----====----====----====----====----====----====----====----
2 Gem.m (jeweltoy)
4 JewelToy is a simple game played against the clock.
5 Copyright (C) 2001  Giles Williams
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 ----====----====----====----====----====----====----====----====----====---- */
23 #import "Gem.h"
25 // Open GL
26 #import "OpenGLSprite.h"
28 @implementation Gem
30 @synthesize gemType           = _gemType;
31 @synthesize image             = _image;
32 @synthesize sprite            = _sprite;
33 @synthesize tinkSound         = _tinkSound;
34 @synthesize sploinkSound      = _sploinkSound;
35 @synthesize state             = _state;
36 @synthesize animationCounter  = _animationCounter;
38 - (id)init
40   self = [super init];
41   if (self != nil) {
42     // TODO: replace @"tink" and @"sploink" with static const NSString*
43     // TODO: retain sounds?
44     _tinkSound    = [NSSound soundNamed:@"tink"];
45     _sploinkSound = [NSSound soundNamed:@"sploink"];
46     
47     // MW...
48     waitForFall= 0;
49   }
50   
51   return self;
54 - (void)dealloc {
55     [super dealloc];
58 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
60   Gem   *gem = [[Gem alloc] init];
62   gem.gemType = d;
63   gem.image = img;
64   gem.tinkSound     = [NSSound soundNamed:@"tink"];
65   gem.sploinkSound  = [NSSound soundNamed:@"sploink"];
66   
67   return [gem autorelease];
70 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)sprite
72   Gem   *gem = [[Gem alloc] init];
73   
74   gem.gemType = d;
75   gem.sprite = sprite;
76   gem.tinkSound     = [NSSound soundNamed:@"tink"];
77   gem.sploinkSound  = [NSSound soundNamed:@"sploink"];
78   
79   return [gem autorelease];
82 - (int) animate
84     if (_state == GEMSTATE_RESTING)
85     {
86         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
87         _animationCounter = 0;
88     }
89     if (_state == GEMSTATE_FADING)
90     {
91         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
92         if (_animationCounter > 0)
93           _animationCounter--;
94     }
95     // MW...
96     if (_state== GEMSTATE_SHIVERING) {
97         positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
98         positionOnScreen.y= positionOnBoard.y*48;
99     }
100     //
101     if (_state == GEMSTATE_FALLING)
102     {
103         if (_animationCounter < waitForFall) {
104             positionOnScreen.x= positionOnBoard.x*48;
105             //positionOnScreen.y= positionOnBoard.y*48;
106             _animationCounter++;
107         }
108         else if (positionOnScreen.y > (positionOnBoard.y*48))
109         {
110             positionOnScreen.y += vy;
111             positionOnScreen.x = positionOnBoard.x*48;
112             vy -= GRAVITY;
113             _animationCounter++;
114         }
115         else
116         {
117                         [_tinkSound play];
118             positionOnScreen.y = positionOnBoard.y * 48;
119             _state = GEMSTATE_RESTING;
120         }
121     }
122     if (_state == GEMSTATE_SHAKING)
123     {
124         positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
125         positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
126         if (_animationCounter > 1) _animationCounter--;
127         else _state = GEMSTATE_RESTING;
128     }
129     if (_state == GEMSTATE_ERUPTING)
130     {
131         if (positionOnScreen.y > -48)
132         {
133             if (_animationCounter < GEM_ERUPT_DELAY)
134             {
135                 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
136                 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
137             }
138             else
139             {
140                 positionOnScreen.y += vy;
141                 positionOnScreen.x += vx;
142                 vy -= GRAVITY;
143             }
144             _animationCounter++;
145         }
146         else
147         {
148           _animationCounter = 0;
149         }
150     }
151     if (_state == GEMSTATE_MOVING)
152     {
153         if (_animationCounter > 0)
154         {
155             positionOnScreen.y += vy;
156             positionOnScreen.x += vx;
157             _animationCounter--;
158         }
159         else _state = GEMSTATE_RESTING;
160     }
161     return _animationCounter;
164 - (void)fade
166   [_sploinkSound play];
167   
168   _state = GEMSTATE_FADING;
169   _animationCounter = FADE_STEPS;
172 - (void)fall
174   _state = GEMSTATE_FALLING;
175   // MW...
176   waitForFall= rand()%6;
177   
178   vx = 0;
179   vy = 0;
180   _animationCounter = 1;
183 // MW...
184 - (void) shiver
186   _state= GEMSTATE_SHIVERING;
187   _animationCounter = 0;
190 - (void) shake
192   _state = GEMSTATE_SHAKING;
193   _animationCounter = 25;
196 - (void) erupt
198   [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
199   
200   _state = GEMSTATE_ERUPTING;
201   _animationCounter = GEM_ERUPT_DELAY;
204 - (void) drawImage
206     if (_state == GEMSTATE_FADING)
207         [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
208     else
209         [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
212 - (void) drawSprite
214     if (_state == GEMSTATE_FADING)
215         [[self sprite] blitToX:positionOnScreen.x
216                              Y:positionOnScreen.y
217                              Z:GEM_SPRITE_Z
218                          Alpha:(_animationCounter / FADE_STEPS)];
219     else
220         [[self sprite] blitToX:positionOnScreen.x
221                              Y:positionOnScreen.y
222                              Z:GEM_SPRITE_Z
223                          Alpha:1.0];
226 - (NSPoint) positionOnScreen
228     return positionOnScreen;
230 - (void) setPositionOnScreen:(int) valx :(int) valy
232     positionOnScreen.x = valx;
233     positionOnScreen.y = valy;
236 - (void) setVelocity:(int) valx :(int) valy :(int) steps
238     vx = valx;
239     vy = valy;
240   
241     _animationCounter = steps;
242     _state = GEMSTATE_MOVING;
245 - (NSPoint) positionOnBoard {
246     return positionOnBoard;
249 - (void)setPositionOnBoard:(int) valx :(int) valy
251     positionOnBoard.x = valx;
252     positionOnBoard.y = valy;
255 @end