void Unit::Update(sf::Vector2f mouseWorldPos) { if (mMoveAvailable) mSprite.setColor(sf::Color::White); else mSprite.setColor(sf::Color(127, 127, 127, 255)); if (mUnitSubState != DEAD && mUnitSubState != DYING) { mUnitSubState = NORMAL; if (mUnitHP <= 0) { mUnitSubState = DYING; mHurtAnimationColor = 255; } } if (mUnitSubState == NORMAL) { SetMoving(!mMovementList.empty()); if (!mMovementList.empty()) mCurrentTargetTile = &mMovementList.front(); if (mCurrentTargetTile != nullptr) { mTargetPosition = sf::Vector2f(mCurrentTargetTile->x * mTileSize, mCurrentTargetTile->y * mTileSize / 2); if (mCurrentPosition.x < mTargetPosition.x) { SetMovingRight(true); mCurrentPosition.x += 4; if (mCurrentPosition.x >= mTargetPosition.x) mCurrentPosition.x = mTargetPosition.x; if (mCurrentPosition.y < mTargetPosition.y) { mCurrentPosition.y += 2; if (mCurrentPosition.y >= mTargetPosition.y) mCurrentPosition.y = mTargetPosition.y; } if (mCurrentPosition.y > mTargetPosition.y) { mCurrentPosition.y -= 2; if (mCurrentPosition.y <= mTargetPosition.y) mCurrentPosition.y = mTargetPosition.y; } } else if (mCurrentPosition.x > mTargetPosition.x) { SetMovingLeft(true); mCurrentPosition.x -= 4; if (mCurrentPosition.x <= mTargetPosition.x) mCurrentPosition.x = mTargetPosition.x; if (mCurrentPosition.y < mTargetPosition.y) { mCurrentPosition.y += 2; if (mCurrentPosition.y >= mTargetPosition.y) mCurrentPosition.y = mTargetPosition.y; } if (mCurrentPosition.y > mTargetPosition.y) { mCurrentPosition.y -= 2; if (mCurrentPosition.y <= mTargetPosition.y) mCurrentPosition.y = mTargetPosition.y; } } else if (mCurrentPosition.y < mTargetPosition.y) { SetMovingDown(true); mCurrentPosition.y += 4; if (mCurrentPosition.y >= mTargetPosition.y) mCurrentPosition = mTargetPosition; } else if (mCurrentPosition.y > mTargetPosition.y) { SetMovingUp(true); mCurrentPosition.y -= 4; if (mCurrentPosition.y <= mTargetPosition.y) mCurrentPosition = mTargetPosition; } if (mCurrentPosition == mTargetPosition) { mMovementList.erase(mMovementList.begin() + 0); mCurrentTargetTile = nullptr; mGridVector = GridVector(mCurrentPosition.x / mTileSize, mCurrentPosition.y / mTileSize / 2); } } if (mMovingLeft) { mSpriteAnimationVector.x = 1; mSprite.setScale(2, 2); mAnimationSpeed = 0.5; } else if (mMovingRight) { mSpriteAnimationVector.x = 1; mSprite.setScale(-2, 2); mAnimationSpeed = 0.5; } else if (mMovingDown) { mSpriteAnimationVector.x = 2; mSprite.setScale(2, 2); mAnimationSpeed = 0.5; } else if (mMovingUp) { mSpriteAnimationVector.x = 3; mSprite.setScale(2, 2); mAnimationSpeed = 0.5; } else if (mMouseover) { if (mSpriteAnimationVector.y <= 0) { mSpriteAnimationVector.y = 1; mAnimationDirection = 1; } mSpriteAnimationVector.x = 4; mSprite.setScale(2, 2); mAnimationSpeed = 0.6; } else { if (mSpriteAnimationVector.y <= 0) { mSpriteAnimationVector.y = 1; mAnimationDirection = 1; } mSpriteAnimationVector.x = 0; mSprite.setScale(2, 2); mAnimationSpeed = 1.0; } int animationElapsed = mAnimationClock.getElapsedTime().asMilliseconds(); int subAnimationElapsed = mSubAnimationClock.getElapsedTime().asMilliseconds(); if (animationElapsed >= 500 * mAnimationSpeed && subAnimationElapsed >= 100 * mAnimationSpeed && !mMoving) { mSubAnimationClock.restart(); if ((mSpriteAnimationVector.y <= 1 && mAnimationDirection == -1) || (mSpriteAnimationVector.y >= 3 && mAnimationDirection == 1)) mAnimationDirection *= -1; mSpriteAnimationVector.y += mAnimationDirection; if (mSpriteAnimationVector.y >= 3 || mSpriteAnimationVector.y <= 1) { mAnimationDirection *= -1; mAnimationClock.restart(); } } if (subAnimationElapsed >= 250 * mAnimationSpeed && mMoving) { mSubAnimationClock.restart(); mAnimationClock.restart(); mSpriteAnimationVector.y++; if (mSpriteAnimationVector.y > 3) mSpriteAnimationVector.y = 0; } mRenderPosition.x = mCurrentPosition.x + mTileSize / 2; mRenderPosition.y = mCurrentPosition.y + mTileSize; mSprite.setPosition(mRenderPosition); if (mSpriteAnimationVector.y == 0 && (mSpriteAnimationVector.x == 0 || mSpriteAnimationVector.x == 4)) mSpriteAnimationVector.y = 1; mSprite.setTextureRect(sf::IntRect(mSpriteAnimationVector.x * mSpriteSize.x, mSpriteAnimationVector.y * mSpriteSize.y + mTeamNumber * mSpriteSize.y * 4, mSpriteSize.x, mSpriteSize.y)); } else if (mUnitSubState == DYING) DyingUpdate(); else if (mUnitSubState == DAMAGED) HurtUpdate(); }
void PlayState::LoadContent() { if (!playFont.loadFromFile("Resources/Fonts/calibri.ttf")) cout << "Could not find font calibri.ttf" << endl; currentLevel = new Level(GridVector(25, 36), GetWindow()); }
bool Map::initialize(Game* gamePtr) { char errorStr[200]; Wall* curWall = NULL; if(!wallTexture.initialize(gamePtr->getGraphics(), "pictures\\wall4x36_grey.png")) { throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing wall texture")); } // initialize walls and make all visible for(int i = 0; i < 2; i++) { std::vector<Wall*>* walls; enum ORIENTATION orientation; if(i == 0) { walls = &horizontalWalls; orientation = HORIZONTAL; } else { walls = &verticalWalls; orientation = VERTICAL; // visible = false; } for(int x = 0; x < MAX_COLS+1; x++) { // +1 to include top/bottom and left/right wall for(int y = 0; y < MAX_ROWS+1; y++) { // visible = !visible; curWall = new Wall; if(!curWall->initialize(gamePtr, &wallTexture)) { sprintf_s(errorStr, "Error initializing wall i = %d, x = %d, y = %d", i, x, y); throw(GameError(gameErrorNS::FATAL_ERROR, errorStr)); return false; } curWall->setCoord(GridVector(x,y), orientation); walls->push_back(curWall); } } } generatePath(GridVector(MAX_COLS/2,MAX_ROWS/2)); /* curWall = firstWall; for(tempSize = size/2; tempSize > 0; tempSize--) { curWall->setCoord(rand() % MAX_COLS, rand() % MAX_ROWS, ROW); curWall = curWall->getNextWall(); } for(tempSize = size/2; tempSize > 0; tempSize--) { curWall->setCoord(rand() % MAX_COLS, rand() % MAX_ROWS, COL); curWall = curWall->getNextWall(); } */ /* while(curWall) { curWall->setCoord(curX, curY, COL); curWall = curWall->getNextWall(); curX += 1; } */ return true; }