#include ".\charactermanager.h"

CharacterManager::CharacterManager(void){
	aStar = new AStarModified();
}

CharacterManager::~CharacterManager(void){
	for ( CharacterListIterator iter = characterList.begin();
		  iter != characterList.end();
		  iter++ ) {
		delete ( *iter );
		( *iter ) = 0;
	}
	if (aStar){
		delete aStar;
		aStar = 0;
	}
}

void CharacterManager::init( GroundManager* newGround, TextManager* newText, CollisionManager* newCollision, 
							ScreenManager* newScreen, TextureManager* newTexture,  LPDIRECT3DDEVICE9& newDevice, 
							DialogueManager* newDialogue, TriggerManager* newTrigger, SoundManager* newSound) {
	groundManager = newGround;
	textManager = newText;
	collisionManager = newCollision;
	screenManager = newScreen;
	textureManager = newTexture;
	device = newDevice;
    dialogueManager = newDialogue;
	triggerManager = newTrigger;
	aStar->setScreenManager(screenManager);
    soundManager = newSound;
}

HRESULT CharacterManager::tick() {
	HRESULT r = S_OK;

	for ( CharacterListIterator iter = characterList.begin();
		  iter != characterList.end();
		  iter++ ) {

        if((*iter)->getState() != Character::DEAD){
		    r = ( *iter )->tick();
		    if ( FAILED( r ) ) {
			    return r;
		    }
        }
        else {
			if ( ( *iter )->getType() != Character::TYPE::MAIN ) {
				r = groundManager->removeMoveableSprite( (*iter)->getCharacterSpriteStamp() );
				if ( FAILED( r ) ) {
					return r;
				}
				triggerManager->resetTrigger( (*iter)->getTrigger() );
				removeCharacter( &iter );
			}
        }
	}
	return r;
}

Character* CharacterManager::getMainCharacter() {
	for ( CharacterListIterator iter = characterList.begin();
		iter != characterList.end();
		iter++ ) {	
		Character* aChar = ( *iter );
		if ( aChar->getType() == Character::TYPE::MAIN ) {
			return aChar;
		}
	}

	return NULL;
}

void CharacterManager::addCharacter( Character* newCharacter ) {
	characterList.push_back( newCharacter );
	groundManager->addMoveableSprite( newCharacter->getCharacterSpriteStamp() );
}

CharacterList* CharacterManager::getCharacters() {
	return &characterList;
}

CharacterManager::CAN_MOVE CharacterManager::canMoveCharacter( Character* moveChar, GameCore::DIRECTION aDirection ) {
    CharacterManager::CAN_MOVE result = CharacterManager::YES;

	//// figure new bounding box
	RECT* newBoundingBox = new RECT();
	newBoundingBox->bottom = moveChar->getBoundingBox()->bottom + moveChar->getY();
	newBoundingBox->top = moveChar->getBoundingBox()->top + moveChar->getY();
	newBoundingBox->left = moveChar->getBoundingBox()->left + moveChar->getX();
	newBoundingBox->right = moveChar->getBoundingBox()->right + moveChar->getX();
	//// Update for which way the character wants to move
	if ( aDirection == GameCore::DIRECTION ::DOWN ) {
		newBoundingBox->bottom += Y_INCREMENT;
		newBoundingBox->top += Y_INCREMENT;
	} else if ( aDirection == GameCore::DIRECTION ::UP ) {
		newBoundingBox->bottom -= Y_INCREMENT;
		newBoundingBox->top -= Y_INCREMENT;
	} else if ( aDirection == GameCore::DIRECTION ::RIGHT ) {
		newBoundingBox->right += X_INCREMENT;
		newBoundingBox->left += X_INCREMENT;
	} else if ( aDirection == GameCore::DIRECTION ::LEFT ) {
		newBoundingBox->right -= X_INCREMENT;
		newBoundingBox->left -= X_INCREMENT;
	}

	//// Check the characters
	lastCollider = intersectCharacter( moveChar, newBoundingBox );
	if ( lastCollider != NULL ) {
        result = CharacterManager::NO;

/* collision walk fix
		collided->collide( moveChar );
		moveChar->collide( collided );

        collided->setAttackedFrom(moveChar->getFacing());
        collided->pushState(Character::STATE::ATTACKED);
*/

	}
    //// Check the tiles
    else if(intersectTiles( moveChar, newBoundingBox, aDirection )){
        result = CharacterManager::NO_CONDITIONAL;
    }
    if(newBoundingBox){
        delete newBoundingBox;
        newBoundingBox = 0;
    }
    if(!result && (moveChar == getMainCharacter())){
        dialogueManager->removeDialogue();
    }
	return result;
}

