Advertisement
CUgopEntity

CustomDetector.cpp

Jan 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.42 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "customdetector.h"
  3. #include "hudmanager.h"
  4. #include "artifact.h"
  5.  
  6. CCustomDetector::CCustomDetector(void) {
  7. //  m_belt = true;
  8. }
  9.  
  10. CCustomDetector::~CCustomDetector(void) {
  11. }
  12.  
  13. BOOL CCustomDetector::net_Spawn(LPVOID DC) {
  14.     BOOL res = inherited::net_Spawn(DC);
  15.  
  16.     CKinematics* V = PKinematics(Visual());
  17.     if(V) V->PlayCycle("idle");
  18.  
  19.     if (0==m_pPhysicsShell) {
  20.         // Physics (Box)
  21.         Fobb obb; Visual()->vis.box.get_CD(obb.m_translate,obb.m_halfsize); obb.m_rotate.identity();
  22.         // Physics (Elements)
  23.         CPhysicsElement* E = P_create_Element(); R_ASSERT(E); E->add_Box(obb);
  24.         // Physics (Shell)
  25.         m_pPhysicsShell = P_create_Shell(); R_ASSERT(m_pPhysicsShell);
  26.         m_pPhysicsShell->add_Element(E);
  27.         m_pPhysicsShell->setDensity(2000.f);
  28.         if(!H_Parent())m_pPhysicsShell->Activate(svXFORM(),0,svXFORM());
  29.         m_pPhysicsShell->mDesired.identity();
  30.         m_pPhysicsShell->fDesiredStrength = 0.f;
  31.     }
  32. //  CCF_Shape*  shape           = xr_new<CCF_Shape> (this);
  33. //  cfModel                     = shape;
  34. //  Fsphere S;  S.P.set         (0,1.f,0); S.R = m_buzzer_radius;
  35. //  shape->add_sphere           (S);
  36. //  shape->ComputeBounds                        ();
  37. //#pragma todo("@@@ WT: Check if detector will work w/o registration.")
  38. //  pCreator->ObjectSpace.Object_Register       (this);
  39. //  cfModel->OnMove                             ();
  40.  
  41.     setVisible(true);
  42.     setEnabled(true);
  43.  
  44.     return res;
  45. }
  46.  
  47. void CCustomDetector::Load(LPCSTR section) {
  48.     // verify class
  49.     LPCSTR Class = pSettings->r_string(section,"class");
  50.     CLASS_ID load_cls = TEXT2CLSID(Class);
  51.     R_ASSERT(load_cls==SUB_CLS_ID);
  52.  
  53.     inherited::Load(section);
  54.  
  55.     m_radius = pSettings->r_float(section,"radius");
  56.     m_buzzer_radius = pSettings->r_float(section,"buzzer_radius");
  57.  
  58.     LPCSTR l_soundName = pSettings->r_string(section,"noise");
  59.     SoundCreate(m_noise, l_soundName);
  60.     l_soundName = pSettings->r_string(section,"buzzer");
  61.     SoundCreate(m_buzzer, l_soundName);
  62.     u32 i = 1;
  63.     string256 temp;
  64.     do {
  65.         sprintf(temp, "zone_class_%d", i);
  66.         if(pSettings->line_exist(section,temp)) {
  67.             LPCSTR z_Class = pSettings->r_string(section,temp);
  68.             CLASS_ID zone_cls = TEXT2CLSID(pSettings->r_string(z_Class,"class"));
  69.             sprintf(temp, "zone_sound_%d", i);
  70.             l_soundName = pSettings->r_string(section,temp);
  71.             sound *l_pSound = xr_new<sound>();
  72.             SoundCreate(*l_pSound, l_soundName);
  73.             m_sounds[zone_cls] = l_pSound;
  74.             i++;
  75.         } else break;
  76.     } while(true);
  77.  
  78. }
  79.  
  80. void CCustomDetector::net_Destroy() {
  81.     if(m_pPhysicsShell) m_pPhysicsShell->Deactivate();
  82.     xr_delete(m_pPhysicsShell);
  83.     inherited::net_Destroy();
  84.     SoundDestroy(m_noise);
  85.     xr_map<CLASS_ID, sound*>::iterator l_it;
  86.     for(l_it = m_sounds.begin(); l_it != m_sounds.end(); l_it++) SoundDestroy(*l_it->second);
  87. }
  88.  
  89. void CCustomDetector::Update(u32 dt) {
  90.     inherited::Update   (dt);
  91.  
  92.     if(!H_Parent()) return;
  93.  
  94.     vPosition.set(H_Parent()->Position());
  95.     UpdateTransform();
  96.  
  97.     //const Fsphere& s      = cfModel->getSphere();
  98.     Fvector                 P; P.set(H_Parent()->Position());
  99.     //H_Parent()->clXFORM().transform_tiny(P,P);
  100.     feel_touch_update       (P,m_radius);
  101.  
  102.     if(H_Parent()) {
  103.         f32 l_maxPow = 0;
  104.         BOOL l_buzzer = false;
  105.         xr_list<CCustomZone*>::iterator l_it;
  106.         for(l_it = m_zones.begin(); l_it != m_zones.end(); l_it++) {
  107.             CCustomZone *l_pZ = *l_it;
  108.             u32 &l_time = m_times[l_pZ];
  109.             //Fvector ZP; ZP.set(0,0,0); l_pZ->clXFORM().transform_tiny(ZP,ZP);
  110.             f32 l_dst = P.distance_to(l_pZ->Position()); if(l_dst > m_radius) l_dst -= m_radius; else l_dst = 0;
  111.             f32 l_relPow = l_pZ->Power(l_dst) / l_pZ->m_maxPower;
  112.             if(l_relPow > 0 && l_pZ->feel_touch_contact(this)) l_buzzer = true;
  113.             l_maxPow = _max(l_maxPow, l_relPow); l_relPow = 1.f - l_relPow;
  114.             if((f32)l_time > 5000.f * (l_relPow/**l_relPow*l_relPow*l_relPow*/)) {
  115.                 l_time = 0;
  116.                 if(m_sounds.find(l_pZ->SUB_CLS_ID) != m_sounds.end()) {
  117.                     sound *l_pSound = m_sounds[l_pZ->SUB_CLS_ID];
  118.                     Sound->play_at_pos(*l_pSound, this, P);
  119.                 }
  120.             } else l_time += dt;
  121.         }
  122.         if(l_buzzer) {
  123.             if(!m_buzzer.feedback) m_buzzer.play_at_pos(this, P, true);
  124.             if(m_buzzer.feedback) m_buzzer.set_position(P);
  125.         } else m_buzzer.stop();
  126.         if(l_maxPow > 0) {
  127.             if(!m_noise.feedback) Sound->play_at_pos(m_noise, this, P, true);
  128.             if(m_noise.feedback) {
  129.                 //l_maxPow = _max(logf(l_maxPow) / 10.f + 1.f, .0f);
  130.                 m_noise.set_volume  (l_maxPow);
  131.                 m_noise.set_position(P);
  132.             }
  133.         } else if(m_noise.feedback) m_noise.stop();
  134.         xr_set<CArtifact*>::iterator l_it2;
  135.         for(l_it2 = CArtifact::m_all.begin(); l_it2 != CArtifact::m_all.end(); l_it2++) {
  136.             CArtifact &l_af = **l_it2;
  137.             float l_dst = P.distance_to(l_af.Position());
  138.             if(!l_af.H_Parent() && l_dst < l_af.m_detectorDist) {
  139.                 if(!l_af.m_detectorSound.feedback) Sound->play_at_pos(l_af.m_detectorSound, this, P, true);
  140.                 if(l_af.m_detectorSound.feedback) {
  141.                     //l_af.m_detectorSound.set_volume(_max(logf((l_af.m_detectorDist-l_dst)/l_af.m_detectorDist) / 10.f + 1.f, .0f));
  142.                     l_af.m_detectorSound.set_volume((l_af.m_detectorDist-l_dst)/l_af.m_detectorDist);
  143.                     l_af.m_detectorSound.set_position(P);
  144.                 }
  145.             } else if(l_af.m_detectorSound.feedback) l_af.m_detectorSound.stop();
  146.         }
  147.     }
  148. }
  149.  
  150. void CCustomDetector::UpdateCL() {
  151.     inherited::UpdateCL();
  152.     /*
  153.     f32 l_zonePow = 0;
  154.     xr_list<CCustomZone*>::iterator l_it;
  155.     for(l_it = m_zones.begin(); l_it != m_zones.end(); l_it++) l_zonePow = _max(l_zonePow, (*l_it)->Power((*l_it)->Position().distance_to(vPosition)));
  156.     CGameFont* H        = HUD().pFontMedium;
  157.     H->SetColor         (0xf0ffffff);
  158.     H->Out              (550,500,"Anomaly force: %.0f", l_zonePow);
  159.     */
  160.     if(getVisible() && m_pPhysicsShell) {
  161.         m_pPhysicsShell->Update ();
  162.         svTransform.set         (m_pPhysicsShell->mXFORM);
  163.         vPosition.set           (svTransform.c);
  164.     }
  165. }
  166.  
  167. void CCustomDetector::feel_touch_new(CObject* O) {
  168.     CCustomZone *l_pZ = dynamic_cast<CCustomZone*>(O);
  169.     if(l_pZ) {
  170.         if(bDebug) HUD().outMessage(0xffffffff,cName(),"started to feel a zone.");
  171.         m_zones.push_back(l_pZ);
  172.         m_times[l_pZ] = 0;
  173.     }
  174. }
  175.  
  176. void CCustomDetector::feel_touch_delete(CObject* O) {
  177.     CCustomZone *l_pZ = dynamic_cast<CCustomZone*>(O);
  178.     if(l_pZ) {
  179.         if(bDebug) HUD().outMessage(0xffffffff,cName(),"stoped to feel a zone.");
  180.         m_zones.erase(std::find(m_zones.begin(), m_zones.end(), l_pZ));
  181.     }
  182. }
  183.  
  184. BOOL CCustomDetector::feel_touch_contact(CObject* O) {
  185.     return dynamic_cast<CCustomZone*>(O) != NULL;
  186. }
  187.  
  188. void CCustomDetector::SoundCreate(sound& dest, LPCSTR s_name, int iType, BOOL bCtrlFreq) {
  189.     string256 temp;
  190.     if (FS.exist(temp,"$game_sounds$",s_name)) {
  191.         Sound->create(dest,TRUE,s_name,iType);
  192.         return;
  193.     }
  194.     Debug.fatal ("Can't find sound '%s'",s_name,cName());
  195. }
  196.  
  197. void CCustomDetector::SoundDestroy(sound& dest) {
  198.     ::Sound->destroy            (dest);
  199. }
  200.  
  201. void CCustomDetector::OnH_A_Chield() {
  202.     inherited::OnH_A_Chield     ();
  203.     setVisible                  (false);
  204.     setEnabled                  (false);
  205. }
  206.  
  207. void CCustomDetector::OnH_B_Independent() {
  208.     inherited::OnH_B_Independent();
  209.     setVisible                  (true);
  210.     setEnabled                  (true);
  211.     m_buzzer.stop               ();
  212.     m_noise.stop                ();
  213.     //NET_Packet            P;
  214.     //u_EventGen            (P,GE_DESTROY,ID());
  215.     //u_EventSend           (P);
  216. }
  217.  
  218. void CCustomDetector::OnRender() {
  219.     if(!bDebug) return;
  220.     RCache.OnFrameEnd();
  221.     Fmatrix l_ball;
  222.     l_ball.scale(m_buzzer_radius, m_buzzer_radius, m_buzzer_radius);
  223.     Fvector l_p; l_p.set(0, 1.f, 0); clTransform.transform(l_p, l_p);
  224.     l_ball.translate_add(l_p);
  225.     RCache.dbg_DrawEllipse(l_ball, D3DCOLOR_XRGB(255,0,255));
  226. }
  227.  
  228. void CCustomDetector::OnVisible() {
  229.     if(getVisible() && !H_Parent()) {
  230.         ::Render->set_Transform     (&clTransform);
  231.         ::Render->add_Visual        (Visual());
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement