Blame view

ios/cocos2d/extensions/Particle3D/PU/CCPULineAffector.cpp 6.39 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
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
  /****************************************************************************
   Copyright (C) 2013 Henry van Merode. All rights reserved.
   Copyright (c) 2015-2016 Chukong Technologies Inc.
   Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
   
   http://www.cocos2d-x.org
   
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
   ****************************************************************************/
  
  #include "CCPULineAffector.h"
  #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  
  NS_CC_BEGIN
  
  // Constants
  const float PULineAffector::DEFAULT_MAX_DEVIATION = 1.0f;
  const float PULineAffector::DEFAULT_TIME_STEP = 0.1f;
  const Vec3 PULineAffector::DEFAULT_END(0, 0, 0);
  const float PULineAffector::DEFAULT_DRIFT = 0.0f;
  
  //-----------------------------------------------------------------------
  PULineAffector::PULineAffector() : 
      PUAffector(),
      _maxDeviation(DEFAULT_MAX_DEVIATION),
      _scaledMaxDeviation(1.0f),
      _end(DEFAULT_END),
      _timeSinceLastUpdate(0.0f),
      _timeStep(DEFAULT_TIME_STEP),
      _drift(DEFAULT_DRIFT),
      _oneMinusDrift(1.0f),
      _update(true),
      _first(true)
  {
  }
  
  PULineAffector::~PULineAffector( void )
  {
  
  }
  //-----------------------------------------------------------------------
  float PULineAffector::getMaxDeviation() const
  {
      return _maxDeviation;
  }
  //-----------------------------------------------------------------------
  void PULineAffector::setMaxDeviation(float maxDeviation)
  {
      _maxDeviation = maxDeviation;
      _scaledMaxDeviation = _maxDeviation * _affectorScale.length();
  }
  //-----------------------------------------------------------------------
  const Vec3& PULineAffector::getEnd() const
  {
      return _end;
  }
  //-----------------------------------------------------------------------
  void PULineAffector::setEnd(const Vec3& end)
  {
      _end = end;
  }
  //-----------------------------------------------------------------------
  float PULineAffector::getTimeStep() const
  {
      return _timeStep;
  }
  //-----------------------------------------------------------------------
  void PULineAffector::setTimeStep(float timeStep)
  {
      _timeStep = timeStep;
  }
  //-----------------------------------------------------------------------
  float PULineAffector::getDrift() const
  {
      return _drift;
  }
  //-----------------------------------------------------------------------
  void PULineAffector::setDrift(float drift)
  {
      _drift = drift;
      _oneMinusDrift = 1.0f - drift;
  }
  //-----------------------------------------------------------------------
  void PULineAffector::notifyRescaled(const Vec3& scale)
  {
      _scaledMaxDeviation = _maxDeviation * scale.length();
  }
  //-----------------------------------------------------------------------
  void PULineAffector::preUpdateAffector(float deltaTime)
  {
      if (/*technique->getNumberOfEmittedParticles()*/static_cast<PUParticleSystem3D *>(_particleSystem)->getAliveParticleCount() > 0)
      {
          _timeSinceLastUpdate += deltaTime;
          while (_timeSinceLastUpdate > _timeStep)
          {
              _timeSinceLastUpdate -= _timeStep;
              _update = true;
          }
      }
      (static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(_end); // Always update
  }
  //-----------------------------------------------------------------------
  
  void PULineAffector::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  {
      //_first = true;
      //for (auto iter : _particleSystem->getParticles())
      {
          //PUParticle3D *particle = iter;
          (static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(particle->originalPosition); // Always update
          if (_update && CCRANDOM_0_1() > 0.5f && !_first)
          {
              // Generate a random vector perpendicular on the line
              Vec3 perpendicular;
              Vec3::cross(_end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
              perpendicular.normalize();
  
              // Determine a random point near the line.
              Vec3 targetPosition = particle->originalPosition + _scaledMaxDeviation * CCRANDOM_0_1() * perpendicular;
  
              /** Set the new position.
              @remarks
                  This affector already takes rotational offset of the particle system into account. This means that there is no need
                  to set the particle system to keep_local to 'true'. The reason is that this is a specialized affector that calculates
                  a new particle position instead of a direction.
              */
              particle->position = _drift * targetPosition + _oneMinusDrift * particle->position;
              (static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(particle->position);
          }
          _first = false;
      }
  }
  
  //-----------------------------------------------------------------------
  void PULineAffector::postUpdateAffector(float /*deltaTime*/)
  {
      _update = false;
  }
  
  void PULineAffector::firstParticleUpdate( PUParticle3D* /*particle*/, float /*deltaTime*/ )
  {
      _first = true;
  }
  
  PULineAffector* PULineAffector::create()
  {
      auto pla = new (std::nothrow) PULineAffector();
      pla->autorelease();
      return pla;
  }
  
  void PULineAffector::copyAttributesTo( PUAffector* affector )
  {
      PUAffector::copyAttributesTo(affector);
  
      PULineAffector* lineAffector = static_cast<PULineAffector*>(affector);
      lineAffector->setMaxDeviation(_maxDeviation);
      lineAffector->_end = _end;
      lineAffector->_timeStep = _timeStep;
      lineAffector->_drift = _drift;
      lineAffector->_oneMinusDrift = _oneMinusDrift;
  }
  
  NS_CC_END