add pause sound
[exterlulz-kokogems.git] / src / ScoreBubble.m
blob904a4eb2f39fdbb61eb52fc6613010d9ac8f997c
1 //
2 //  ScoreBubble.m
3 //  jeweltoy
4 //
5 //  Created by Mike Wessler on Sat Jun 15 2002.
6 //
7 /*
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  ----====----====----====----====----====----====----====----====----====---- */
23 #import "ScoreBubble.h"
25 #import "OpenGLSprite.h"
27 NSMutableDictionary *stringAttributes;
29 // Open GL Z value for scorebubbles
30 #define SCOREBUBBLE_SPRITE_Z    -0.30
32 @implementation ScoreBubble
34 @synthesize value           = _value;
35 @synthesize screenLocation  = _screenLocation;
36 @synthesize animationCount  = _animationCount;
37 @synthesize image           = _image;
39 + (ScoreBubble *)scoreWithValue:(int)value at:(NSPoint)location duration:(int)count
41   // FIXME: possible bug, is it [self alloc] or [[self class] alloc]???
42   ScoreBubble *scoreBubble = [[self alloc] initWithValue:value
43                                                       at:location
44                                                 duration:count];
45   return [scoreBubble autorelease];
48 -(id)initWithValue:(int)value at:(NSPoint)location duration:(int)count
50     NSString *str= [NSString stringWithFormat:@"%d", value];
51     NSSize strsize;
52   self = [super init];
53     if (self != nil) {
54         if (!stringAttributes) {
55             stringAttributes= [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"ArialNarrow-Bold" size:18],
56                 NSFontAttributeName, [NSColor blackColor], NSForegroundColorAttributeName, NULL];
57             [stringAttributes retain];
58         }
59         strsize= [str sizeWithAttributes:stringAttributes];
60         strsize.width+= 3;
61         strsize.height+=1;
62         _value= value;
63         _screenLocation = location;
64         _screenLocation.x -= strsize.width / 2;
65         _screenLocation.y -= strsize.height / 2;
66         _animationCount= count;
67         _image = [[NSImage alloc] initWithSize:strsize];
68         [_image lockFocus];
69         [stringAttributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];        
70         [str drawAtPoint:NSMakePoint(2,0) withAttributes:stringAttributes];
71         [stringAttributes setObject:[NSColor yellowColor] forKey:NSForegroundColorAttributeName];       
72         [str drawAtPoint:NSMakePoint(1,1) withAttributes:stringAttributes];
73         [_image unlockFocus];
75         // Open GL
76       NSSize imageSize = _image.size;
77         sprite = [[OpenGLSprite alloc] initWithImage:_image
78                                        cropRectangle:NSMakeRect(0, 0, imageSize.width, imageSize.height)
79                                                 size:imageSize];
80     }
81   
82     return self;
85 - (void)dealloc
87   [_image release];
88   [sprite release];
89   
90   [super dealloc];
93 -(void)drawImage
95     float alpha = (float)_animationCount/20;
96     if (alpha>1) {
97         alpha= 1;
98     }
99   
100     [_image compositeToPoint:_screenLocation
101                   operation:NSCompositeSourceOver
102                    fraction:alpha];
105 -(void)drawSprite
107     float alpha= (float)_animationCount/20;
108     if (alpha>1) {
109         alpha= 1;
110     }
111     [sprite blitToX:_screenLocation.x
112                   Y:_screenLocation.y
113                   Z:SCOREBUBBLE_SPRITE_Z
114               Alpha:alpha];
117 -(int)animate
119   if (_animationCount > 0) {
120     _screenLocation.y++;
121     _animationCount--;
122   }
123   
124   return _animationCount;
127 @end