|
|
1.1 root 1:
2: #import "Spot.h"
3: #import "Thinker.h"
4: #import <appkit/color.h>
5:
6: @implementation Spot
7:
8: // This should return a float between 0 and 1
9: float frandom()
10: {
11: float val = (random() & 0x7fffffff);
12: val /= 0x7fffffff;
13: return val;
14: }
15:
16: float randBetween(float a, float b)
17: {
18: float val, scale, t;
19:
20: if (a > b)
21: { t = a; a = b; b = t;
22: }
23:
24: scale = (b-a);
25: val = scale * frandom();
26: return (a + val);
27: }
28:
29: - init
30: {
31: float r,g,b;
32: srandom(time(0));
33: r = frandom();
34: g = frandom();
35: b = frandom();
36: color = NXConvertRGBToColor(r,g,b);
37: location.x = randBetween(0,320);
38: location.y = randBetween(0,220);
39: isValid = YES;
40: return self;
41: }
42:
43: - (NXColor) color
44: { return color;
45: }
46:
47: - (NXPoint) location
48: { return location;
49: }
50:
51: - (BOOL) setLocation:(NXPoint)pnt
52: {
53: if (!isValid)
54: {
55: [[NXApp delegate] spotDidChange];
56: return NO;
57: }
58: if (pnt.x < -15) pnt.x = -15;
59: else if (pnt.x > 335) pnt.x = 335;
60: if (pnt.y < -15) pnt.y = -15;
61: else if (pnt.y > 235) pnt.y = 235;
62: location = pnt;
63:
64: [[NXApp delegate] spotDidChange];
65: return YES;
66: }
67:
68: - (BOOL) doLock
69: {
70: if (locked) return NO;
71: locked = YES;
72: return YES;
73: }
74:
75: - unlock
76: {
77: locked = NO;
78: return self;
79: }
80:
81: - invalidate
82: { isValid = NO;
83: return nil;
84: }
85:
86: - (BOOL) isValid
87: { return isValid;
88: }
89:
90: - encodeRemotelyFor:(NXConnection *)connection
91: freeAfterEncoding:(BOOL *)flagp
92: isBycopy:(BOOL)isBycopy
93: {
94: if (isBycopy) return self;
95: return [super encodeRemotelyFor:connection
96: freeAfterEncoding:flagp
97: isBycopy:isBycopy];
98: }
99:
100: - encodeUsing:(id <NXEncoding>)portal
101: {
102: //xxx should encode valid flag
103:
104: float r,g,b;
105: NXConvertColorToRGB(color, &r, &g, &b);
106: [portal encodeData:&location.x ofType:"f"];
107: [portal encodeData:&location.y ofType:"f"];
108:
109: [portal encodeData:&r ofType:"f"];
110: [portal encodeData:&g ofType:"f"];
111: [portal encodeData:&b ofType:"f"];
112:
113: return self;
114: }
115:
116: - decodeUsing:(id <NXDecoding>)portal
117: {
118: float r,g,b;
119: NXPoint p;
120: NXColor c;
121:
122: [portal decodeData:&p.x ofType:"f"];
123: [portal decodeData:&p.y ofType:"f"];
124:
125: [portal decodeData:&r ofType:"f"];
126: [portal decodeData:&g ofType:"f"];
127: [portal decodeData:&b ofType:"f"];
128:
129: c = NXConvertRGBToColor(r,g,b);
130:
131: // [self init]; //not really necessary in this case...
132: color = c;
133: location = p;
134:
135: return self;
136: }
137:
138: - (unsigned) references
139: {
140: return refs;
141: }
142:
143: - addReference
144: {
145: refs++;
146: // printf("Spot: adding reference, refs = %d\n",(int)refs);
147: return self;
148: }
149:
150: - free
151: {
152: refs--;
153: // printf("Spot: decrementing reference, refs = %d\n", (int)refs);
154: if (refs > 0) return self;
155:
156: // printf("Spot: ...and freeing\n");
157:
158: // this cleans up remote connections that still have refs to theSpot
159: [NXConnection removeObject:self];
160:
161: return [super free];
162: }
163:
164: @end
165:
166:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.