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"
31 @synthesize _animationCounter;
37 [self setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
50 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
52 Gem *gem = [[Gem alloc] init];
55 [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
57 return [gem autorelease];
60 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)aSprite
62 Gem *gem = [[Gem alloc] init];
64 [gem setSprite:aSprite];
65 [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
67 return [gem autorelease];
72 if (_state == GEMSTATE_RESTING)
74 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
75 _animationCounter = 0;
77 if (_state == GEMSTATE_FADING)
79 [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
80 if (_animationCounter > 0)
84 if (_state== GEMSTATE_SHIVERING) {
85 positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
86 positionOnScreen.y= positionOnBoard.y*48;
89 if (_state == GEMSTATE_FALLING)
91 if (_animationCounter < waitForFall) {
92 positionOnScreen.x= positionOnBoard.x*48;
93 //positionOnScreen.y= positionOnBoard.y*48;
96 else if (positionOnScreen.y > (positionOnBoard.y*48))
98 positionOnScreen.y += vy;
99 positionOnScreen.x = positionOnBoard.x*48;
106 positionOnScreen.y = positionOnBoard.y * 48;
107 _state = GEMSTATE_RESTING;
110 if (_state == GEMSTATE_SHAKING)
112 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
113 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
114 if (_animationCounter > 1) _animationCounter--;
115 else _state = GEMSTATE_RESTING;
117 if (_state == GEMSTATE_ERUPTING)
119 if (positionOnScreen.y > -48)
121 if (_animationCounter < GEM_ERUPT_DELAY)
123 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
124 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
128 positionOnScreen.y += vy;
129 positionOnScreen.x += vx;
136 _animationCounter = 0;
139 if (_state == GEMSTATE_MOVING)
141 if (_animationCounter > 0)
143 positionOnScreen.y += vy;
144 positionOnScreen.x += vx;
147 else _state = GEMSTATE_RESTING;
149 return _animationCounter;
155 _state = GEMSTATE_FADING;
156 _animationCounter = FADE_STEPS;
161 _state = GEMSTATE_FALLING;
163 waitForFall= rand()%6;
167 _animationCounter = 1;
173 _state= GEMSTATE_SHIVERING;
174 _animationCounter = 0;
179 _state = GEMSTATE_SHAKING;
180 _animationCounter = 25;
185 [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
187 _state = GEMSTATE_ERUPTING;
188 _animationCounter = GEM_ERUPT_DELAY;
195 - (void)setGemType:(int)d {
199 - (NSImage *) image {
203 - (void) setImage:(NSImage *) value {
209 if (_state == GEMSTATE_FADING)
210 [[self image] compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
212 [[self image] compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
215 - (OpenGLSprite *) sprite {
219 - (void) setSprite:(OpenGLSprite *) value {
225 if (_state == GEMSTATE_FADING)
226 [[self sprite] blitToX:positionOnScreen.x
229 Alpha:(_animationCounter / FADE_STEPS)];
231 [[self sprite] blitToX:positionOnScreen.x
237 - (NSPoint) positionOnScreen
239 return positionOnScreen;
241 - (void) setPositionOnScreen:(int) valx :(int) valy
243 positionOnScreen.x = valx;
244 positionOnScreen.y = valy;
247 - (void) setVelocity:(int) valx :(int) valy :(int) steps
252 _animationCounter = steps;
253 _state = GEMSTATE_MOVING;
256 - (NSPoint) positionOnBoard {
257 return positionOnBoard;
260 - (void)setPositionOnBoard:(int) valx :(int) valy
262 positionOnBoard.x = valx;
263 positionOnBoard.y = valy;
266 - (void) setSoundsTink:(NSSound *) tinkSound Sploink:(NSSound *) sploinkSound
269 sploink = sploinkSound;