source: trunk/monsterz/title.cpp @ 333

Last change on this file since 333 was 333, checked in by sam, 10 years ago

Remove mini monsterz from the logo. We can paste them later.

File size: 5.8 KB
Line 
1//
2// Monsterz
3//
4// Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19
20#include "core.h"
21#include "monsterz.h"
22#include "title.h"
23
24/*
25 * Title implementation class
26 */
27
28class TitleData
29{
30    friend class Title;
31
32private:
33    int title, logo;
34    int anim[6];
35    int event[7];
36
37    enum
38    {
39        IDLE,
40        ANIM,
41        EVENT,
42    }
43    state;
44
45    enum
46    {
47        DAY,
48        NIGHT,
49        STEALTH,
50        RADIO,
51    }
52    period, nextperiod;
53
54    float timer, length;
55    int nframes, animid;
56};
57
58/*
59 * Public Title class
60 */
61
62Title::Title()
63  : data(new TitleData())
64{
65    srand(rand() ^ time(NULL));
66
67    data->logo = Tiler::Register(PNG_LOGO, 380, 140, 1.0f);
68    data->title = Tiler::Register(PNG_TITLE, 384, 384, 1.0f);
69    for (int n = 0; n < 6; n++)
70        data->anim[n] = Tiler::Register(PNG_TITLEANIM[n], 384, 384, 1.0f);
71    for (int n = 0; n < 7; n++)
72        data->event[n] = Tiler::Register(PNG_TITLEEVENT[n], 384, 384, 1.0f);
73    data->state = TitleData::IDLE;
74    data->period = TitleData::DAY;
75    data->nextperiod = TitleData::DAY;
76    data->timer = RandF(2000.0f, 3000.0f);
77
78    position = int3(24, 72, 1);
79    bbox[0] = position;
80    bbox[1] = bbox[0] + int3(384, 384, 0);
81
82    Input::TrackMouse(this);
83}
84
85void Title::TickGame(float deltams)
86{
87    Entity::TickGame(deltams);
88
89    data->timer -= deltams;
90
91    /* Probability of playing an animation given the current period */
92    static float const p1[] = { 0.6f, 0.3f, 0.0f, 0.2f };
93    /* Probability of a period change given the current period */
94    static float const p2[] = { 0.1f, 0.2f, 0.5f, 0.5f };
95    /* Number of frames in transition anims and events */
96    static int const t1[] = { 31, 26, 7, 7, 32, 6, 31 };
97    static int const t2[] = { 2, 4, 11, 4, 6, 2 };
98
99    switch (data->state)
100    {
101    case TitleData::IDLE:
102        if (data->timer > 0.0f)
103            break;
104        if (RandF() < p1[data->period])
105        {
106            switch (data->period)
107            {
108            case TitleData::DAY:
109                data->animid = rand() % 4;
110                break;
111            case TitleData::NIGHT:
112                data->animid = (rand() % 2) ? 4 : 6;
113                break;
114            case TitleData::STEALTH:
115                /* XXX: we should not be here! */
116                break;
117            case TitleData::RADIO:
118                data->animid = 5;
119                break;
120            }
121            data->state = TitleData::EVENT;
122            data->nframes = t1[data->animid];
123            data->timer = data->length = data->nframes * 100.0f;
124        }
125        else if (RandF() < p2[data->period])
126        {
127            switch (data->period)
128            {
129            case TitleData::DAY:
130                data->nextperiod = TitleData::NIGHT;
131                data->animid = 0;
132                break;
133            case TitleData::NIGHT:
134                if (RandF() < 0.6f)
135                {
136                    data->nextperiod = TitleData::DAY;
137                    data->animid = 5;
138                }
139                else
140                {
141                    data->nextperiod = TitleData::STEALTH;
142                    data->animid = 1;
143                }
144                break;
145            case TitleData::STEALTH:
146                if (RandF() < 0.3f)
147                {
148                    data->nextperiod = TitleData::RADIO;
149                    data->animid = 2;
150                }
151                else
152                {
153                    data->nextperiod = TitleData::NIGHT;
154                    data->animid = 4;
155                }
156                break;
157            case TitleData::RADIO:
158                data->nextperiod = TitleData::STEALTH;
159                data->animid = 3;
160                break;
161            }
162            data->state = TitleData::ANIM;
163            data->nframes = t2[data->animid];
164            data->timer = data->length = data->nframes * 100.0f;
165        }
166        else
167        {
168            data->timer = RandF(500.0f, 1000.0f);
169        }
170        break;
171    case TitleData::ANIM:
172        if (data->timer < 0.0f)
173            data->period = data->nextperiod;
174            /* Fall through */
175    case TitleData::EVENT:
176        if (data->timer < 0.0f)
177        {
178            data->state = TitleData::IDLE;
179            data->timer = RandF(500.0f, 1000.0f);
180        }
181        break;
182    }
183}
184
185void Title::TickDraw(float deltams)
186{
187    Entity::TickDraw(deltams);
188
189    int tiler = data->title;
190    int id = 0;
191
192    switch (data->state)
193    {
194    case TitleData::IDLE:
195        id = (int)data->period;
196        break;
197    case TitleData::ANIM:
198        tiler = data->anim[data->animid];
199        id = data->nframes - 1 - (data->nframes * data->timer / data->length);
200        if (id < 0) id = 0;
201        if (id > data->nframes - 1) id = data->nframes - 1;
202        break;
203    case TitleData::EVENT:
204        tiler = data->event[data->animid];
205        id = data->nframes - 1 - (data->nframes * data->timer / data->length);
206        if (id < 0) id = 0;
207        if (id > data->nframes - 1) id = data->nframes - 1;
208        break;
209    }
210
211    Scene::GetDefault()->AddTile((tiler << 16) | id, 24, 72, 1, 0);
212    Scene::GetDefault()->AddTile((data->logo << 16) | 0, 26, 250, 2, 0);
213}
214
215int Title::IsClicked() const
216{
217    return clicked[0];
218}
219
220Title::~Title()
221{
222    Input::UntrackMouse(this);
223    Tiler::Deregister(data->logo);
224    Tiler::Deregister(data->title);
225    for (int n = 0; n < 6; n++)
226        Tiler::Deregister(data->anim[n]);
227    for (int n = 0; n < 7; n++)
228        Tiler::Deregister(data->event[n]);
229    delete data;
230}
231
Note: See TracBrowser for help on using the repository browser.