Minor reorg of unit crap.
authordsc <david.schoonover@gmail.com>
Mon, 6 Jun 2011 08:38:55 +0000 (01:38 -0700)
committerdsc <david.schoonover@gmail.com>
Mon, 6 Jun 2011 08:38:55 +0000 (01:38 -0700)
32 files changed:
src/Tanks.h
src/TanksMacros.h
src/game/QQGame.h
src/game/QQGame.mm
src/game/QQThing.h
src/game/ability/QQStats.h [moved from src/game/ability/QQStat.h with 100% similarity]
src/game/ability/QQStats.mm [moved from src/game/ability/QQStat.mm with 100% similarity]
src/game/actor/QQActors.h [deleted file]
src/game/actor/QQIUnit.h [deleted file]
src/game/unit/QQActor.h [moved from src/game/actor/QQActor.h with 93% similarity]
src/game/unit/QQActor.mm [moved from src/game/actor/QQActor.mm with 89% similarity]
src/game/unit/QQActorDelegate.h [moved from src/game/actor/QQActorDelegate.h with 100% similarity]
src/game/unit/QQActors.h [new file with mode: 0644]
src/game/unit/QQBullet.h [moved from src/game/actor/bullet/QQBullet.h with 85% similarity]
src/game/unit/QQBullet.mm [moved from src/game/actor/bullet/QQBullet.mm with 100% similarity]
src/game/unit/QQTank.h [moved from src/game/actor/unit/QQTank.h with 84% similarity]
src/game/unit/QQTank.mm [moved from src/game/actor/unit/QQTank.mm with 98% similarity]
src/game/unit/QQUnit.h [moved from src/game/actor/QQUnit.h with 89% similarity]
src/game/unit/QQUnit.mm [moved from src/game/actor/QQUnit.mm with 98% similarity]
src/physics/QQWorld.h
src/physics/event/QQContactNotification.h
src/physics/event/QQPhysicalEvents.mm
src/qq/event/QQNotification.h [moved from src/qq/QQNotification.h with 100% similarity]
src/qq/event/QQNotification.mm [moved from src/qq/QQNotification.mm with 98% similarity]
src/qq/event/QQNotificationCenter.h [moved from src/qq/QQNotificationCenter.h with 94% similarity]
src/qq/event/QQNotificationCenter.mm [moved from src/qq/QQNotificationCenter.mm with 98% similarity]
src/qq/event/QQNotificationProxy.h [moved from src/qq/QQNotificationProxy.h with 92% similarity]
src/qq/event/QQNotificationProxy.mm [moved from src/qq/QQNotificationProxy.mm with 98% similarity]
src/qq/event/QQNotifier.h [moved from src/qq/QQNotifier.h with 95% similarity]
src/qq/event/QQNotifiers.h [moved from src/qq/QQNotifiers.h with 94% similarity]
src/qq/event/QQObservable.h [moved from src/qq/QQObservable.h with 100% similarity]
tanks.xcodeproj/project.pbxproj

index 6a4d3c0..a2a448b 100644 (file)
@@ -1,9 +1,14 @@
 #ifndef TANKS_H
 #define TANKS_H
 
+#ifdef DEBUG
+#define TANKS_DEBUG_LOG 1
+#else
 #define TANKS_DEBUG_LOG 0
+#endif // DEBUG
 
-#endif
+#endif // TANKS_H
 
 #import "TanksMacros.h"
+#import "qq/NSSet+QQExtensions.h"
 #import "render/QQSparrowExtensions.h"
index e85b6b2..b36a57c 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef TANKS_MACROS_H
 #define TANKS_MACROS_H
 
-#define qqBoolToString(__val) ((__val)?("YES"):("NO"))
+#define b2s(__val) ((__val)?("YES"):("NO"))
 
 
 #endif
\ No newline at end of file
index 1c5e0a4..f69644d 100644 (file)
@@ -1,19 +1,21 @@
 #import "Sparrow.h"
 
-#import "qq/QQNotificationCenter.h"
+#import "qq/event/QQNotificationCenter.h"
+
 #import "physics/QQWorld.h"
 #import "physics/debug/QQPhysicsDebugView.h"
