|
| 1 | +#include "TaskEnemy.h" |
| 2 | +#include "../XCRender/XCImageHelper.h" |
| 3 | +#include "../XCRender/RenderManager.h" |
| 4 | +#include "TaskDispatcher.h" |
| 5 | +TaskEnemy::TaskEnemy(std::string taskUuid, std::string targetTaskUuid, int repeatTime, int intervalFrame, |
| 6 | + std::string enemyImage, glm::vec2 dInfo, glm::vec3 sInfo, glm::vec2 sbInfo, glm::vec2 wInfo, |
| 7 | + glm::vec3 iCoord, float v, float a, float agle, float agleA, int type, float hp):Task(taskUuid, targetTaskUuid, repeatTime, intervalFrame) |
| 8 | +{ |
| 9 | + imagePath = enemyImage; |
| 10 | + divideInfo = dInfo; |
| 11 | + scaleInfo = sInfo; |
| 12 | + standbyInfo = sbInfo; |
| 13 | + walkInfo = wInfo; |
| 14 | + NowPosition = iCoord; |
| 15 | + |
| 16 | + velocity = v; |
| 17 | + acceleration = a; |
| 18 | + angle = agle; |
| 19 | + angleAcceleration = agleA; |
| 20 | + |
| 21 | + colorType = type; |
| 22 | + nowLife = hp; |
| 23 | +} |
| 24 | + |
| 25 | +void TaskEnemy::TaskInit() |
| 26 | +{ |
| 27 | + if (!taskIsInit) { |
| 28 | + TaskDispatcher::addTask(targetUUID); |
| 29 | + auto iterBegin = subUnitGroup.begin(); |
| 30 | + auto iterEnd = subUnitGroup.end(); |
| 31 | + for (auto unit = iterBegin; unit != iterEnd; unit++) { |
| 32 | + (*unit)->UnitInit(); |
| 33 | + } |
| 34 | + |
| 35 | + XCImageHelper *image = new XCImageHelper(imagePath, true); |
| 36 | + enemyImage = new EnemyObject(image, divideInfo, scaleInfo, standbyInfo, walkInfo, |
| 37 | + NowPosition, velocity, acceleration, angle, angleAcceleration, colorType); |
| 38 | + enemyImage->Init(); |
| 39 | + taskIsInit = true; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +void TaskEnemy::TaskWork() |
| 46 | +{ |
| 47 | + if (taskNowDurationFrame < taskDurationFrame || taskDurationFrame < 0) { |
| 48 | + if (taskAccumlateTime >= taskIntervalFrame) { |
| 49 | + if (!haveImageAddInQueue) { |
| 50 | + RenderManager::getInstance()->AddRenderObject(taskUUID, enemyImage); |
| 51 | + haveImageAddInQueue = true; |
| 52 | + } |
| 53 | + else { |
| 54 | + NowPosition = enemyImage->getNowPosition(); |
| 55 | + } |
| 56 | + auto iterBegin = subUnitGroup.begin(); |
| 57 | + auto iterEnd = subUnitGroup.end(); |
| 58 | + for (auto unit = iterBegin; unit != iterEnd; unit++) { |
| 59 | + if (!(*unit)->IsAddToQueue()) { |
| 60 | + (*unit)->setBulletInitCoord(NowPosition[0], NowPosition[1], NowPosition[2]); |
| 61 | + (*unit)->UnitWork(); |
| 62 | + } |
| 63 | + if ((*unit)->IsAddToQueue()) {//release here |
| 64 | + (*unit)->UnitRelease(); |
| 65 | + delete (*unit); |
| 66 | + if (std::next(unit) == subUnitGroup.end()) { |
| 67 | + subUnitGroup.erase(unit); |
| 68 | + break; |
| 69 | + } |
| 70 | + else { |
| 71 | + unit = subUnitGroup.erase(unit); |
| 72 | + iterEnd = subUnitGroup.end(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + if ( (subUnitGroup.empty() && RenderManager::getInstance()->CheckRenderComplete(taskUUID)) || enemyImage->getIsTerminate()) |
| 77 | + taskFinish = true; |
| 78 | + taskNowDurationFrame++; |
| 79 | + } |
| 80 | + taskAccumlateTime++; |
| 81 | + } |
| 82 | + else { |
| 83 | + taskFinish = true; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void TaskEnemy::TaskRelease() |
| 88 | +{ |
| 89 | + TaskDispatcher::updateTask(taskUUID, true); |
| 90 | + auto iterBegin = subUnitGroup.begin(); |
| 91 | + auto iterEnd = subUnitGroup.end(); |
| 92 | + for (auto unit = iterBegin; unit != iterEnd; unit++) { |
| 93 | + (*unit)->UnitRelease(); |
| 94 | + delete (*unit); |
| 95 | + } |
| 96 | + RenderManager::getInstance()->CleanRenderObject(taskUUID); |
| 97 | + subUnitGroup.clear(); |
| 98 | + |
| 99 | + if (!haveImageAddInQueue) { |
| 100 | + enemyImage->Release(); |
| 101 | + delete enemyImage; |
| 102 | + } |
| 103 | + taskIsInit = false; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments