/nebula/code/src/pdserver/collserver2d_main.cc
C++ | 200 lines | 95 code | 22 blank | 83 comment | 21 complexity | 1fc5f25a424712266eb4e018e767032d MD5 | raw file
Possible License(s): AGPL-3.0, 0BSD, BSD-3-Clause
- #define N_IMPLEMENTS pdCollideServer2D
- //------------------------------------------------------------------------------
- // cwcollserver2d_main.cc
- // (C) 2002 Clockwise Ltd. -- I.Kliot
- //------------------------------------------------------------------------------
-
- #include "pdnode/collidenode2d.h"
- #include "pdserver/collideserver2d.h"
-
- nNebulaScriptClass(pdCollideServer2D, "nroot");
- //------------------------------------------------------------------------------
- /**
- pdCollideServer2D()
- default constructor
- created 16-May-2002 -- Ilya Kliot
- */
- pdCollideServer2D::pdCollideServer2D() : nRoot(), visualize(false), active(true)
- {
- this->AddClass("default");
- this->SetRelation("default", "default", false);
- }
-
- //------------------------------------------------------------------------------
- /**
- ~pdCollideServer2D()
- destructor
- created 16-May-2002 -- Ilya Kliot
- */
- pdCollideServer2D::~pdCollideServer2D()
- {
- this->curr_context.clear();
- this->next_context.clear();
- this->collnodes_list.clear();
- this->classes.clear();
- }
-
- //------------------------------------------------------------------------------
- /**
- AddCollNode(pdCollideNode2D* node)
- add node to list of collision nodes
- 1 time per node occurred
- created 20-May-2002 -- Ilya Kliot
- */
- void pdCollideServer2D::AddCollNode(pdCollideNode2D* node)
- {
- this->collnodes_list.push_back(node);
- }
-
- //------------------------------------------------------------------------------
- /**
- Attach(pdCollideNode2D* node)
- add node to render context
- 1 time per node occurred
- created 20-May-2002 -- Ilya Kliot
- */
- pdCollContext2D* pdCollideServer2D::Attach(pdCollideNode2D* node)
- {
- this->next_context.push_back(node);
- return &this->curr_context;
- }
-
- //------------------------------------------------------------------------------
- /**
- Remove(pdCollideNode2D* node)
- remove node from contexts
- created 20-May-2002 -- Ilya Kliot
- */
- void pdCollideServer2D::Remove(pdCollideNode2D* node)
- {
- this->curr_context.remove(node);
- this->next_context.remove(node);
- this->collnodes_list.remove(node);
- }
-
- //------------------------------------------------------------------------------
- /**
- created 16-may-2002 -- Ilya Kliot
- clear current context
- */
- void pdCollideServer2D::ClearContext()
- {
- for (pdCollContext2D::iterator i = this->curr_context.begin();
- i != this->curr_context.end(); i++)
- (*i)->SetContext(0);
-
- this->next_context.clear();
- }
-
- //------------------------------------------------------------------------------
- /**
- created 09-Jul-2002 -- Ilya Kliot
- get id of class by name
- */
- long pdCollideServer2D::find_class(const char* name) const
- {
- size_t num = this->classes.size();
-
- for (size_t i = 0; i < num; i++)
- if (!n_stricmp(name, this->classes[i].c_str())) return (long)i;
-
- return -1;
- }
-
- //------------------------------------------------------------------------------
- /**
- created 09-Jul-2002 -- Ilya Kliot
- add class of collision objects
- */
- void pdCollideServer2D::AddClass(const char* name)
- {
- if (this->find_class(name) < 0)
- {
- this->classes.push_back(name);
- this->rel_mx.inc();
- }
- }
-
- //------------------------------------------------------------------------------
- /**
- created 09-Jul-2002 -- Ilya Kliot
- get name of class by id
- */
- const char* pdCollideServer2D::GetCClassName(long id) const
- {n_precondition(id >= 0 && id < (long)this->classes.size() && "id of class out of range");
- return classes[id].c_str();
- }
-
- //------------------------------------------------------------------------------
- /**
- created 09-Jul-2002 -- Ilya Kliot
- enables or disables collision between 2 classes
- */
- void pdCollideServer2D::SetRelation(const char* name1, const char* name2, bool check)
- {
- long id1(this->find_class(name1));
- long id2(this->find_class(name2));
- n_assert(id1 >= 0 && "first class undefined");
- n_assert(id2 >= 0 && "second class undefined");
- this->rel_mx[id1][id2] = check;
- this->rel_mx[id2][id1] = check;
- }
-
- //------------------------------------------------------------------------------
- /**
- created 09-Jul-2002 -- Ilya Kliot
- check relation between 2 classes
- */
- bool pdCollideServer2D::GetRelation(long id1, long id2) const
- {n_precondition(id1 >=0 && id1 < (long)this->rel_mx.dim() && "class 1 not declared");
- n_assert(id2 >=0 && id2 < (long)this->rel_mx.dim() && "class 2 not declared");
- return this->rel_mx[id1][id2];
- }
-
- //------------------------------------------------------------------------------
- /**
- created 09-Jul-2002 -- Ilya Kliot
- check relation between 2 classes by names
- */
- bool pdCollideServer2D::GetRelation(const char* name1, const char* name2) const
- {
- return this->GetRelation(this->find_class(name1), this->find_class(name2));
- }
-
- //------------------------------------------------------------------------------
- /**
- created 16-may-2002 -- Ilya Kliot
- swap contexts and begin new context;
- */
- void pdCollideServer2D::Trigger()
- {
- if (!this->GetActive())
- return;
-
- this->curr_context.swap(this->next_context);
-
- this->ClearContext();
- }
-
- //------------------------------------------------------------------------------
- /**
- created 16-jul-2002 -- Ilya Kliot
- check for collision between node and
- all nodes in current_context
- */
- void pdCollideServer2D::Compute(pdCollideNode2D* node)
- {
- if (!this->GetActive()) return;
- //|| !node->GetActive() || node->GetCRType() == pdCollideNode2D::STATIC) return;
-
- if (this->curr_context.size() < 2) return;
-
- for (pdCollContext2D::iterator i = this->curr_context.begin();
- i != this->curr_context.end(); i++){
- if (node != *i)
- {
- if (this->GetRelation((*i)->GetClassId(), node->GetClassId()))
- node->CheckCollide(**i);
- }
- }
- }