source: trunk/monsterz/mash.cpp @ 279

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

Play a few additional sounds (whip and pop).

File size: 3.2 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    Game *game;
33    int duh, pop;
34    Piece *pieces[8 * 8];
35    Int2 cells[8 * 8];
36    int npieces;
37
38    enum
39    {
40        WAIT,
41        POP,
42        DEAD,
43    }
44    state;
45};
46
47/*
48 * Public Mash class
49 */
50
51Mash::Mash(Game *game)
52  : data(new MashData()),
53    nextmash(NULL)
54{
55    data->game = game;
56    Ticker::Ref(game);
57    data->duh = Sampler::Register(WAV_DUH);
58    data->pop = Sampler::Register(WAV_POP);
59    data->npieces = 0;
60    data->state = MashData::WAIT;
61}
62
63void Mash::AddPiece(Piece *piece)
64{
65    data->pieces[data->npieces] = piece;
66    data->cells[data->npieces] = piece->GetCell();
67    data->npieces++;
68}
69
70int Mash::IsDead() const
71{
72    return data->state == MashData::DEAD;
73}
74
75void Mash::TickGame(float deltams)
76{
77    Entity::TickGame(deltams);
78
79    switch (data->state)
80    {
81    case MashData::WAIT:
82    {
83        int allready = 1;
84        for (int n = 0; n < data->npieces && allready; n++)
85        {
86            Int2 dest = data->pieces[n]->GetCell() * 48;
87            Int2 cur = data->pieces[n]->GetPos();
88
89            /* If piece is still too high, don't start the animation */
90            if (cur.y - dest.y > 2 || abs(cur.x - dest.x) > 2)
91                allready = 0;
92        }
93
94        if (allready)
95        {
96            Sampler::PlaySample(data->pop);
97            for (int n = 0; n < data->npieces; n++)
98                data->pieces[n]->Pop();
99            data->state = MashData::POP;
100        }
101        break;
102    }
103    case MashData::POP:
104    {
105        int alldead = 1;
106        for (int n = 0; n < data->npieces && alldead; n++)
107            if (!data->pieces[n]->IsDead())
108                alldead = 0;
109        if (alldead)
110        {
111            for (int n = 0; n < data->npieces; n++)
112            {
113                /* Remove links for this piece */
114                Piece *above = data->pieces[n]->GetAbove();
115                Piece *below = data->pieces[n]->GetBelow();
116                data->pieces[n]->SetAbove(NULL);
117                data->pieces[n]->SetBelow(NULL);
118                /* If there is a new link to create, create it */
119                if (above && below)
120                    above->SetBelow(below);
121                /* Give away references to this piece */
122                Ticker::Unref(data->pieces[n]);
123            }
124            data->npieces = 0;
125            data->state = MashData::DEAD;
126        }
127        break;
128    }
129    case MashData::DEAD:
130        break;
131    }
132}
133
134void Mash::TickDraw(float deltams)
135{
136    Entity::TickDraw(deltams);
137}
138
139Mash::~Mash()
140{
141    for (int n = 0; n < data->npieces; n++)
142        Ticker::Unref(data->pieces[n]);
143    Sampler::Deregister(data->duh);
144    Sampler::Deregister(data->pop);
145    Ticker::Unref(data->game);
146    delete data;
147}
148
Note: See TracBrowser for help on using the repository browser.