-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickup.cpp
126 lines (104 loc) · 4.43 KB
/
pickup.cpp
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
/* heXon
// Copyright (C) 2015 LucKey Productions (luckeyproductions.nl)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
#include <Urho3D/Urho3D.h>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/Physics/CollisionShape.h>
#include <Urho3D/Physics/PhysicsEvents.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Graphics/ParticleEmitter.h>
#include <Urho3D/Graphics/ParticleEffect.h>
#include <Urho3D/Audio/Sound.h>
#include <Urho3D/Audio/SoundSource.h>
*/
#include "tilemaster.h"
#include "pickup.h"
#include "spawnmaster.h"
#include "player.h"
Pickup::Pickup(Context *context, MasterControl *masterControl):
SceneObject(context, masterControl)
{
rootNode_->SetName("Pickup");
model_ = rootNode_->CreateComponent<StaticModel>();
rootNode_->SetScale(0.8f);
rigidBody_ = rootNode_->CreateComponent<RigidBody>();
rigidBody_->SetRestitution(0.666f);
rigidBody_->SetMass(0.5f);
rigidBody_->SetLinearFactor(Vector3::ONE - Vector3::UP);
rigidBody_->SetLinearDamping(0.5f);
rigidBody_->SetFriction(0.0f);
rigidBody_->SetAngularDamping(0.0f);
rigidBody_->ApplyTorque(Vector3::UP * 32.0);
rigidBody_->SetLinearRestThreshold(0.0f);
rigidBody_->SetAngularRestThreshold(0.0f);
CollisionShape* collisionShape = rootNode_->CreateComponent<CollisionShape>();
collisionShape->SetSphere(1.5f);
masterControl_->tileMaster_->AddToAffectors(WeakPtr<Node>(rootNode_), WeakPtr<RigidBody>(rigidBody_));
Node* triggerNode = rootNode_->CreateChild("PickupTrigger");
triggerBody_ = triggerNode->CreateComponent<RigidBody>();
triggerBody_->SetTrigger(true);
triggerBody_->SetMass(0.0f);
triggerBody_->SetLinearFactor(Vector3::ZERO);
CollisionShape* triggerShape = triggerNode->CreateComponent<CollisionShape>();
triggerShape->SetSphere(2.5f);
particleEmitter_ = rootNode_->CreateComponent<ParticleEmitter>();
particleEmitter_->SetEffect(masterControl_->cache_->GetTempResource<ParticleEffect>("Resources/Particles/Shine.xml"));
sample_ = masterControl_->cache_->GetResource<Sound>("Resources/Samples/Pickup.ogg");
sample_->SetLooped(false);
sampleSource_ = rootNode_->CreateComponent<SoundSource>();
sampleSource_->SetGain(0.6f);
sampleSource_->SetSoundType(SOUND_EFFECT);
SubscribeToEvent(triggerNode, E_NODECOLLISIONSTART, HANDLER(Pickup, HandleTriggerStart));
SubscribeToEvent(E_SCENEUPDATE, HANDLER(Pickup, HandleSceneUpdate));
}
void Pickup::HandleTriggerStart(StringHash eventType, VariantMap &eventData)
{
using namespace NodeCollisionStart;
PODVector<RigidBody*> collidingBodies;
triggerBody_->GetCollidingBodies(collidingBodies);
for (int i = 0; i < collidingBodies.Length(); i++) {
RigidBody* collider = collidingBodies[i];
if (collider->GetNode()->GetNameHash() == N_PLAYER) {
Reset();
sampleSource_->Play(sample_);
masterControl_->player_->Pickup(rootNode_->GetNameHash());
}
}
}
void Pickup::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
{
using namespace Update;
float timeStep = eventData[P_TIMESTEP].GetFloat();
//Emerge
if (rootNode_->GetPosition().y_ < -0.1f) {
rootNode_->Translate(Vector3::UP * timeStep * (0.25f - rootNode_->GetPosition().y_));
}
triggerBody_->SetPosition(rootNode_->GetPosition());
if ((rootNode_->GetPosition()*(Vector3::ONE-Vector3::UP)).Length() > 23.666f) Reset();
}
void Pickup::Reset()
{
rootNode_->SetPosition(masterControl_->spawnMaster_->CreateSpawnPoint());
rigidBody_->SetLinearVelocity(Vector3::ZERO);
rigidBody_->ResetForces();
}