afxForceSet.cpp
Engine/source/afx/forces/afxForceSet.cpp
Detailed Description
1 2 3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames 5// Copyright (C) 2015 Faust Logic, Inc. 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to 9// deal in the Software without restriction, including without limitation the 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11// sell copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in 15// all copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23// IN THE SOFTWARE. 24// 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 26 27#include "afx/arcaneFX.h" 28 29#include "afx/forces/afxForceSet.h" 30#include "afx/forces/afxForce.h" 31 32//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 33 34afxForceSet::afxForceSet(const char* name) 35{ 36 mName = (name) ? StringTable->insert(name) : ST_NULLSTRING; 37 mUpdate_dt = 10.0f; // seems like an ok maximum, force-xmods will probably lower it. 38 mElapsed_dt = 0.0f; 39 mElapsed_ms = 0; 40 mNum_updates = 0; 41 mLast_num_updates = 0; 42} 43 44void afxForceSet::remove(afxForce* force) 45{ 46 for (S32 i = 0; i < mForce_v.size(); i++) 47 { 48 if (mForce_v[i] == force) 49 { 50 mForce_v.erase(i); 51 return; 52 } 53 } 54} 55 56S32 afxForceSet::updateDT(F32 dt) 57{ 58 U32 now = Platform::getVirtualMilliseconds(); 59 60 if (mElapsed_ms == now) 61 return mLast_num_updates; 62 63 mElapsed_ms = now; 64 mElapsed_dt += dt; 65 66 if (mElapsed_dt < mUpdate_dt) 67 { 68 mLast_num_updates = 0; 69 return 0; 70 } 71 72 mNum_updates = mFloor(mElapsed_dt/mUpdate_dt); 73 mElapsed_dt -= mUpdate_dt*mNum_updates; 74 mLast_num_updates = mNum_updates; 75 76 return mNum_updates; 77} 78 79//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 80 81afxForceSetMgr::afxForceSetMgr() 82{ 83} 84 85afxForceSetMgr::~afxForceSetMgr() 86{ 87 for (S32 i = 0; i < forces_sets.size(); i++) 88 { 89 if (forces_sets[i]) 90 delete forces_sets[i]; 91 } 92} 93 94afxForceSet* afxForceSetMgr::findForceSet(StringTableEntry forces_set_name) 95{ 96 for (S32 i = 0; i < forces_sets.size(); i++) 97 { 98 if (forces_sets[i] && forces_sets[i]->getName() == forces_set_name) 99 return forces_sets[i]; 100 } 101 102 return 0; 103} 104 105void afxForceSetMgr::registerForce(StringTableEntry forces_set_name, afxForce* force) 106{ 107 if (!force) 108 return; 109 110 // find forceSet by name 111 afxForceSet* fset = findForceSet(forces_set_name); 112 113 // create forceSet if it does not already exist 114 if (!fset) 115 { 116 fset = new afxForceSet(forces_set_name); 117 forces_sets.push_back(fset); 118 } 119 120 // add force to set 121 fset->add(force); 122} 123 124void afxForceSetMgr::unregisterForce(StringTableEntry forces_set_name, afxForce* force) 125{ 126 if (!force) 127 return; 128 129 afxForceSet* fset = findForceSet(forces_set_name); 130 if (!fset) 131 return; 132 133 fset->remove(force); 134} 135 136