2020-11-01 16:45:29 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "TPlunger.h"
|
2021-01-07 17:00:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "control.h"
|
|
|
|
#include "loader.h"
|
|
|
|
#include "maths.h"
|
|
|
|
#include "pb.h"
|
|
|
|
#include "render.h"
|
|
|
|
#include "TBall.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "TPinballTable.h"
|
|
|
|
|
2022-09-06 15:57:56 +02:00
|
|
|
TPlunger::TPlunger(TPinballTable* table, int groupIndex) : TCollisionComponent(table, groupIndex, true)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
|
|
|
visualStruct visual{};
|
|
|
|
|
|
|
|
loader::query_visual(groupIndex, 0, &visual);
|
2021-01-23 11:33:30 +01:00
|
|
|
Boost = 0.0;
|
2021-01-07 17:00:38 +01:00
|
|
|
BallFeedTimer_ = 0;
|
|
|
|
PullbackTimer_ = 0;
|
|
|
|
SoundIndexP1 = visual.SoundIndex4;
|
|
|
|
SoundIndexP2 = visual.SoundIndex3;
|
2021-01-23 11:33:30 +01:00
|
|
|
HardHitSoundId = visual.Kicker.HardHitSoundId;
|
|
|
|
Threshold = 1000000000.0;
|
2022-08-25 16:09:17 +02:00
|
|
|
|
|
|
|
// In FT, default max pullback is 50.
|
|
|
|
if (pb::FullTiltMode)
|
|
|
|
MaxPullback = 50;
|
|
|
|
else
|
|
|
|
MaxPullback = 100;
|
|
|
|
|
2021-01-23 11:33:30 +01:00
|
|
|
Elasticity = 0.5f;
|
|
|
|
Smoothness = 0.5f;
|
2022-08-25 16:09:17 +02:00
|
|
|
PullbackIncrement = MaxPullback / (ListBitmap->size() * 8.0f);
|
|
|
|
PullbackDelay = 0.025f;
|
2021-01-07 17:00:38 +01:00
|
|
|
float* floatArr = loader::query_float_attribute(groupIndex, 0, 601);
|
|
|
|
table->PlungerPositionX = floatArr[0];
|
|
|
|
table->PlungerPositionY = floatArr[1];
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:51:00 +02:00
|
|
|
void TPlunger::Collision(TBall* ball, vector2* nextPosition, vector2* direction, float distance, TEdgeSegment* edge)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2022-08-25 16:09:17 +02:00
|
|
|
if (PinballTable->TiltLockFlag || SomeCounter > 0)
|
|
|
|
{
|
|
|
|
auto boost = RandFloat() * MaxPullback * 0.1f + MaxPullback;
|
|
|
|
maths::basic_collision(ball, nextPosition, direction, Elasticity, Smoothness, 0, boost);
|
|
|
|
if (SomeCounter)
|
|
|
|
SomeCounter--;
|
2022-09-06 15:57:56 +02:00
|
|
|
Message(MessageCode::PlungerInputReleased, 0.0);
|
2022-08-25 16:09:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto boost = RandFloat() * Boost * 0.1f + Boost;
|
|
|
|
maths::basic_collision(ball, nextPosition, direction, Elasticity, Smoothness, Threshold, boost);
|
|
|
|
}
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 15:57:56 +02:00
|
|
|
int TPlunger::Message(MessageCode code, float value)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
|
|
|
switch (code)
|
|
|
|
{
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlungerInputPressed:
|
2022-08-25 16:09:17 +02:00
|
|
|
if (!PullbackStartedFlag && PinballTable->MultiballCount > 0 && !PinballTable->TiltLockFlag)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2022-08-25 16:09:17 +02:00
|
|
|
PullbackStartedFlag = true;
|
2021-01-23 11:33:30 +01:00
|
|
|
Boost = 0.0;
|
|
|
|
Threshold = 1000000000.0;
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
loader::play_sound(HardHitSoundId, this, "TPlunger1");
|
2021-01-07 17:00:38 +01:00
|
|
|
PullbackTimer(0, this);
|
|
|
|
}
|
2022-08-25 16:09:17 +02:00
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlungerFeedBall:
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2022-08-25 16:09:17 +02:00
|
|
|
RectF rect{};
|
|
|
|
rect.XMin = PinballTable->CollisionCompOffset * -1.2f + PinballTable->PlungerPositionX;
|
|
|
|
rect.XMax = PinballTable->CollisionCompOffset * 1.2f + PinballTable->PlungerPositionX;
|
|
|
|
rect.YMin = PinballTable->CollisionCompOffset * -1.2f + PinballTable->PlungerPositionY;
|
|
|
|
rect.YMax = PinballTable->CollisionCompOffset * 1.2f + PinballTable->PlungerPositionY;
|
|
|
|
if(PinballTable->BallCountInRect(rect))
|
|
|
|
{
|
|
|
|
timer::set(1.0f, this, BallFeedTimer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto ball = PinballTable->AddBall(PinballTable->PlungerPositionX, PinballTable->PlungerPositionY);
|
|
|
|
assertm(ball, "Failure to create ball in plunger");
|
|
|
|
PinballTable->MultiballCount++;
|
|
|
|
PinballTable->BallInDrainFlag = 0;
|
|
|
|
pb::tilt_no_more();
|
|
|
|
}
|
|
|
|
break;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlungerStartFeedTimer:
|
2022-08-25 16:09:17 +02:00
|
|
|
timer::set(0.95999998f, this, BallFeedTimer);
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
loader::play_sound(SoundIndexP1, this, "TPlunger2");
|
2022-08-25 16:09:17 +02:00
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlungerLaunchBall:
|
2022-08-25 16:09:17 +02:00
|
|
|
PullbackStartedFlag = true;
|
|
|
|
Boost = MaxPullback;
|
2022-09-06 15:57:56 +02:00
|
|
|
Message(MessageCode::PlungerInputReleased, 0.0f);
|
2022-08-25 16:09:17 +02:00
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlungerRelaunchBall:
|
2022-08-25 16:09:17 +02:00
|
|
|
SomeCounter++;
|
|
|
|
timer::set(value, this, BallFeedTimer);
|
|
|
|
loader::play_sound(SoundIndexP1, this, "TPlunger2_1");
|
|
|
|
PullbackStartedFlag = true;
|
|
|
|
PullbackTimer(0, this);
|
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlayerChanged:
|
2022-08-25 16:09:17 +02:00
|
|
|
PullbackStartedFlag = false;
|
|
|
|
Boost = 0.0f;
|
|
|
|
Threshold = 1000000000.0f;
|
|
|
|
SomeCounter = 0;
|
|
|
|
timer::kill(BallFeedTimer);
|
|
|
|
timer::kill(PullbackTimer);
|
|
|
|
timer::kill(ReleasedTimer);
|
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::SetTiltLock:
|
2022-08-25 16:09:17 +02:00
|
|
|
SomeCounter = 0;
|
|
|
|
timer::kill(BallFeedTimer);
|
2021-01-07 17:00:38 +01:00
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::PlungerInputReleased:
|
|
|
|
case MessageCode::Resume:
|
|
|
|
case MessageCode::LooseFocus:
|
2022-08-25 16:09:17 +02:00
|
|
|
if (PullbackStartedFlag && !SomeCounter)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2022-08-25 16:09:17 +02:00
|
|
|
PullbackStartedFlag = false;
|
2021-01-23 11:33:30 +01:00
|
|
|
Threshold = 0.0;
|
2021-01-07 17:00:38 +01:00
|
|
|
if (PullbackTimer_)
|
|
|
|
timer::kill(PullbackTimer_);
|
|
|
|
PullbackTimer_ = 0;
|
2022-08-25 16:09:17 +02:00
|
|
|
loader::play_sound(SoundIndexP2, this, "TPlunger3");
|
2022-09-22 16:46:00 +02:00
|
|
|
SpriteSet(0);
|
2022-08-25 16:09:17 +02:00
|
|
|
timer::set(PullbackDelay, this, ReleasedTimer);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2022-08-25 16:09:17 +02:00
|
|
|
break;
|
2022-09-05 09:17:37 +02:00
|
|
|
case MessageCode::Reset:
|
2022-08-25 16:09:17 +02:00
|
|
|
{
|
|
|
|
PullbackStartedFlag = false;
|
|
|
|
Boost = 0.0f;
|
|
|
|
Threshold = 1000000000.0f;
|
|
|
|
SomeCounter = 0;
|
|
|
|
|
|
|
|
timer::kill(BallFeedTimer);
|
|
|
|
timer::kill(PullbackTimer);
|
|
|
|
timer::kill(ReleasedTimer);
|
2022-09-22 16:46:00 +02:00
|
|
|
|
|
|
|
SpriteSet(0);
|
2022-08-25 16:09:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-01-07 17:00:38 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-08-25 16:09:17 +02:00
|
|
|
|
2022-09-07 15:01:38 +02:00
|
|
|
control::handler(code, this);
|
2021-01-07 17:00:38 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TPlunger::BallFeedTimer(int timerId, void* caller)
|
|
|
|
{
|
|
|
|
auto plunger = static_cast<TPlunger*>(caller);
|
2022-09-06 15:57:56 +02:00
|
|
|
plunger->Message(MessageCode::PlungerFeedBall, 0.0);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TPlunger::PullbackTimer(int timerId, void* caller)
|
|
|
|
{
|
|
|
|
auto plunger = static_cast<TPlunger*>(caller);
|
2022-08-25 16:09:17 +02:00
|
|
|
plunger->Boost += plunger->PullbackIncrement;
|
|
|
|
if (plunger->Boost <= plunger->MaxPullback)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2022-08-25 16:09:17 +02:00
|
|
|
if (plunger->SomeCounter)
|
|
|
|
{
|
|
|
|
plunger->PullbackTimer_ = timer::set(plunger->PullbackDelay / 4.0f, plunger, PullbackTimer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plunger->PullbackTimer_ = timer::set(plunger->PullbackDelay, plunger, PullbackTimer);
|
|
|
|
}
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plunger->PullbackTimer_ = 0;
|
2022-08-25 16:09:17 +02:00
|
|
|
plunger->Boost = plunger->MaxPullback;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2022-08-25 16:09:17 +02:00
|
|
|
|
2021-01-07 17:00:38 +01:00
|
|
|
int index = static_cast<int>(floor(
|
2021-10-01 17:55:44 +02:00
|
|
|
static_cast<float>(plunger->ListBitmap->size() - 1) *
|
2022-08-25 16:09:17 +02:00
|
|
|
(plunger->Boost / plunger->MaxPullback)));
|
2022-09-22 16:46:00 +02:00
|
|
|
plunger->SpriteSet(index);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
2022-08-25 16:09:17 +02:00
|
|
|
void TPlunger::ReleasedTimer(int timerId, void* caller)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
|
|
|
auto plunger = static_cast<TPlunger*>(caller);
|
2021-01-23 11:33:30 +01:00
|
|
|
plunger->Threshold = 1000000000.0;
|
|
|
|
plunger->Boost = 0.0;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|