void Enemy::update(const std::vector<std::string>& levelData, Player* player, float deltaTime) { // Get the direction vector towards the player m_direction = glm::normalize(player->getPosition() - m_position); m_position += m_direction * m_speed * deltaTime; // Do collision collideWithLevel(levelData); }
void Enemy::update(const std::vector<std::vector<Tile>>& levelData, std::vector<Human*>& humans, std::vector<Enemy*>& enemy) { _isMoving = false; // Update only when enemy is not dead if (_isDead == false) { // Pathfind every ten frames _frameCount++; if (_frameCount == 10) { path.clear(); _frameCount = 0; if (_ai.getPathFinder().findPathToHuman(glm::floor(_position.x / TILE_WIDTH), glm::floor(_position.y / TILE_WIDTH), levelData, 20, path)) { // The last node in the path is always the current node, so we don have to path to it. path.pop_back(); } } // If we arent already at the spot if (path.size()) { glm::vec2 centerOfTile = glm::vec2(path.back() * TILE_WIDTH) + glm::vec2(TILE_WIDTH / 2.0f); // For now, move towards closest tile _direction = glm::normalize(centerOfTile - _position); _position += _direction * _speed; _isMoving = true; } ////Get the closest human //Human* closestHuman = getNearestHuman(humans); ////If we got the closest human then... //if (closestHuman != nullptr) //{ // // //...calculate the direction btween them // _direction = glm::normalize(closestHuman->getPosition() - _position); // //...move towards the closest human // _position += _direction * _speed; // _isMoving = true; // } // collideWithLevel(levelData); } }
void Player::update(const std::vector<std::string>& levelData, std::vector<Human*>& humans, std::vector<Zombie*>& zobies, float deltaTime) { if (_inputMgr->isKeyDown(SDLK_w)) { _position.y += _speed * deltaTime; } else if (_inputMgr->isKeyDown(SDLK_s)) { _position.y -= _speed * deltaTime; } else if (_inputMgr->isKeyDown(SDLK_a)) { _position.x -= _speed * deltaTime; } else if (_inputMgr->isKeyDown(SDLK_d)) { _position.x += _speed * deltaTime; } if (_inputMgr->isKeyPressed(SDLK_1) && _guns.size() > 0) { _currGunIdx = 0; } else if (_inputMgr->isKeyPressed(SDLK_2) && _guns.size() > 1) { _currGunIdx = 1; } else if (_inputMgr->isKeyPressed(SDLK_3) && _guns.size() > 2) { _currGunIdx = 2; } glm::vec2 mouseCoords = _inputMgr->getMouseCoords(); mouseCoords = _camera->convertScreenToWorld(mouseCoords); glm::vec2 centerPosition = _position + glm::vec2(AGENT_RADIUS); _direction = glm::normalize(mouseCoords - centerPosition); if (_currGunIdx != -1) { _guns[_currGunIdx]->update(_inputMgr->isKeyDown(SDL_BUTTON_LEFT), centerPosition, _direction, *_bullets, deltaTime); } collideWithLevel(levelData); }
void Player::update(const std::vector<std::string>& levelData, std::vector<Human*>& humans, std::vector<Zombie*>& zombies, float deltaTime){ if (m_inputManager->isKeyDown(SDLK_w)) { m_position.y += m_speed * deltaTime; } else if (m_inputManager->isKeyDown(SDLK_s)) { m_position.y -= m_speed * deltaTime; } if (m_inputManager->isKeyDown(SDLK_a)) { m_position.x -= m_speed * deltaTime; } else if (m_inputManager->isKeyDown(SDLK_d)) { m_position.x += m_speed * deltaTime; } if (m_inputManager->isKeyDown(SDLK_1) && m_guns.size() >= 0) { m_currentGunIndex = 0; } else if (m_inputManager->isKeyDown(SDLK_2) && m_guns.size() >= 1) { m_currentGunIndex = 1; } else if (m_inputManager->isKeyDown(SDLK_3) && m_guns.size() >= 2) { m_currentGunIndex = 2; } glm::vec2 mouseCoords = m_inputManager->getMouseCoords(); mouseCoords = m_camera->convertScreenToWorld(mouseCoords); glm::vec2 centerPlayerPosition = m_position + glm::vec2(AGENT_RADIUS); //Mouse direction m_direction = glm::normalize(mouseCoords - centerPlayerPosition); if (m_currentGunIndex != -1) { m_guns[m_currentGunIndex]->update(m_inputManager->isKeyDown(SDL_BUTTON_LEFT), centerPlayerPosition, m_direction, *m_bullets, deltaTime); } collideWithLevel(levelData); }
void Zombie::update(const std::vector<std::string>& levelData, std::vector<Human*>& humans, std::vector<Zombie*>& zombies, float deltaTime) { Human* closestHuman = getNearestHuman(humans); if (closestHuman != nullptr) { _direction = glm::normalize(closestHuman->getPosition() - _position); _position += _direction * _speed * deltaTime; } collideWithLevel(levelData); }
void Zombie::update(const std::vector<std::string>& levelData, std::vector<Human*>& humans, std::vector<Zombie*>& zombies) { // Find the closest human Human* closestHuman = getNearestHuman(humans); // If we found a human, move towards him if (closestHuman != nullptr) { // Get the direction vector twoards the player glm::vec2 direction = glm::normalize(closestHuman->getPosition() - _position); _position += direction * _speed; } // Do collision collideWithLevel(levelData); }
void UFO::update(const std::vector<std::string>& levelData, std::vector<SpaceShip*>& ships, std::vector<UFO*>& UFOs) { // Find the closest ship SpaceShip* closestShip = getNearestShip(ships); // If we found a ship, move towards him if (closestShip != nullptr) { // Get the direction vector twoards the player glm::vec2 direction = glm::normalize(closestShip->getPosition() - _position); _position += direction * _speed; } // Do collision collideWithLevel(levelData); }
void Human::update(const std::vector<std::string>& levelData, float deltaTime) { static std::mt19937 randomEngine(time(nullptr)); static std::uniform_real_distribution<float> randRotate(-40.0f, 40.0f); _position += m_direction * _speed * deltaTime; // Randomly change direction every 20 frames if (_frames == 20) { m_direction = glm::rotate(m_direction, randRotate(randomEngine)); _frames = 0; } else { _frames++; } if (collideWithLevel(levelData)) { m_direction = glm::rotate(m_direction, randRotate(randomEngine)); } }
void Human::update(const std::vector<std::string>& levelData, std::vector<Human*>& humans, std::vector<Zombie*>& zombies, float deltaTime) { const float DEG_TO_RAD = 3.14159265359f / 180.0f; static std::mt19937 randomEngine(time(nullptr)); static std::uniform_real_distribution<float> randRotate(-40.0f * DEG_TO_RAD, 40.0f * DEG_TO_RAD); _position += _direction * _speed * deltaTime; //randomly change direction every 20 frames if (_frames == 60) { _direction = glm::rotate(_direction, randRotate(randomEngine)); _frames = 0; } else { _frames++; } if (collideWithLevel(levelData)) { //_direction = glm::rotate(_direction, randRotate(randomEngine)); } }
void Player::update(const std::vector<std::string>& levelData, std::vector<Human*>& humans, std::vector<Zombie*>& zombies, float deltaTime){ //判断周围是否有僵尸 for (int i = 0; i < zombies.size(); i++){ glm::vec2 zombiePosition = zombies[i]->getAgentPos(); glm::vec2 distance = _position - zombiePosition; float playToZombie = glm::length(distance); if (playToZombie < _scan){//僵尸进入视野 //播放僵尸音效 playSound("zombie"); } } //按键事件 if (_inputManager->isKeyPressed(SDLK_w)){ _position.y += _speed*deltaTime; //playSound("walk");///<<<<<<此方式冗余,需优化 }else if (_inputManager->isKeyPressed(SDLK_s)){ _position.y -= _speed*deltaTime; //playSound("walk");///< } if (_inputManager->isKeyPressed(SDLK_a)){ //playSound("walk");///< _position.x -= _speed*deltaTime; }else if (_inputManager->isKeyPressed(SDLK_d)){ //playSound("walk");///< _position.x += _speed*deltaTime; } else if (_inputManager->isKeyPressed(SDLK_ESCAPE)){ exit(0); } if(_inputManager->isKeyPressed(SDLK_1) && _guns.size()>=0){ playSound("changeGun");///< _currentGunIndex = 0; } if (_inputManager->isKeyPressed(SDLK_2) && _guns.size()>= 1){ playSound("changeGun");///< _currentGunIndex = 1; } if (_inputManager->isKeyPressed(SDLK_3) && _guns.size() >= 2){ playSound("changeGun");///< _currentGunIndex = 2; } if (_inputManager->isKeyPressed(SDLK_4) && _guns.size() >= 3){ playSound("changeGun");///< _currentGunIndex = 3; } glm::vec2 mouseCoords = _inputManager->getMouseCoords(); mouseCoords = _camera->convertScreenToWorld(mouseCoords); glm::vec2 centerPosition = _position + glm::vec2(AGENT_RADIUS); _direction = glm::normalize(mouseCoords - centerPosition); if (_currentGunIndex!=-1){ //std::cout << _direction.x << "_" << _direction.y << ::std::endl; _guns[_currentGunIndex]->update(_inputManager->isKeyPressed(SDL_BUTTON_LEFT), centerPosition+(_direction.x*5.0f,_direction.y*5.0f),_direction,*_bullets,deltaTime); } collideWithLevel(levelData); }
//when update returns true, delete bulet bool Projectile::update(const std::vector<std::string>& levelData, float deltaTime) { _position += _direction * _speed * deltaTime; return collideWithLevel(levelData); }