SuperMarioOdysseyOnline/source/server/hns/GameModeTimer.cpp

126 lines
3.5 KiB
C++
Raw Normal View History

2022-06-16 21:33:18 +00:00
#include "server/gamemode/GameModeTimer.hpp"
#include <math.h>
#include "al/util/ControllerUtil.h"
#include "server/DeltaTime.hpp"
2022-08-07 21:42:56 +00:00
#include "logger.hpp"
2022-06-16 21:33:18 +00:00
GameModeTimer::GameModeTimer(bool isCountUp, float milli, int seconds, int minutes, int hours) {
mIsCountUp = isCountUp;
setTime(milli, seconds, minutes, hours);
}
GameModeTimer::GameModeTimer(float milli, int seconds, int minutes, int hours) {
mIsCountUp = false;
setTime(milli, seconds, minutes, hours);
}
GameModeTimer::GameModeTimer(GameTime const& time) {
mIsCountUp = true;
setTime(time);
}
GameModeTimer::GameModeTimer() {
mIsCountUp = true;
resetTime();
}
void GameModeTimer::setTime(float milli, int seconds, int minutes, int hours) {
2022-08-07 21:42:56 +00:00
if (milli >= 0)
mTime.mMilliseconds = milli;
if (seconds >= 0)
mTime.mSeconds = seconds;
if (minutes >= 0)
mTime.mMinutes = minutes;
if (hours >= 0)
mTime.mHours = hours;
2022-06-16 21:33:18 +00:00
}
void GameModeTimer::setTime(GameTime const& time) {
mTime = time;
}
void GameModeTimer::updateTimer() {
if (mIsUseControl) {
timerControl();
}
if (mIsEnabled) {
if (mIsCountUp) {
mTime.mMilliseconds += Time::deltaTime;
2022-08-07 21:42:56 +00:00
if (mTime.mMilliseconds >= 1) {
2022-06-16 21:33:18 +00:00
mTime.mMilliseconds--;
mTime.mSeconds++;
2022-08-07 21:42:56 +00:00
if (mTime.mSeconds >= 60) {
2022-06-16 21:33:18 +00:00
mTime.mSeconds = 0;
mTime.mMinutes++;
if (mTime.mMinutes >= 60) {
mTime.mMinutes = 0;
mTime.mHours++;
}
}
}
} else {
mTime.mMilliseconds += Time::deltaTime;
if (mTime.mMilliseconds >= 1) {
mTime.mMilliseconds--;
mTime.mSeconds--;
2022-08-07 21:42:56 +00:00
if (mTime.mSeconds < 0) {
2022-06-16 21:33:18 +00:00
mTime.mSeconds = 59;
mTime.mMinutes--;
if (mTime.mMinutes < 0) {
if (mTime.mHours > 0) {
mTime.mMinutes = 59;
mTime.mHours--;
} else {
mIsEnabled = false;
}
}
}
}
}
}
}
void GameModeTimer::timerControl() {
2022-08-07 21:42:56 +00:00
if (al::isPadHoldL(-1)) {
if (al::isPadTriggerDown(-1)) {
mTime.mMilliseconds = 0;
2022-06-16 21:33:18 +00:00
mTime.mSeconds = 0;
2022-08-07 21:42:56 +00:00
mTime.mMinutes = 0;
mTime.mHours = 0;
2022-06-16 21:33:18 +00:00
}
2022-08-07 21:42:56 +00:00
} else {
if (al::isPadHoldRight(-1)) {
mTime.mMilliseconds = 0;
mTime.mSeconds++;
if (mTime.mSeconds >= 60) {
mTime.mSeconds = 0;
mTime.mMinutes++;
if (mTime.mMinutes >= 60) {
2022-06-16 21:33:18 +00:00
mTime.mMinutes = 0;
2022-08-07 21:42:56 +00:00
mTime.mHours++;
2022-06-16 21:33:18 +00:00
}
}
}
2022-08-07 21:42:56 +00:00
if (al::isPadTriggerLeft(-1)) {
mTime.mMilliseconds = 0;
if (mTime.mMinutes != 0) {
mTime.mSeconds--;
if (mTime.mSeconds < 0) {
mTime.mSeconds = 59;
mTime.mMinutes--;
if (mTime.mMinutes < 0) {
if (mTime.mHours > 0) {
mTime.mMinutes = 59;
mTime.mHours--;
} else {
mTime.mMinutes = 0;
}
}
}
}
}
2022-06-16 21:33:18 +00:00
}
}