source: trunk/monsterz/mash.cpp @ 339

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

Board size and number of available pieces is now a parameter of
the Board class ctor.

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