#include "StdAfx.h" #include "CResFile.h" #include "GlobalFunc.h" #include "T2BitImage.h" #include "T2DateTime.h" #include "T2DLL.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 = var1C * 4 + theNow->mSeconds; } } var1C += mStation; if (var1C < GetWorldDef()->GetWidth()) { T2TowerMainView *theMainView = inDoc->GetMainView(); 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->GetZoomLevel()); CRect visibleRect; theMainView->GetClientRect(&visibleRect); visibleRect.OffsetRect(theMainView->mScrollOffset); visibleRect.right += UT2Coordinate::UnitHSize(inDoc->GetZoomLevel()); 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); }