void CharacterManager::moveCharacter( Character* moveChar, GameCore::DIRECTION aDirection ) {
    CharacterManager::CAN_MOVE canMove = canMoveCharacter(moveChar, aDirection);

	if( moveChar->getState() != Character::ATTACK       && 
        moveChar->getState() != Character::ATTACKED     && 
        moveChar->getState() != Character::ATTACK_READY && 
        moveChar->getState() != Character::MAGIC        &&
        moveChar->getState() != Character::CASTING      &&
        moveChar->getState() != Character::WALKING      &&
        moveChar->getState() != Character::DYING        &&
        moveChar->getState() != Character::DEAD         ){
		moveChar->pushState( Character::WALKING );
	}

    switch (canMove){
        case CharacterManager::YES:
            moveChar->move( aDirection );
            break;
        case CharacterManager::NO_CONDITIONAL:
            if(moveChar->getType() == Character::TYPE::PROJECTILE){
                moveChar->pushState(Character::STATE::DEAD);
            }else {
                if(aDirection == GameCore::DIRECTION ::UP || aDirection == GameCore::DIRECTION ::DOWN){
                    if(canMoveCharacter(moveChar, GameCore::DIRECTION ::LEFT) == CharacterManager::YES){
                        moveChar->move( GameCore::DIRECTION ::LEFT, aDirection );
                    }else if(canMoveCharacter(moveChar, GameCore::DIRECTION ::RIGHT) == CharacterManager::YES){
                        moveChar->move( GameCore::DIRECTION ::RIGHT, aDirection );
                    }
                } else if(aDirection == GameCore::DIRECTION ::LEFT || aDirection == GameCore::DIRECTION ::RIGHT){
                    if(canMoveCharacter(moveChar, GameCore::DIRECTION ::UP) == CharacterManager::YES){
                        moveChar->move( GameCore::DIRECTION ::UP, aDirection );
                    }else if(canMoveCharacter(moveChar, GameCore::DIRECTION ::DOWN) == CharacterManager::YES){
                        moveChar->move( GameCore::DIRECTION ::DOWN, aDirection );
                    }
                }
            }
            break;
        case CharacterManager::NO:

            if(lastCollider){
                if(moveChar == getMainCharacter()){
                    if( lastCollider->getType() == Character::TYPE::TOWNPERSON  || 
                        lastCollider->getType() == Character::TYPE::WORLDPERSON ){
                        lastCollider->collide( moveChar );
                    } else {
                        moveChar->setAttackedFrom(moveChar->getFacing());
                        moveChar->collide( lastCollider );
                    }
                } else if (moveChar->getType() == Character::TYPE::PROJECTILE  ){
                    if( lastCollider->getType() == Character::TYPE::WORLDPERSON ||
                        lastCollider->getType() == Character::TYPE::TOWNPERSON  ){
                        moveChar->pushState(Character::STATE::DEAD);
                    } else {
                        lastCollider->collide( moveChar );
		                lastCollider->setAttacker(moveChar);
                        lastCollider->setAttackedFrom(moveChar->getFacing());
                        if(lastCollider->getState() != Character::STATE::ATTACKED){
                            lastCollider->pushState(Character::STATE::ATTACKED);
                        }
                        moveChar->setAttackee(lastCollider);
                        moveChar->pushState(Character::STATE::ATTACK);
                    }
                }
            }

            break;
    }
	moveChar->setClip( aDirection );

	if( moveChar->getType() == Character::TYPE::MAIN ) {
        RECT* rect = new RECT();
        rect->bottom = moveChar->getBoundingBox()->bottom + moveChar->getY();
        rect->top = moveChar->getBoundingBox()->top + moveChar->getY();
        rect->left = moveChar->getBoundingBox()->left + moveChar->getX();
        rect->right = moveChar->getBoundingBox()->right + moveChar->getX();
        triggerManager->checkTriggerCollisions( rect );
        if (rect){
            delete rect;
            rect = 0;
        }
    }
}

