|
| 1 | +#include "LaserTypeBullet.h" |
| 2 | +#include "../../XCFrameInfo.h" |
| 3 | +#include "../XCCollide/CollideInfo.h" |
| 4 | +#include "../XCRender/ParticleHelper.h" |
| 5 | +#include "../XCRender/RenderManager.h" |
| 6 | +#include <glm/glm.hpp> |
| 7 | +#include <glm/gtc/matrix_transform.hpp> |
| 8 | +#include <glm/gtc/type_ptr.hpp> |
| 9 | +#include <GL3/gl3w.h> |
| 10 | +#include <iostream> |
| 11 | +LaserTypeBullet::LaserTypeBullet(std::string bulletImagePath, glm::vec4 dInfo, glm::vec3 sInfo, glm::vec3 cSize, glm::vec3 initCoord, |
| 12 | + float v, float a, float agl, float incA, int rbTime, bool ap, |
| 13 | + int pDensity, float pFinishTime, float pVelocity, float pSize, glm::vec4 pColor) |
| 14 | +{ |
| 15 | + //init part |
| 16 | + imagePath = bulletImagePath; |
| 17 | + divideInfo = dInfo; |
| 18 | + scaleInfo = sInfo; |
| 19 | + collideSize = cSize; |
| 20 | + NowPosition[0] = initCoord[0]; |
| 21 | + NowPosition[1] = initCoord[1]; |
| 22 | + NowPosition[2] = initCoord[2]; |
| 23 | + //work part |
| 24 | + velocity = v; |
| 25 | + acceleration = a; |
| 26 | + angle = agl; |
| 27 | + increaseAngle = incA; |
| 28 | + reBoundTime = rbTime; |
| 29 | + aimToPlayer = ap; |
| 30 | + //release part |
| 31 | + particleDensity = pDensity; |
| 32 | + particleFinishTime = pFinishTime; |
| 33 | + particleVelocity = pVelocity; |
| 34 | + particleSize = pSize; |
| 35 | + particleColor = pColor; |
| 36 | + |
| 37 | + //define current type |
| 38 | + setCurrentType(BulletType); |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +void LaserTypeBullet::Init() |
| 43 | +{ |
| 44 | + if (!isInit) { |
| 45 | + image = new XCImageHelper(imagePath, true); |
| 46 | + |
| 47 | + isInit = true; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void LaserTypeBullet::Render() |
| 52 | +{ |
| 53 | + if (isInit) { |
| 54 | + timer.Tick(); |
| 55 | + auto pPlayerHelper1 = CollideInfo::getCollideHelper(); |
| 56 | + float* playerPos = (pPlayerHelper1 == nullptr ? nullptr : pPlayerHelper1->getPlayerPosition()); |
| 57 | + Bullet::solveBulletMovement(aimToPlayer, playerPos, velocity, angle, acceleration, increaseAngle, timer.getDeltaFrame()); |
| 58 | + |
| 59 | + |
| 60 | + glEnable(GL_BLEND); |
| 61 | + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 62 | + image->Render(glm::vec3(NowPosition[0], NowPosition[1], NowPosition[2]), glm::vec4(1.0f), glm::radians(angle), glm::vec3(0, 0, 1), |
| 63 | + scaleInfo * glm::vec3(XCFrameInfo::FrameRight, XCFrameInfo::FrameTop, 1.0f), |
| 64 | + IRenderHelper::GetSpecificTexture(divideInfo[0], divideInfo[1], divideInfo[2], divideInfo[3])); |
| 65 | + glDisable(GL_BLEND); |
| 66 | + if (Bullet::checkReboundOrOverflow(&reBoundTime, &angle, scaleInfo[0], scaleInfo[1])) {//超出边界不渲染结束特效 |
| 67 | + isWorkFinish = true; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void LaserTypeBullet::setBulletTerminate() |
| 73 | +{ |
| 74 | + |
| 75 | + ParticleHelper* particleGroup = new ParticleHelper; |
| 76 | + particleGroup->addNewParticle(particleDensity, particleSize, particleVelocity, particleFinishTime, particleColor, |
| 77 | + glm::vec3(NowPosition[0], NowPosition[1], NowPosition[2])); |
| 78 | + RenderManager::getInstance()->AddRenderObject(ParticleGroupUuid, particleGroup); |
| 79 | + isWorkFinish = true; |
| 80 | +} |
| 81 | +void LaserTypeBullet::Release() |
| 82 | +{ |
| 83 | + if (isInit) { |
| 84 | + image->Release(); |
| 85 | + delete image; |
| 86 | + |
| 87 | + image = nullptr; |
| 88 | + isInit = false; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +bool LaserTypeBullet::BulletCollideWithPoint(float x, float y, bool &haveGraze) |
| 93 | +{ |
| 94 | + bool value = false; |
| 95 | + if (isInit && timer.getAccumlateTime() >= 0.2f) { |
| 96 | + x -= NowPosition[0]; |
| 97 | + y -= NowPosition[1]; |
| 98 | + |
| 99 | + float theta = glm::radians(angle); |
| 100 | + float NewX = x * cos(theta) + y * sin(theta); |
| 101 | + float NewY = -x * sin(theta) + y * cos(theta); |
| 102 | + |
| 103 | + float distance = pow(NewX, 2) + pow(NewY, 2); |
| 104 | + float ovalParameter = pow(NewX, 2) / pow(collideSize.x, 2) + pow(NewY, 2) / pow(collideSize.y, 2); |
| 105 | + |
| 106 | + if (!haveCheckGraze && distance <= CollideInfo::getGrazeDistance()) { |
| 107 | + haveGraze = true; |
| 108 | + haveCheckGraze = true; |
| 109 | + } |
| 110 | + value = (ovalParameter < 1.0f); |
| 111 | + } |
| 112 | + |
| 113 | + return value; |
| 114 | +} |
| 115 | + |
0 commit comments