diff options
Diffstat (limited to 'source/gameengine/GameLogic/SCA_ISensor.cpp')
-rw-r--r-- | source/gameengine/GameLogic/SCA_ISensor.cpp | 82 |
1 files changed, 72 insertions, 10 deletions
diff --git a/source/gameengine/GameLogic/SCA_ISensor.cpp b/source/gameengine/GameLogic/SCA_ISensor.cpp index 9fdee0c19da..2dc49924062 100644 --- a/source/gameengine/GameLogic/SCA_ISensor.cpp +++ b/source/gameengine/GameLogic/SCA_ISensor.cpp @@ -41,7 +41,8 @@ void SCA_ISensor::ReParent(SCA_IObject* parent) { SCA_ILogicBrick::ReParent(parent); - m_eventmgr->RegisterSensor(this); + // will be done when the sensor is activated + //m_eventmgr->RegisterSensor(this); this->SetActive(false); } @@ -52,8 +53,11 @@ SCA_ISensor::SCA_ISensor(SCA_IObject* gameobj, SCA_ILogicBrick(gameobj,T), m_triggered(false) { + m_links = 0; m_suspended = false; m_invert = false; + m_level = false; + m_reset = false; m_pos_ticks = 0; m_neg_ticks = 0; m_pos_pulsemode = false; @@ -94,6 +98,10 @@ void SCA_ISensor::SetInvert(bool inv) { m_invert = inv; } +void SCA_ISensor::SetLevel(bool lvl) { + m_level = lvl; +} + float SCA_ISensor::GetNumber() { return IsPositiveTrigger(); @@ -111,6 +119,26 @@ void SCA_ISensor::Resume() { m_suspended = false; } +void SCA_ISensor::Init() { + printf("Sensor %s has no init function, please report this bug to Blender.org\n", m_name.Ptr()); +} + +void SCA_ISensor::DecLink() { + m_links--; + if (m_links < 0) + { + printf("Warning: sensor %s has negative m_links: %d\n", m_name.Ptr(), m_links); + m_links = 0; + } + if (!m_links) + { + // sensor is detached from all controllers, initialize it so that it + // is fresh as at startup when it is reattached again. + UnregisterToManager(); + Init(); + } +} + /* python integration */ PyTypeObject SCA_ISensor::Type = { @@ -142,21 +170,25 @@ PyMethodDef SCA_ISensor::Methods[] = { {"isPositive", (PyCFunction) SCA_ISensor::sPyIsPositive, METH_VARARGS, IsPositive_doc}, {"getUsePosPulseMode", (PyCFunction) SCA_ISensor::sPyGetUsePosPulseMode, - METH_VARARGS, GetUsePosPulseMode_doc}, + METH_NOARGS, GetUsePosPulseMode_doc}, {"setUsePosPulseMode", (PyCFunction) SCA_ISensor::sPySetUsePosPulseMode, METH_VARARGS, SetUsePosPulseMode_doc}, {"getFrequency", (PyCFunction) SCA_ISensor::sPyGetFrequency, - METH_VARARGS, GetFrequency_doc}, + METH_NOARGS, GetFrequency_doc}, {"setFrequency", (PyCFunction) SCA_ISensor::sPySetFrequency, METH_VARARGS, SetFrequency_doc}, {"getUseNegPulseMode", (PyCFunction) SCA_ISensor::sPyGetUseNegPulseMode, - METH_VARARGS, GetUseNegPulseMode_doc}, + METH_NOARGS, GetUseNegPulseMode_doc}, {"setUseNegPulseMode", (PyCFunction) SCA_ISensor::sPySetUseNegPulseMode, METH_VARARGS, SetUseNegPulseMode_doc}, {"getInvert", (PyCFunction) SCA_ISensor::sPyGetInvert, - METH_VARARGS, GetInvert_doc}, + METH_NOARGS, GetInvert_doc}, {"setInvert", (PyCFunction) SCA_ISensor::sPySetInvert, METH_VARARGS, SetInvert_doc}, + {"getLevel", (PyCFunction) SCA_ISensor::sPyGetLevel, + METH_NOARGS, GetLevel_doc}, + {"setLevel", (PyCFunction) SCA_ISensor::sPySetLevel, + METH_VARARGS, SetLevel_doc}, {NULL,NULL} //Sentinel }; @@ -173,11 +205,17 @@ void SCA_ISensor::RegisterToManager() m_eventmgr->RegisterSensor(this); } +void SCA_ISensor::UnregisterToManager() +{ + m_eventmgr->RemoveSensor(this); +} + void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr, CValue* event) { // calculate if a __triggering__ is wanted - if (!m_suspended) { + // don't evaluate a sensor that is not connected to any controller + if (m_links && !m_suspended) { bool result = this->Evaluate(event); if (result) { logicmgr->AddActivatedSensor(this); @@ -228,7 +266,7 @@ PyObject* SCA_ISensor::PyIsPositive(PyObject* self, PyObject* args, PyObject* kw char SCA_ISensor::GetUsePosPulseMode_doc[] = "getUsePosPulseMode()\n" "\tReturns whether positive pulse mode is active.\n"; -PyObject* SCA_ISensor::PyGetUsePosPulseMode(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PyGetUsePosPulseMode(PyObject* self) { return BoolToPyArg(m_pos_pulsemode); } @@ -255,7 +293,7 @@ PyObject* SCA_ISensor::PySetUsePosPulseMode(PyObject* self, PyObject* args, PyOb char SCA_ISensor::GetFrequency_doc[] = "getFrequency()\n" "\tReturns the frequency of the updates in pulse mode.\n" ; -PyObject* SCA_ISensor::PyGetFrequency(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PyGetFrequency(PyObject* self) { return PyInt_FromLong(m_pulse_frequency); } @@ -290,7 +328,7 @@ PyObject* SCA_ISensor::PySetFrequency(PyObject* self, PyObject* args, PyObject* char SCA_ISensor::GetInvert_doc[] = "getInvert()\n" "\tReturns whether or not pulses from this sensor are inverted.\n" ; -PyObject* SCA_ISensor::PyGetInvert(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PyGetInvert(PyObject* self) { return BoolToPyArg(m_invert); } @@ -307,10 +345,34 @@ PyObject* SCA_ISensor::PySetInvert(PyObject* self, PyObject* args, PyObject* kwd Py_Return; } +char SCA_ISensor::GetLevel_doc[] = +"getLevel()\n" +"\tReturns whether this sensor is a level detector or a edge detector.\n" +"\tIt makes a difference only in case of logic state transition (state actuator).\n" +"\tA level detector will immediately generate a pulse, negative or positive\n" +"\tdepending on the sensor condition, as soon as the state is activated.\n" +"\tA edge detector will wait for a state change before generating a pulse.\n"; +PyObject* SCA_ISensor::PyGetLevel(PyObject* self) +{ + return BoolToPyArg(m_level); +} + +char SCA_ISensor::SetLevel_doc[] = +"setLevel(level?)\n" +"\t- level?: Detect level instead of edge? (KX_TRUE, KX_FALSE)\n" +"\tSet whether to detect level or edge transition when entering a state.\n"; +PyObject* SCA_ISensor::PySetLevel(PyObject* self, PyObject* args, PyObject* kwds) +{ + int pyarg = 0; + if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } + m_level = PyArgToBool(pyarg); + Py_Return; +} + char SCA_ISensor::GetUseNegPulseMode_doc[] = "getUseNegPulseMode()\n" "\tReturns whether negative pulse mode is active.\n"; -PyObject* SCA_ISensor::PyGetUseNegPulseMode(PyObject* self, PyObject* args, PyObject* kwds) +PyObject* SCA_ISensor::PyGetUseNegPulseMode(PyObject* self) { return BoolToPyArg(m_neg_pulsemode); } |