bool CharacterManager::intersectTiles( Character* moveChar, RECT* newBounds, GameCore::DIRECTION aDirection ) {
	// figure old bounding center
	POINT* boundCenter = new POINT();
	RECT* oldBoundingBox = moveChar->getBoundingBox();
	boundCenter->x = ( oldBoundingBox->right - oldBoundingBox->left ) / 2 + moveChar->getX() + oldBoundingBox->left;
	boundCenter->y = ( oldBoundingBox->bottom - oldBoundingBox->top ) / 2 + moveChar->getY() + oldBoundingBox->top;

	bool amMainChar;
    if ( moveChar->getType() == Character::TYPE::MAIN ) {
        amMainChar = true;
    } else {
        amMainChar = false;
    }

	// get tile x and y
	TILE* aTile = screenManager->getTileFromWorldPoint( boundCenter );

	// figure 3 possible tile intersections
	TILE* tile1 = new TILE();
	TILE* tile2 = new TILE();
	TILE* tile3 = new TILE();

	// test which direction we're headed
	if ( aDirection == GameCore::DIRECTION ::UP ) {
		// check where we are to tell what the next tiles are
		if ( aTile->y % 2 == 0 ) {
			// Even tile
			tile1->x = aTile->x - 1;
			tile1->y = aTile->y - 1;
			tile2->x = aTile->x;
			tile2->y = aTile->y - 2;
			tile3->x = aTile->x;
			tile3->y = aTile->y - 1;
		} else {
			// Odd tile
			tile1->x = aTile->x;
			tile1->y = aTile->y - 1;
			tile2->x = aTile->x;
			tile2->y = aTile->y - 2;
			tile3->x = aTile->x + 1;			
			tile3->y = aTile->y - 1;
		}
	} else if ( aDirection == GameCore::DIRECTION ::DOWN ) {
		// check where we are to tell what the next tiles are
		if ( aTile->y % 2 == 0 ) {
			// Even tile
			tile1->x = aTile->x - 1;
			tile1->y = aTile->y + 1;
			tile2->x = aTile->x;
			tile2->y = aTile->y + 2;
			tile3->x = aTile->x;
			tile3->y = aTile->y + 1;
		} else {
			// Odd tile
			tile1->x = aTile->x;
			tile1->y = aTile->y + 1;
			tile2->x = aTile->x;
			tile2->y = aTile->y + 2;
			tile3->x = aTile->x + 1;			
			tile3->y = aTile->y + 1;
		}
	} else if ( aDirection == GameCore::DIRECTION ::LEFT ) {
		// check where we are to tell what the next tiles are
		if ( aTile->y % 2 == 0 ) {
			// Even tile
			tile1->x = aTile->x - 1;
			tile1->y = aTile->y - 1;
			tile2->x = aTile->x - 1;
			tile2->y = aTile->y;
			tile3->x = aTile->x - 1;
			tile3->y = aTile->y + 1;
		} else {
			// Odd tile
			tile1->x = aTile->x;
			tile1->y = aTile->y - 1;
			tile2->x = aTile->x - 1;
			tile2->y = aTile->y;
			tile3->x = aTile->x;			
			tile3->y = aTile->y + 1;
		}
	} else if ( aDirection == GameCore::DIRECTION ::RIGHT ) {
		// check where we are to tell what the next tiles are
		if ( aTile->y % 2 == 0 ) {
			// Even tile
			tile1->x = aTile->x;
			tile1->y = aTile->y - 1;
			tile2->x = aTile->x + 1;
			tile2->y = aTile->y;
			tile3->x = aTile->x;
			tile3->y = aTile->y + 1;
		} else {
			// Odd tile
			tile1->x = aTile->x + 1;
			tile1->y = aTile->y - 1;
			tile2->x = aTile->x + 1;
			tile2->y = aTile->y;
			tile3->x = aTile->x + 1;			
			tile3->y = aTile->y + 1;
		}
	}

    TilePoints* tilePoints = 0;
    bool collided = false;

    if (!collided){
		tilePoints = collisionManager->getTilePoints(tile1->x, tile1->y);

        RECT* newBoundsForTile = new RECT();
		newBoundsForTile->top = newBounds->top - tilePoints->getTop()->y;
        newBoundsForTile->bottom = newBounds->bottom - tilePoints->getTop()->y;
		newBoundsForTile->left = newBounds->left - tilePoints->getLeft()->x;
        newBoundsForTile->right = newBounds->right - tilePoints->getLeft()->x;

		collided = collisionManager->checkForCollision(newBounds, tilePoints, aDirection );
		if ( collided ) {
			collided = !textureManager->getWalkability( screenManager->getTextureId( tile1 ), newBoundsForTile, amMainChar );
		}

        if ( newBoundsForTile ) {
            delete newBoundsForTile;
            newBoundsForTile = NULL;
        }
    }
    if(tilePoints){
        delete tilePoints;
        tilePoints = 0;
    }
    if (!collided){
		tilePoints = collisionManager->getTilePoints(tile2->x, tile2->y);

        RECT* newBoundsForTile = new RECT();
		newBoundsForTile->top = newBounds->top - tilePoints->getTop()->y;
        newBoundsForTile->bottom = newBounds->bottom - tilePoints->getTop()->y;
		newBoundsForTile->left = newBounds->left - tilePoints->getLeft()->x;
        newBoundsForTile->right = newBounds->right - tilePoints->getLeft()->x;

		collided = collisionManager->checkForCollision(newBounds, tilePoints, aDirection );
		if ( collided ) {
			collided = !textureManager->getWalkability( screenManager->getTextureId( tile2 ), newBoundsForTile, amMainChar );
		}

        if ( newBoundsForTile ) {
            delete newBoundsForTile;
            newBoundsForTile = NULL;
        }
    }
    if(tilePoints){
        delete tilePoints;
        tilePoints = 0;
    }
    if (!collided){
		tilePoints = collisionManager->getTilePoints(tile3->x, tile3->y);

        RECT* newBoundsForTile = new RECT();
		newBoundsForTile->top = newBounds->top - tilePoints->getTop()->y;
        newBoundsForTile->bottom = newBounds->bottom - tilePoints->getTop()->y;
		newBoundsForTile->left = newBounds->left - tilePoints->getLeft()->x;
        newBoundsForTile->right = newBounds->right - tilePoints->getLeft()->x;

		collided = collisionManager->checkForCollision(newBounds, tilePoints, aDirection );
		if ( collided ) {
			collided = !textureManager->getWalkability( screenManager->getTextureId( tile3 ), newBoundsForTile, amMainChar );
		}

        if ( newBoundsForTile ) {
            delete newBoundsForTile;
            newBoundsForTile = NULL;
        }
    }
    if(tilePoints){
        delete tilePoints;
        tilePoints = 0;
    }
	// Check our current tile since we're using masks
	if (!collided){
		tilePoints = collisionManager->getTilePoints(aTile->x, aTile->y);

        RECT* newBoundsForTile = new RECT();
		newBoundsForTile->top = newBounds->top - tilePoints->getTop()->y;
        newBoundsForTile->bottom = newBounds->bottom - tilePoints->getTop()->y;
		newBoundsForTile->left = newBounds->left - tilePoints->getLeft()->x;
        newBoundsForTile->right = newBounds->right - tilePoints->getLeft()->x;

		// We definately collide with the current tile, so go ahead and check it
		collided = !textureManager->getWalkability( screenManager->getTextureId( aTile ), newBoundsForTile, amMainChar );

        if ( newBoundsForTile ) {
            delete newBoundsForTile;
            newBoundsForTile = NULL;
        }
    }
    if(tilePoints){
        delete tilePoints;
        tilePoints = 0;
    }

    return collided;
}

