// $Revision: 1.11 $ //------------------------------------------------------------------------------ // // Copyright (C) 1994 Taligent, Inc. All rights reserved. // // Project: ConcurrentActors // File: UniverseSubsytem.h // Build/Version: 1.0.0 // // Description: The Universe subsystem lays the groundwork for the program. // It supplies an abstraction for actors (such as game pieces), // a central universe class for controlling the program's // multiple threads, and a universe thread that supports the // orderly insertion and removal of actors. // //------------------------------------------------------------------------------ #ifndef TaligentSamples_UNIVERSESUBSYSTEM #define TaligentSamples_UNIVERSESUBSYSTEM class TUniverseThread; class TUniverse; class TUniverseActorNote; class TAbstractActor; class TActorEntry; #ifndef Taligent_COREPRIMITIVECLASSES #include #endif #ifndef Taligent_NOTIFICATION #include #endif #ifndef Taligent_COLLECTIONS #include #endif #ifndef Taligent_CLOCK #include #endif #ifndef TaligentSamples_MOVEMENTSUBSYSTEM #include "MovementSubsystem.h" #endif #ifndef TaligentSamples_COLLISIONSUBSYSTEM #include "CollisionSubsystem.h" #endif #ifndef TaligentSamples_REFRESHSUBSYSTEM #include "RefreshSubsystem.h" #endif #ifndef TaligentSamples_LOCALUTILITIES #include "LocalUtilities.h" #endif //============================================================================== // TUniverseActorNote class TUniverseActorNote : public TNotification { public: MCollectibleDeclarationsMacro(TUniverseActorNote); public: TUniverseActorNote(const TInterest& interest, TAbstractActor* actor); TUniverseActorNote(TInterest* interestToAdopt, TAbstractActor* actor); TUniverseActorNote(const TUniverseActorNote& source); virtual ~TUniverseActorNote(); TUniverseActorNote& operator=(const TUniverseActorNote& source); virtual TStream& operator>>=(TStream& toStream) const; virtual TStream& operator<<=(TStream& fromStream); virtual bool IsEqual(const MCollectible* other) const; virtual long Hash() const; virtual TAbstractActor* GetActor() const; protected: TUniverseActorNote(); private: enum {kOriginalVersion}; TAbstractActor* fActor; }; //============================================================================== // TAbstractActor class TAbstractActor : public MMoving, public MColliding, public MRefreshable { public: TaligentTypeExtensionDeclarationsMacro_Abstract(TAbstractActor) public: TAbstractActor(); TAbstractActor(const TAbstractActor& source); virtual ~TAbstractActor(); TAbstractActor& operator=(const TAbstractActor& source); virtual TStream& operator>>=(TStream& toStream) const; virtual TStream& operator<<=(TStream& fromStream); virtual bool IsA(const TType& type) const; virtual void PreMove(const TTime& interval); virtual void PostMove(const TTime& interval); virtual void PreRefresh(const TTime& interval); virtual void PostRefresh(const TTime& interval); virtual TInterest* CreateUniverseAffectInterest(); virtual bool IsDead() const; virtual void Lock(); virtual void Unlock(); protected: virtual void SelfDestruct(); virtual void RegisterOffspring(TAbstractActor* living); virtual void CollisionCheckNeeded(); virtual TNotifier& GetNotifier() const; private: virtual void NotifyCollisionPossible(); enum {kOriginalVersion}; bool fIsDeadAlready; TNotifier fNotifier; TLocalSemaphore fLock; bool fCollisionCheckNeeded; }; //============================================================================== // TActorEntry class TActorEntry { public: TActorEntry(TAbstractActor* actor); virtual ~TActorEntry(); private: TAbstractActor* fActor; }; //============================================================================== // TUniverseThread class TUniverseThread : public TBoundThreadProgram { public: TUniverseThread(TUniverse* universe); virtual ~TUniverseThread(); virtual void Watch(TAbstractActor* interestToAdopt); virtual void Ignore(TAbstractActor& interestToAdopt); virtual void IgnoreAll(); protected: virtual void BoundRun(); virtual void BoundExit(); virtual void HandleNote(const TNotification& noteIn); private: enum {kOriginalVersion}; TUniverse* fUniverse; TPCQueue fQueue; TMemberFunctionConnectionTo fConnection; }; //============================================================================== // TUniverse class TUniverse { public: TUniverse(TGrafPort* refreshPort, TDequeOf* actorsCollection, TDequeOf* behaviorCollection, MRefreshable* background); virtual ~TUniverse(); virtual void AdoptActor(TAbstractActor* actor); virtual TAbstractActor* OrphanActor(TAbstractActor& actor); virtual void Start(); virtual void Stop(); virtual void Pause(); virtual void Resume(); virtual TInterest* CreateRefreshInterest(); virtual TInterest* CreateAdoptInterest(); virtual TInterest* CreateOrphanInterest(); private: void ConnectToBehaviors(TAbstractActor* actor); void DisconnectFromBehaviors(TAbstractActor& actor); void ConnectBehavior(TCollisionBehavior* behavior); void DisconnectBehavior(TCollisionBehavior& behavior); void DisconnectAllBehaviors(); MRefreshable* fBackground; TDequeOf* fActors; TDequeOf* fBehaviors; TRefreshThread fRefreshThread; TMoverThread fMoverThread; TUniverseThread fUniverseAffectThread; TNotifier fNotifier; }; #endif