1 /***************************************************************************
2 					movementmanager.cpp  -  description
3 							-------------------
4 	begin                : may 16th, 2004
5 	copyright            : (C) 2004-2007 by Duong Khang NGUYEN
6 	email                : neoneurone @ gmail com
7 
8 	$Id: movementmanager.cpp 375 2008-10-28 14:47:15Z neoneurone $
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   any later version.                                                    *
17  *                                                                         *
18  ***************************************************************************/
19 
20 // Useful enumerations
21 #include "opencity_direction.h"
22 
23 // OpenCity headers
24 #include "movement.h"
25 #include "movementmanager.h"
26 #include "graphicmanager.h"
27 
28 
29    /*=====================================================================*/
MovementManager(const GraphicManager * gm,const Map * map)30 MovementManager::MovementManager(
31 	const GraphicManager* gm,
32 	const Map* map):
33 _uiCount(0),
34 pcGraphicMgr( gm ),
35 pcMap( map )
36 {
37 	OPENCITY_DEBUG("ctor");
38 
39    // tab movement initialization
40 	for (uint i = 0; i < OC_MOVEMENT_MAX; i++) {
41 		tabmvt[ i ] = NULL;
42 	}
43 }
44 
45 
46    /*=====================================================================*/
~MovementManager()47 MovementManager::~MovementManager()
48 {
49 	OPENCITY_DEBUG("dtor");
50 
51 	for (uint i = 0; i < OC_MOVEMENT_MAX; i++) {
52 		delete tabmvt[ i ];
53 		tabmvt[ i ] = NULL;		// Safe
54 	}
55 	_uiCount = 0;
56 }
57 
58 
59    /*=====================================================================*/
60 const int
Add(Movement * const pNew)61 MovementManager::Add(
62 	Movement* const pNew )
63 {
64 	for (uint i = 0; i < OC_MOVEMENT_MAX; i++) {
65 		if (tabmvt[ i ] == NULL) {
66 			tabmvt[ i ] = pNew;
67 			++_uiCount;
68 			return i;
69 		}
70 	}
71 
72 	return -1;
73 }
74 
75 
76    /*=====================================================================*/
77 const bool
IsFull()78 MovementManager::IsFull()
79 {
80 	return _uiCount == OC_MOVEMENT_MAX;
81 }
82 
83 
84    /*=====================================================================*/
85 void
Remove(const int ciIndex)86 MovementManager::Remove(
87 	const int ciIndex )
88 {
89 // IF the user has specified the index of the element
90 // THEN only remove that element
91 	if ((ciIndex > -1) && (ciIndex < OC_MOVEMENT_MAX )) {
92 		if (tabmvt[ ciIndex ] != NULL) {
93 			delete tabmvt[ ciIndex ];
94 			tabmvt[ ciIndex ] = NULL;
95 			--_uiCount;
96 		}
97 		return;
98 	}
99 
100 // ELSE remove all the contained movement
101 	for (uint i = 0; i < OC_MOVEMENT_MAX; i++) {
102 		if (tabmvt[ i ] != NULL) {
103 			delete tabmvt[ i ];
104 			tabmvt[ i ] = NULL;
105 		}
106 	}
107 	_uiCount = 0;
108 }
109 
110 
111    /*=====================================================================*/
112 void
Move(const int ciIndex)113 MovementManager::Move(
114 	const int ciIndex )
115 {
116 // IF the user has specified the index of the element
117 // THEN only call that element
118 	if ((ciIndex > -1) && (ciIndex < OC_MOVEMENT_MAX )) {
119 		if (tabmvt[ ciIndex ] != NULL and tabmvt[ ciIndex ]->Move() == false) {
120 			delete tabmvt[ ciIndex ];
121 			tabmvt[ ciIndex ] = NULL;
122 			--_uiCount;
123 		}
124 		return;
125 	}
126 
127 // ELSE move all the contained movement
128 	for (uint i = 0; i < OC_MOVEMENT_MAX; i++) {
129 		if (tabmvt[ i ] != NULL and tabmvt[ i ]->Move() == false) {
130 			delete tabmvt[ i ];
131 			tabmvt[ i ] = NULL;
132 			--_uiCount;
133 		}
134 	}
135 }
136 
137 
138    /*=====================================================================*/
139 void
Display(const int ciIndex)140 MovementManager::Display(
141 	const int ciIndex )
142 {
143 // IF the user has specified the index of the element
144 // THEN only call that element
145 	if ((ciIndex > -1) && (ciIndex < OC_MOVEMENT_MAX )) {
146 		if (tabmvt[ ciIndex ] != NULL)
147 			this->pcGraphicMgr->Display(
148 				tabmvt[ ciIndex ]->_fCurrentW,
149 				tabmvt[ ciIndex ]->_fCurrentL,
150 				tabmvt[ ciIndex ]->_fCurrentH,
151 				tabmvt[ ciIndex ] );
152 		return;
153 	}
154 
155 // ELSE display all the contained movement
156 	for (uint i = 0; i < OC_MOVEMENT_MAX; i++) {
157 		if (tabmvt[ i ] != NULL)
158 			this->pcGraphicMgr->Display(
159 				tabmvt[ i ]->_fCurrentW,
160 				tabmvt[ i ]->_fCurrentL,
161 				tabmvt[ i ]->_fCurrentH,
162 				tabmvt[ i ] );
163 	}
164 }
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198