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 state = _state;
33 @synthesize animationCounter = _animationCounter;
39 [self setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
52 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
54 Gem *gem = [[Gem alloc] init];
57 [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
59 return [gem autorelease];
62 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)aSprite
64 Gem *gem = [[Gem alloc] init];
66 [gem setSprite:aSprite];
67 [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
69 return [gem autorelease];
74 if (_state == GEMSTATE_RESTING)
76 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
77 _animationCounter = 0;
79 if (_state == GEMSTATE_FADING)
81 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
82 if (_animationCounter > 0)
86 if (_state== GEMSTATE_SHIVERING) {
87 positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
88 positionOnScreen.y= positionOnBoard.y*48;
91 if (_state == GEMSTATE_FALLING)
93 if (_animationCounter < waitForFall) {
94 positionOnScreen.x= positionOnBoard.x*48;
95 //positionOnScreen.y= positionOnBoard.y*48;
98 else if (positionOnScreen.y > (positionOnBoard.y*48))
100 positionOnScreen.y += vy;
101 positionOnScreen.x = positionOnBoard.x*48;
108 positionOnScreen.y = positionOnBoard.y * 48;
109 _state = GEMSTATE_RESTING;
112 if (_state == GEMSTATE_SHAKING)
114 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
115 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
116 if (_animationCounter > 1) _animationCounter--;
117 else _state = GEMSTATE_RESTING;
119 if (_state == GEMSTATE_ERUPTING)
121 if (positionOnScreen.y > -48)
123 if (_animationCounter < GEM_ERUPT_DELAY)
125 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
126 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
130 positionOnScreen.y += vy;
131 positionOnScreen.x += vx;
138 _animationCounter = 0;
141 if (_state == GEMSTATE_MOVING)
143 if (_animationCounter > 0)
145 positionOnScreen.y += vy;
146 positionOnScreen.x += vx;
149 else _state = GEMSTATE_RESTING;
151 return _animationCounter;
157 _state = GEMSTATE_FADING;
158 _animationCounter = FADE_STEPS;
163 _state = GEMSTATE_FALLING;
165 waitForFall= rand()%6;
169 _animationCounter = 1;
175 _state= GEMSTATE_SHIVERING;
176 _animationCounter = 0;
181 _state = GEMSTATE_SHAKING;
182 _animationCounter = 25;
187 [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
189 _state = GEMSTATE_ERUPTING;
190 _animationCounter = GEM_ERUPT_DELAY;
195 if (_state == GEMSTATE_FADING)
196 [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
198 [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
201 - (OpenGLSprite *) sprite {
205 - (void) setSprite:(OpenGLSprite *) value {
211 if (_state == GEMSTATE_FADING)
212 [[self sprite] blitToX:positionOnScreen.x
215 Alpha:(_animationCounter / FADE_STEPS)];
217 [[self sprite] blitToX:positionOnScreen.x
223 - (NSPoint) positionOnScreen
225 return positionOnScreen;
227 - (void) setPositionOnScreen:(int) valx :(int) valy
229 positionOnScreen.x = valx;
230 positionOnScreen.y = valy;
233 - (void) setVelocity:(int) valx :(int) valy :(int) steps
238 _animationCounter = steps;
239 _state = GEMSTATE_MOVING;
242 - (NSPoint) positionOnBoard {
243 return positionOnBoard;
246 - (void)setPositionOnBoard:(int) valx :(int) valy
248 positionOnBoard.x = valx;
249 positionOnBoard.y = valy;
252 - (void) setSoundsTink:(NSSound *) tinkSound Sploink:(NSSound *) sploinkSound
255 sploink = sploinkSound;