clean some properties
[exterlulz-kokogems.git] / src / Gem.m
blobcbad1a06190acaa3fabfe28df3c3354a46cc360f
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 gemType           = _gemType;
31 @synthesize image             = _image;
32 @synthesize state             = _state;
33 @synthesize animationCounter  = _animationCounter;
35 - (id)init
37   self = [super init];
38   if (self != nil) {
39     [self setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
40     
41     // MW...
42     waitForFall= 0;
43   }
44   
45   return self;
48 - (void)dealloc {
49     [super dealloc];
52 + (Gem *)gemWithNumber:(int)d andImage:(NSImage *)img
54   Gem   *gem = [[Gem alloc] init];
55   [gem setGemType:d];
56   gem.image = img;
57   [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
58   
59   return [gem autorelease];
62 + (Gem *)gemWithNumber:(int)d andSprite:(OpenGLSprite *)aSprite
64   Gem   *gem = [[Gem alloc] init];
65   [gem setGemType:d];
66   [gem setSprite:aSprite];
67   [gem setSoundsTink:[NSSound soundNamed:@"tink"] Sploink:[NSSound soundNamed:@"sploink"]];
68   
69   return [gem autorelease];
72 - (int) animate
74     if (_state == GEMSTATE_RESTING)
75     {
76         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
77         _animationCounter = 0;
78     }
79     if (_state == GEMSTATE_FADING)
80     {
81         [self setPositionOnScreen:positionOnBoard.x*48:positionOnBoard.y*48];
82         if (_animationCounter > 0)
83           _animationCounter--;
84     }
85     // MW...
86     if (_state== GEMSTATE_SHIVERING) {
87         positionOnScreen.x= positionOnBoard.x*48+(rand()%3)-1;
88         positionOnScreen.y= positionOnBoard.y*48;
89     }
90     //
91     if (_state == GEMSTATE_FALLING)
92     {
93         if (_animationCounter < waitForFall) {
94             positionOnScreen.x= positionOnBoard.x*48;
95             //positionOnScreen.y= positionOnBoard.y*48;
96             _animationCounter++;
97         }
98         else if (positionOnScreen.y > (positionOnBoard.y*48))
99         {
100             positionOnScreen.y += vy;
101             positionOnScreen.x = positionOnBoard.x*48;
102             vy -= GRAVITY;
103             _animationCounter++;
104         }
105         else
106         {
107                         [tink play];
108             positionOnScreen.y = positionOnBoard.y * 48;
109             _state = GEMSTATE_RESTING;
110         }
111     }
112     if (_state == GEMSTATE_SHAKING)
113     {
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;
118     }
119     if (_state == GEMSTATE_ERUPTING)
120     {
121         if (positionOnScreen.y > -48)
122         {
123             if (_animationCounter < GEM_ERUPT_DELAY)
124             {
125                 positionOnScreen.x = positionOnBoard.x*48+(rand()%5)-2;
126                 positionOnScreen.y = positionOnBoard.y*48+(rand()%5)-2;
127             }
128             else
129             {
130                 positionOnScreen.y += vy;
131                 positionOnScreen.x += vx;
132                 vy -= GRAVITY;
133             }
134             _animationCounter++;
135         }
136         else
137         {
138           _animationCounter = 0;
139         }
140     }
141     if (_state == GEMSTATE_MOVING)
142     {
143         if (_animationCounter > 0)
144         {
145             positionOnScreen.y += vy;
146             positionOnScreen.x += vx;
147             _animationCounter--;
148         }
149         else _state = GEMSTATE_RESTING;
150     }
151     return _animationCounter;
154 - (void) fade
156     [sploink play];
157     _state = GEMSTATE_FADING;
158     _animationCounter = FADE_STEPS;
161 - (void)fall
163   _state = GEMSTATE_FALLING;
164   // MW...
165   waitForFall= rand()%6;
166   
167   vx = 0;
168   vy = 0;
169   _animationCounter = 1;
172 // MW...
173 - (void) shiver
175   _state= GEMSTATE_SHIVERING;
176   _animationCounter = 0;
179 - (void) shake
181   _state = GEMSTATE_SHAKING;
182   _animationCounter = 25;
185 - (void) erupt
187   [self setVelocity:(rand()%5)-2:(rand()%7)-2:1];
188   
189   _state = GEMSTATE_ERUPTING;
190   _animationCounter = GEM_ERUPT_DELAY;
193 - (void) drawImage
195     if (_state == GEMSTATE_FADING)
196         [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver fraction:(_animationCounter / FADE_STEPS)];
197     else
198         [_image compositeToPoint:[self positionOnScreen] operation:NSCompositeSourceOver];
201 - (OpenGLSprite *) sprite {
202     return sprite;
205 - (void) setSprite:(OpenGLSprite *) value {
206     sprite = value;
209 - (void) drawSprite
211     if (_state == GEMSTATE_FADING)
212         [[self sprite] blitToX:positionOnScreen.x
213                              Y:positionOnScreen.y
214                              Z:GEM_SPRITE_Z
215                          Alpha:(_animationCounter / FADE_STEPS)];
216     else
217         [[self sprite] blitToX:positionOnScreen.x
218                              Y:positionOnScreen.y
219                              Z:GEM_SPRITE_Z
220                          Alpha:1.0];
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
235     vx = valx;
236     vy = valy;
237   
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
254     tink = tinkSound;
255     sploink = sploinkSound;
258 @end