void Enemy::move_enemy(sf::Vector2f fullLength, string x, string y, float lengthOfLine) { cout << lengthOfLine << endl; // This whole thing works like this: // You have 2 sums. One is for keeping the current sum of both xs and ys (basically, where the object is at this moment) // And another sum, which keeps track of the previous iteration of sum (where the object was an iteration ago) // Then the program finds the difference between the current and previous sums and then gets the integer amount of that difference. // That difference is exactly the amount of pixels the object should move in the next iteration. // All of this is necessary because it`s not possible to move pixels by a float value (e.g. 3.45) // Here`s an example - you have to move 3.45 pixels as y coordinate, every iteration. This is how the sum will look like: // 3.45, 6.9, 10.35, 13.8, 17.25 and here`s how much y coordinate should be moved every iteration: // 3, 3, 4, 3, 4 and so on... movedSumBefore.y = movedSum.y; fractPartBefore.y = modf(movedSumBefore.y, &intPartBefore.y); movedSumBefore.x = movedSum.x; fractPartBefore.x = modf(movedSumBefore.x, &intPartBefore.x); // Now the sqrt(2) part is a bit different - the sqrt(2) itself is the ratio between moving in straight line and diagonally: // Example - if your movement speed is 10 pixels per frame, then by moving in a straight line, you`ll move 100 pixels in 10 frames. // But if you`re moving diagonally, you`ll move same 100 pixels in only 7 frames (because you move both x and y by 10, thus when you get to 100 pixels in length (hypotenuse), // You will have moved 71 pixels in X axis and 71 in Y, thus the divisor can be calculated ny taking square root from 2 (1 pixel x and 1 pixel y) // It also has to be divided by 2 (that`s how we get how much to move both x and y (identical, about 0.707 each) and finally by multiplying by movement speed, we get the // Amount of pixels the player should move with each frame if (y == "+") { movedSum.y += sqrt(2) / 2 * movementSpeed; } else if (y == "-") { movedSum.y -= sqrt(2) / 2 * movementSpeed; } fractPart.y = modf(movedSum.y, &intPart.y); if (x == "+") { movedSum.x += sqrt(2) / 2 * movementSpeed; } else if (x == "-") { movedSum.x -= sqrt(2) / 2 * movementSpeed; } fractPart.x = modf(movedSum.x, &intPart.x); timesToMoveInteger.y = intPart.y - intPartBefore.y; timesToMoveInteger.x = intPart.x - intPartBefore.x; if (lengthOfLine >= distanceFromPlayer) { // Move the enemy and it`s health bar move_sprite(timesToMoveInteger); moveHealthBar(); } }
void IndianaJonesUnit::update() { if (isDead() || reachedGoal){ return; } if (slowtimer > 0) { slowtimer -= gameEngine->deltaTime.asMilliseconds(); if (slowtimer <= 0) { speed = maxspeed; slowed = false; } } updateMovement(); sf::Vector2f offset(moveDirection.x * speed * gameEngine->deltaTime.asMilliseconds(), (moveDirection.y * speed * gameEngine->deltaTime.asMilliseconds())); posX += offset.x; posY += offset.y; moveHealthBar(offset); moveAnimations[currentMoveAnimationIndex]->setPos(posX, posY); moveAnimations[currentMoveAnimationIndex]->update(); float percentageHP = currentHealth / maxHealth; if (percentageHP < 0) { percentageHP = 0; } healthBarFG->setSize(healthBarBG->getSize().x * percentageHP, healthBarFG->getSize().y); groundTilesChanged = false; towerRemoved = false; }
void Enemy::move_sprite(sf::Vector2f move) { currentPosition += move; enemySprite.setPosition(currentPosition); moveHealthBar(); }