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"
32 @synthesize _animationCounter;
38 [self setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
51 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
53 Gem *gem = [[Gem alloc] init];
56 [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
58 return [gem autorelease];
61 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)aSprite
63 Gem *gem = [[Gem alloc] init];
65 [gem setSprite:aSprite];
66 [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
68 return [gem autorelease];
73 if (_state == GEMSTATE_RESTING)
75 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
76 _animationCounter = 0;
78 if (_state == GEMSTATE_FADING)
80 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
81 if (_animationCounter > 0)
85 if (_state== GEMSTATE_SHIVERING) {
86 positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
87 positionOnScreen.y= positionOnBoard.y*48;
90 if (_state == GEMSTATE_FALLING)
92 if (_animationCounter < waitForFall) {
93 positionOnScreen.x= positionOnBoard.x*48;
94 //positionOnScreen.y= positionOnBoard.y*48;
97 else if (positionOnScreen.y > (positionOnBoard.y*48))
99 positionOnScreen.y += vy;
100 positionOnScreen.x = positionOnBoard.x*48;
107 positionOnScreen.y = positionOnBoard.y * 48;
108 _state = GEMSTATE_RESTING;
111 if (_state == GEMSTATE_SHAKING)
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;
118 if (_state == GEMSTATE_ERUPTING)
120 if (positionOnScreen.y > -48)
122 if (_animationCounter < GEM_ERUPT_DELAY)
124 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
125 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
129 positionOnScreen.y += vy;
130 positionOnScreen.x += vx;
137 _animationCounter = 0;
140 if (_state == GEMSTATE_MOVING)
142 if (_animationCounter > 0)
144 positionOnScreen.y += vy;
145 positionOnScreen.x += vx;
148 else _state = GEMSTATE_RESTING;
150 return _animationCounter;
156 _state = GEMSTATE_FADING;
157 _animationCounter = FADE_STEPS;
162 _state = GEMSTATE_FALLING;
164 waitForFall= rand()%6;
168 _animationCounter = 1;
174 _state= GEMSTATE_SHIVERING;
175 _animationCounter = 0;
180 _state = GEMSTATE_SHAKING;
181 _animationCounter = 25;
186 [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
188 _state = GEMSTATE_ERUPTING;
189 _animationCounter = GEM_ERUPT_DELAY;
196 - (void)setGemType:(int)d {
202 if (_state == GEMSTATE_FADING)
203 [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
205 [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
208 - (OpenGLSprite *) sprite {
212 - (void) setSprite:(OpenGLSprite *) value {
218 if (_state == GEMSTATE_FADING)
219 [[self sprite] blitToX:positionOnScreen.x
222 Alpha:(_animationCounter / FADE_STEPS)];
224 [[self sprite] blitToX:positionOnScreen.x
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
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
262 sploink = sploinkSound;