RType
TimeManagement.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-type
4 ** File description:
5 ** R-type
6 */
7 
9 #include "Timer.hpp"
10 
11 /**
12  * @brief update, update the time
13  * @param event
14  * @param component
15  */
16 void TimeManagement::update(std::shared_ptr<Event> event, std::shared_ptr<IComponentRType> component)
17 {
18  auto time = std::dynamic_pointer_cast<Timer>(component);
19  if (time) {
20  gettimeofday(&time->_now, nullptr);
21  timersub(&time->_now, &time->_startTime, &time->_diff);
22  if (time->_diff.tv_sec >= time->_targetTime.tv_sec && time->_diff.tv_usec >= time->_targetTime.tv_usec) {
23  if (!time->isActive())
24  return;
25  auto drawable = time->getTarget();
26 
27  if (!drawable)
28  return;
29 
30  auto position = drawable->getPosition();
31  if (std::string(drawable->getAttribute()).find("bullet") != std::string::npos)
32  drawable->setPosition(std::make_tuple(std::get<0>(position) + 55, std::get<1>(position)));
33  else if (std::string(drawable->getAttribute()).find("boss attack") != std::string::npos)
34  drawable->setPosition(std::make_tuple(std::get<0>(position) + 2 * time->getDirection(), std::get<1>(position)));
35  else
36  drawable->setPosition(std::make_tuple(std::get<0>(position) + 10 * time->getDirection(), std::get<1>(position)));
37  if (std::string(drawable->getAttribute()).find("sprite boss") != std::string::npos && std::get<0>(position) < 470)
38  time->setActive(false);
39  time->_startTime = time->_now;
40  }
41  }
42 }