summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2SantaClaus.cpp
blob: 00bd1fc2279b522c251efa09dd6ebe4a620640d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "StdAfx.h"
#include "CResFile.h"
#include "GlobalFunc.h"
#include "T2BitImage.h"
#include "T2DateTime.h"
#include "T2ImageObj.h"
#include "../T2MainWindow.h"
#include "T2SantaClaus.h"
#include "T2Sprite.h"
#include "T2ToolWindow.h"
#include "../T2TowerDoc.h"
#include "T2TowerEvent.h"
#include "../T2TowerMainView.h"
#include "T2WorldDef.h"
#include "UT2Coordinate.h"

T2SantaClaus::T2SantaClaus(T2TowerEvent* inTowerEvent, CResFile* inResFile, int inSubID)
    : T2EventItem(inTowerEvent, inResFile, inSubID)
{
    *inResFile >> m30;
    if (m30 == 0)
        m30 = 32;

    mWidth = GetWorldDef()->GetWidth();

    mPosition.x = mWidth;
    mPosition.y = GetWorldDef()->mTopFloorLine;

    m48 = 0;
    mCounter = 0;

    mSpriteID = 0;
    mSprite = NULL;
    mImage = NULL;
    mImageObj = NULL;

    mSpriteWidth = 0;
    mSpriteHeight = 0;

    mAttach = NULL;
    mMainView = NULL;

    m6C = false;
    mVisible = false;

    mBeginTime = 1440 - (mWidth / 2);
}

/*virtual*/ T2SantaClaus::~T2SantaClaus() {
    StopEvent(mTowerEvent->mDocument);

    if (mImage)
        delete mImage;
    if (mImageObj)
        delete mImageObj;
}

/*virtual*/ BOOL T2SantaClaus::Start(T2TowerDoc* inDoc) {
    T2DateTime *theNow = inDoc->GetNow();
    m48 = theNow ? (theNow->GetRawMinutes() - mBeginTime) : 0;
    m4C = -6;

    mSprite = &inDoc->mSprite;

    mCounter = 0;

    if (!mImageObj) {
        try {
            mImage = new T2BitImage(inDoc->mWorldDef->mModuleHandle, 5003, true);
        } catch (int err) {
            return false;
        }
        mImageObj = new T2ImageObj;
        mImageObj->AddObject(inDoc->mWorldDef->mModuleHandle, 5003, mImage);
    }

    SIZE theSize;
    mImageObj->GetObjectSize(0, &theSize);
    mSpriteWidth = theSize.cx;
    mSpriteHeight = theSize.cy;
    mSpriteID = mSprite->NewSprite(*mImageObj, "SANTA", T2Sprite::ELayer_3);

    CPoint position = mPosition;
    position.x -= m48;
    UT2Coordinate::UnitToQD(position, 0);

    int yOffset = (m48 * 4) / m30;
    position.y += yOffset;

    mSprite->MoveSprite(mSpriteID, position);
    mSprite->ShowSprite(mSpriteID, true);
    mVisible = true;

    mMainView = inDoc->GetMainView();
    if (mMainView) {
        mAttach = new T2SantaClausAttach(this, 0, true);
        mMainView->AddAttachment(mAttach, NULL, true);
        return true;
    } else {
        return false;
    }
}

/*virtual*/ BOOL T2SantaClaus::IsBeginTime(unsigned int inTime) {
    m48 = inTime - mBeginTime;
    return (m48 >= 0);
}

/*virtual*/ int T2SantaClaus::Exec(T2TowerDoc* inDoc) {
    if (!mMainView)
        mMainView = inDoc->GetMainView();

    if (!mMainView)
        return 0;

    if ((mCounter % 4) == 0) {
        m48++;

        CPoint position = mPosition;
        position.x -= m48;
        UT2Coordinate::UnitToQD(position, 0);

        int vOffset = mCounter / m30;
        position.y += vOffset;
        position.y -= mSpriteWidth;

        CRect viewArea;
        CRect spriteArea;
        CRect intersect;

        mMainView->GetClientRect(viewArea);
        viewArea.OffsetRect(mMainView->mScrollOffset);

        spriteArea.SetRect(position.x, position.y, position.x + mSpriteHeight, position.y + mSpriteWidth);

        if (intersect.IntersectRect(viewArea, spriteArea)) {
            Show();
            mSprite->MoveSprite(mSpriteID, position);
            if (m6C) {
                m4C = m48;
                m6C = false;
            }

            int pattern = (m48 & 1) + (((m48 - m4C) < 6) ? 2 : 0);
            mSprite->ChangePattern(mSpriteID, pattern);
        } else {
            Hide();
            if (m48 > mWidth)
                StopEvent(inDoc);
        }
    }

    mCounter++;
    return mSprite ? 0 : 3;
}

/*virtual*/ void T2SantaClaus::StopEvent(T2TowerDoc* inDoc) {
    if (mSprite) {
        mSprite->DeleteSprite(mSpriteID);

        delete mImageObj;
        mImageObj = NULL;
        delete mImage;
        mImage = NULL;

        mSprite = NULL;
    }

    if (mAttach) {
        mMainView->RemoveAttachment(mAttach);
        delete mAttach;
        mAttach = NULL;
    }
}

void T2SantaClaus::Show() {
    if (!mVisible) {
        mSprite->ShowSprite(mSpriteID, true);
        mVisible = true;
    }
}

void T2SantaClaus::Hide() {
    if (mVisible) {
        CPoint pt = mPosition;
        UT2Coordinate::UnitToQD(pt, 0);
        mSprite->MoveSprite(mSpriteID, pt);

        mSprite->ShowSprite(mSpriteID, false);
        mVisible = false;
    }
}



T2SantaClausAttach::T2SantaClausAttach(T2SantaClaus* inOwner, unsigned int inMessage, BOOL inExecuteHost)
    : LAttachment(inMessage, inExecuteHost)
{
    mOwner = inOwner;
}

/*virtual*/ T2SantaClausAttach::~T2SantaClausAttach() {
}

/*virtual*/ void T2SantaClausAttach::ExecuteSelf(unsigned int inMessage, void* ioData) {
    if (inMessage == WM_LBUTTONDOWN) {
        MSG *messagePtr = (MSG *) ioData;

        int id = T2_MAIN_WINDOW->mToolWindow->GetSel();
        CString *theIconName = T2_MAIN_WINDOW->mToolWindow->GetIconName(id);
        T2TemplatePlugin *theTemplatePlugin = T2_MAIN_WINDOW->mToolWindow->GetIconPlugin(id);
        CProgramPlugin *theIconProgramPlugin = theTemplatePlugin->GetPlugin();
        unsigned int theIconValiation = T2_MAIN_WINDOW->mToolWindow->GetIconValiation(id);
        DWORD theIconOption = T2_MAIN_WINDOW->mToolWindow->GetIconOption(id);

        if (theIconOption == kToolOption0 && *theIconName == "ToolInfo") {
            CPoint pt(LOWORD(messagePtr->lParam), HIWORD(messagePtr->lParam));
            pt.Offset(GetCurrentT2TowerDoc()->GetMainView()->mScrollOffset);

            if (mOwner) {
                CRect rect;
                mOwner->mSprite->GetSpriteRect(mOwner->mSpriteID, rect);
                mOwner->m6C = rect.PtInRect(pt);
            }
        }
    }

    SetExecuteHost(true);
}