Character* CharacterManager::intersectCharacter( Character* aCharacter, RECT* newBounds )
{
	Character* intersected = NULL;

	// Loop over the characters testing their bounding box against the bounding box passed in
	for ( CharacterListIterator iter = characterList.begin();
		iter != characterList.end();
		iter++ ) {	
		Character* tester = ( *iter );
		if ( tester != aCharacter && aCharacter->getState() != Character::DEAD     && 
                                     aCharacter->getState() != Character::DYING    &&
                                     aCharacter->getState() != Character::CREATION &&
                                     tester->getState() != Character::DEAD     && 
                                     tester->getState() != Character::DYING    &&
                                     tester->getState() != Character::CREATION ){
			RECT* testerBounds = tester->getBoundingBox();
			RECT* testerScreenBounds = new RECT();
			testerScreenBounds->left = tester->getX() + testerBounds->left;
			testerScreenBounds->right = tester->getX() + testerBounds->right;
			testerScreenBounds->top = tester->getY() + testerBounds->top;
			testerScreenBounds->bottom = tester->getY() + testerBounds->bottom;

			bool collided = collisionManager->checkForCollision( newBounds, testerScreenBounds );
			if ( testerScreenBounds ) {
				delete testerScreenBounds;
				testerScreenBounds = NULL;
			}

			if ( collided ) {
				intersected = ( *iter );
				break;
			} 
		}
	}

	return intersected;
}

