diff options
author | Ash Wolf <ninji@wuffs.org> | 2023-06-28 22:22:32 +0100 |
---|---|---|
committer | Ash Wolf <ninji@wuffs.org> | 2023-06-28 22:22:32 +0100 |
commit | c0c336500955a23e344651e5412c9d9d441ef4ee (patch) | |
tree | 790769c748db307cf3314f6e896e2f61c68561a2 /src/T2DLL/T2MetroRailway.cpp | |
parent | 37e364b2c6cc7487a1c888d256a73e5337bb7189 (diff) | |
download | t2win-c0c336500955a23e344651e5412c9d9d441ef4ee.tar.gz t2win-c0c336500955a23e344651e5412c9d9d441ef4ee.zip |
first pass of T2DLL
Diffstat (limited to '')
-rw-r--r-- | src/T2DLL/T2MetroRailway.cpp | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/src/T2DLL/T2MetroRailway.cpp b/src/T2DLL/T2MetroRailway.cpp new file mode 100644 index 0000000..246012f --- /dev/null +++ b/src/T2DLL/T2MetroRailway.cpp @@ -0,0 +1,234 @@ +#include "CResFile.h" +#include "GlobalFunc.h" +#include "T2BitImage.h" +#include "T2DateTime.h" +#include "T2ImageObj.h" +#include "T2MetroRailway.h" +#include "T2SoundPlayer.h" +#include "T2Sprite.h" +#include "T2TowerDoc.h" +#include "T2TowerEvent.h" +#include "T2TowerMainView.h" +#include "T2WorldDef.h" +#include "UT2Coordinate.h" + +static CString spriteNames[4] = { + "MET1", + "MET2", + "MET3", + "MET4" +}; + +T2MetroRailway::T2MetroRailway(T2TowerEvent* inTowerEvent, CResFile* inResFile, int inSubID) + : T2EventItem(inTowerEvent, inResFile, inSubID) +{ + *inResFile >> m74; + + int count; + *inResFile >> count; + + mCurrentSprite = -1; + for (int i = 0; i < count; i++) { + T2MetroRailway *obj = (i == 0) ? this : new T2MetroRailway(mTowerEvent, inSubID + i, mLevelBit, mExclusive, mCycleDay, mBeginTime, mEndTime); + if (obj) { + obj->Init(this, inResFile); + AddTransport(obj); + if (obj != this) + mTowerEvent->AppendStandby(obj); + } + } + + CString s; + + s.Format("METRO:%d", 0); + Sounds->AddSound(s, SoundPriority_1, 8000, GetWorldModuleHandle()); + s.Format("METRO:%d", 1); + Sounds->AddSound(s, SoundPriority_1, 8001, GetWorldModuleHandle()); + s.Format("METRO:%d", 2); + Sounds->AddSound(s, SoundPriority_1, 8002, GetWorldModuleHandle()); +} + +T2MetroRailway::T2MetroRailway(T2TowerEvent* inTowerEvent, int inSubID, int inLevelBit, BOOL inExclusive, int inCycleDay, int inBeginTime, int inEndTime) + : T2EventItem(inTowerEvent, inSubID, inLevelBit, inExclusive, inCycleDay, inBeginTime, inEndTime) +{ +} + +/*virtual*/ T2MetroRailway::~T2MetroRailway() { + if (mParent == this) { + for (int i = 0; i <= 3; i++) + GetTowerDoc()->mSprite.DeleteSprite(mTrainSpriteIndices[i]); + + delete mImage; + delete mImageObj; + } + + if (Sounds) { + CString s; + for (int i = 0; i < 3; i++) { + s.Format("METRO:%d", i); + Sounds->Stop(s); + Sounds->DeleteSound(s); + } + } +} + +void T2MetroRailway::Init(T2Transport* inParent, CResFile* inResFile) { + T2Transport::Init(inParent, inResFile); + + mCurrentSprite = -1; + mTrainSpriteIndices[3] = 0; + mTrainSpriteIndices[2] = 0; + mTrainSpriteIndices[1] = 0; + mTrainSpriteIndices[0] = 0; + + mImage = NULL; + mImageObj = NULL; + + for (int i = 0; i <= 3; i++) { + if (inParent == this) { + if (i == 0) { + mImage = new T2BitImage(GetWorldDef()->mModuleHandle, 8000, true); + mImageObj = new T2ImageObj; + if (mImageObj && mImage) + mImageObj->AddObject(GetWorldDef()->mModuleHandle, 8000, mImage); + } + + mTrainSpriteIndices[i] = GetCurrentT2TowerDoc()->mSprite.NewSprite(*mImageObj, spriteNames[i], T2Sprite::ELayer_2); + } else { + mTrainSpriteIndices[i] = ((T2MetroRailway *) inParent)->mTrainSpriteIndices[i]; + } + } + + mVisible = false; + + m74 = ((T2MetroRailway *) mParent)->m74; + _24 = mWidth / 4; + mBeginTime = mArriveTime - _24; + _2C = 8; +} + +/*virtual*/ BOOL T2MetroRailway::IsBeginTime(unsigned int inTime) { + int diff = inTime - mBeginTime; + return (diff >= 0 && diff < (int) (mWidth / 4)); +} + +/*virtual*/ int T2MetroRailway::Exec(T2TowerDoc *inDoc) { + if (inDoc) { + int whichSound = -1; + T2DateTime *theNow = inDoc->mNow; + unsigned int minutes = theNow->GetRawMinutes(); + int var1C; + + if ((mArriveTime + _24) > 1440 && minutes < mBeginTime) + minutes += 1440; + + if (minutes < mArriveTime) { + var1C = 0 - (((mArriveTime - minutes) * 4) - theNow->mSeconds); + if ((var1C + 16) == 0 && mFlag && mVisible) + whichSound = 0; + } else { + var1C = mFlag ? (mArriveTime + _2C) : mArriveTime; + var1C = minutes - var1C; + if (var1C < 0) { + if ((_2C + var1C) == (_2C / 2) && mFlag && mVisible) + whichSound = 1; + var1C = 0; + } else { + var1C += theNow->mSeconds * 4; + } + } + + var1C += mStation; + + if (var1C < GetWorldDef()->GetWidth()) { + T2TowerMainView *theMainView = inDoc->GetTowerMainView(); + if (theMainView) { + POINT p; + RECT metroRect; + + p.y = GetWorldDef()->mBottomFloorLine; + p.x = var1C; + UT2Coordinate::MakeRect(metroRect, p, 40, 1); + UT2Coordinate::UnitToQD(metroRect, 0); + OffsetRect(&metroRect, 0, -8); + + p.y = metroRect.top; + p.x = metroRect.left; + UT2Coordinate::ZoomOut(metroRect, inDoc->towerDoc_vf108()); + + CRect visibleRect; + theMainView->GetClientRect(&visibleRect); + visibleRect.OffsetRect(theMainView->m64); + visibleRect.right += UT2Coordinate::UnitHSize(inDoc->towerDoc_vf108()); + + RECT intersection; + if (IntersectRect(&intersection, &metroRect, &visibleRect)) { + if (mCurrentSprite == -1) { + CreateSpriteObj(inDoc); + whichSound = 2; + } + + inDoc->mSprite.MoveSprite(mCurrentSprite, p); + Show(); + + if (whichSound >= 0) { + CString name; + name.Format("METRO:%d", whichSound); + Sounds->Play(name, SoundMask_10, SoundFlags_10 | SoundFlags_10000, NULL, PlayMode_2, 100); + } + } else { + Hide(); + } + + return 0; + } + } + } + + StopEvent(inDoc); + return 3; +} + +/*virtual*/ void T2MetroRailway::StopEvent(T2TowerDoc* inDoc) { + Hide(); + + mCurrentSprite = -1; + mVisible = false; + + T2Transport::Stop(inDoc); +} + +/*virtual*/ int T2MetroRailway::Start(T2TowerDoc* inDoc) { + SetStatus(1); + return 1; +} + +BOOL T2MetroRailway::CreateSpriteObj(T2TowerDoc *inDoc) { + if (mCurrentSprite == -1) { + mCurrentSprite = mTrainSpriteIndices[mTrainType]; + mVisible = false; + } + return mCurrentSprite != -1; +} + +void T2MetroRailway::Show() { + if (GetTowerDoc() && !mVisible && mCurrentSprite != -1) { + GetTowerDoc()->mSprite.ShowSprite(mCurrentSprite, true); + mVisible = true; + } +} + +void T2MetroRailway::Hide() { + if (GetTowerDoc() && mVisible && mCurrentSprite != -1) { + GetTowerDoc()->mSprite.ShowSprite(mCurrentSprite, false); + mVisible = false; + } +} + +/*virtual*/ void T2MetroRailway::Read(T2Archive& inArchive) { + T2Transport::Read(inArchive); +} + +/*virtual*/ void T2MetroRailway::Write(T2Archive& inArchive) { + T2Transport::Write(inArchive); +} |