add koko_fantomes_80
[exterlulz-kokogems.git] / src / Gem.m
blobbaad1dec05ec0140c3e746226d6c68f8d34ca2b0
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 _state;
31 @synthesize _animationCounter;
33 - (id)init
35   self = [super init];
36   if (self != nil) {
37     [self setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
38     
39     // MW...
40     waitForFall= 0;
41   }
42   
43   return self;
46 - (void)dealloc {
47     [super dealloc];
50 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
52   Gem   *gem = [[Gem alloc] init];
53   [gem setGemType:d];
54   [gem setImage:img];
55   [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
56   
57   return [gem autorelease];
60 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)aSprite
62   Gem   *gem = [[Gem alloc] init];
63   [gem setGemType:d];
64   [gem setSprite:aSprite];
65   [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
66   
67   return [gem autorelease];
70 - (int) animate
72     if (_state == GEMSTATE_RESTING)
73     {
74         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
75         _animationCounter = 0;
76     }
77     if (_state == GEMSTATE_FADING)
78     {
79         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
80         if (_animationCounter > 0)
81           _animationCounter--;
82     }
83     // MW...
84     if (_state== GEMSTATE_SHIVERING) {
85         positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
86         positionOnScreen.y= positionOnBoard.y*48;
87     }
88     //
89     if (_state == GEMSTATE_FALLING)
90     {
91         if (_animationCounter < waitForFall) {
92             positionOnScreen.x= positionOnBoard.x*48;
93             //positionOnScreen.y= positionOnBoard.y*48;
94             _animationCounter++;
95         }
96         else if (positionOnScreen.y > (positionOnBoard.y*48))
97         {
98             positionOnScreen.y += vy;
99             positionOnScreen.x = positionOnBoard.x*48;
100             vy -= GRAVITY;
101             _animationCounter++;
102         }
103         else
104         {
105                         [tink play];
106             positionOnScreen.y = positionOnBoard.y * 48;
107             _state = GEMSTATE_RESTING;
108         }
109     }
110     if (_state == GEMSTATE_SHAKING)
111     {
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;
116     }
117     if (_state == GEMSTATE_ERUPTING)
118     {
119         if (positionOnScreen.y > -48)
120         {
121             if (_animationCounter < GEM_ERUPT_DELAY)
122             {
123                 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
124                 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
125             }
126             else
127             {
128                 positionOnScreen.y += vy;
129                 positionOnScreen.x += vx;
130                 vy -= GRAVITY;
131             }
132             _animationCounter++;
133         }
134         else
135         {
136           _animationCounter = 0;
137         }
138     }
139     if (_state == GEMSTATE_MOVING)
140     {
141         if (_animationCounter > 0)
142         {
143             positionOnScreen.y += vy;
144             positionOnScreen.x += vx;
145             _animationCounter--;
146         }
147         else _state = GEMSTATE_RESTING;
148     }
149     return _animationCounter;
152 - (void) fade
154     [sploink play];
155     _state = GEMSTATE_FADING;
156     _animationCounter = FADE_STEPS;
159 - (void)fall
161   _state = GEMSTATE_FALLING;
162   // MW...
163   waitForFall= rand()%6;
164   
165   vx = 0;
166   vy = 0;
167   _animationCounter = 1;
170 // MW...
171 - (void) shiver
173   _state= GEMSTATE_SHIVERING;
174   _animationCounter = 0;
177 - (void) shake
179   _state = GEMSTATE_SHAKING;
180   _animationCounter = 25;
183 - (void) erupt
185   [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
186   
187   _state = GEMSTATE_ERUPTING;
188   _animationCounter = GEM_ERUPT_DELAY;
191 - (int)gemType {
192     return gemType;
195 - (void)setGemType:(int)d {
196     gemType = d;
199 - (NSImage *) image {
200     return image;
203 - (void) setImage:(NSImage *) value {
204     image = value;
207 - (void) drawImage
209     if (_state == GEMSTATE_FADING)
210         [[self image] compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
211     else
212         [[self image] compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
215 - (OpenGLSprite *) sprite {
216     return sprite;
219 - (void) setSprite:(OpenGLSprite *) value {
220     sprite = value;
223 - (void) drawSprite
225     if (_state == GEMSTATE_FADING)
226         [[self sprite] blitToX:positionOnScreen.x
227                              Y:positionOnScreen.y
228                              Z:GEM_SPRITE_Z
229                          Alpha:(_animationCounter / FADE_STEPS)];
230     else
231         [[self sprite] blitToX:positionOnScreen.x
232                              Y:positionOnScreen.y
233                              Z:GEM_SPRITE_Z
234                          Alpha:1.0];
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
249     vx = valx;
250     vy = valy;
251   
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
268     tink = tinkSound;
269     sploink = sploinkSound;
272 @end