1 /* ----====----====----====----====----====----====----====----====----====----
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 ----====----====----====----====----====----====----====----====----====---- */
26 #import "OpenGLSprite.h"
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;
42 // TODO: replace @"tink" and @"sploink" with static const NSString*
43 // TODO: retain sounds?
44 _tinkSound = [NSSound soundNamed:@"tink"];
45 _sploinkSound = [NSSound soundNamed:@"sploink"];
58 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
60 Gem *gem = [[Gem alloc] init];
64 gem.tinkSound = [NSSound soundNamed:@"tink"];
65 gem.sploinkSound = [NSSound soundNamed:@"sploink"];
67 return [gem autorelease];
70 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)sprite
72 Gem *gem = [[Gem alloc] init];
76 gem.tinkSound = [NSSound soundNamed:@"tink"];
77 gem.sploinkSound = [NSSound soundNamed:@"sploink"];
79 return [gem autorelease];
84 if (_state == GEMSTATE_RESTING)
86 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
87 _animationCounter = 0;
89 if (_state == GEMSTATE_FADING)
91 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
92 if (_animationCounter > 0)
96 if (_state== GEMSTATE_SHIVERING) {
97 positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
98 positionOnScreen.y= positionOnBoard.y*48;
101 if (_state == GEMSTATE_FALLING)
103 if (_animationCounter < waitForFall) {
104 positionOnScreen.x= positionOnBoard.x*48;
105 //positionOnScreen.y= positionOnBoard.y*48;
108 else if (positionOnScreen.y > (positionOnBoard.y*48))
110 positionOnScreen.y += vy;
111 positionOnScreen.x = positionOnBoard.x*48;
118 positionOnScreen.y = positionOnBoard.y * 48;
119 _state = GEMSTATE_RESTING;
122 if (_state == GEMSTATE_SHAKING)
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;
129 if (_state == GEMSTATE_ERUPTING)
131 if (positionOnScreen.y > -48)
133 if (_animationCounter < GEM_ERUPT_DELAY)
135 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
136 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
140 positionOnScreen.y += vy;
141 positionOnScreen.x += vx;
148 _animationCounter = 0;
151 if (_state == GEMSTATE_MOVING)
153 if (_animationCounter > 0)
155 positionOnScreen.y += vy;
156 positionOnScreen.x += vx;
159 else _state = GEMSTATE_RESTING;
161 return _animationCounter;
166 [_sploinkSound play];
168 _state = GEMSTATE_FADING;
169 _animationCounter = FADE_STEPS;
174 _state = GEMSTATE_FALLING;
176 waitForFall= rand()%6;
180 _animationCounter = 1;
186 _state= GEMSTATE_SHIVERING;
187 _animationCounter = 0;
192 _state = GEMSTATE_SHAKING;
193 _animationCounter = 25;
198 [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
200 _state = GEMSTATE_ERUPTING;
201 _animationCounter = GEM_ERUPT_DELAY;
206 if (_state == GEMSTATE_FADING)
207 [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
209 [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
214 if (_state == GEMSTATE_FADING)
215 [[self sprite] blitToX:positionOnScreen.x
218 Alpha:(_animationCounter / FADE_STEPS)];
220 [[self sprite] blitToX:positionOnScreen.x
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
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;