28 |
#include <Foundation/NSNotification.h> |
#include <Foundation/NSNotification.h> |
29 |
#include <Foundation/NSValue.h> |
#include <Foundation/NSValue.h> |
30 |
|
|
31 |
|
#include "Ode.h" |
32 |
#include "GameEngine/HgObject.h" |
#include "GameEngine/HgObject.h" |
33 |
#include "GameEngine/HgGame.h" |
#include "GameEngine/HgGame.h" |
34 |
#include "GameEngine/HgPlayer.h" |
#include "GameEngine/HgPlayer.h" |
35 |
|
|
36 |
|
@interface HgObject (Private) |
37 |
|
- (void) _initDynamics; |
38 |
|
@end |
39 |
|
|
40 |
/** |
/** |
41 |
* HgObject stores all principal caracteritic wich belongs to the |
* HgObject stores all principal caracteritic wich belongs to the |
42 |
* current object. This class is abstract and must be redefined |
* current object. This class is abstract and must be redefined |
65 |
} |
} |
66 |
|
|
67 |
/** |
/** |
68 |
|
* Initialise the dynamics associated with the object, like its body |
69 |
|
* and bounding box. |
70 |
|
*/ |
71 |
|
- (void) _initDynamics |
72 |
|
{ |
73 |
|
_dBody = dBodyCreate ([[self game] dWorld]); |
74 |
|
dBodySetAutoDisableSF1 (_dBody, YES); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
* Initialise an object which will be contained in game. |
* Initialise an object which will be contained in game. |
79 |
* The position must be contained in the game map. |
* The position must be contained in the game map. |
80 |
* The HgPlayer's pointer is necessary to know the player who owes this object. |
* The HgPlayer's pointer is necessary to know the player who owes this object. |
87 |
/* |
/* |
88 |
* FIXME - overflow risk |
* FIXME - overflow risk |
89 |
*/ |
*/ |
90 |
static unsigned lastIdentifier = 0; |
static unsigned lastIdentifier = 1; |
91 |
|
|
92 |
NSParameterAssert (game); |
NSParameterAssert (game); |
93 |
|
NSParameterAssert (player); |
94 |
|
|
95 |
/* |
/* |
96 |
* FIXME - needs to be checked |
* FIXME - needs to be checked |
101 |
if (self != nil) |
if (self != nil) |
102 |
{ |
{ |
103 |
_identifier = lastIdentifier++; |
_identifier = lastIdentifier++; |
104 |
_delegate = nil; |
_data = nil; |
105 |
[self setPosition: position]; |
[self _initDynamics]; |
106 |
|
[self setPosition: position]; |
107 |
_game = RETAIN(game); |
_game = RETAIN(game); |
108 |
|
_player = RETAIN(player); |
|
if (player != nil) |
|
|
_playerIdentifier = [[NSNumber alloc] |
|
|
initWithUnsignedInt: [player identifier]]; |
|
|
else |
|
|
_playerIdentifier = nil; |
|
109 |
} |
} |
110 |
|
|
111 |
return self; |
return self; |
113 |
|
|
114 |
- (void) dealloc |
- (void) dealloc |
115 |
{ |
{ |
116 |
RELEASE(_delegate); |
dBodyDestroy (_dBody); |
117 |
|
RELEASE(_data); |
118 |
RELEASE(_game); |
RELEASE(_game); |
119 |
RELEASE(_playerIdentifier); |
RELEASE(_player); |
120 |
|
|
121 |
[super dealloc]; |
[super dealloc]; |
122 |
} |
} |
130 |
} |
} |
131 |
|
|
132 |
/** |
/** |
133 |
* Returns the object delegate. |
* Returns the object data. |
134 |
*/ |
*/ |
135 |
- (id) delegate |
- (id) data |
136 |
{ |
{ |
137 |
return _delegate; |
return _data; |
138 |
} |
} |
139 |
|
|
140 |
/** |
/** |
141 |
* Sets the object delegate. |
* Sets the object data. |
142 |
*/ |
*/ |
143 |
- (void) setDelegate: (id)delegate |
- (void) setData: (id)data |
144 |
{ |
{ |
145 |
_delegate = RETAIN(delegate); |
_data = RETAIN(data); |
146 |
} |
} |
147 |
|
|
148 |
/** |
/** |
154 |
return @"DEFAULT"; |
return @"DEFAULT"; |
155 |
} |
} |
156 |
|
|
|
|
|
157 |
/** |
/** |
158 |
* Returns the current object position. |
* Returns the current object position. |
159 |
*/ |
*/ |
160 |
- (coord_t) position |
- (coord_t) position |
161 |
{ |
{ |
162 |
return _position; |
return CoordFromOde (dBodyGetPosition (_dBody)); |
163 |
} |
} |
164 |
|
|
165 |
/** |
/** |
174 |
* FIXME - check position |
* FIXME - check position |
175 |
*/ |
*/ |
176 |
//[_position mapContainsPosition: position] |
//[_position mapContainsPosition: position] |
177 |
|
|
178 |
_position = position; |
dBodySetPosition (_dBody, position.x, position.y, position.z); |
179 |
|
|
180 |
|
|
181 |
[[NSNotificationCenter defaultCenter] postNotificationName: @"updateObject" |
[[NSNotificationCenter defaultCenter] postNotificationName: @"updateObject" |
182 |
object: _game |
object: _game |
183 |
userInfo: [NSDictionary dictionaryWithObject: self |
userInfo: [NSDictionary dictionaryWithObject: self |
184 |
forKey: @"HgObject"]]; |
forKey: @"HgObject"]]; |
|
|
|
185 |
} |
} |
186 |
|
|
187 |
/** |
/** |
188 |
* Returns the object orientation. |
* Returns the object orientation. |
189 |
*/ |
*/ |
190 |
- (coord_t) orientation |
- (Quaternion *) orientation |
191 |
{ |
{ |
192 |
return _orientation; |
return QuaternionFromOde (dBodyGetQuaternion (_dBody)); |
193 |
} |
} |
194 |
|
|
195 |
/** |
/** |
197 |
* Post an notification named "updateObject" containing the current game and |
* Post an notification named "updateObject" containing the current game and |
198 |
* the current object. |
* the current object. |
199 |
*/ |
*/ |
200 |
- (void) setOrientation: (coord_t)orientation |
- (void) setOrientation: (Quaternion *)orientation |
201 |
{ |
{ |
202 |
_orientation = orientation; |
dQuaternion quaternion; |
203 |
|
QuaternionToOde(orientation, quaternion); |
204 |
|
dBodySetQuaternion (_dBody, quaternion); |
205 |
|
|
206 |
[[NSNotificationCenter defaultCenter] postNotificationName: @"updateObject" |
[[NSNotificationCenter defaultCenter] postNotificationName: @"updateObject" |
207 |
object: _game |
object: _game |
222 |
*/ |
*/ |
223 |
- (HgPlayer *) player |
- (HgPlayer *) player |
224 |
{ |
{ |
225 |
return [[_game players] objectForKey: _playerIdentifier]; |
return _player; |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
|
* Returns the dynamic body associated with the object. |
230 |
|
*/ |
231 |
|
- (dBodyID) dBody |
232 |
|
{ |
233 |
|
return _dBody; |
234 |
} |
} |
235 |
|
|
236 |
@end |
@end |
242 |
*/ |
*/ |
243 |
- (void)encodeWithCoder: (NSCoder *)encoder |
- (void)encodeWithCoder: (NSCoder *)encoder |
244 |
{ |
{ |
245 |
|
NSParameterAssert (encoder); |
246 |
|
|
247 |
[encoder encodeValueOfObjCType: "I" at: &_identifier]; |
[encoder encodeValueOfObjCType: "I" at: &_identifier]; |
248 |
[encoder encodeValueOfObjCType: @encode(coord_t) at: &_position]; |
coord_t position = [self position]; |
249 |
[encoder encodeValueOfObjCType: @encode(coord_t) at: &_orientation]; |
[encoder encodeValueOfObjCType: @encode(coord_t) at: &position]; |
250 |
[encoder encodeObject: _playerIdentifier]; |
[encoder encodeValueOfObjCType: @encode(Quaternion) at: [self orientation]]; |
251 |
// [encoder encodeValueOfObjCType: @encode(NSNumber) at: &_playerIdentifier]; |
unsigned playerIdentifier = [_player identifier]; |
252 |
|
[encoder encodeValueOfObjCType: "I" at: &playerIdentifier]; |
253 |
} |
} |
254 |
|
|
255 |
- (id)initWithCoder: (NSCoder *)decoder |
- (id)initWithCoder: (NSCoder *)decoder |
256 |
{ |
{ |
257 |
|
NSParameterAssert (decoder); |
258 |
|
|
259 |
[decoder decodeValueOfObjCType: "I" at: &_identifier]; |
[decoder decodeValueOfObjCType: "I" at: &_identifier]; |
260 |
NSNumber *numIdent = [NSNumber numberWithInt: _identifier]; |
NSNumber *numIdent = [NSNumber numberWithInt: _identifier]; |
261 |
|
|
270 |
else |
else |
271 |
{ |
{ |
272 |
self = [super init]; |
self = [super init]; |
273 |
|
_data = nil; |
274 |
|
[self _initDynamics]; |
275 |
|
_game = RETAIN(currentGame); |
276 |
} |
} |
277 |
|
|
278 |
if (self != nil) |
if (self != nil) |
279 |
{ |
{ |
280 |
[decoder decodeValueOfObjCType: @encode(coord_t) at: &_position]; |
coord_t position; |
281 |
[decoder decodeValueOfObjCType: @encode(coord_t) at: &_orientation]; |
Quaternion *orientation; |
282 |
_playerIdentifier = RETAIN([decoder decodeObject]); |
unsigned playerIdentifier; |
283 |
|
NSNumber *playerKey; |
284 |
|
|
285 |
|
[decoder decodeValueOfObjCType: @encode(coord_t) at: &position]; |
286 |
|
[self setPosition: position]; |
287 |
|
[decoder decodeValueOfObjCType: @encode(Quaternion) at: orientation]; |
288 |
|
[self setOrientation: orientation]; |
289 |
|
|
290 |
|
[decoder decodeValueOfObjCType: "I" at: &playerIdentifier]; |
291 |
|
playerKey = [NSNumber numberWithUnsignedInt: playerIdentifier]; |
292 |
|
ASSIGN(_player, [[currentGame players] objectForKey: playerKey]); |
293 |
} |
} |
294 |
|
|
295 |
return self; |
return self; |