+
 #import "game/QQGameTime.h"
-#import "game/actor/QQActor.h"
+#import "game/unit/QQActor.h"
 #import "game/map/QQLevel.h"
 
 
+
 @interface QQGame : QQNotificationCenter <QQGameTime> {
 @private
     float _now;
     float _elapsed;
     long  _ticks;
-    long  _realtime;
     
     BOOL _running;
     BOOL _debugDrawingEnabled;
 @property (nonatomic, readonly, retain) NSSet* actors;
 
 
-- (void) tick;
+- (void) tick:(float)elapsed;
 
+/** Alerts the game to a new actor.  */
 - (QQGame*) addActor:(QQActor*)actor;
-// - (QQGame*) addActorAndShape:(QQActor*)actor;
 - (QQGame*) removeActor:(QQActor*)actor;
-- (QQGame*) destroyActor:(QQActor*)actor;
 
+/** Starts/unpauses the game. */
 - (void) start;
+
+/** Pauses the game. */
 - (void) stop;
 
 
index 1a06899..ad60683 100644 (file)
@@ -1,16 +1,11 @@
 #import <Box2D/Box2D.h>
 
 #import "Tanks.h"
-#import "qq/NSSet+QQExtensions.h"
 #import "game/QQGame.h"
-#import "game/actor/QQActors.h"
+#import "game/unit/QQActors.h"
 #import "physics/event/QQContactNotification.h"
 
 
-static QQGame* _CurrentGame = NULL;
-
-
-
 @interface QQGame ()
 @property (nonatomic, readwrite, retain) NSSet* actors;
 
@@ -19,8 +14,17 @@ static QQGame* _CurrentGame = NULL;
 @end
 
 
+static QQGame* _CurrentGame = NULL;
+
 @implementation QQGame
 
++ (QQGame*) current {
+    if (!_CurrentGame)
+        [[[QQGame alloc] init]  // init assigns to singleton, but singleton is a weakref,
+         autorelease];       // XXX: so game will still be released if not retained
+    return _CurrentGame;
+}
+
 - (id) init {
     if (_CurrentGame) {
         [self release];
@@ -32,6 +36,8 @@ static QQGame* _CurrentGame = NULL;
     }
     
     if ( (self = [super init]) ){
+        // We set the singleton here, in init, in case someone referenced during initialization
+        // asks for the current game (causing a loop).
         _CurrentGame = self;
         _now = 0;
         _ticks = 0;
@@ -64,7 +70,9 @@ static QQGame* _CurrentGame = NULL;
         [[[QQTank alloc] initAtX:wMax/6 y:hMax/6 width:50 height:50 color:0xFF0071] autorelease];
         [[[QQUnit alloc] initAtX:wMax/3 y:hMax/2 width:50 height:50 color:0x4596FF] autorelease];
         
+#if TANKS_DEBUG_LOG
         [_stage addEventListener:@selector(logEvent:) atObject:self forType:SP_EVENT_TYPE_ANY];
+#endif
     }
     return self;
 }
@@ -117,7 +125,22 @@ static QQGame* _CurrentGame = NULL;
 
 /// methods
 
+- (void) tick:(float)elapsed {
+    if (!self.isPaused) return;
+    _ticks++;
+    _elapsed  = elapsed;
+    _now     += elapsed;
+    
+    for (QQActor* actor in self.actors)
+        [actor tick];
+    [self.world step];
+    for (QQActor* actor in self.actors)
+        [actor draw];
+}
+
 - (QQGame*) addActor:(QQActor*)actor {
+    // Create a new copy of the mutated set on change to avoid the need to copy
+    // on every iteration, which happens WAAAY more often
     self.actors = [self.actors setByAddingObject:actor];
 //    if (actor.isActive)
 //        _awake = [_awake setByAddingObject:actor];
@@ -129,6 +152,8 @@ static QQGame* _CurrentGame = NULL;
 }
 
 - (QQGame*) removeActor:(QQActor*)actor {
+    // Create a new copy of the mutated set on change to avoid the need to copy
+    // on every iteration, which happens WAAAY more often
     self.actors = [self.actors setByRemovingObject:actor];
 //    if (actor.isActive)
 //        _awake = [_awake setByRemovingObject:actor];
@@ -136,58 +161,27 @@ static QQGame* _CurrentGame = NULL;
 //        _units = [_units   setByRemovingObject:actor];
 //    if ([actor isKindOfClass:[QQBullet class]])
 //        _bullets = [_bullets setByRemovingObject:actor];
-    return self;
-}
-
-- (QQGame*) destroyActor:(QQActor*)actor {
     [_world destroy:actor];
-    return [self removeActor:actor];
-}
-
-
-- (void) tick {
-#if TANKS_DEBUG_LOG
-    if ((_ticks % 100) == 0) {
-        NSLog(@"[%ld] Time passed since last 100 frames: %f", _ticks, _elapsed);
-        for (QQActor* actor in self.actors) {
-            b2Vec2 v = actor.body->GetLinearVelocity();
-            NSLog(@"[%@ impulse:(%f,%f)]", actor, v.x,v.y);
-        }
-    }
-#endif
-    
-    for (QQActor* actor in self.actors)
-        [actor tick];
-    [self.world step];
-    for (QQActor* actor in self.actors)
-        if (!actor.dead) [actor draw];
+    return self;
 }
 
-
 - (void) start {
-    if (!_running) {
-        _running = YES;
-        [_stage addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
-    }
+    if (_running) return;
+    _running = YES;
+    [_stage addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
 }
 
 - (void) stop {
-    if (_running) {
-        [_stage removeEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
-        _running = NO;
-    }
+    if (!_running) return;
+    [_stage removeEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
+    _running = NO;
 }
 
 
 /// event handlers
 
 - (void) onEnterFrame:(SPEnterFrameEvent*)event {
-    if (_running) {
-        _ticks++;
-        _elapsed = event.passedTime;
-        _now += _elapsed; 
-        [self tick];
-    }
+    [self tick:event.passedTime];
 }
 
 - (void) onCollide:(QQContactNotification*)msg {
@@ -195,23 +189,17 @@ static QQGame* _CurrentGame = NULL;
 }
 
 - (void) logEvent:(SPEvent*)evt {
+    // ignore enter-frame events, as they happen constantly
     if ([evt.type isEqualToString:SP_EVENT_TYPE_ENTER_FRAME]) return;
     NSLog(@"%@", evt);
 }
 
+
 - (NSString*) description {
-    return [NSString stringWithFormat:@"<%@: %x> {running=%s, #actors=%i, time={now=%f, elapsed=%f, ticks=%l}}",
-            [self class], (long)self, qqBoolToString(self.isPaused), [self.actors count],
+    return [NSString stringWithFormat:@"<%@: %p> {running=%s, #actors=%i, time={now=%f, elapsed=%f, ticks=%l}}",
+            [self class], (long)self, b2s(self.isPaused), [self.actors count],
             self.now, self.elapsed, self.ticks];
 }
 
 
-+ (QQGame*) current {
-    if (!_CurrentGame)
-        [[[QQGame alloc] init]  // init assigns to singleton, but singleton is a weakref,
-            autorelease];       // XXX: so game will still be released if not retained...
-    return _CurrentGame;
-}
-
-
 @end
\ No newline at end of file
index e10aa65..8ee089f 100644 (file)
@@ -1,4 +1,4 @@
-#import "qq/QQObservable.h"
+#import "qq/event/QQObservable.h"
 
 @class QQGame;
 @class QQWorld;
diff --git a/src/game/actor/QQActors.h b/src/game/actor/QQActors.h
deleted file mode 100644 (file)
index f7bad80..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-#import "game/actor/QQActor.h"
-#import "game/actor/QQActorDelegate.h"
-#import "game/actor/QQUnit.h"
-#import "game/actor/bullet/QQBullet.h"
-#import "game/actor/unit/QQTank.h"
diff --git a/src/game/actor/QQIUnit.h b/src/game/actor/QQIUnit.h
deleted file mode 100644 (file)
index 15878ea..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#import "game/actor/QQActor.h"
-#import "game/QQThing.h"
-
-
-@protocol QQUnit <QQThing>
-
-@property (nonatomic, readonly) BOOL canAttack;
-
-- (void) attackTarget:(QQActor*)actor;
-- (void) attackToward:(CGPoint)point;
-- (void) attackTowardX:(float)x y:(float)y;
-
-
-@end
-
-//typedef id<QQUnit> QQUnit;
similarity index 93%
rename from src/game/actor/QQActor.h
rename to src/game/unit/QQActor.h
index 4eaaf09..b5dc695 100644 (file)
@@ -1,11 +1,11 @@
 #include <Box2D/Box2D.h>
 
-#import "qq/QQNotificationProxy.h"
+#import "qq/event/QQNotificationProxy.h"
 #import "game/QQThing.h"
 #import "game/QQActive.h"
 #import "game/QQPhysical.h"
 #import "game/QQDisplayable.h"
-#import "game/actor/QQActorDelegate.h"
+#import "game/unit/QQActorDelegate.h"
 #import "physics/QQWorld.h"
 
 @class QQGame;
similarity index 89%
rename from src/game/actor/QQActor.mm
rename to src/game/unit/QQActor.mm
index 241226b..8421992 100644 (file)
 /// methods
 
 - (void) destroy {
-    NSLog(@"retainCount=%i", [self retainCount]);
-    if (!self.dead) {
-        _dead = YES;
-        self.shape = nil;
-        [self.game destroyActor:self];
-        NSLog(@"%@ \v\tretainCount=%i \v\tgame=%@", self, [self retainCount], self.game);
-    }
+    if (self.dead) return;
+    
+    _dead = YES;
+    self.shape = nil;
+    [self.game removeActor:self];
 }
 
 - (void) tick {
-    if (!self.dead) {
-        [self.cooldowns makeObjectsPerformSelector:@selector(tick:)withObject:self.game.time];
-        if (self.isActive) [self act];
-    }
+    if (self.dead) return;
+    
+    [self.cooldowns makeObjectsPerformSelector:@selector(tick:) withObject:self.game.time];
+    if (self.isActive)
+        [self act];
 }
 
 - (void) act {}
 }
 
 - (void) draw {
+    if (self.dead) return;
+    
     [self updateShape];
 }
 
diff --git a/src/game/unit/QQActors.h b/src/game/unit/QQActors.h
new file mode 100644 (file)
index 0000000..1bb6ca8
--- /dev/null
@@ -0,0 +1,5 @@
+#import "game/unit/QQActor.h"
+#import "game/unit/QQActorDelegate.h"
+#import "game/unit/QQUnit.h"
+#import "game/unit/QQBullet.h"
+#import "game/unit/QQTank.h"
similarity index 85%
rename from src/game/actor/bullet/QQBullet.h
rename to src/game/unit/QQBullet.h
index 4a61675..b4a6241 100644 (file)
@@ -1,4 +1,4 @@
-#import "game/actor/QQUnit.h"
+#import "game/unit/QQUnit.h"
 
 
 @interface QQBullet : QQUnit {
similarity index 84%
rename from src/game/actor/unit/QQTank.h
rename to src/game/unit/QQTank.h
index ee8c4c1..c4c5971 100644 (file)
@@ -1,4 +1,4 @@
-#import "game/actor/QQUnit.h"
+#import "game/unit/QQUnit.h"
 #import "game/ability/QQCooldown.h"
 
 @interface QQTank : QQUnit {
similarity index 98%
rename from src/game/actor/unit/QQTank.mm
rename to src/game/unit/QQTank.mm
index 4b2622b..edd130a 100644 (file)
@@ -1,7 +1,7 @@
 #import "QQTank.h"
 #import "render/QQSparrowExtensions.h"
 #import "game/QQGame.h"
-#import "game/actor/bullet/QQBullet.h"
+#import "game/unit/QQBullet.h"
 
 
 // private interface
similarity index 89%
rename from src/game/actor/QQUnit.h
rename to src/game/unit/QQUnit.h
index e963b45..6fdee22 100644 (file)
@@ -2,8 +2,8 @@
 #import "Sparrow.h"
 
 #import "physics/QQWorld.h"
-#import "game/actor/QQActor.h"
-#import "game/actor/QQActorDelegate.h"
+#import "game/unit/QQActor.h"
+#import "game/unit/QQActorDelegate.h"
 #import "render/QQSparrowExtensions.h"
 
 
similarity index 98%
rename from src/game/actor/QQUnit.mm
rename to src/game/unit/QQUnit.mm
index f68c5fc..0959cfb 100644 (file)
@@ -1,7 +1,7 @@
 #include <Box2D/Box2D.h>
 #import "Sparrow.h"
 
-#import "game/actor/QQUnit.h"
+#import "game/unit/QQUnit.h"
 #import "game/QQGame.h"
 #import "render/QQSparrowExtensions.h"
 
index 3b47e73..54bf6c9 100644 (file)
@@ -1,6 +1,6 @@
 #include <Box2D/Box2D.h>
-#import "qq/QQNotifier.h"
-#import "qq/QQNotificationProxy.h"
+#import "qq/event/QQNotifier.h"
+#import "qq/event/QQNotificationProxy.h"
 #import "physics/event/QQPhysicalEvents.h"
 #import "physics/debug/QQGLESDebugDraw.h"
 #import "game/QQPhysical.h"
index 4c173f3..b7a815c 100644 (file)
@@ -1,6 +1,7 @@
-#include "qq/QQNotification.h"
 #include <Box2D/Box2D.h>
-#include "game/actor/QQActor.h"
+
+#import "qq/event/QQNotification.h"
+#import "game/unit/QQActor.h"
 
 
 @interface QQContactNotification : QQNotification {
@@ -8,10 +9,6 @@
     b2Contact* _contact;
 }
 
-- (id) initWithName:(NSString*)name contact:(b2Contact*)contact;
-- (id) initWithName:(NSString*)name object:(id)obj contact:(b2Contact*)contact;
-- (id) initWithName:(NSString*)name object:(id)obj userInfo:(NSDictionary*)info contact:(b2Contact*)contact;
-
 @property (nonatomic, readonly) b2Contact* contact;
 
 @property (nonatomic, readonly) QQActor* actorA;
 @property (nonatomic, readonly) b2Body* bodyB;
 
 
+- (id) initWithName:(NSString*)name contact:(b2Contact*)contact;
+- (id) initWithName:(NSString*)name object:(id)obj contact:(b2Contact*)contact;
+- (id) initWithName:(NSString*)name object:(id)obj userInfo:(NSDictionary*)info contact:(b2Contact*)contact;
+
+
 + (QQContactNotification*) notification:(NSString*)name contact:(b2Contact*)contact;
 + (QQContactNotification*) notification:(NSString*)name object:(id)obj contact:(b2Contact*)contact;
 + (QQContactNotification*) notification:(NSString*)name object:(id)obj userInfo:(NSDictionary*)info contact:(b2Contact*)contact;
index 6d722f0..bfba110 100644 (file)
@@ -1,4 +1,4 @@
-#include "QQPhysicalEvents.h"
+#import "QQPhysicalEvents.h"
 
 
 void QQContactListener::BeginContact(b2Contact* contact) {
similarity index 98%
rename from src/qq/QQNotification.mm
rename to src/qq/event/QQNotification.mm
index e4b2448..d72e3f4 100644 (file)
@@ -1,4 +1,4 @@
-#include "qq/QQNotification.h"
+#import "qq/event/QQNotification.h"
 
 
 @implementation QQNotification
similarity index 94%
rename from src/qq/QQNotificationCenter.h
rename to src/qq/event/QQNotificationCenter.h
index 65c60ec..661d909 100644 (file)
@@ -1,4 +1,4 @@
-#import "qq/QQNotifier.h"
+#import "qq/event/QQNotifier.h"
 
 @interface QQNotificationCenter : NSNotificationCenter <QQObservableNotifier> {
 @private
similarity index 98%
rename from src/qq/QQNotificationCenter.mm
rename to src/qq/event/QQNotificationCenter.mm
index 232c794..64e5703 100644 (file)
@@ -1,4 +1,4 @@
-#import "qq/QQNotificationCenter.h"
+#import "qq/event/QQNotificationCenter.h"
 
 
 @implementation QQNotificationCenter
diff --git