BGE: This patch adds a character wrapper (similar to the already implemented vehicle...
[blender.git] / source / gameengine / Ketsji / KX_CharacterWrapper.cpp
1 /** \file gameengine/Ketsji/KX_CharacterWrapper.cpp
2  *  \ingroup ketsji
3  */
4
5
6 #include "KX_CharacterWrapper.h"
7 #include "PHY_ICharacter.h"
8
9 KX_CharacterWrapper::KX_CharacterWrapper(PHY_ICharacter* character) :
10                 PyObjectPlus(),
11                 m_character(character)
12 {
13 }
14
15 KX_CharacterWrapper::~KX_CharacterWrapper()
16 {
17         if (m_character)
18                 delete m_character; // We're responsible for the character object!
19 }
20
21 #ifdef WITH_PYTHON
22
23 PyTypeObject KX_CharacterWrapper::Type = {
24         PyVarObject_HEAD_INIT(NULL, 0)
25         "KX_CharacterWrapper",
26         sizeof(PyObjectPlus_Proxy),
27         0,
28         py_base_dealloc,
29         0,
30         0,
31         0,
32         0,
33         py_base_repr,
34         0,0,0,0,0,0,0,0,0,
35         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
36         0,0,0,0,0,0,0,
37         Methods,
38         0,
39         0,
40         &PyObjectPlus::Type,
41         0,0,0,0,0,0,
42         py_base_new
43 };
44
45 PyAttributeDef KX_CharacterWrapper::Attributes[] = {
46         KX_PYATTRIBUTE_RO_FUNCTION("onGround", KX_CharacterWrapper, pyattr_get_onground),
47         KX_PYATTRIBUTE_RW_FUNCTION("gravity", KX_CharacterWrapper, pyattr_get_gravity, pyattr_set_gravity),
48         { NULL }        //Sentinel
49 };
50
51 PyObject *KX_CharacterWrapper::pyattr_get_onground(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
52 {
53         KX_CharacterWrapper* self = static_cast<KX_CharacterWrapper*>(self_v);
54
55         return PyBool_FromLong(self->m_character->OnGround());
56 }
57
58 PyObject *KX_CharacterWrapper::pyattr_get_gravity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
59 {
60         KX_CharacterWrapper* self = static_cast<KX_CharacterWrapper*>(self_v);
61
62         return PyFloat_FromDouble(self->m_character->GetGravity());
63 }
64
65 int KX_CharacterWrapper::pyattr_set_gravity(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
66 {
67         KX_CharacterWrapper* self = static_cast<KX_CharacterWrapper*>(self_v);
68         double param = PyFloat_AsDouble(value);
69
70         if (param == -1)
71         {
72                 PyErr_SetString(PyExc_ValueError, "KX_CharacterWrapper.gravity: expected a float");
73                 return PY_SET_ATTR_FAIL;
74         }
75
76         self->m_character->SetGravity((float)param);
77         return PY_SET_ATTR_SUCCESS;
78 }
79
80 PyMethodDef KX_CharacterWrapper::Methods[] = {
81         KX_PYMETHODTABLE_NOARGS(KX_CharacterWrapper, jump),
82         {NULL,NULL} //Sentinel
83 };
84
85 KX_PYMETHODDEF_DOC_NOARGS(KX_CharacterWrapper, jump,
86         "jump()\n"
87         "makes the character jump.\n")
88 {
89         m_character->Jump();
90
91         Py_RETURN_NONE;
92 }
93
94 #endif // WITH_PYTHON