RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
cplayer.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include <string>
31 #include "misc/btcustom.h"
32 #include "misc/mathhelper.hpp"
35 #include "cengine/ccamera.h"
36 #include "cengine/csoundplayer.h"
37 #include "cengine/cgamescene.h"
38 #include "cengine/cdirector.h"
39 #include "cengine/csingleton.h"
40 #include "cengine/cenemy.h"
41 #include "shootingrange/cplayer.h"
42 
43 //-----------------------------------------------------------------------------
44 
45 const std::string CPlayer::k_PlayerCameraId = "__PlayerCamera__";
46 
47 //-----------------------------------------------------------------------------
48 
50  m_weapon = NULL;
51  recoil_action = NULL;
52  m_previousMousePosition = new USERINPUT::MousePosition();
53  CDIRECTOR->createCamera(k_PlayerCameraId);
54 }
55 
56 //-----------------------------------------------------------------------------
57 
59  // let's create the weapon
60  m_weapon = parentScene()->createGameObjectWithNode("AK-47", false);
61  addChild(m_weapon);
62  m_weapon->setRotation(0, -90.0f, 0);
63  m_weapon->transform().setLocalPosition(1.0f, -0.5f,-1.2f);
64  m_weapon->setScale(0.5f);
65  CDIRECTOR->getCameraWithId(k_PlayerCameraId)->reset();
66 }
67 
68 //-----------------------------------------------------------------------------
69 
72 }
73 
74 //-----------------------------------------------------------------------------
75 
77  CCamera* players_camera = CDIRECTOR->getCameraWithId(k_PlayerCameraId);
78  Transform* camera_transform = &players_camera->transform();
79 
80  // -------------------- Joystick controls
81  float left_joystick_y = INPUTMANAGER->getLeftJoystick()->getY();
82  float left_joystick_x = INPUTMANAGER->getLeftJoystick()->getX();
83 
84  float right_joystick_y = INPUTMANAGER->getRightJoystick()->getY();
85  float right_joystick_x = INPUTMANAGER->getRightJoystick()->getX();
86 
87  float rot_factor = 2.5f;
88 
89  left_joystick_y -= (INPUTMANAGER->getKeyDown('W') ? 1.0f : 0.0f);
90  left_joystick_y += (INPUTMANAGER->getKeyDown('S') ? 1.0f : 0.0f);
91  left_joystick_x -= (INPUTMANAGER->getKeyDown('A') ? 1.0f : 0.0f);
92  left_joystick_x += (INPUTMANAGER->getKeyDown('D') ? 1.0f : 0.0f);
93 
94  const USERINPUT::MousePosition* current_mouse_position = INPUTMANAGER->getMousePosition();
95  float dx = (float)(current_mouse_position->x - m_previousMousePosition->x) * k_InputManager_MouseSensitivity;
96  float dy = (float)(current_mouse_position->y - m_previousMousePosition->y) * k_InputManager_MouseSensitivity;
97 
98  dx = MathHelper::clampf(dx, -5.0f, 5.0f);
99  dy = MathHelper::clampf(dy, -5.0f, 5.0f);
100  right_joystick_x += dx;
101  right_joystick_y += dy * 0.7f; // y axis movement must be slower
102 
103 
104  if (right_joystick_x != 0) {
105  transform().rotateY(-right_joystick_x * rot_factor, Transform::LOCAL);
106  camera_transform->rotateY(right_joystick_x* rot_factor);
107  }
108 
109  if (right_joystick_y != 0) {
110  transform().rotateX(-right_joystick_y * rot_factor, Transform::LOCAL);
111  camera_transform->rotateX(right_joystick_y* rot_factor);
112  }
113 
114  if (left_joystick_y != 0) {
115  transform().moveZ(-left_joystick_y * k_QGLGameEditorViewport_SizeFactor, Transform::LOCAL);
116  }
117 
118  if (left_joystick_x != 0) {
119  transform().moveX(left_joystick_x * k_QGLGameEditorViewport_SizeFactor, Transform::LOCAL);
120  }
121 
122  // -------------------- Keyboard & Mouse controls
123 
124  if(nextShotReady() && (INPUTMANAGER->getJoystickButtonDown(6) || INPUTMANAGER->getMouseButtonDown(USERINPUT::LeftButton))) {
125  fire();
126  }
127 
128  m_previousMousePosition->x = current_mouse_position->x;
129  m_previousMousePosition->y = current_mouse_position->y;
130 }
131 
132 //-----------------------------------------------------------------------------
133 
135  transform().setLocalPosition(transform().localPosition().x(), 2.0f, transform().localPosition().z());
136  CDIRECTOR->getCameraWithId(k_PlayerCameraId)->transform().setLocalPosition(transform().localPosition() * k_QGLGameEditorViewport_SizeFactor);
137 }
138 
139 //-----------------------------------------------------------------------------
140 
141 std::string CPlayer::getCameraId() const {
142  return k_PlayerCameraId;
143 }
144 
145 //-----------------------------------------------------------------------------
146 
148  return (recoil_action == NULL);
149 }
150 
151 //-----------------------------------------------------------------------------
152 
154  CMoveBy* recoil = new CMoveBy(0.04f, btVector3(0, 0, 0.1f));
155  recoil->autorelease();
156  CMoveBy* reversed_recoil = static_cast<CMoveBy*>(recoil->reverse());
157  reversed_recoil->autorelease();
158  recoil_action = new CSequence();
159  recoil_action->autorelease();
160  recoil_action->initHavingActions(recoil, reversed_recoil);
161  connect(recoil_action, SIGNAL(onActionDone(CAction*)),
162  this, SLOT(onActionDone(CAction*)));
163  m_weapon->runAction(recoil_action);
164  CSOUNDPLAYER->play2D("ak47show.wav");
165 
166  // let's create the bullet
167  CGameObject* bullet = parentScene()->createGameObjectWithNode("bullet", true, "CBullet");
168  bullet->transform().setLocalPosition(transform().localPosition());
169  bullet->transform().setLocalRotation(transform().localRotation() * btQuaternion(0, DEGTORAD(180.0f), 0));
170  bullet->transform().setLocalScale(0.2f);
171  bullet->bulletProperties.setMass(0.1f);
172  bullet->deployIntoDynamicsWorld();
173  bullet->bulletProperties.rigidBody()->setGravity(btCustom::zero_vector());
174  bullet->bulletProperties.rigidBody()->activate(true);
175  bullet->setPosition(bullet->position());
176  bullet->transform().moveZ(-6.0f, Transform::TransformMode::LOCAL);
177  bullet->transform().moveY(0.5f, Transform::TransformMode::LOCAL);
178 
179  // raycast
180  btVector3 forward = btMatrix3x3(transform().localRotation()) * btVector3(0,0,-100.0f);
181  btVector3 start = position(Transform::UnitType::PHYSICSWORLD);
182  btVector3 end = start + forward;
183 
184  btCollisionWorld::ClosestRayResultCallback RayCallback( start, end );
185  parentScene()->dynamicsWorld()->rayTest(start, end, RayCallback);
186 
187  if(RayCallback.hasHit()) {
188  CGameObject* game_obj = static_cast<CGameObject*>(RayCallback.m_collisionObject->getUserPointer());
189  LOG("Bullet hit object named: %s", qPrintable(game_obj->objectName()));
190  if(game_obj) {
191  CEnemy* enemy = qobject_cast<CEnemy*>(game_obj);
192  bool is_enemy = enemy != NULL;
193  if (is_enemy) {
194  enemy->damageReceived(1);
195  }
196  }
197  }
198 
199  btVector3 centerOfMass = bullet->bulletProperties.rigidBody()->getCenterOfMassPosition();
200  btVector3 force = btMatrix3x3(transform().localRotation()) * btVector3(0,0,-5.0f);
201  btVector3 hitPoint = RayCallback.m_hitPointWorld;
202  bullet->bulletProperties.rigidBody()->applyImpulse(force * 4.0f, hitPoint - centerOfMass);
203 }
204 
205 //-----------------------------------------------------------------------------
206 
208  recoil_action = NULL;
209 }
210 
211 //-----------------------------------------------------------------------------
212 
214  SAFE_RELEASE(m_weapon);
215  SAFE_RELEASE(m_previousMousePosition);
216 }
~CPlayer(void)
Definition: cplayer.cpp:213
#define LOG
Definition: macro.h:43
#define CSOUNDPLAYER
Definition: csoundplayer.h:38
btVector3 zero_vector()
Definition: btcustom.cpp:26
void setLocalScale(float x, float y, float z)
Definition: transform.cpp:180
std::string getCameraId() const
Definition: cplayer.cpp:141
virtual void damageReceived(int damage)
void moveX(float dx, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:242
static float clampf(float value, float min, float max)
Definition: mathhelper.hpp:43
void setLocalRotation(float w, float x, float y, float z)
Definition: transform.cpp:209
void moveY(float dy, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:250
CSequence * initHavingActions(CFiniteTimeAction *firstAction, CFiniteTimeAction *secondAction)
void moveZ(float dz, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:258
CameraTransform & transform()
Definition: ccamera.cpp:42
Definition: cenemy.h:35
virtual void update()
Definition: cplayer.cpp:70
#define SAFE_RELEASE(x)
Definition: cobject.h:36
CObject * autorelease()
Definition: cobject.cpp:87
virtual bool nextShotReady()
Definition: cplayer.cpp:147
virtual void fire()
Definition: cplayer.cpp:153
CPlayer(void)
Definition: cplayer.cpp:49
void rotateX(float angle, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:277
virtual void start()
Definition: cplayer.cpp:58
#define DEGTORAD(x)
Definition: macro.h:52
btRigidBody * rigidBody() const
virtual CActionInterval * reverse()
btVector3 forward()
Definition: btcustom.cpp:66
#define CDIRECTOR
Definition: cdirector.h:37
void setLocalPosition(float x, float y, float z)
Definition: transform.cpp:165
void onActionDone(CAction *action)
Definition: cplayer.cpp:207
#define INPUTMANAGER
Definition: inputmanager.h:39
void rotateY(float angle, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:284
void setMass(float mass)
Transform & transform()
Definition: clnode.cpp:69
virtual void postWorldUpdate()
Definition: cplayer.cpp:134
static const std::string k_PlayerCameraId
Definition: cplayer.h:57
void updatePositionAndOrientation()
Definition: cplayer.cpp:76