| 141 | } |
| 142 | |
| 143 | void OnUpdate(float elapsedTime) override |
| 144 | { |
| 145 | const float escapeMaxDist = 50.f; |
| 146 | const float speed = 200.f; |
| 147 | |
| 148 | Nz::UInt64 curTime = Nz::GetElapsedMilliseconds(); |
| 149 | std::uniform_real_distribution<float> dis(-escapeMaxDist, escapeMaxDist); |
| 150 | |
| 151 | for (const Ndk::EntityHandle& entity : GetEntities()) |
| 152 | { |
| 153 | auto& nodeComponent = entity->GetComponent<Ndk::NodeComponent>(); |
| 154 | auto& spaceshipComponent = entity->GetComponent<SpaceshipComponent>(); |
| 155 | auto& velocityComponent = entity->GetComponent<Ndk::VelocityComponent>(); |
| 156 | |
| 157 | //< I agree, I need some kind of SoundEmitterComponent |
| 158 | spaceshipComponent.engineSound.SetPosition(nodeComponent.GetPosition()); |
| 159 | spaceshipComponent.engineSound.SetVelocity(velocityComponent.linearVelocity); |
| 160 | |
| 161 | spaceshipComponent.hitSound.SetPosition(nodeComponent.GetPosition()); |
| 162 | spaceshipComponent.hitSound.SetVelocity(velocityComponent.linearVelocity); |
| 163 | |
| 164 | spaceshipComponent.laserSound.SetPosition(nodeComponent.GetPosition()); |
| 165 | spaceshipComponent.laserSound.SetVelocity(velocityComponent.linearVelocity); |
| 166 | |
| 167 | Nz::Vector3f targetDir = spaceshipComponent.targetPos - nodeComponent.GetPosition(); |
| 168 | targetDir.Normalize(); |
| 169 | |
| 170 | Nz::Quaternionf targetRotation = Nz::Quaternionf::RotationBetween(Nz::Vector3f::Forward(), targetDir); |
| 171 | |
| 172 | nodeComponent.SetRotation(Nz::Quaternionf::Slerp(nodeComponent.GetRotation(), targetRotation, elapsedTime * 1.5f)); |
| 173 | |
| 174 | Nz::Vector3f actualDir = nodeComponent.GetForward(); |
| 175 | float sqDistance = spaceshipComponent.targetPos.SquaredDistance(nodeComponent.GetPosition()); |
| 176 | |
| 177 | if (spaceshipComponent.attacking) |
| 178 | { |
| 179 | float dotProduct = targetDir.DotProduct(actualDir); |
| 180 | if (dotProduct > 0.9f && sqDistance < (150.f * 150.f) && !entity->HasComponent<LaserBeamComponent>()) |
| 181 | { |
| 182 | auto& laserBeam = entity->AddComponent<LaserBeamComponent>(); |
| 183 | laserBeam.origin = Nz::Vector3f::Forward() * 12.f + Nz::Vector3f::Down() * 2.f; |
| 184 | } |
| 185 | |
| 186 | if (sqDistance < (100.f * 100.f)) |
| 187 | { |
| 188 | entity->RemoveComponent<LaserBeamComponent>(); |
| 189 | |
| 190 | spaceshipComponent.targetPos -= Nz::Vector3f(dis(m_sharedData.randomGen), dis(m_sharedData.randomGen), dis(m_sharedData.randomGen)) * -actualDir * escapeMaxDist / 2.f; |
| 191 | spaceshipComponent.attacking = false; |
| 192 | } |
| 193 | } |
| 194 | else if (sqDistance < (50.f * 50.f) && spaceshipComponent.hitTime == 0) |
| 195 | { |
| 196 | spaceshipComponent.targetPos = Nz::Vector3f::Zero(); |
| 197 | spaceshipComponent.attacking = true; |
| 198 | } |
| 199 | |
| 200 | if (spaceshipComponent.hitTime == 0 || curTime - spaceshipComponent.hitTime <= 1000) |
nothing calls this directly
no test coverage detected