1
0
Fork 0
mirror of https://github.com/k4zmu2a/SpaceCadetPinball.git synced 2024-11-10 20:48:04 +01:00
SpaceCadetPinball/SpaceCadetPinball/TLightGroup.cpp

465 lines
12 KiB
C++
Raw Normal View History

#include "pch.h"
#include "TLightGroup.h"
2021-01-05 10:02:43 +01:00
#include "control.h"
#include "loader.h"
#include "timer.h"
#include "TLight.h"
#include "TPinballTable.h"
TLightGroup::TLightGroup(TPinballTable* table, int groupIndex) : TPinballComponent(table, groupIndex, false)
2021-01-05 10:02:43 +01:00
{
Timer = 0;
NotifyTimer = 0;
TLightGroup::Reset();
2021-01-05 10:02:43 +01:00
if (groupIndex > 0)
{
int count;
Timer1TimeDefault = *loader::query_float_attribute(groupIndex, 0, 903);
int16_t* groupIndArr = loader::query_iattribute(groupIndex, 1027, &count);
2021-01-05 10:02:43 +01:00
for (int index = 0; index < count; ++groupIndArr)
{
2021-01-30 12:19:25 +01:00
auto comp = dynamic_cast<TLight*>(table->find_component(*groupIndArr));
2021-01-05 10:02:43 +01:00
if (comp)
List.push_back(comp);
2021-01-05 10:02:43 +01:00
++index;
}
}
}
int TLightGroup::Message(MessageCode code, float value)
2021-01-05 10:02:43 +01:00
{
auto const count = static_cast<int>(List.size());
2021-01-05 10:02:43 +01:00
switch (code)
{
case MessageCode::SetTiltLock:
case MessageCode::GameOver:
2021-01-05 10:02:43 +01:00
break;
case MessageCode::PlayerChanged:
2021-01-05 10:02:43 +01:00
{
auto playerPtr = &PlayerData[PinballTable->CurrentPlayer];
playerPtr->MessageField = MessageField;
playerPtr->MessageField2 = MessageField2;
playerPtr->Timer1Time = Timer1Time;
Reset();
playerPtr = &PlayerData[static_cast<int>(floor(value))];
MessageField = playerPtr->MessageField;
MessageField2 = playerPtr->MessageField2;
Timer1Time = playerPtr->Timer1Time;
if (!(MessageField == 0))
TimerExpired(0, this);
break;
}
case MessageCode::Reset:
2021-01-05 10:02:43 +01:00
Reset();
for (auto index = 0; index < PinballTable->PlayerCount; index++)
{
auto playerPtr = &PlayerData[index];
playerPtr->MessageField = MessageField;
playerPtr->MessageField2 = MessageField2;
playerPtr->Timer1Time = Timer1Time;
}
break;
case MessageCode::TLightGroupStepBackward:
2021-01-05 10:02:43 +01:00
{
auto lastLight = List.at(count - 1);
if (lastLight->FlasherOnFlag || lastLight->ToggledOnFlag || lastLight->ToggledOffFlag)
2021-01-05 10:02:43 +01:00
break;
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
{
TLightGroup::Message(MessageCode::TLightGroupReset, 0.0);
2021-01-05 10:02:43 +01:00
}
AnimationFlag = 1;
MessageField2 = code;
auto lastMessage = lastLight->MessageField;
auto lastStatus = lastLight->LightOnFlag;
2021-01-05 10:02:43 +01:00
for (auto index = count - 1; index > 0; --index)
{
auto lightCur = List.at(index);
auto lightPrev = List.at(index - 1);
lightCur->Message(lightPrev->LightOnFlag ? MessageCode::TLightTurnOn : MessageCode::TLightTurnOff, 0.0);
2021-01-05 10:02:43 +01:00
lightCur->MessageField = lightPrev->MessageField;
}
auto firstLight = List.at(0);
firstLight->Message(lastStatus ? MessageCode::TLightTurnOn : MessageCode::TLightTurnOff, 0.0);
firstLight->MessageField = lastMessage;
2021-01-05 10:02:43 +01:00
reschedule_animation(value);
break;
}
case MessageCode::TLightGroupStepForward:
2021-01-05 10:02:43 +01:00
{
auto lastLight = List.at(count - 1);
if (lastLight->FlasherOnFlag || lastLight->ToggledOnFlag || lastLight->ToggledOffFlag)
2021-01-05 10:02:43 +01:00
break;
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
{
TLightGroup::Message(MessageCode::TLightGroupReset, 0.0);
2021-01-05 10:02:43 +01:00
}
auto firstLight = List.at(0);
2021-01-05 10:02:43 +01:00
AnimationFlag = 1;
MessageField2 = code;
auto firstMessage = firstLight->MessageField;
auto firstStatus = firstLight->LightOnFlag;
2021-01-05 10:02:43 +01:00
for (auto index = 0; index < count - 1; index++)
{
auto lightCur = List.at(index);
auto lightNext = List.at(index + 1);
lightCur->Message(lightNext->LightOnFlag ? MessageCode::TLightTurnOn : MessageCode::TLightTurnOff, 0.0);
2021-01-05 10:02:43 +01:00
lightCur->MessageField = lightNext->MessageField;
}
lastLight->Message(firstStatus ? MessageCode::TLightTurnOn : MessageCode::TLightTurnOff, 0.0);
lastLight->MessageField = firstMessage;
2021-01-05 10:02:43 +01:00
reschedule_animation(value);
break;
}
case MessageCode::TLightGroupAnimationBackward:
2021-01-05 10:02:43 +01:00
{
if (AnimationFlag || MessageField2 == MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
MessageField2 = code;
AnimationFlag = 0;
auto lastLight = List.at(count - 1);
auto lastStatus = lastLight->ToggledOnFlag;
2021-01-05 10:02:43 +01:00
for (auto i = count - 1; i > 0; --i)
{
auto lightCur = List.at(i);
auto lightPrev = List.at(i - 1);
lightCur->Message(lightPrev->ToggledOnFlag ? MessageCode::TLightTurnOnTimed : MessageCode::TLightTurnOffTimed, 0.0);
2021-01-05 10:02:43 +01:00
}
auto firstLight = List.at(0);
firstLight->Message(lastStatus ? MessageCode::TLightTurnOnTimed : MessageCode::TLightTurnOffTimed, 0);
2021-01-05 10:02:43 +01:00
reschedule_animation(value);
break;
}
case MessageCode::TLightGroupAnimationForward:
2021-01-05 10:02:43 +01:00
{
if (AnimationFlag || MessageField2 == MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
MessageField2 = code;
AnimationFlag = 0;
auto firstLight = List.at(0);
auto firstStatus = firstLight->ToggledOnFlag;
2021-01-05 10:02:43 +01:00
for (auto i = 0; i < count - 1; i++)
{
auto lightCur = List.at(i);
auto lightNext = List.at(i + 1);
lightCur->Message(lightNext->ToggledOnFlag ? MessageCode::TLightTurnOnTimed : MessageCode::TLightTurnOffTimed, 0.0);
2021-01-05 10:02:43 +01:00
}
auto lastLight = List.at(count - 1);
lastLight->Message(firstStatus ? MessageCode::TLightTurnOnTimed : MessageCode::TLightTurnOffTimed, 0);
2021-01-05 10:02:43 +01:00
reschedule_animation(value);
break;
}
case MessageCode::TLightGroupLightShowAnimation:
2021-01-05 10:02:43 +01:00
{
if (AnimationFlag || MessageField2 == MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
MessageField2 = code;
AnimationFlag = 0;
for (auto light : List)
2021-01-05 10:02:43 +01:00
{
if (rand() % 100 > 70)
{
auto randVal = RandFloat() * value * 3.0f + 0.1f;
light->Message(MessageCode::TLightTurnOnTimed, randVal);
2021-01-05 10:02:43 +01:00
}
}
reschedule_animation(value);
break;
}
case MessageCode::TLightGroupGameOverAnimation:
2021-01-05 10:02:43 +01:00
{
if (AnimationFlag || MessageField2 == MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
MessageField2 = code;
AnimationFlag = 0;
for (auto light : List)
2021-01-05 10:02:43 +01:00
{
auto randVal = static_cast<float>(rand() % 100 > 70);
light->Message(MessageCode::TLightResetAndToggleValue, randVal);
2021-01-05 10:02:43 +01:00
}
reschedule_animation(value);
break;
}
case MessageCode::TLightGroupRandomAnimationSaturation:
2021-01-05 10:02:43 +01:00
{
auto noBmpInd1Count = 0;
for (auto light : List)
2021-01-05 10:02:43 +01:00
{
if (!light->LightOnFlag)
2021-01-05 10:02:43 +01:00
++noBmpInd1Count;
}
if (!noBmpInd1Count)
break;
auto randModCount = rand() % noBmpInd1Count;
for (auto it = List.rbegin(); it != List.rend(); ++it)
2021-01-05 10:02:43 +01:00
{
auto light = *it;
if (!light->LightOnFlag && randModCount-- == 0)
2021-01-05 10:02:43 +01:00
{
light->Message(MessageCode::TLightTurnOn, 0.0);
2021-01-05 10:02:43 +01:00
break;
}
}
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
break;
}
case MessageCode::TLightGroupRandomAnimationDesaturation:
2021-01-05 10:02:43 +01:00
{
auto bmpInd1Count = 0;
for (auto light : List)
2021-01-05 10:02:43 +01:00
{
if (light->LightOnFlag)
2021-01-05 10:02:43 +01:00
++bmpInd1Count;
}
if (!bmpInd1Count)
break;
auto randModCount = rand() % bmpInd1Count;
for (auto it = List.rbegin(); it != List.rend(); ++it)
2021-01-05 10:02:43 +01:00
{
auto light = *it;
if (light->LightOnFlag && randModCount-- == 0)
2021-01-05 10:02:43 +01:00
{
light->Message(MessageCode::TLightTurnOff, 0.0);
2021-01-05 10:02:43 +01:00
break;
}
}
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
break;
}
case MessageCode::TLightGroupOffsetAnimationForward:
2021-01-05 10:02:43 +01:00
{
auto index = next_light_up();
if (index < 0)
break;
List.at(index)->Message(MessageCode::TLightTurnOn, 0.0);
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
return 1;
}
case MessageCode::TLightGroupOffsetAnimationBackward:
2021-01-05 10:02:43 +01:00
{
auto index = next_light_down();
if (index < 0)
break;
List.at(index)->Message(MessageCode::TLightTurnOff, 0.0);
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
return 1;
}
case MessageCode::TLightGroupReset:
2021-01-05 10:02:43 +01:00
{
if (Timer)
timer::kill(Timer);
Timer = 0;
if (MessageField2 == MessageCode::TLightGroupAnimationBackward ||
MessageField2 == MessageCode::TLightGroupAnimationForward || MessageField2 == MessageCode::TLightGroupLightShowAnimation)
TLightGroup::Message(MessageCode::TLightResetTimed, 0.0);
MessageField2 = MessageCode::TLightGroupNull;
2021-01-05 10:02:43 +01:00
AnimationFlag = 0;
break;
}
case MessageCode::TLightGroupTurnOnAtIndex:
2021-01-05 10:02:43 +01:00
{
auto index = static_cast<int>(floor(value));
if (index >= count || index < 0)
2021-01-05 10:02:43 +01:00
break;
auto light = List.at(index);
light->Message(MessageCode::TLightTurnOn, 0.0);
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
break;
}
case MessageCode::TLightGroupTurnOffAtIndex:
2021-01-05 10:02:43 +01:00
{
auto index = static_cast<int>(floor(value));
if (index >= count || index < 0)
2021-01-05 10:02:43 +01:00
break;
auto light = List.at(index);
light->Message(MessageCode::TLightTurnOff, 0.0);
if (MessageField2 != MessageCode::TLightGroupNull)
2021-01-05 10:02:43 +01:00
start_animation();
break;
}
case MessageCode::TLightGroupGetOnCount:
2021-01-05 10:02:43 +01:00
{
auto bmp1Count = 0;
for (auto light : List)
2021-01-05 10:02:43 +01:00
{
if (light->LightOnFlag)
2021-01-21 15:58:05 +01:00
++bmp1Count;
2021-01-05 10:02:43 +01:00
}
return bmp1Count;
}
case MessageCode::TLightGroupGetLightCount:
return count;
case MessageCode::TLightGroupGetMessage2:
return static_cast<int>(MessageField2);
case MessageCode::TLightGroupGetAnimationFlag:
2021-01-05 10:02:43 +01:00
return AnimationFlag;
case MessageCode::TLightGroupResetAndTurnOn:
2021-01-05 10:02:43 +01:00
{
auto index = next_light_up();
if (index < 0)
break;
if (MessageField2 != MessageCode::TLightGroupNull || AnimationFlag)
TLightGroup::Message(MessageCode::TLightGroupReset, 0.0);
List.at(index)->Message(MessageCode::TLightFlasherStartTimedThenStayOn, value);
2021-01-05 10:02:43 +01:00
return 1;
}
case MessageCode::TLightGroupResetAndTurnOff:
2021-01-05 10:02:43 +01:00
{
auto index = next_light_down();
if (index < 0)
break;
if (MessageField2 != MessageCode::TLightGroupNull || AnimationFlag)
TLightGroup::Message(MessageCode::TLightGroupReset, 0.0);
List.at(index)->Message(MessageCode::TLightFlasherStartTimedThenStayOff, value);
2021-01-05 10:02:43 +01:00
return 1;
}
case MessageCode::TLightGroupRestartNotifyTimer:
2021-01-05 10:02:43 +01:00
if (NotifyTimer)
timer::kill(NotifyTimer);
NotifyTimer = 0;
if (value > 0.0f)
2021-01-05 10:02:43 +01:00
NotifyTimer = timer::set(value, this, NotifyTimerExpired);
break;
case MessageCode::TLightGroupFlashWhenOn:
2021-01-05 10:02:43 +01:00
{
for (auto it = List.rbegin(); it != List.rend(); ++it)
2021-01-05 10:02:43 +01:00
{
auto light = *it;
if (light->LightOnFlag)
2021-01-05 10:02:43 +01:00
{
light->Message(MessageCode::TLightTurnOff, 0.0);
light->Message(MessageCode::TLightFlasherStartTimedThenStayOff, value);
2021-01-05 10:02:43 +01:00
}
}
break;
}
case MessageCode::TLightGroupToggleSplitIndex:
2021-01-05 10:02:43 +01:00
{
control::handler(code, this);
2021-01-05 10:02:43 +01:00
auto index = static_cast<int>(floor(value));
if (index >= 0 && index < count)
2021-01-05 10:02:43 +01:00
{
// Turn off lights (index, end]
for (auto i = count - 1; i > index; i--)
2021-01-05 10:02:43 +01:00
{
List.at(i)->Message(MessageCode::TLightResetAndTurnOff, 0.0);
}
// Turn on lights [begin, index]
for (auto i = index; i >= 0; i--)
{
List.at(i)->Message(MessageCode::TLightResetAndTurnOn, 0.0);
2021-01-05 10:02:43 +01:00
}
}
break;
}
case MessageCode::TLightGroupStartFlasher:
2021-01-05 10:02:43 +01:00
{
auto index = next_light_down();
if (index >= 0)
{
List.at(index)->Message(MessageCode::TLightFlasherStart, 0.0);
2021-01-05 10:02:43 +01:00
}
break;
}
default:
for (auto it = List.rbegin(); it != List.rend(); ++it)
2021-01-05 10:02:43 +01:00
{
(*it)->Message(code, value);
2021-01-05 10:02:43 +01:00
}
break;
}
return 0;
}
void TLightGroup::Reset()
{
if (Timer)
timer::kill(Timer);
Timer = 0;
if (NotifyTimer)
timer::kill(NotifyTimer);
NotifyTimer = 0;
MessageField2 = MessageCode::TLightGroupNull;
2021-01-05 10:02:43 +01:00
AnimationFlag = 0;
Timer1Time = Timer1TimeDefault;
}
void TLightGroup::reschedule_animation(float time)
{
if (Timer)
timer::kill(Timer);
Timer = 0;
if (time == 0)
{
MessageField2 = MessageCode::TLightGroupNull;
2021-01-05 10:02:43 +01:00
AnimationFlag = 0;
return;
}
Timer1Time = time > 0.0f ? time : Timer1TimeDefault;
2021-01-05 10:02:43 +01:00
Timer = timer::set(Timer1Time, this, TimerExpired);
}
void TLightGroup::start_animation()
{
for (auto it = List.rbegin(); it != List.rend(); ++it)
2021-01-05 10:02:43 +01:00
{
auto light = *it;
if (light->LightOnFlag)
light->Message(MessageCode::TLightTurnOnTimed, 0.0);
2021-01-05 10:02:43 +01:00
else
light->Message(MessageCode::TLightTurnOffTimed, 0.0);
2021-01-05 10:02:43 +01:00
}
}
int TLightGroup::next_light_up()
{
for (auto index = 0u; index < List.size(); ++index)
2021-01-05 10:02:43 +01:00
{
if (!List[index]->LightOnFlag)
return static_cast<int>(index);
2021-01-05 10:02:43 +01:00
}
return -1;
}
int TLightGroup::next_light_down()
{
for (auto index = static_cast<int>(List.size()) - 1; index >= 0; --index)
2021-01-05 10:02:43 +01:00
{
if (List.at(index)->LightOnFlag)
2021-01-05 10:02:43 +01:00
return index;
}
return -1;
}
void TLightGroup::TimerExpired(int timerId, void* caller)
{
auto group = static_cast<TLightGroup*>(caller);
group->Timer = 0;
group->Message(group->MessageField2, group->Timer1Time);
2021-01-05 10:02:43 +01:00
}
void TLightGroup::NotifyTimerExpired(int timerId, void* caller)
{
auto group = static_cast<TLightGroup*>(caller);
group->NotifyTimer = 0;
control::handler(MessageCode::ControlNotifyTimerExpired, group);
2021-01-05 10:02:43 +01:00
}