void CharacterManager::drawBounds( LPDIRECT3DDEVICE9& device ) {
	// Loop over character list
	D3DRECT* dBounds = new D3DRECT();
	for ( CharacterListIterator iter = characterList.begin();
		iter != characterList.end();
		iter++ ) {	
		Character* aChar = ( *iter );

        // sprite image
		dBounds->x1 = aChar->getX() - screenManager->getScreenX();
        dBounds->x2 = dBounds->x1+2;//aChar->currentSpriteState->width + aChar->getX() - screenManager->getScreenX();
		dBounds->y1 = aChar->getY() - screenManager->getScreenY();
        dBounds->y2 = aChar->getCurrentSpriteState()->height + aChar->getY() - screenManager->getScreenY();
        device->Clear( 1, dBounds, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 0, 0), 2.0f, 0 );

        // bounding box
		RECT* bounds = aChar->getBoundingBox();
		
		dBounds->x1 = bounds->left + aChar->getX() - screenManager->getScreenX();
		dBounds->x2 = bounds->right + aChar->getX() - screenManager->getScreenX();
		dBounds->y1 = bounds->top + aChar->getY() - screenManager->getScreenY();
		dBounds->y2 = bounds->bottom + aChar->getY() - screenManager->getScreenY();
		device->Clear( 1, dBounds, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255), 2.0f, 0 );
	}
    if( dBounds ){
        delete dBounds;
        dBounds = 0;
    }

	//get direction main character is facing
	GameCore::DIRECTION facing = getMainCharacter()->getFacing();
	//Compute the attack box based on direction facing
	D3DRECT* attackBounds = new D3DRECT();
	attackBounds->y2 = getMainCharacter()->getBoundingBox()->bottom + getMainCharacter()->getY() - screenManager->getScreenY();
	attackBounds->y1 = getMainCharacter()->getBoundingBox()->top + getMainCharacter()->getY() - screenManager->getScreenY();
	attackBounds->x1 = getMainCharacter()->getBoundingBox()->left + getMainCharacter()->getX() - screenManager->getScreenX();
	attackBounds->x2 = getMainCharacter()->getBoundingBox()->right + getMainCharacter()->getX() - screenManager->getScreenX();
	int heightDiff = attackBounds->y2 - attackBounds->y1;
	int widthDiff = attackBounds->x2 - attackBounds->x1;

	//// Update for which way the character wants to move
	if ( facing == GameCore::DIRECTION::DOWN ) {
		attackBounds->y1 = attackBounds->y2;
		attackBounds->y2 += (2*heightDiff);
		attackBounds->x2 += (int)(.5 * widthDiff );
	} else if ( facing == GameCore::DIRECTION::UP ) {
		attackBounds->y2 = attackBounds->y1;
		attackBounds->y1 -= (2*heightDiff);
		attackBounds->x1 -= (int)(.5 * widthDiff);
	} else if ( facing == GameCore::DIRECTION::RIGHT ) {
		attackBounds->x1 = attackBounds->x2;
		attackBounds->x2 += (2*widthDiff);
		attackBounds->y2 += (int)(.5 * heightDiff);
	} else if ( facing == GameCore::DIRECTION::LEFT ) {
		attackBounds->x2 = attackBounds->x1;
		attackBounds->x1 -= (2*widthDiff);
		attackBounds->y2 += (int)(.5 * heightDiff);
	}

	device->Clear( 1, attackBounds, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 255, 0), 2.0f, 0 );

	if( attackBounds ) {
			delete attackBounds;
			attackBounds = NULL;
		}
}

void CharacterManager::stopMovingMainChar() {
	Character* mainCharacter = getMainCharacter();
    mainCharacter->setWalking(false);
}

CollisionManager* CharacterManager::getCollisionManager(){
	return collisionManager;
}

void CharacterManager::removeCharacter( Character* aCharacter ){
	CharacterListIterator iter = characterList.begin();
	while ((iter != characterList.end()) && ((*iter) != aCharacter)){
		iter++;
	}
    if((*iter) == aCharacter){
	    characterList.erase(iter);
    }
}

