ActorManager.cpp
上传用户:lian_0917
上传日期:2013-03-24
资源大小:1151k
文件大小:6k
- /********************************************************************
- created: 2004/07/09
- created: 9:7:2004 23:57
- filename: d:WorksGameLib_sdkActorManager.cpp
- file path: d:WorksGameLib_sdk
- file base: ActorManager
- file ext: cpp
- author: lazybug
-
- purpose: 人物对象管理类的实现部分
- *********************************************************************/
- #include "StdAfx.h"
- #include ".ActorManager.h"
- #include <utility>
- #include <algorithm>
- #include "GameLogic.h"
- using namespace std ;
- CGameLogic* CPlayer::context ;
- CActorManager::CActorManager(CGameLogic* context)
- {
- m_activePlayer = NULL ;
- m_context = context ;
- CPlayer::context = context ;
- m_user_attention_player = NULL ;
- }
- CActorManager::~CActorManager(void)
- {
- std::vector<pair<std::string, CPlayer*> >::iterator pos1 ;
- for ( pos1 = m_enum_data.begin(); pos1 != m_enum_data.end(); ++pos1 )
- {
- (*pos1).second->active_in_scene = true ;
- }
- push_enum_state() ;
- // 删除所有的Player和SimpleObj
- CPlayer* actor = this->enumActor();
- while ( actor != NULL )
- {
- delete actor ;
- actor = this->enumActor() ;
- }
- pop_enum_state() ;
- std::vector< std::pair<std::string, CSimpleObj*> >::iterator pos ;
- for ( pos = m_simple_data.begin(); pos != m_simple_data.end(); ++pos)
- {
- delete (*pos).second ;
- }
- }
- void CActorManager::CreateActors()
- {
- CPlayer* myActor ;
- GameRes* res_info ;
- CSimpleObj* myObj ;
- fstream in ;
- string name ;
- m_enum_data.clear() ;
- /*
- * 下面根据列表文件创建简单对象,比如萝卜
- */
- in.open( ".\Actor\SimpleObj.profile" ) ;
- if ( in.is_open() )
- {
- in >> name ;
- while ( !in.eof() )
- {
- res_info = m_context->GetRes(name) ;
- myObj = new CSimpleObj(res_info) ;
- m_simple_data.push_back( make_pair(name, myObj) ) ;
- in >> name ;
- }
- }
- else
- {
- throw runtime_error("SimpleObj.profile not found!") ;
- }
- /*
- * 创建人物对象
- */
- myActor = new CPlayer("牛二") ;
- m_enum_data.push_back( make_pair("牛二", myActor) ) ;
- myActor = new CPlayer("老掌柜") ;
- m_enum_data.push_back( make_pair("老掌柜", myActor) ) ;
- myActor = new CPlayer( "管家") ;
- m_enum_data.push_back( make_pair("管家", myActor) ) ;
- myActor = new CPlayer( "栓柱" ) ;
- m_enum_data.push_back( make_pair("栓柱", myActor) ) ;
- myActor = new CPlayer( "厨师" ) ;
- m_enum_data.push_back( make_pair("厨师", myActor) ) ;
- myActor = new CPlayer( "虫老大" ) ;
- m_enum_data.push_back( make_pair("虫老大", myActor) ) ;
- myActor->AutoAttack(true);
- myActor = new CPlayer( "虫老二" ) ;
- m_enum_data.push_back( make_pair("虫老二", myActor) ) ;
- myActor->AutoAttack(true);
- myActor = new CPlayer( "虫老五" ) ;
- m_enum_data.push_back( make_pair("虫老五", myActor) ) ;
- myActor->AutoAttack(true);
- myActor = new CPlayer( "虫阿三" ) ;
- m_enum_data.push_back( make_pair("虫阿三", myActor) ) ;
- myActor->AutoAttack(true);
- myActor = new CPlayer( "虎头怪" ) ;
- m_enum_data.push_back( make_pair("虎头怪", myActor) ) ;
- myActor->AutoAttack(true);
- this->DisableAllActors() ;
- m_enum_pos = m_enum_data.begin() ;
- }
- CPlayer* CActorManager::enumActor()
- {
- CPlayer* ret ;
- if ( m_enum_pos == m_enum_data.end() )
- {
- SortActors() ;
- m_enum_pos = m_enum_data.begin() ;
- return NULL ;
- }
- else
- {
- ret = (*m_enum_pos).second ;
- ++m_enum_pos ;
- while ( ret->active_in_scene == false )
- {
- ret = (*m_enum_pos).second ;
- if ( m_enum_pos == m_enum_data.end() )
- {
- SortActors() ;
- m_enum_pos = m_enum_data.begin() ;
- ret = NULL ;
- break ;
- }
- m_enum_pos++ ;
- }
- return ret ;
- }
- }
- void CActorManager::DisableAllActors()
- {
- std::vector<pair<std::string, CPlayer*> >::iterator pos ;
- for ( pos = m_enum_data.begin(); pos != m_enum_data.end(); ++pos )
- {
- (*pos).second->active_in_scene = false ;
- }
- }
- CPlayer& CActorManager::GetActor(const std::string& name)
- {
- std::vector<pair<std::string, CPlayer*> >::iterator pos ;
- for ( pos = m_enum_data.begin(); pos != m_enum_data.end(); ++pos )
- {
- if ( (*pos).first == name )
- {
- return *((*pos).second) ;
- }
- }
-
- string err = "Actor Not Found: " ;
- err += name ;
- throw exception( name.c_str() ) ;
- }
- CSimpleObj& CActorManager::GetSimpleObj(const std::string& name)
- {
- std::vector< std::pair<std::string, CSimpleObj*> >::iterator pos ;
- for ( pos = m_simple_data.begin(); pos != m_simple_data.end(); ++pos)
- {
- if ( (*pos).first == name )
- {
- return *((*pos).second) ;
- }
- }
- string err = "Try to get nonexsisted simple object: " ;
- err += name ;
- throw exception( name.c_str() ) ;
- }
- void CActorManager::SetActiveActor(const std::string& name)
- {
- CPlayer* p ;
- try
- {
- p = &this->GetActor(name) ;
- }
- catch (exception e)
- {
- m_activePlayer = NULL ;
- return ;
- }
- m_activePlayer = p ;
- }
- CPlayer& CActorManager::GetActiveActor()
- {
- if ( m_activePlayer == NULL )
- {
- throw exception("No active actor found!") ;
- }
- else
- {
- return *m_activePlayer ;
- }
- }
- void CActorManager::SortActors()
- {
- sort( m_enum_data.begin(), m_enum_data.end(), BaseYcomp() ) ;
- }
- void CActorManager::push_enum_state()
- {
- m_enum_stack.push_back( m_enum_pos ) ;
- m_enum_pos = m_enum_data.begin() ;
- }
- void CActorManager::pop_enum_state()
- {
- if ( !m_enum_stack.empty() )
- {
- m_enum_pos = m_enum_stack.back() ;
- m_enum_stack.pop_back() ;
- }
- else
- {
- throw logic_error( "Try to pop an empty enum stack!" ) ;
- }
- }
- /*
- * 刷新用户关注npc,当ActiveActor与某个npc的距离小于★两倍★的敏感距离,就认为关注
- */
- void CActorManager::RefreshUserAttention()
- {
- push_enum_state() ;
- CPlayer* Actor = this->enumActor() ;
- CPlayer* dest = NULL ;
- while ( Actor != NULL )
- {
- if ( Actor != m_activePlayer && Actor->active_in_scene )
- {
- if ( sqrt( (double)( Actor->x - m_activePlayer->x ) * ( Actor->x - m_activePlayer->x )
- + ( Actor->y - m_activePlayer->y ) * ( Actor->y - m_activePlayer->y ) ) <= 1.25 * this->m_context->GetSensitiveLength() )
- {
- dest = Actor ;
- }
- }
- Actor = this->enumActor() ;
- }
- m_user_attention_player = dest ;
- pop_enum_state() ;
- }
- CPlayer* CActorManager::GetUserAttention()
- {
- return m_user_attention_player ;
- }