-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.cpp
More file actions
185 lines (138 loc) · 4.13 KB
/
Player.cpp
File metadata and controls
185 lines (138 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//================================================//
#include "Player.hpp"
//================================================//
Player::Player(Ogre::SceneManager* mgr, Profile* profile)
{
// Set the scene manager pointer
m_pSceneMgr = mgr;
m_pProfile = profile;
// Set weapon to NULL for now
m_nWeapon = Weapon::NONE;
m_pWeapon = nullptr;
// How close a player should be to trigger an action
m_actionRange = 10.0;
}
//================================================//
Player::~Player(void)
{
if(m_pCamera != nullptr) delete m_pCamera;
if(m_pWeapon != nullptr) delete m_pWeapon;
if(m_pFlashlight != nullptr) delete m_pFlashlight;
// Free sounds
delete m_pSoundRetrieve;
}
//================================================//
bool Player::init(void)
{
bool ret = true;
// Create the camera
m_pCamera = new Sparks::Camera(m_pSceneMgr);
m_pCamera->setMode(Sparks::Camera::MODE_FIRST_PERSON);
// Load sounds
m_pSoundRetrieve = new Sound(Sound::TYPE_RETRIEVE_MAGIC_CUBE);
// Flashlight
this->initFlashlight();
return ret;
}
//================================================//
void Player::spawn(Ogre::SceneNode* node)
{
this->setPosition(node->_getDerivedPosition());
// Rotate to face the direction of the cone's point
m_pCamera->getYawNode()->yaw(Ogre::Radian((Ogre::Math::PI / 2.0)));
node->detachAllObjects();
}
//================================================//
void Player::action(EventManager* eventManager)
{
Rayhit* rayhit = m_pCamera->getNegativeZRayhit();
if(rayhit->hasHit){
printf("Object hit %s\nDistance: %.2f\n", rayhit->node->getName().c_str(), rayhit->distance);
// Iterate through dynamic objects
DynamicObjectList objects = eventManager->getDynamicObjectManager()->getObjects();
for(DynamicObjectListIterator itr = objects.begin();
itr != objects.end();
++itr){
if((*itr)->getSceneNode() == rayhit->node){
if(rayhit->distance <= m_actionRange){
(*itr)->send(DynamicObject::ARG_ACTION);
if((*itr)->isRetrievable()){
(*itr)->retrieve();
this->processRetrieval((*itr));
}
break;
}
}
}
}
}
//================================================//
void Player::processRetrieval(DynamicObject* object)
{
printf("Typeid: %s\n", typeid(*object).name());
// Determine type of object
if(typeid(*object) == typeid(MagicCube)){
m_pProfile->getInventory().addMagicCube();
m_pSoundRetrieve->play();
// Add text
Text* text = new Text();
text->text = "Magic Cube Acquired";
text->font = "StarWars";
text->pos = TextRenderer::LOWER;
text->timeout = 2000;
TextRenderer::getSingletonPtr()->setText(text);
}
}
//================================================//
void Player::initFlashlight(void)
{
m_pFlashlight = new Flashlight(m_pSceneMgr, m_pCamera->getRollNode());
}
//================================================//
void Player::initGlasses(Sparks::Compositor* compositor)
{
m_pGlasses = new Glasses(compositor);
}
//================================================//
void Player::setWeapon(unsigned weapon, EventManager* pEventManager)
{
if(weapon != Weapon::NONE){
if(m_nWeapon == weapon){
return;
}
}
// Set new weapon and delete the old class
m_nWeapon = weapon;
if(m_pWeapon != nullptr)
delete m_pWeapon;
// Load the new derived class for the weapon
switch(m_nWeapon){
case Weapon::NONE:
default:
m_pWeapon = new WeaponNone(m_pSceneMgr);
break;
case Weapon::TEST_GUN:
m_pWeapon = new MLP(m_pSceneMgr);
break;
case Weapon::TEST_SWORD:
m_pWeapon = new Sword(m_pSceneMgr);
break;
}
// Initialise the weapon
m_pWeapon->init(m_pCamera->getRollNode());
//m_pWeapon->init(m_pCamera->getSceneNode());
// Set manager pointers
m_pWeapon->setNPCManager(pEventManager->getNPCManager());
m_pWeapon->setProjectileManager(pEventManager->getProjectileManager());
}
//================================================//
void Player::update(double timeSinceLastFrame)
{
// Update the camera
m_pCamera->update(timeSinceLastFrame);
// Update the player's weapon
m_pWeapon->update(timeSinceLastFrame);
// Update player's flashlight
m_pFlashlight->update(timeSinceLastFrame);
}
//================================================//