source: trunk/monsterz/title.cpp @ 331

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

Add an Interface class that displays the general interface.

File size: 5.6 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, 181, 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
79void Title::TickGame(float deltams)
80{
81    Entity::TickGame(deltams);
82
83    data->timer -= deltams;
84
85    /* Probability of playing an animation given the current period */
86    static float const p1[] = { 0.6f, 0.3f, 0.0f, 0.2f };
87    /* Probability of a period change given the current period */
88    static float const p2[] = { 0.1f, 0.2f, 0.5f, 0.5f };
89    /* Number of frames in transition anims and events */
90    static int const t1[] = { 31, 26, 7, 7, 32, 6, 31 };
91    static int const t2[] = { 2, 4, 11, 4, 6, 2 };
92
93    switch (data->state)
94    {
95    case TitleData::IDLE:
96        if (data->timer > 0.0f)
97            break;
98        if (RandF() < p1[data->period])
99        {
100            switch (data->period)
101            {
102            case TitleData::DAY:
103                data->animid = rand() % 4;
104                break;
105            case TitleData::NIGHT:
106                data->animid = (rand() % 2) ? 4 : 6;
107                break;
108            case TitleData::STEALTH:
109                /* XXX: we should not be here! */
110                break;
111            case TitleData::RADIO:
112                data->animid = 5;
113                break;
114            }
115            data->state = TitleData::EVENT;
116            data->nframes = t1[data->animid];
117            data->timer = data->length = data->nframes * 100.0f;
118        }
119        else if (RandF() < p2[data->period])
120        {
121            switch (data->period)
122            {
123            case TitleData::DAY:
124                data->nextperiod = TitleData::NIGHT;
125                data->animid = 0;
126                break;
127            case TitleData::NIGHT:
128                if (RandF() < 0.6f)
129                {
130                    data->nextperiod = TitleData::DAY;
131                    data->animid = 5;
132                }
133                else
134                {
135                    data->nextperiod = TitleData::STEALTH;
136                    data->animid = 1;
137                }
138                break;
139            case TitleData::STEALTH:
140                if (RandF() < 0.3f)
141                {
142                    data->nextperiod = TitleData::RADIO;
143                    data->animid = 2;
144                }
145                else
146                {
147                    data->nextperiod = TitleData::NIGHT;
148                    data->animid = 4;
149                }
150                break;
151            case TitleData::RADIO:
152                data->nextperiod = TitleData::STEALTH;
153                data->animid = 3;
154                break;
155            }
156            data->state = TitleData::ANIM;
157            data->nframes = t2[data->animid];
158            data->timer = data->length = data->nframes * 100.0f;
159        }
160        else
161        {
162            data->timer = RandF(500.0f, 1000.0f);
163        }
164        break;
165    case TitleData::ANIM:
166        if (data->timer < 0.0f)
167            data->period = data->nextperiod;
168            /* Fall through */
169    case TitleData::EVENT:
170        if (data->timer < 0.0f)
171        {
172            data->state = TitleData::IDLE;
173            data->timer = RandF(500.0f, 1000.0f);
174        }
175        break;
176    }
177}
178
179void Title::TickDraw(float deltams)
180{
181    Entity::TickDraw(deltams);
182
183    int tiler = data->title;
184    int id = 0;
185
186    switch (data->state)
187    {
188    case TitleData::IDLE:
189        id = (int)data->period;
190        break;
191    case TitleData::ANIM:
192        tiler = data->anim[data->animid];
193        id = data->nframes - 1 - (data->nframes * data->timer / data->length);
194        if (id < 0) id = 0;
195        if (id > data->nframes - 1) id = data->nframes - 1;
196        break;
197    case TitleData::EVENT:
198        tiler = data->event[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    }
204
205    Scene::GetDefault()->AddTile((tiler << 16) | id, 24, 72, 1, 0);
206    Scene::GetDefault()->AddTile((data->logo << 16) | 0, 26, 250, 2, 0);
207}
208
209Title::~Title()
210{
211    Tiler::Deregister(data->logo);
212    Tiler::Deregister(data->title);
213    for (int n = 0; n < 6; n++)
214        Tiler::Deregister(data->anim[n]);
215    for (int n = 0; n < 7; n++)
216        Tiler::Deregister(data->event[n]);
217    delete data;
218}
219
Note: See TracBrowser for help on using the repository browser.