source: trunk/monsterz/mash.cpp @ 863

Last change on this file since 863 was 863, checked in by sam, 9 years ago

core: rename vec2i to ivec2 etc. to better match GLSL.

File size: 3.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 <cstdlib>
16#include <cmath>
17
18#include "core.h"
19
20using namespace std;
21using namespace lol;
22
23#include "mash.h"
24#include "monsterz.h"
25
26/*
27 * Mash implementation class
28 */
29
30class MashData
31{
32    friend class Mash;
33
34private:
35    Emitter *emitter;
36    int duh, pop;
37    Piece *pieces[MAX_WIDTH * MAX_HEIGHT];
38    ivec2 cells[MAX_WIDTH * MAX_HEIGHT];
39    int npieces;
40    float timer;
41
42    enum
43    {
44        WAIT,
45        DUH,
46        POP,
47        DEAD,
48    }
49    state;
50};
51
52/*
53 * Public Mash class
54 */
55
56Mash::Mash(Emitter *emitter)
57  : data(new MashData()),
58    nextmash(NULL)
59{
60    data->emitter = emitter;
61    Ticker::Ref(emitter);
62    data->duh = Sampler::Register(WAV_DUH);
63    data->pop = Sampler::Register(WAV_POP);
64    data->npieces = 0;
65    data->state = MashData::WAIT;
66}
67
68void Mash::AddPiece(Piece *piece)
69{
70    data->pieces[data->npieces] = piece;
71    data->cells[data->npieces] = piece->GetCell();
72    data->npieces++;
73}
74
75int Mash::IsDead() const
76{
77    return data->state == MashData::DEAD;
78}
79
80void Mash::TickGame(float deltams)
81{
82    Entity::TickGame(deltams);
83
84    switch (data->state)
85    {
86    case MashData::WAIT:
87    {
88        int allready = 1;
89        for (int n = 0; n < data->npieces && allready; n++)
90        {
91            /* If piece is still too far, don't start the animation */
92            ivec2 shift = data->pieces[n]->GetShift();
93            if (abs(shift.y) + abs(shift.x) > 0)
94                allready = 0;
95        }
96
97        if (allready)
98        {
99            Sampler::PlaySample(data->duh);
100            for (int n = 0; n < data->npieces; n++)
101                data->pieces[n]->Pop();
102            data->timer = DELAY_DUH;
103            data->state = MashData::DUH;
104        }
105        break;
106    }
107    case MashData::DUH:
108        if ((data->timer -= deltams) < 0.0f)
109        {
110            Sampler::PlaySample(data->pop);
111            data->state = MashData::POP;
112        }
113        break;
114    case MashData::POP:
115    {
116        int alldead = 1;
117        for (int n = 0; n < data->npieces && alldead; n++)
118            if (!data->pieces[n]->IsDead())
119                alldead = 0;
120        if (alldead)
121        {
122            for (int n = 0; n < data->npieces; n++)
123            {
124                /* Remove links for this piece */
125                Piece *above = data->pieces[n]->GetAbove();
126                Piece *below = data->pieces[n]->GetBelow();
127                data->pieces[n]->SetAbove(NULL);
128                data->pieces[n]->SetBelow(NULL);
129                /* If there is a new link to create, create it */
130                if (above && below)
131                    above->SetBelow(below);
132                /* Give away references to this piece */
133                Ticker::Unref(data->pieces[n]);
134            }
135            data->npieces = 0;
136            data->state = MashData::DEAD;
137        }
138        break;
139    }
140    case MashData::DEAD:
141        break;
142    }
143}
144
145void Mash::TickDraw(float deltams)
146{
147    Entity::TickDraw(deltams);
148}
149
150Mash::~Mash()
151{
152    for (int n = 0; n < data->npieces; n++)
153    {
154        data->pieces[n]->SetBelow(NULL);
155        Ticker::Unref(data->pieces[n]);
156    }
157    Sampler::Deregister(data->duh);
158    Sampler::Deregister(data->pop);
159    Ticker::Unref(data->emitter);
160    delete data;
161}
162
Note: See TracBrowser for help on using the repository browser.