change key shortcut
[exterlulz-kokogems.git] / src / Gem.m
blob84f26f3c02be06c520abaeac39a403c778e71666
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 _image;
31 @synthesize _state;
32 @synthesize _animationCounter;
34 - (id)init
36   self = [super init];
37   if (self != nil) {
38     [self setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
39     
40     // MW...
41     waitForFall= 0;
42   }
43   
44   return self;
47 - (void)dealloc {
48     [super dealloc];
51 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
53   Gem   *gem = [[Gem alloc] init];
54   [gem setGemType:d];
55   gem._image = img;
56   [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
57   
58   return [gem autorelease];
61 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)aSprite
63   Gem   *gem = [[Gem alloc] init];
64   [gem setGemType:d];
65   [gem setSprite:aSprite];
66   [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
67   
68   return [gem autorelease];
71 - (int) animate
73     if (_state == GEMSTATE_RESTING)
74     {
75         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
76         _animationCounter = 0;
77     }
78     if (_state == GEMSTATE_FADING)
79     {
80         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
81         if (_animationCounter > 0)
82           _animationCounter--;
83     }
84     // MW...
85     if (_state== GEMSTATE_SHIVERING) {
86         positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
87         positionOnScreen.y= positionOnBoard.y*48;
88     }
89     //
90     if (_state == GEMSTATE_FALLING)
91     {
92         if (_animationCounter < waitForFall) {
93             positionOnScreen.x= positionOnBoard.x*48;
94             //positionOnScreen.y= positionOnBoard.y*48;
95             _animationCounter++;
96         }
97         else if (positionOnScreen.y > (positionOnBoard.y*48))
98         {
99             positionOnScreen.y += vy;
100             positionOnScreen.x = positionOnBoard.x*48;
101             vy -= GRAVITY;
102             _animationCounter++;
103         }
104         else
105         {
106                         [tink play];
107             positionOnScreen.y = positionOnBoard.y * 48;
108             _state = GEMSTATE_RESTING;
109         }
110     }
111     if (_state == GEMSTATE_SHAKING)
112     {
113         positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
114         positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
115         if (_animationCounter > 1) _animationCounter--;
116         else _state = GEMSTATE_RESTING;
117     }
118     if (_state == GEMSTATE_ERUPTING)
119     {
120         if (positionOnScreen.y > -48)
121         {
122             if (_animationCounter < GEM_ERUPT_DELAY)
123             {
124                 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
125                 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
126             }
127             else
128             {
129                 positionOnScreen.y += vy;
130                 positionOnScreen.x += vx;
131                 vy -= GRAVITY;
132             }
133             _animationCounter++;
134         }
135         else
136         {
137           _animationCounter = 0;
138         }
139     }
140     if (_state == GEMSTATE_MOVING)
141     {
142         if (_animationCounter > 0)
143         {
144             positionOnScreen.y += vy;
145             positionOnScreen.x += vx;
146             _animationCounter--;
147         }
148         else _state = GEMSTATE_RESTING;
149     }
150     return _animationCounter;
153 - (void) fade
155     [sploink play];
156     _state = GEMSTATE_FADING;
157     _animationCounter = FADE_STEPS;
160 - (void)fall
162   _state = GEMSTATE_FALLING;
163   // MW...
164   waitForFall= rand()%6;
165   
166   vx = 0;
167   vy = 0;
168   _animationCounter = 1;
171 // MW...
172 - (void) shiver
174   _state= GEMSTATE_SHIVERING;
175   _animationCounter = 0;
178 - (void) shake
180   _state = GEMSTATE_SHAKING;
181   _animationCounter = 25;
184 - (void) erupt
186   [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
187   
188   _state = GEMSTATE_ERUPTING;
189   _animationCounter = GEM_ERUPT_DELAY;
192 - (int)gemType {
193     return gemType;
196 - (void)setGemType:(int)d {
197     gemType = d;
200 - (void) drawImage
202     if (_state == GEMSTATE_FADING)
203         [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
204     else
205         [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
208 - (OpenGLSprite *) sprite {
209     return sprite;
212 - (void) setSprite:(OpenGLSprite *) value {
213     sprite = value;
216 - (void) drawSprite
218     if (_state == GEMSTATE_FADING)
219         [[self sprite] blitToX:positionOnScreen.x
220                              Y:positionOnScreen.y
221                              Z:GEM_SPRITE_Z
222                          Alpha:(_animationCounter / FADE_STEPS)];
223     else
224         [[self sprite] blitToX:positionOnScreen.x
225                              Y:positionOnScreen.y
226                              Z:GEM_SPRITE_Z
227                          Alpha:1.0];
230 - (NSPoint) positionOnScreen
232     return positionOnScreen;
234 - (void) setPositionOnScreen:(int) valx :(int) valy
236     positionOnScreen.x = valx;
237     positionOnScreen.y = valy;
240 - (void) setVelocity:(int) valx :(int) valy :(int) steps
242     vx = valx;
243     vy = valy;
244   
245     _animationCounter = steps;
246     _state = GEMSTATE_MOVING;
249 - (NSPoint) positionOnBoard {
250     return positionOnBoard;
253 - (void)setPositionOnBoard:(int) valx :(int) valy
255     positionOnBoard.x = valx;
256     positionOnBoard.y = valy;
259 - (void) setSoundsTink:(NSSound *) tinkSound Sploink:(NSSound *) sploinkSound
261     tink = tinkSound;
262     sploink = sploinkSound;
265 @end