void CharacterManager::removeCharacter( CharacterListIterator* iter ){
    Character* holder = (**iter);
    *iter = characterList.erase(*iter);
    delete holder;
    holder = 0;
}

void CharacterManager::checkAttack(Character* attacker) {
	//get direction main character is facing
	GameCore::DIRECTION facing = attacker->getFacing();

	//Compute the attack box based on direction facing
	RECT* attackBounds = new RECT();
	attackBounds->bottom = attacker->getBoundingBox()->bottom + attacker->getY();
	attackBounds->top    = attacker->getBoundingBox()->top    + attacker->getY();
	attackBounds->left   = attacker->getBoundingBox()->left   + attacker->getX();
	attackBounds->right  = attacker->getBoundingBox()->right  + attacker->getX();

	int heightDiff = attackBounds->bottom - attackBounds->top;
	int widthDiff = attackBounds->right - attackBounds->left;

	//// Update for which way the character wants to move
	if ( facing == GameCore::DIRECTION::DOWN ) {
		attackBounds->top = attackBounds->bottom;
		attackBounds->bottom += (int)(2*heightDiff);
		attackBounds->right += (int)(.5 * widthDiff );
	} else if ( facing == GameCore::DIRECTION::UP ) {
		attackBounds->bottom = attackBounds->top;
		attackBounds->top -= (int)(2*heightDiff);
		attackBounds->left -= (int)(.5 * widthDiff);
	} else if ( facing == GameCore::DIRECTION::RIGHT ) {
		attackBounds->left = attackBounds->right;
		attackBounds->right += (int)(2*widthDiff);
		attackBounds->bottom += (int)(.5 * heightDiff);
	} else if ( facing == GameCore::DIRECTION::LEFT ) {
		attackBounds->right = attackBounds->left;
		attackBounds->left -= (int)(2*widthDiff);
		attackBounds->bottom += (int)(.5 * heightDiff);
	}

	// Loop over the characters testing their bounding box against the attack box
	for ( CharacterListIterator iter = characterList.begin();
		iter != characterList.end();
		iter++ ) {	
		Character* tester = ( *iter );
		if ( tester != attacker && tester->getState() != Character::DEAD        && 
								   tester->getState() != Character::DYING       &&
								   tester->getState() != Character::CREATION    &&
                                   tester->getType()  != Character::TOWNPERSON  &&
                                   tester->getType()  != Character::WORLDPERSON ){
			RECT* testerBounds = tester->getBoundingBox();
			RECT* testerScreenBounds = new RECT();
			testerScreenBounds->left = tester->getX() + testerBounds->left;
			testerScreenBounds->right = tester->getX() + testerBounds->right;
			testerScreenBounds->top = tester->getY() + testerBounds->top;
			testerScreenBounds->bottom = tester->getY() + testerBounds->bottom;

			bool collided = collisionManager->checkForCollision( attackBounds, testerScreenBounds );

			if ( collided ){
                tester->setAttackedFrom( facing );
                tester->setAttacker(attacker);
                if (tester->getState() != Character::STATE::ATTACKED){
                    tester->pushState(Character::STATE::ATTACKED);
                }
			} 

			if ( testerScreenBounds ) {
				delete testerScreenBounds;
				testerScreenBounds = NULL;
			}
		}
	}
	if( attackBounds ) {
		delete attackBounds;
		attackBounds = NULL;
	}
}

AStarModified* CharacterManager::getPathAlgorithm(){
	return aStar;
}
ScreenManager* CharacterManager::getScreenManager(){
	return screenManager;
}

void CharacterManager::removeEnemies() {
	for ( CharacterListIterator iter = characterList.begin();
		iter != characterList.end();
		iter++ ) {
			if( (*iter)->getType() == Character::ENEMY ) {
				(*iter)->pushState( Character::DEAD );
			}
		}
}
void CharacterManager::removeTownpeople() {
	for ( CharacterListIterator iter = characterList.begin();
		iter != characterList.end();
		iter++ ) {
			if( (*iter)->getType() == Character::TOWNPERSON ) {
				(*iter)->pushState( Character::DEAD );
			}
		}
}

void CharacterManager::addToMainCharacterExperience(int experience){
    Character* main;
    
    main = getMainCharacter();
    main->addExperience(experience);
}

void CharacterManager::playSound(int whichSound ) {
	soundManager->playSoundFX( whichSound );
}
