Test Code: Atoms and Managers

Discussions related to programming Transport Empire.

Moderator: Transport Empire Moderators

Post Reply
User avatar
aarona
Traffic Manager
Traffic Manager
Posts: 221
Joined: 26 May 2006 15:54
Location: Perth, Australia
Contact:

Test Code: Atoms and Managers

Post by aarona » 17 Jun 2006 16:00

I've thrown together some concepts which involve memory management and dynamic object creation.

Any comments?

Code: Select all

#include <iostream>
#include <vector>
using namespace std;

// Global vars defined in the source with main in it, and relies on
// the "*Manager.h" files

#ifdef _WRITTEN_
extern MapMgr 			MMgr;
extern RuleMgr 			RMgr;
extern CompanyManager 	CMgr;
intern GameManager		GMgr;	/// Yeah I know intern doesn't exist :D
#endif


/// For convienience!
typedef unsigned int uint;

/// aManager defines a simple set of requirements for all potential managers.
class aManager
{
	public:
		void AddAllTick();
		virtual void AddSmallTick(uint i) = 0;
		virtual uint Number() = 0;
};

/// Tells the manager to update each element in its list.
void aManager::AddAllTick()
{
	for (uint i = 0; i < Number(); i++)
		AddSmallTick(i);
}

/// Atoms are individual game entities.
class anAtom
{
	public:
		virtual void AddTick() = 0;

		/// This is a way to short-cut the process, as we skip updates on
		/// those things which aren't active.
		bool Active;
};

// This saves on writing the same thing over again.
#define MANAGERMAKE(MGR, ATM)						\
class MGR : public aManager							\
{													\
	std::vector<ATM> atom;							\
													\
	public:												\
		void AddSmallTick(uint i);							\
		ATM GetAtom(uint i) { return atom[i]; }					\
		void DeleteAtom(uint i) { atom.erase(atom.begin() + i); } 	\
		void NewAtom(ATM tmp) { atom.push_back(tmp); }			\
		uint Number() { return atom.size(); }				\
};														\
													\
void MGR::AddSmallTick(uint i) 						\
{ 													\
	ATM tmp = atom[i]; 								\
	if (tmp->Active) tmp->AddTick(); 				\
}

// These will actually be defined in their respective "*Atom.h" files
//{
class IndustryAtom 	: public anAtom {};
class StationAtom 	: public anAtom {};
class VehicleAtom 	: public anAtom {};
class RouteAtom 	: public anAtom {};
class TownAtom 		: public anAtom {};
class TrackAtom 	: public anAtom {};
class BypassAtom 	: public anAtom {};
class SignalAtom 	: public anAtom {};
//}

// One must remember that these are based on macro defintions
//{
MANAGERMAKE(IndustryManager , 	IndustryAtom* )
MANAGERMAKE(StationManager , 	StationAtom* )
MANAGERMAKE(VehicleManager , 	VehicleAtom* )
MANAGERMAKE(RouteManager , 		RouteAtom* )
MANAGERMAKE(TownManager , 		TownAtom* )
MANAGERMAKE(TrackManager , 		TrackAtom* )
MANAGERMAKE(BypassManager , 	BypassAtom* )
MANAGERMAKE(SignalManager , 	SignalAtom* )
//}

/// The GameManager manages the other managers and those items in the game
/// which are considered more volatile in terms of data.
class GameManager
{
	IndustryManager* 	IndMgr;
	StationManager* 	StnMgr;
	VehicleManager* 	VehMgr;
	RouteManager* 		RteMgr;
	TownManager* 		TwnMgr;
	TrackManager* 		TrkMgr;
	BypassManager* 		BypMgr;
	SignalManager* 		SigMgr;

	public:

	GameManager();
	void AddTick();
};

/// When the GameManager state is created in the global vars before main
/// we need to ensure that all managers are loaded up.
GameManager::GameManager()
{
	IndMgr = new IndustryManager;
	StnMgr = new StationManager;
	VehMgr = new VehicleManager;
	RteMgr = new RouteManager;
	TwnMgr = new TownManager;
	TrkMgr = new TrackManager;
	BypMgr = new BypassManager;
	SigMgr = new SignalManager;
}

/// A Tick is a frame or an update. There will be X updates per second
/// (i.e. 10 frames).
/// We then have to go though everything which is non-static and update
/// all their information. For example, a vehicle moves, an industry produces
/// goods, a town grows, etc.
void GameManager::AddTick()
{
	/// Industries produces stuff, grow, etc.
	IndMgr->AddAllTick();

	/// Stations will gather passengers or goods, etc.
	StnMgr->AddAllTick();

	/// Vehicles move naturally.
	VehMgr->AddAllTick();

	/// Towns grow, change their opinions.
	/// This might not need to be updated every tick, depends on what
	/// option it is.
	TwnMgr->AddAllTick();

	/// [Perhaps?] Signals which work on timers are non-static.
	SigMgr->AddAllTick();

	/// What age-related phenomena are associated with the following?
		/// Route Manager
	RteMgr->AddAllTick();
		/// Track Manager
	TrkMgr->AddAllTick();
		/// Bypass Manager (bypasses are bridges, tunnels, elevations)
	BypMgr->AddAllTick();

}

Post Reply

Return to “Transport Empire Coding”

Who is online

Users browsing this forum: No registered users and 2 guests